greater rewrite of some time ago, commited for reference

This commit is contained in:
Georg Schlisio
2022-01-20 10:09:46 +01:00
parent 89e163a3fd
commit 6be90ff148
2 changed files with 122 additions and 58 deletions

View File

@@ -1,33 +1,72 @@
import time
import logging
class ParseMessage():
parms = {}
raw=None
table_commands = ["Sensors", "EGains", "InletInfo", "DetectorInfo", "AnalogInputInfo",
"AnalogOutputInfo", "DigitalInfo", "SourceAlignmentInfo", "SourceResolutionInfo",
"DiagnosticInputInfo", "RunDiagnostics", ""]
verbose_commands = ["DiagnosticInput", "DigitalPortChange", "AnalogInput", "MassReading", "ZeroReading",
"StartingScan", "FilamentStatus", ""] # commands that feel special and don't submit to the usual pattern of command - status
def __init__(self, raw):
"""
Parse incoming binary message
:param raw: raw string
"""
self.time = time.time()
self.is_table = False
self.type = None
self.status = None
self.parms = {}
self.table_head = None
self.table_body = None
self.raw = raw
self.parse()
self.parse(raw)
def parse(self):
def parse(self, raw):
"""
Parser for incoming messages.
:return:
"""
for i, line in enumerate(self.raw.split("\r\n")):
entries = line.split()
if entries == []:
continue
if i == 0:
self.type = entries[0]
if len(entries) < 2:
raise NotImplementedError()
elif len(entries) == 2:
self.parms[entries[0]] = entries[1]
else:
self.parms[entries[0]] = entries[1:]
# split message at newline indicators and get rid of empty entries
linebyline = list(filter(None, raw.split("\r\n")))
# first line contains command and status or more
line1 = linebyline[0].split()
self.type = line1[0]
if self.type in self.table_commands:
self.is_table = True
if len(line1) == 1:
pass
elif len(line1) == 2:
self.type = line1[0]
self.status = line1[1]
else:
assert self.type in self.verbose_commands
self.parms[self.type] = line1[1:]
# terminate here for one-line commands
if len(linebyline) == 1:
return
# following lines
if self.is_table:
# for table commands we split table head and body
thead = linebyline[1].split()
try:
tbody = [linebyline[i].split() for i in range(len(linebyline[2:]))]
except IndexError:
logging.warning(f"Digesting message {self.type}: no table body found")
tbody = []
self.table_head = thead
self.table_body = tbody
else:
# for other commands we make a dictionary with key [values, ..]
try:
self.parms.update({line.split()[0]: line.split()[1:] for line in linebyline[1:]})
except IndexError:
raise NotImplementedError("this should not happen")
class ComposeMessage():
def __init__(self):
@@ -65,14 +104,19 @@ if __name__ == "__main__":
"InletInfo": 'InletInfo OK\r\n Automatic Yes \r\nActiveInlet 0\r\n Factor Fixed CanCalibrate DefaultFactor TypeName\r\n 1 Yes No 1 "Process Chamber direct"\r\n',
"Release": "Release Ok\r\n",
#"AcceptProtocol":
"initiate": "MKSRGA Single\r\n Protocol_Revision 1.1\r\n Min_Compatibility 1.1\r\n\r\n\r\r",
"initiate": "MKSRGA Single\r\n Protocol_Revision 1.1\r\n Min_Compatibility 1.1\r\n\r\n",
"Error": 'command ERROR\r\n Number 200\r\n Description "err description"\r\n\r\n',
"Sensors": "Sensors OK\r\n State SerialNumber Name\r\n Ready LM70-00197021 “Chamber A”\r\n Ready LM70-00198021 “Chamber B”\r\n\r\n",
"DiagnosticInput": "DiagnosticInput 0 3.2745\r\n\r\n",
"FilamentStatus": "FilamentStatus 1 OFF\r\n Trip None\r\n Drive Off\r\n EmissionTripState OK\r\n ExternalTripState OK\r\n RVCTripState OK",
}
cm = ComposeMessage()
#bmess = cm(mtype="Select", args=["LM70-00197021"])
#smess = bmess.decode("ASCII")
pm = ParseMessage(sample_messages["InletInfo"])
for m in sample_messages:
pm = ParseMessage(sample_messages[m])
print(pm.type, pm.status, pm.is_table)
print(pm.type, pm.parms, pm.time)
#print(pm.type, pm.parms, pm.time)