error count and error signal

This commit is contained in:
2019-07-10 12:08:31 +02:00
parent 87e2e65ce2
commit a1acf04dbe
2 changed files with 16 additions and 2 deletions

View File

@ -3,6 +3,10 @@ import datetime
import RS485Ext import RS485Ext
import RegisterDatapoint import RegisterDatapoint
from pymodbus.client.sync import ModbusSerialClient from pymodbus.client.sync import ModbusSerialClient
import wiringpi2
ERROR_PIN = 29
class CommunicationProcessor(threading.Thread): class CommunicationProcessor(threading.Thread):
def __init__(self, config, queue, pubQueue): def __init__(self, config, queue, pubQueue):
@ -10,6 +14,8 @@ class CommunicationProcessor(threading.Thread):
self.config = config self.config = config
self.queue = queue self.queue = queue
self.pubQueue = pubQueue self.pubQueue = pubQueue
wiringpi2.wiringPiSetup()
wiringpi2.pinMode(ERROR_PIN, wiringpi2.OUTPUT)
self.daemon = True self.daemon = True
def __getSerial(self): def __getSerial(self):
@ -25,10 +31,12 @@ class CommunicationProcessor(threading.Thread):
while True: while True:
r = self.queue.get() r = self.queue.get()
try: try:
wiringpi2.digitalWrite(ERROR_PIN, wiringpi2.LOW)
print("Dequeued: {0!s}".format(r)) print("Dequeued: {0!s}".format(r))
r.enqueued = False r.enqueued = False
r.process(client, self.pubQueue) r.process(client, self.pubQueue)
except RegisterDatapoint.DatapointException as e: except RegisterDatapoint.DatapointException as e:
wiringpi2.digitalWrite(ERROR_PIN, wiringpi2.HIGH)
print("ERROR when processing '{0}': {1!s}".format(r.label, e)) print("ERROR when processing '{0}': {1!s}".format(r.label, e))
if client.socket is None: if client.socket is None:
print("renew socket") print("renew socket")

View File

@ -16,6 +16,7 @@ class AbstractModbusDatapoint(object):
self.type = 'abstract data point' self.type = 'abstract data point'
self.enqueued = False self.enqueued = False
self.lastContact = None self.lastContact = None
self.errorCount = 0
if self.scanRate: if self.scanRate:
self.priority = 1 self.priority = 1
else: else:
@ -23,9 +24,10 @@ class AbstractModbusDatapoint(object):
def __str__(self): def __str__(self):
return ("{0}, {1}: unit: {2}, address: {3}, count: {4}, scanRate: {5}, " return ("{0}, {1}: unit: {2}, address: {3}, count: {4}, scanRate: {5}, "
"enqueued: {6}, lastContact: {7}" "enqueued: {6}, lastContact: {7}, errorCount: {8}"
.format(self.type, self.label, self.unit, self.address, self.count, .format(self.type, self.label, self.unit, self.address, self.count,
self.scanRate, self.enqueued, self.lastContact)) self.scanRate, self.enqueued, self.lastContact,
self.errorCount))
def process(self, client): def process(self, client):
raise NotImplementedError raise NotImplementedError
@ -67,6 +69,7 @@ class HoldingRegisterDatapoint(AbstractModbusDatapoint):
count=self.count, count=self.count,
unit=self.unit) unit=self.unit)
if type(result) in [ExceptionResponse, ModbusIOException]: if type(result) in [ExceptionResponse, ModbusIOException]:
self.errorCount += 1
raise DatapointException(result) raise DatapointException(result)
print("{0}: {1!s}".format(self.label, result.registers)) print("{0}: {1!s}".format(self.label, result.registers))
pubQueue.put(MqttProcessor.PublishItem(self.publishTopic, str(result.registers))) pubQueue.put(MqttProcessor.PublishItem(self.publishTopic, str(result.registers)))
@ -112,6 +115,7 @@ class InputRegisterDatapoint(ReadOnlyDatapoint):
count=self.count, count=self.count,
unit=self.unit) unit=self.unit)
if type(result) in [ExceptionResponse, ModbusIOException]: if type(result) in [ExceptionResponse, ModbusIOException]:
self.errorCount += 1
raise DatapointException(result) raise DatapointException(result)
if not self.updateOnly or (result.registers != self.lastValue): if not self.updateOnly or (result.registers != self.lastValue):
self.lastValue = result.registers self.lastValue = result.registers
@ -143,6 +147,7 @@ class DiscreteInputDatapoint(ReadOnlyDatapoint):
count=self.count, count=self.count,
unit=self.unit) unit=self.unit)
if type(result) in [ExceptionResponse, ModbusIOException]: if type(result) in [ExceptionResponse, ModbusIOException]:
self.errorCount += 1
raise DatapointException(result) raise DatapointException(result)
if not self.updateOnly or (result.bits != self.lastValue): if not self.updateOnly or (result.bits != self.lastValue):
self.lastValue = result.bits self.lastValue = result.bits
@ -164,6 +169,7 @@ class DiscreteInputDatapoint(ReadOnlyDatapoint):
def checkRegisterList(registers): def checkRegisterList(registers):
for r in registers: for r in registers:
if not isinstance(r, AbstractModbusDatapoint): if not isinstance(r, AbstractModbusDatapoint):
r.errorCount = 0
raise ValueError('Entry in register list {0!s} is not derived from class AbstractModbusDatapoint'.format(r)) raise ValueError('Entry in register list {0!s} is not derived from class AbstractModbusDatapoint'.format(r))