SignalInitTableCheck.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # -*- coding: UTF-8 -*-
  2. """
  3. @Project :Matrix_Check
  4. @File :SignalInitTableCheck.py
  5. @Author :haojiang
  6. @Date :2022/9/28 18:15
  7. """
  8. import csv
  9. import os
  10. from enum import Enum
  11. import logging
  12. from pandas import DataFrame
  13. import pandas as pd
  14. from Base.MatrixParser import MatrixParser
  15. class SignalAttrType(Enum):
  16. SIGNAL_NAME = 0
  17. SIGNAL_TYPE = 1
  18. SIGNAL_BIT_LEN = 2
  19. SIGNAL_VALUE = 3
  20. class SignalValueCheck:
  21. # is signed
  22. Type_Mapping = {
  23. 'Boolean': False,
  24. 'UInt': False,
  25. 'SInt': True,
  26. 'Composite': None,
  27. 'Bytes': None
  28. }
  29. def __init__(self, signal_attr: dict):
  30. self.signal_attr = signal_attr
  31. def _get_range_by_bit_length(self, bit_len: int, is_signed: bool = False):
  32. # [0, 2^n-1] 2^n = 1<<n
  33. # [-2^{n-1}, 2^{n-1}-1]
  34. if is_signed:
  35. return (-(1 << (bit_len - 1)), (1 << (bit_len - 1)) - 1)
  36. else:
  37. return (0, (1 << bit_len) - 1)
  38. def _is_signal_value_missing(self, raw_value: str, signal_type: str) -> bool:
  39. if raw_value == '' and signal_type != 'Composite':
  40. return True
  41. else:
  42. return False
  43. def _format_str_signal_value(self, raw_value: str):
  44. ret_value = raw_value.replace('(', '').replace(')', '').upper()
  45. if ret_value in ['TRUE', 'FALSE']:
  46. ret_value = bool(ret_value)
  47. else:
  48. ret_value = self._format_str_to_number(ret_value)
  49. return ret_value
  50. def _format_str_to_number(self, str_num) -> int:
  51. try:
  52. ret = int(str_num)
  53. return ret
  54. except ValueError as e:
  55. # logging.error(f'Transform str {str_num} to number failed!')
  56. pass
  57. def check_attribute(self) -> bool:
  58. ret_status = False
  59. ret_code = 0
  60. signal_name = self.signal_attr[SignalAttrType.SIGNAL_NAME.name]
  61. signal_type = self.signal_attr[SignalAttrType.SIGNAL_TYPE.name]
  62. signal_bit_len_str = self.signal_attr[SignalAttrType.SIGNAL_BIT_LEN.name]
  63. signal_value = self.signal_attr[SignalAttrType.SIGNAL_VALUE.name]
  64. # # raw missing validation
  65. # raw_value = row[TableColumnHeaderType.RawValue.name]
  66. # # logging.error('raw_value' + raw_value)
  67. #
  68. # # type validation, between[Boolean Composite SInt UInt Bytes]
  69. # signal_value_type = row[TableColumnHeaderType.Type.name]\
  70. is_value_missing = self._is_signal_value_missing(signal_value, signal_type)
  71. if is_value_missing:
  72. logging.error(f'Signal {signal_name} Value missing!')
  73. ret_code = 1
  74. else:
  75. value_number = self._format_str_signal_value(signal_value)
  76. if signal_type in SignalValueCheck.Type_Mapping.keys():
  77. if SignalValueCheck.Type_Mapping[signal_type] == None:
  78. ret_status = True
  79. else:
  80. signal_bit_len = self._format_str_to_number(signal_bit_len_str)
  81. range_min, range_max = self._get_range_by_bit_length(signal_bit_len)
  82. if value_number >= range_min and value_number <= range_max:
  83. ret_status = True
  84. else:
  85. logging.error(f'Signal {signal_name} value out of range!')
  86. ret_code = 2
  87. else:
  88. logging.error(f'Unknown Signal Type! {signal_name}-{signal_type}' )
  89. ret_code = 3
  90. return (ret_status, ret_code)
  91. class TableColumnHeaderType(Enum):
  92. Signal = 0
  93. Type = 1
  94. Bit = 2
  95. App = 3
  96. AppValue = 4
  97. Raw = 5
  98. RawValue = 6
  99. Controller = 7
  100. Frame = 8
  101. PDU = 9
  102. class SignalInitValueTableCheck:
  103. Column_Need_Check = {
  104. SignalAttrType.SIGNAL_NAME: TableColumnHeaderType.Signal,
  105. SignalAttrType.SIGNAL_TYPE: TableColumnHeaderType.Type,
  106. SignalAttrType.SIGNAL_BIT_LEN: TableColumnHeaderType.Bit,
  107. SignalAttrType.SIGNAL_VALUE: TableColumnHeaderType.RawValue,
  108. }
  109. def __init__(self, project_path: str):
  110. self.project = project_path
  111. def get_signal_init_value_table(self) -> list:
  112. rx_file, tx_file = self.get_rx_tx_csv_file_name()
  113. if not os.path.isfile(rx_file) \
  114. or not os.path.isfile(tx_file):
  115. logging.error(f'rx_csv {rx_file}, tx_csv {tx_file}')
  116. logging.error('Can not find rx or tx init value csv file!')
  117. return []
  118. else:
  119. rx_data = self.get_csv_data(rx_file)
  120. tx_data = self.get_csv_data(tx_file)
  121. rx_data.extend(tx_data)
  122. self.concat_two_csv_file(rx_file, tx_file)
  123. return rx_data
  124. def concat_two_csv_file(self, rx_csv, tx_csv):
  125. dtype = {'Signal': str,
  126. 'Type': str,
  127. 'L[bit]': str,
  128. 'App': str,
  129. 'App.Value': str,
  130. 'Raw': str,
  131. 'Raw.Value': str,
  132. 'Controller': str,
  133. 'Frame': str,
  134. 'PDU': str}
  135. rx_df = pd.read_csv(rx_csv, dtype=dtype)
  136. tx_df = pd.read_csv(tx_csv, dtype=dtype)
  137. all_df = pd.concat([rx_df, tx_df])
  138. file_name = os.path.basename(rx_csv).split('.')[0]
  139. all_df.to_excel(f'./output/{file_name}-合并初值表.xls', index=False)
  140. def get_rx_tx_csv_file_name(self) -> tuple:
  141. files = os.listdir(self.project)
  142. rx_csv = ''
  143. tx_csv = ''
  144. for file in files:
  145. file_name_upper = file.upper()
  146. if 'RX' in file_name_upper and 'ARXML' not in file_name_upper:
  147. rx_csv = os.path.join(self.project, file)
  148. if 'TX' in file_name_upper:
  149. tx_csv = os.path.join(self.project, file)
  150. return (rx_csv, tx_csv)
  151. def get_csv_data(self, file_name: str) -> list:
  152. with open(file_name, 'r') as csvfile:
  153. spamreader = csv.reader(csvfile, delimiter=',')
  154. is_header_readed = False
  155. table_data = list()
  156. for row in spamreader:
  157. if not is_header_readed:
  158. is_header_readed = True
  159. continue
  160. row_data = dict()
  161. for attr_name, attr_col_number in SignalInitValueTableCheck.Column_Need_Check.items():
  162. row_data[attr_name.name] = row[attr_col_number.value]
  163. table_data.append(row_data)
  164. return table_data
  165. class MatrixSignalInitValueCheck:
  166. def _isignal_type_format(self, item):
  167. if item == 'BOOLEAN':
  168. return 'Boolean'
  169. if 'UINT' in item:
  170. return 'UInt'
  171. elif 'SINT' in item:
  172. return 'SInt'
  173. else:
  174. return None
  175. def get_signal_attr_data_by_dataframe(self, df: DataFrame):
  176. df['ISignal Type'] = df['ISignal Type'].apply(self._isignal_type_format)
  177. df.fillna('', inplace=True)
  178. table_data = []
  179. for index, row in df.iterrows():
  180. row_dict = dict()
  181. row_dict[SignalAttrType.SIGNAL_NAME.name] = str(row['Signal Name'])
  182. row_dict[SignalAttrType.SIGNAL_TYPE.name] = str(row['ISignal Type'])
  183. row_dict[SignalAttrType.SIGNAL_BIT_LEN.name] = str(row['ISignal Bit Length'])
  184. row_dict[SignalAttrType.SIGNAL_VALUE.name] = str(row['ISignal Init Value'])
  185. table_data.append(row_dict)
  186. return table_data
  187. if __name__ == '__main__':
  188. # matrix_path = r'D:\01_Work\02_WP\EP39_EREV\branch\src\MatrixAssistant_V2.0\EP39_EREV_SIMU+_GW'
  189. #
  190. # sc = SignalInitValueTableCheck(matrix_path)
  191. #
  192. #
  193. # data = sc.get_signal_init_value_table()
  194. #
  195. # for row in data:
  196. # # sc.check_row(row)
  197. # scb = SignalValueCheck(row)
  198. # scb.check_attribute()
  199. # mp = MatrixParser(
  200. # r'E:\Project\EP39\EP39_GW04_V1.1a_20220628_Integation\InputFiles\MA_20220607_qy1_EP39EV_simu_GWC-arxmal-V01.1.arxml')
  201. #
  202. mc = MatrixSignalInitValueCheck()
  203. df = pd.read_csv('test.csv', dtype=str)
  204. print(df.dtypes)
  205. all_data = mc.get_signal_attr_data_by_dataframe(df)
  206. for row in all_data:
  207. # print(row)
  208. scb = SignalValueCheck(row)
  209. scb.check_attribute()