diff --git a/src/RegisterDatapoint.py b/src/RegisterDatapoint.py index f837478..13dced1 100644 --- a/src/RegisterDatapoint.py +++ b/src/RegisterDatapoint.py @@ -6,28 +6,6 @@ import logging import json -class JsonifyEncoder(json.JSONEncoder): - def default(self, o): - res = None - try: - res = o.jsonify() - except (TypeError, AttributeError): - if type(o) == datetime.timedelta: - res = o.total_seconds() - else: - res = super().default(o) - return res - -def datapointObjectHook(j): - if type(j) == dict and 'type' in j and 'args' in j: - klass = eval(j['type']) - o = klass(**j['args']) - return o - else: - return j - - - class DatapointException(Exception): pass class AbstractModbusDatapoint(object): @@ -171,6 +149,27 @@ class DiscreteInputDatapoint(ReadOnlyDatapoint): + +class JsonifyEncoder(json.JSONEncoder): + def default(self, o): + res = None + try: + res = o.jsonify() + except (TypeError, AttributeError): + if type(o) == datetime.timedelta: + res = o.total_seconds() + else: + res = super().default(o) + return res + +def datapointObjectHook(j): + if type(j) == dict and 'type' in j and 'args' in j: + klass = eval(j['type']) + o = klass(**j['args']) + return o + else: + return j + def saveRegisterList(registerList, registerListFile): js = json.dumps(registerList, cls=JsonifyEncoder, sort_keys=True, indent=4) with open(registerListFile, 'w') as f: