Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import dateutil.parser
- import re
- import uuid
- import hashlib
- def isnumeric(val):
- try:
- float(val)
- return True
- except ValueError:
- return False
- #endtry
- #enddef
- def parseData_disp_pwr(line):
- # line format:
- # ID Time Vo(mV) Cu(mA) Tempr BTlow BThigh BVlow BVhigh Base.St Volt.St Curr.St Temp.St Per% Coul(mAH) Per% Coul(mWH) B.V.St B.T.St U.V.St U.T.St UTlow UThigh UVlow UVhigh BTAvg
- # 1 22-06-04 18:33:03 411986 0 40000 27000 29000 3431 3436 Idle Normal Normal Normal 100% 36981 100% 14200320 Normal Normal Normal Normal 27000 29000 102979 103022 27866
- # [
- # '1',
- # '22-06-04',
- # '18:33:03',
- # '411986',
- # '0',
- # '40000',
- # '27000',
- # '29000',
- # '3431',
- # '3436',
- # 'Idle',
- # 'Normal',
- # 'Normal',
- # 'Normal',
- # '100%',
- # '36981',
- # '100%',
- # '14200320',
- # 'Normal',
- # 'Normal',
- # 'Normal',
- # 'Normal',
- # '27000',
- # '29000',
- # '102979',
- # '103022',
- # '27866',
- # ]
- columnsDefList = [
- None,
- 'timestamp_date',
- 'timestamp_time',
- 'voltage',
- 'current',
- 'temperature',
- 'min_cell_temperature',
- 'max_cell_temperature',
- 'min_cell_voltage',
- 'max_cell_voltage',
- 'base_state_label',
- 'voltage_state_label',
- 'current_state_label',
- 'temperature_state_label',
- 'soc',
- 'coulomb_ah',
- 'coulomb_percent',
- 'coulomb_kwh',
- 'cell_voltage_state_label',
- 'cell_temperature_state_label',
- 'unit_voltage_state_label',
- 'unit_temperature_state_label',
- 'min_unit_temperature',
- 'max_unit_temperature',
- 'min_unit_voltage',
- 'max_unit_voltage',
- 'avg_cell_temperature',
- ]
- columns = line.split()
- if (len(columns) < len(columnsDefList)):
- return None
- #endif
- columnValues = {}
- for columnIndex, columnName in enumerate(columnsDefList):
- if (columnName == None):
- continue
- #endif
- columnValues[columnName] = columns[columnIndex]
- #endfor
- columnValues['timestamp'] = dateutil.parser.parse('20%s %s' % (columnValues['timestamp_date'], columnValues['timestamp_time']))
- del columnValues['timestamp_date']
- del columnValues['timestamp_time']
- for columnName in [
- 'voltage',
- 'current',
- 'temperature',
- 'min_cell_temperature',
- 'max_cell_temperature',
- 'min_cell_voltage',
- 'max_cell_voltage',
- 'coulomb_ah',
- 'min_unit_temperature',
- 'max_unit_temperature',
- 'min_unit_voltage',
- 'max_unit_voltage',
- 'avg_cell_temperature',
- ]:
- columnValues[columnName] = float(columnValues[columnName]) / 1000
- #endfor
- columnValues['soc'] = float(columnValues['soc'].replace('%', ''))
- columnValues['coulomb_percent'] = float(columnValues['coulomb_percent'].replace('%', ''))
- columnValues['coulomb_kwh'] = float(columnValues['coulomb_kwh']) / 1000000
- return columnValues
- #enddef
- def parseData_stat(data):
- #print(data);return{}
- columnValues = {}
- for line in data.split("\n"):
- line = line.strip()
- if (line):
- #print(line)
- if (':' in line):
- (key, value) = line.split(":", 1)
- columnValues[key.strip()] = float(value.strip())
- #endif
- #endif
- #endfor
- return columnValues
- #enddef
- def parseData_sysinfo(data):
- #print(data);return{}
- columnValues = {}
- for line in data.split("\n"):
- line = line.strip()
- if (line):
- #print(line)
- if (':' in line):
- (key, value) = line.split(":", 1)
- value = value.strip().split()[0].strip()
- columnValues[key.strip()] = float(value) if isnumeric(value) else value
- #endif
- #endif
- #endfor
- return columnValues
- #enddef
- def parseData_bat(data):
- #print(data);return{}
- columnValues = {}
- batteries = []
- columnValues['uuid'] = str(uuid.uuid4())
- part = None
- for line in data.split("\n"):
- line = line.strip()
- if (line):
- #print(line)
- if (part == None and ':' in line):
- part = 'values-with-colons'
- #endif
- if (part == 'values-with-colons'):
- if (':' in line):
- (key, value) = line.split(":", 1)
- firstValue = value.strip().split()[0].strip()
- columnValues[key.strip()] = float(firstValue) if isnumeric(firstValue) else value.strip()
- elif (re.match('^Bat +Volt +Curr +Tempr.+', line)):
- part = 'batteries'
- continue
- #endif
- #endif
- if (part == 'batteries'):
- headerDef = 'Bat|Volt|Curr|Tempr|V.State|T.State|AH(%)|AH(mAH)|WH(%)|WH(mWH)|Bal'
- header = headerDef.split('|')
- #print(header)
- values = line.split()
- #print(values)
- #print(values)
- if len(values) == len(header):
- batteries.append(dict([(header[i], values[i]) for i, col in enumerate(header)]))
- batteries[-1]["parent_uuid"] = columnValues['uuid']
- batteries[-1]["AH(%)"] = batteries[-1]["AH(%)"].rstrip("%")
- batteries[-1]["WH(%)"] = batteries[-1]["WH(%)"].rstrip("%")
- else:
- part = 'end'
- #endif
- #endif
- #endif
- #endfor
- return {
- 'columnValues':columnValues,
- 'batteries':batteries,
- }
- #enddef
- def parseData_soh(data):
- #print(data);return{}
- columnValues = {}
- batteries = []
- columnValues['uuid'] = str(uuid.uuid4())
- part = None
- for line in data.split("\n"):
- line = line.strip()
- if (line):
- #print(line)
- if (part == None \
- # and ':' in line
- ):
- part = 'values-with-colons'
- #endif
- if (part == 'values-with-colons'):
- if (':' in line):
- (key, value) = line.split(":", 1)
- firstValue = value.strip().split()[0].strip()
- columnValues[key.strip()] = float(firstValue) if isnumeric(firstValue) else value.strip()
- elif (re.match('^Battery +Voltage +SOHCount.+', line)):
- part = 'batteries'
- continue
- #endif
- #endif
- if (part == 'batteries'):
- headerDef = 'Battery|Voltage|SOHCount|SOHStatus'
- header = headerDef.split('|')
- #print(header)
- values = line.split()
- #print(values)
- #print(values)
- if len(values) == len(header):
- batteries.append(dict([(header[i], values[i]) for i, col in enumerate(header)]))
- batteries[-1]["parent_uuid"] = columnValues['uuid']
- else:
- part = 'end'
- #endif
- #endif
- #endif
- #endfor
- return {
- 'columnValues':columnValues,
- 'batteries':batteries,
- }
- #enddef
- def parseData_log(data):
- #print(data);return{}
- records = []
- columnValues = {}
- for line in data.split("\n"):
- line = line.strip()
- if (line):
- #print(line)
- if (':' in line):
- (key, value) = line.split(":", 1)
- key = key.strip()
- value = value.strip()
- if (key == "Time"):
- value = dateutil.parser.parse('20%s' % value)
- #endif
- columnValues[key] = value
- #endif
- else:
- if (columnValues):
- records.append(columnValues)
- #endif
- columnValues = {}
- #endif
- #endfor
- if (columnValues):
- records.append(columnValues)
- #endif
- # pridat id jako kombinace casu a textu
- for record in records:
- record['uuid'] = hashlib.md5(("%s/%s" % (record['Time'].timestamp(), record['Info'])).encode('utf-8')).hexdigest()
- #endfor
- return records
- #enddef
- def parseData_data_X(data):
- #print(data);return{}
- columnValues = {}
- batteries = []
- units = []
- part = None
- for line in data.split("\n"):
- line = line.strip()
- if (line):
- #print(line)
- if (part == None and ':' in line):
- part = 'values-with-colons'
- #endif
- if (part == 'values-with-colons'):
- if (':' in line):
- (key, value) = line.split(":", 1)
- key = key.strip()
- value = value.strip()
- firstValue = value.split()[0].strip()
- if (key == "Time"):
- value = dateutil.parser.parse('20%s' % value)
- #endif
- columnValues[key] = float(firstValue) if isnumeric(firstValue) else value
- elif (re.match('^Battery +Volt +Tempr.+', line)):
- columnValues['uuid'] = hashlib.md5(("%s" % (columnValues['Time'].timestamp(), )).encode('utf-8')).hexdigest()
- part = 'batteries'
- continue
- #endif
- #endif
- if (part == 'batteries'):
- headerDef = 'Battery|Volt|Tempr|Volt. State|Temp. State|Coulomb(%)'
- header = headerDef.split('|')
- #print(header)
- values = line.split()
- #print(values)
- #print(values)
- if len(values) == len(header):
- batteries.append(dict([(header[i], values[i]) for i, col in enumerate(header)]))
- batteries[-1]["parent_uuid"] = columnValues['uuid']
- batteries[-1]["Coulomb(%)"] = batteries[-1]["Coulomb(%)"].rstrip("%")
- elif (re.match('^Unit +Volt +Tempr +BTLow +BTHigh.+', line)):
- part = 'units'
- continue
- #endif
- #endif
- if (part == 'units'):
- headerDef = 'Unit|Volt|Tempr|BTLow|BTHigh|BVLow|BVHigh|Volt. State|Temp. State|Coulomb(%)'
- header = headerDef.split('|')
- #print(header)
- values = line.split()
- #print(values)
- #print(values)
- if len(values) == len(header):
- units.append(dict([(header[i], values[i]) for i, col in enumerate(header)]))
- units[-1]["parent_uuid"] = columnValues['uuid']
- units[-1]["Coulomb(%)"] = units[-1]["Coulomb(%)"].rstrip("%")
- else:
- part = 'end'
- #endif
- #endif
- #endif
- #endfor
- return {
- 'columnValues':columnValues,
- 'batteries':batteries,
- 'units':units,
- }
- #enddef
- def parseData_data_event(data):
- return parseData_data_X(data)
- #enddef
- def parseData_data_history(data):
- return parseData_data_X(data)
- #enddef
- def parseData_data_misc(data):
- return parseData_data_X(data)
- #enddef
Advertisement
Add Comment
Please, Sign In to add comment