-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogcfg.py
137 lines (114 loc) · 5.1 KB
/
logcfg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from convertion import Conversion
from xml.dom import minidom
import argparse
def infixToPosfix(src: str) -> str:
obj = Conversion(len(src))
return obj.infixToPostfix(src)
def getParamsLogger(xmlFile: str, protocol: str) -> dict:
logger = minidom.parse(xmlFile)
for item in logger.getElementsByTagName('protocol'):
if protocol == item.getAttribute('id'):
items = item.getElementsByTagName('parameter')
dictParams = {elem.getAttribute('id'): elem
for elem in items}
return dictParams
raise Exception("Invalid XML file: No protocol {}".format(protocol))
def readProfile(xmlFile: str) -> list:
xmlProfile = minidom.parse(xmlFile)
profile = xmlProfile.getElementsByTagName('profile')[0]
protocol = profile.getAttribute('protocol')
params = profile.getElementsByTagName('parameter')
selected = [elem for elem in params
if elem.getAttribute('dash') == "selected"]
dictSelected = {elem.getAttribute('id'): elem.getAttribute('units')
for elem in selected}
return [protocol, dictSelected]
def getParam(paramsLogger, id: str, unit: str) -> dict:
#print(type(paramsLogger))
for key, elem in paramsLogger.items():
if key == id:
return paramsToDict(elem, unit)
return None
def getConversion(nodeParam: minidom.Element, unit: str) -> str:
for item in nodeParam.getElementsByTagName('conversion'):
if unit == item.getAttribute('units'):
expr = item.getAttribute('expr')
return infixToPosfix(expr)
return None
def getStorageSize(nodeParam: minidom.Element, unit: str) -> str:
addr = nodeParam.getElementsByTagName('address')
if addr and addr[0]:
addrLength = addr[0].getAttribute('length')
if addrLength:
return 8 * int(addrLength)
for item in nodeParam.getElementsByTagName('conversion'):
if unit == item.getAttribute('units'):
storagetype = item.getAttribute('storagetype')
if storagetype == 'uint16':
return 16
return None
def paramsToDict(nodeParam, unit: str):
dictRet = dict()
dictRet['id'] = nodeParam.getAttribute('id')
dictRet['name'] = nodeParam.getAttribute('name')
dictRet['paramname'] = (
dictRet['name'] + "_({})".format(unit)).replace(' ', '_')
addr = nodeParam.getElementsByTagName('address')
if addr and addr[0]:
dictRet['paramid'] = addr[0].firstChild.nodeValue
else:
dictRet['paramid'] = None
dictRet['depends'] = list()
depends = nodeParam.getElementsByTagName('ref')
for ref in depends:
dictRet['depends'].append(ref.getAttribute('parameter'))
dictRet['unit'] = unit
dictRet['scalingrpn'] = getConversion(nodeParam, unit)
dictRet['databits'] = getStorageSize(nodeParam, unit)
return dictRet
class LOGCFG:
def __init__(self, rr_log_xml, logger_xml):
self.protocol, self.paramsProfile = readProfile(rr_log_xml)
self.paramsLogger = getParamsLogger(logger_xml, self.protocol)
self.paramSelected = {key: elem
for key, elem in self.paramsLogger.items()
if key in self.paramsProfile.keys()}
self.dictParams = {key: paramsToDict(elem, self.paramsProfile[key])
for key, elem in self.paramsLogger.items()
if key in self.paramsProfile.keys()}
def print(self, fileType='ssmk'):
print('type = {}'.format(fileType))
idx = 1
for key, elem in self.dictParams.items():
skip = ''
if 'P' in elem['scalingrpn']:
skip = ';'
print('')
print(";{:03d}: {} - {} ({})".format(idx,
elem['id'], elem['name'], elem['unit']))
for key in ['paramname', 'paramid', 'databits', 'scalingrpn']:
if elem.get(key):
print("{}{} = {}".format(skip, key, elem[key]))
idx += 1
paramRPM = self.dictParams['P8']['paramname']
print('')
print('; Start log when RPM > 0')
print("conditionrpn = {},0,>".format(paramRPM))
print('action = start')
print('')
print('; Stop log when RPM = 0')
print("conditionrpn = {},0,==".format(paramRPM))
print('action = stop')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--type', type=str,
help='logcfg type (ssmk or ssmcan)')
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument(
'--profile', type=str, help='RomRaider XML Profile', required=True)
requiredNamed.add_argument(
'--logger', type=str, help='Logger XML file', required=True)
args = parser.parse_args()
logcfg = LOGCFG(args.profile, args.logger)
fileType = 'ssmcan' if 'can' in args.type else 'ssmk'
logcfg.print(fileType)