updateonly handling
This commit is contained in:
@ -115,30 +115,37 @@ class CmdInterpreter(cmd.Cmd):
|
|||||||
|
|
||||||
def do_add_ir(self, arg):
|
def do_add_ir(self, arg):
|
||||||
try:
|
try:
|
||||||
(label, unit, address, count, scanrate, readTopic) = self.splitterRe.split(arg)
|
(label, unit, address, count, scanrate, updateOnly, readTopic) = self.splitterRe.split(arg)
|
||||||
self.__println("Label: {0}".format(label))
|
self.__println("Label: {0}".format(label))
|
||||||
self.__println("Unit: {0}".format(unit))
|
self.__println("Unit: {0}".format(unit))
|
||||||
self.__println("Address: {0}".format(address))
|
self.__println("Address: {0}".format(address))
|
||||||
self.__println("Count: {0}".format(count))
|
self.__println("Count: {0}".format(count))
|
||||||
self.__println("ScanRate: {0}".format(scanrate))
|
self.__println("ScanRate: {0}".format(scanrate))
|
||||||
|
self.__println("UpdateOnly: {0}".format(updateOnly))
|
||||||
self.__println("ReadTopic: {0}".format(readTopic))
|
self.__println("ReadTopic: {0}".format(readTopic))
|
||||||
|
|
||||||
if readTopic == 'None':
|
if readTopic == 'None':
|
||||||
readTopic = None
|
readTopic = None
|
||||||
|
if updateOnly in ['true', 'True', 'yes', 'Yes']:
|
||||||
|
updateOnly = True
|
||||||
|
elif updateOnly in ['false', 'False', 'no', 'No']:
|
||||||
|
updateOnly = False
|
||||||
|
else:
|
||||||
|
raise CmdInterpreterException('updateOnly must be true or false, yes or no')
|
||||||
unit = parseIntArbitraryBase(unit)
|
unit = parseIntArbitraryBase(unit)
|
||||||
address = parseIntArbitraryBase(address)
|
address = parseIntArbitraryBase(address)
|
||||||
count = parseIntArbitraryBase(count)
|
count = parseIntArbitraryBase(count)
|
||||||
scanrate = float(scanrate)
|
scanrate = float(scanrate)
|
||||||
if scanrate == 0.0:
|
if scanrate == 0.0:
|
||||||
raise CmdInterpreterException('scanRate must not be zero')
|
raise CmdInterpreterException('scanRate must not be zero')
|
||||||
r = RegisterDatapoint.InputRegisterDatapoint(label, unit, address, count, datetime.timedelta(seconds=scanrate), readTopic)
|
r = RegisterDatapoint.InputRegisterDatapoint(label, unit, address, count, datetime.timedelta(seconds=scanrate), updateOnly, readTopic)
|
||||||
self.registers.append(r)
|
self.registers.append(r)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
self.__println("ERROR: {0!s}, {1!s}".format(e.__class__.__name__, e))
|
self.__println("ERROR: {0!s}, {1!s}".format(e.__class__.__name__, e))
|
||||||
|
|
||||||
def help_add_ir(self):
|
def help_add_ir(self):
|
||||||
self.__println("Usage: add <Label> <Unit> <Address> <Count> <ScanRate>")
|
self.__println("Usage: add <Label> <Unit> <Address> <Count> <ScanRate>")
|
||||||
self.__println(" <ReadTopic>")
|
self.__println(" <UpdateOnly> <ReadTopic>")
|
||||||
self.__println("Adds an input register")
|
self.__println("Adds an input register")
|
||||||
self.__println("DO NOT FORGET TO SAVE AFTERWARDS!")
|
self.__println("DO NOT FORGET TO SAVE AFTERWARDS!")
|
||||||
self.__println("---------------------------------------------------------------------")
|
self.__println("---------------------------------------------------------------------")
|
||||||
@ -147,34 +154,42 @@ class CmdInterpreter(cmd.Cmd):
|
|||||||
self.__println("<Address> Register address within the device")
|
self.__println("<Address> Register address within the device")
|
||||||
self.__println("<Count> Count of registers to be read in words")
|
self.__println("<Count> Count of registers to be read in words")
|
||||||
self.__println("<ScanRate> Scanrate in seconds (float)")
|
self.__println("<ScanRate> Scanrate in seconds (float)")
|
||||||
|
self.__println("<UpdateOnly> Publish only when value has changed")
|
||||||
self.__println("<ReadTopic> Topic to publish read data")
|
self.__println("<ReadTopic> Topic to publish read data")
|
||||||
|
|
||||||
def do_add_di(self, arg):
|
def do_add_di(self, arg):
|
||||||
try:
|
try:
|
||||||
(label, unit, address, count, scanrate, readTopic) = self.splitterRe.split(arg)
|
(label, unit, address, count, scanrate, updateOnly, readTopic) = self.splitterRe.split(arg)
|
||||||
self.__println("Label: {0}".format(label))
|
self.__println("Label: {0}".format(label))
|
||||||
self.__println("Unit: {0}".format(unit))
|
self.__println("Unit: {0}".format(unit))
|
||||||
self.__println("Address: {0}".format(address))
|
self.__println("Address: {0}".format(address))
|
||||||
self.__println("Count: {0}".format(count))
|
self.__println("Count: {0}".format(count))
|
||||||
self.__println("ScanRate: {0}".format(scanrate))
|
self.__println("ScanRate: {0}".format(scanrate))
|
||||||
|
self.__println("UpdateOnly: {0}".format(updateOnly))
|
||||||
self.__println("ReadTopic: {0}".format(readTopic))
|
self.__println("ReadTopic: {0}".format(readTopic))
|
||||||
|
|
||||||
if readTopic == 'None':
|
if readTopic == 'None':
|
||||||
readTopic = None
|
readTopic = None
|
||||||
|
if updateOnly in ['true', 'True', 'yes', 'Yes']:
|
||||||
|
updateOnly = True
|
||||||
|
elif updateOnly in ['false', 'False', 'no', 'No']:
|
||||||
|
updateOnly = False
|
||||||
|
else:
|
||||||
|
raise CmdInterpreterException('updateOnly must be true or false, yes or no')
|
||||||
unit = parseIntArbitraryBase(unit)
|
unit = parseIntArbitraryBase(unit)
|
||||||
address = parseIntArbitraryBase(address)
|
address = parseIntArbitraryBase(address)
|
||||||
count = parseIntArbitraryBase(count)
|
count = parseIntArbitraryBase(count)
|
||||||
scanrate = float(scanrate)
|
scanrate = float(scanrate)
|
||||||
if scanrate == 0.0:
|
if scanrate == 0.0:
|
||||||
raise CmdInterpreterException('scanRate must not be zero')
|
raise CmdInterpreterException('scanRate must not be zero')
|
||||||
r = RegisterDatapoint.DiscreteInputDatapoint(label, unit, address, count, datetime.timedelta(seconds=scanrate), readTopic)
|
r = RegisterDatapoint.DiscreteInputDatapoint(label, unit, address, count, datetime.timedelta(seconds=scanrate), updateOnly, readTopic)
|
||||||
self.registers.append(r)
|
self.registers.append(r)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
self.__println("ERROR: {0!s}, {1!s}".format(e.__class__.__name__, e))
|
self.__println("ERROR: {0!s}, {1!s}".format(e.__class__.__name__, e))
|
||||||
|
|
||||||
def help_add_di(self):
|
def help_add_di(self):
|
||||||
self.__println("Usage: add <Label> <Unit> <Address> <Count> <ScanRate>")
|
self.__println("Usage: add <Label> <Unit> <Address> <Count> <ScanRate>")
|
||||||
self.__println(" <ReadTopic>")
|
self.__println(" <UpdateOnly> <ReadTopic>")
|
||||||
self.__println("Adds a discrete input")
|
self.__println("Adds a discrete input")
|
||||||
self.__println("DO NOT FORGET TO SAVE AFTERWARDS!")
|
self.__println("DO NOT FORGET TO SAVE AFTERWARDS!")
|
||||||
self.__println("---------------------------------------------------------------------")
|
self.__println("---------------------------------------------------------------------")
|
||||||
@ -183,6 +198,7 @@ class CmdInterpreter(cmd.Cmd):
|
|||||||
self.__println("<Address> Register address within the device")
|
self.__println("<Address> Register address within the device")
|
||||||
self.__println("<Count> Count of registers to be read in words")
|
self.__println("<Count> Count of registers to be read in words")
|
||||||
self.__println("<ScanRate> Scanrate in seconds (float)")
|
self.__println("<ScanRate> Scanrate in seconds (float)")
|
||||||
|
self.__println("<UpdateOnly> Publish only when value has changed")
|
||||||
self.__println("<ReadTopic> Topic to publish read data")
|
self.__println("<ReadTopic> Topic to publish read data")
|
||||||
|
|
||||||
def do_list(self, arg):
|
def do_list(self, arg):
|
||||||
|
@ -86,8 +86,10 @@ class HoldingRegisterDatapoint(AbstractModbusDatapoint):
|
|||||||
|
|
||||||
|
|
||||||
class InputRegisterDatapoint(AbstractModbusDatapoint):
|
class InputRegisterDatapoint(AbstractModbusDatapoint):
|
||||||
def __init__(self, label, unit, address, count, scanRate, publishTopic):
|
def __init__(self, label, unit, address, count, scanRate, updateOnly, publishTopic):
|
||||||
super().__init__(label, unit, address, count, scanRate)
|
super().__init__(label, unit, address, count, scanRate)
|
||||||
|
self.updateOnly = updateOnly
|
||||||
|
self.lastValue = None
|
||||||
self.publishTopic = publishTopic
|
self.publishTopic = publishTopic
|
||||||
self.lastContact = None
|
self.lastContact = None
|
||||||
self.type = 'input register'
|
self.type = 'input register'
|
||||||
@ -105,8 +107,10 @@ class InputRegisterDatapoint(AbstractModbusDatapoint):
|
|||||||
unit=self.unit)
|
unit=self.unit)
|
||||||
if type(result) in [ExceptionResponse, ModbusIOException]:
|
if type(result) in [ExceptionResponse, ModbusIOException]:
|
||||||
raise DatapointException(result)
|
raise DatapointException(result)
|
||||||
# print("{0}: {1!s}".format(self.label, result.registers))
|
if not self.updateOnly or (result.registers != self.lastValue):
|
||||||
pubQueue.put(MqttProcessor.PublishItem(self.publishTopic, str(result.registers)))
|
self.lastValue = result.registers
|
||||||
|
# print("{0}: {1!s}".format(self.label, result.registers))
|
||||||
|
pubQueue.put(MqttProcessor.PublishItem(self.publishTopic, str(result.registers)))
|
||||||
|
|
||||||
if successFull:
|
if successFull:
|
||||||
self.lastContact = datetime.datetime.now()
|
self.lastContact = datetime.datetime.now()
|
||||||
@ -120,8 +124,10 @@ class InputRegisterDatapoint(AbstractModbusDatapoint):
|
|||||||
|
|
||||||
|
|
||||||
class DiscreteInputDatapoint(AbstractModbusDatapoint):
|
class DiscreteInputDatapoint(AbstractModbusDatapoint):
|
||||||
def __init__(self, label, unit, address, count, scanRate, publishTopic):
|
def __init__(self, label, unit, address, count, scanRate, updateOnly, publishTopic):
|
||||||
super().__init__(label, unit, address, count, scanRate)
|
super().__init__(label, unit, address, count, scanRate)
|
||||||
|
self.updateOnly = updateOnly
|
||||||
|
self.lastValue = None
|
||||||
self.publishTopic = publishTopic
|
self.publishTopic = publishTopic
|
||||||
self.lastContact = None
|
self.lastContact = None
|
||||||
self.type = 'discrete input'
|
self.type = 'discrete input'
|
||||||
@ -139,8 +145,9 @@ class DiscreteInputDatapoint(AbstractModbusDatapoint):
|
|||||||
unit=self.unit)
|
unit=self.unit)
|
||||||
if type(result) in [ExceptionResponse, ModbusIOException]:
|
if type(result) in [ExceptionResponse, ModbusIOException]:
|
||||||
raise DatapointException(result)
|
raise DatapointException(result)
|
||||||
# print("{0}: {1!s}".format(self.label, result.bits))
|
if not self.updateOnly or (result.registers != self.lastValue):
|
||||||
pubQueue.put(MqttProcessor.PublishItem(self.publishTopic, str(result.bits)))
|
# print("{0}: {1!s}".format(self.label, result.bits))
|
||||||
|
pubQueue.put(MqttProcessor.PublishItem(self.publishTopic, str(result.bits)))
|
||||||
|
|
||||||
if successFull:
|
if successFull:
|
||||||
self.lastContact = datetime.datetime.now()
|
self.lastContact = datetime.datetime.now()
|
||||||
|
@ -4,9 +4,9 @@ import pickle
|
|||||||
|
|
||||||
|
|
||||||
datapoints = [
|
datapoints = [
|
||||||
RegisterDatapoint.InputRegisterDatapoint('Temperature', 5, 0x0001, 1, datetime.timedelta(seconds=1.0), 'Pub/Temperature'),
|
RegisterDatapoint.InputRegisterDatapoint('Temperature', 5, 0x0001, 1, datetime.timedelta(seconds=1.0), True, 'Pub/Temperature'),
|
||||||
RegisterDatapoint.InputRegisterDatapoint('Humidity', 5, 0x0002, 1, datetime.timedelta(seconds=1.0), 'Pub/Humidity'),
|
RegisterDatapoint.InputRegisterDatapoint('Humidity', 5, 0x0002, 1, datetime.timedelta(seconds=1.0), True, 'Pub/Humidity'),
|
||||||
RegisterDatapoint.DiscreteInputDatapoint('Switches', 4, 0x0000, 1, datetime.timedelta(seconds=1.0), 'Pub/Switches'),
|
RegisterDatapoint.DiscreteInputDatapoint('Switches', 4, 0x0000, 1, datetime.timedelta(seconds=1.0), True, 'Pub/Switches'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user