updateonly handling

This commit is contained in:
2019-07-08 16:25:13 +02:00
parent 99238720c9
commit e1750e5387
3 changed files with 38 additions and 15 deletions

View File

@ -115,30 +115,37 @@ class CmdInterpreter(cmd.Cmd):
def do_add_ir(self, arg):
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("Unit: {0}".format(unit))
self.__println("Address: {0}".format(address))
self.__println("Count: {0}".format(count))
self.__println("ScanRate: {0}".format(scanrate))
self.__println("UpdateOnly: {0}".format(updateOnly))
self.__println("ReadTopic: {0}".format(readTopic))
if 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)
address = parseIntArbitraryBase(address)
count = parseIntArbitraryBase(count)
scanrate = float(scanrate)
if scanrate == 0.0:
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)
except ValueError as e:
self.__println("ERROR: {0!s}, {1!s}".format(e.__class__.__name__, e))
def help_add_ir(self):
self.__println("Usage: add <Label> <Unit> <Address> <Count> <ScanRate>")
self.__println(" <ReadTopic>")
self.__println(" <UpdateOnly> <ReadTopic>")
self.__println("Adds an input register")
self.__println("DO NOT FORGET TO SAVE AFTERWARDS!")
self.__println("---------------------------------------------------------------------")
@ -147,34 +154,42 @@ class CmdInterpreter(cmd.Cmd):
self.__println("<Address> Register address within the device")
self.__println("<Count> Count of registers to be read in words")
self.__println("<ScanRate> Scanrate in seconds (float)")
self.__println("<UpdateOnly> Publish only when value has changed")
self.__println("<ReadTopic> Topic to publish read data")
def do_add_di(self, arg):
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("Unit: {0}".format(unit))
self.__println("Address: {0}".format(address))
self.__println("Count: {0}".format(count))
self.__println("ScanRate: {0}".format(scanrate))
self.__println("UpdateOnly: {0}".format(updateOnly))
self.__println("ReadTopic: {0}".format(readTopic))
if 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)
address = parseIntArbitraryBase(address)
count = parseIntArbitraryBase(count)
scanrate = float(scanrate)
if scanrate == 0.0:
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)
except ValueError as e:
self.__println("ERROR: {0!s}, {1!s}".format(e.__class__.__name__, e))
def help_add_di(self):
self.__println("Usage: add <Label> <Unit> <Address> <Count> <ScanRate>")
self.__println(" <ReadTopic>")
self.__println(" <UpdateOnly> <ReadTopic>")
self.__println("Adds a discrete input")
self.__println("DO NOT FORGET TO SAVE AFTERWARDS!")
self.__println("---------------------------------------------------------------------")
@ -183,6 +198,7 @@ class CmdInterpreter(cmd.Cmd):
self.__println("<Address> Register address within the device")
self.__println("<Count> Count of registers to be read in words")
self.__println("<ScanRate> Scanrate in seconds (float)")
self.__println("<UpdateOnly> Publish only when value has changed")
self.__println("<ReadTopic> Topic to publish read data")
def do_list(self, arg):

View File

@ -86,8 +86,10 @@ class HoldingRegisterDatapoint(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)
self.updateOnly = updateOnly
self.lastValue = None
self.publishTopic = publishTopic
self.lastContact = None
self.type = 'input register'
@ -105,6 +107,8 @@ class InputRegisterDatapoint(AbstractModbusDatapoint):
unit=self.unit)
if type(result) in [ExceptionResponse, ModbusIOException]:
raise DatapointException(result)
if not self.updateOnly or (result.registers != self.lastValue):
self.lastValue = result.registers
# print("{0}: {1!s}".format(self.label, result.registers))
pubQueue.put(MqttProcessor.PublishItem(self.publishTopic, str(result.registers)))
@ -120,8 +124,10 @@ class InputRegisterDatapoint(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)
self.updateOnly = updateOnly
self.lastValue = None
self.publishTopic = publishTopic
self.lastContact = None
self.type = 'discrete input'
@ -139,6 +145,7 @@ class DiscreteInputDatapoint(AbstractModbusDatapoint):
unit=self.unit)
if type(result) in [ExceptionResponse, ModbusIOException]:
raise DatapointException(result)
if not self.updateOnly or (result.registers != self.lastValue):
# print("{0}: {1!s}".format(self.label, result.bits))
pubQueue.put(MqttProcessor.PublishItem(self.publishTopic, str(result.bits)))

View File

@ -4,9 +4,9 @@ import pickle
datapoints = [
RegisterDatapoint.InputRegisterDatapoint('Temperature', 5, 0x0001, 1, datetime.timedelta(seconds=1.0), 'Pub/Temperature'),
RegisterDatapoint.InputRegisterDatapoint('Humidity', 5, 0x0002, 1, datetime.timedelta(seconds=1.0), 'Pub/Humidity'),
RegisterDatapoint.DiscreteInputDatapoint('Switches', 4, 0x0000, 1, datetime.timedelta(seconds=1.0), 'Pub/Switches'),
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), True, 'Pub/Humidity'),
RegisterDatapoint.DiscreteInputDatapoint('Switches', 4, 0x0000, 1, datetime.timedelta(seconds=1.0), True, 'Pub/Switches'),
]