fix style issues
This commit is contained in:
parent
77d01ca675
commit
94e60ee172
@ -12,6 +12,7 @@ import logging
|
|||||||
|
|
||||||
ERROR_PIN = 29
|
ERROR_PIN = 29
|
||||||
|
|
||||||
|
|
||||||
class CommunicationProcessor(threading.Thread):
|
class CommunicationProcessor(threading.Thread):
|
||||||
def __init__(self, config, queue, pubQueue):
|
def __init__(self, config, queue, pubQueue):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@ -33,7 +34,6 @@ class CommunicationProcessor(threading.Thread):
|
|||||||
return MyRS485.MyRS485(port=self.config.serialPort, baudrate=self.config.serialBaudRate, stopbits=1,
|
return MyRS485.MyRS485(port=self.config.serialPort, baudrate=self.config.serialBaudRate, stopbits=1,
|
||||||
timeout=1)
|
timeout=1)
|
||||||
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
client = ModbusSerialClient(method='rtu')
|
client = ModbusSerialClient(method='rtu')
|
||||||
client.socket = self.__getSerial()
|
client.socket = self.__getSerial()
|
||||||
@ -56,7 +56,3 @@ class CommunicationProcessor(threading.Thread):
|
|||||||
client.socket = self.__getSerial()
|
client.socket = self.__getSerial()
|
||||||
finally:
|
finally:
|
||||||
time.sleep(self.config.interCommDelay)
|
time.sleep(self.config.interCommDelay)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,4 +28,3 @@ Converters = {
|
|||||||
"out": None
|
"out": None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ import MqttProcessor
|
|||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
class Heartbeat(threading.Thread):
|
class Heartbeat(threading.Thread):
|
||||||
def __init__(self, config, pubQueue):
|
def __init__(self, config, pubQueue):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -4,6 +4,7 @@ from NotificationForwarder import AbstractNotificationReceiver
|
|||||||
import logging
|
import logging
|
||||||
import Pins
|
import Pins
|
||||||
|
|
||||||
|
|
||||||
class PublishItem(object):
|
class PublishItem(object):
|
||||||
def __init__(self, topic, payload):
|
def __init__(self, topic, payload):
|
||||||
self.topic = topic
|
self.topic = topic
|
||||||
@ -12,15 +13,19 @@ class PublishItem(object):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Topic: {0}, Payload: {1}'.format(self.topic, self.payload)
|
return 'Topic: {0}, Payload: {1}'.format(self.topic, self.payload)
|
||||||
|
|
||||||
|
|
||||||
def mqttOnConnectCallback(client, userdata, flags, rc):
|
def mqttOnConnectCallback(client, userdata, flags, rc):
|
||||||
userdata.onConnect()
|
userdata.onConnect()
|
||||||
|
|
||||||
|
|
||||||
def mqttOnMessageCallback(client, userdata, message):
|
def mqttOnMessageCallback(client, userdata, message):
|
||||||
userdata.onMessage(message.topic, message.payload)
|
userdata.onMessage(message.topic, message.payload)
|
||||||
|
|
||||||
|
|
||||||
def mqttOnDisconnectCallback(client, userdata, rc):
|
def mqttOnDisconnectCallback(client, userdata, rc):
|
||||||
userdata.onDisconnect(rc)
|
userdata.onDisconnect(rc)
|
||||||
|
|
||||||
|
|
||||||
class MqttProcessor(threading.Thread, AbstractNotificationReceiver):
|
class MqttProcessor(threading.Thread, AbstractNotificationReceiver):
|
||||||
def __init__(self, config, registers, queue, pubQueue):
|
def __init__(self, config, registers, queue, pubQueue):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@ -37,7 +42,8 @@ class MqttProcessor(threading.Thread, AbstractNotificationReceiver):
|
|||||||
def __processUpdatedRegisters(self, force=False):
|
def __processUpdatedRegisters(self, force=False):
|
||||||
self.logger.debug("MqttProcessor.__updateSubscriptions")
|
self.logger.debug("MqttProcessor.__updateSubscriptions")
|
||||||
|
|
||||||
subscribeTopics = [ r.subscribeTopic for r in self.registers if hasattr(r,'subscribeTopic') and r.subscribeTopic]
|
subscribeTopics = [r.subscribeTopic for r in self.registers if hasattr(r, 'subscribeTopic')
|
||||||
|
and r.subscribeTopic]
|
||||||
self.logger.debug("Topics: {0!s}".format(subscribeTopics))
|
self.logger.debug("Topics: {0!s}".format(subscribeTopics))
|
||||||
|
|
||||||
for subscribeTopic in subscribeTopics:
|
for subscribeTopic in subscribeTopics:
|
||||||
@ -52,7 +58,8 @@ class MqttProcessor(threading.Thread, AbstractNotificationReceiver):
|
|||||||
self.client.unsubscribe(subscription)
|
self.client.unsubscribe(subscription)
|
||||||
self.subscriptions.remove(subscription)
|
self.subscriptions.remove(subscription)
|
||||||
|
|
||||||
self.topicRegisterMap = { r.subscribeTopic: r for r in self.registers if hasattr(r,'subscribeTopic') and r.subscribeTopic }
|
self.topicRegisterMap = {r.subscribeTopic: r for r in self.registers if hasattr(r, 'subscribeTopic')
|
||||||
|
and r.subscribeTopic}
|
||||||
|
|
||||||
def receiveNotification(self, arg):
|
def receiveNotification(self, arg):
|
||||||
self.logger.info("MqttProcessor:registersChanged")
|
self.logger.info("MqttProcessor:registersChanged")
|
||||||
@ -76,7 +83,6 @@ class MqttProcessor(threading.Thread, AbstractNotificationReceiver):
|
|||||||
else:
|
else:
|
||||||
self.logger.error("Invalid object in publish queue")
|
self.logger.error("Invalid object in publish queue")
|
||||||
|
|
||||||
|
|
||||||
def onConnect(self):
|
def onConnect(self):
|
||||||
# print("MqttProcessor.onConnect")
|
# print("MqttProcessor.onConnect")
|
||||||
self.__processUpdatedRegisters(force=True)
|
self.__processUpdatedRegisters(force=True)
|
||||||
@ -91,4 +97,3 @@ class MqttProcessor(threading.Thread, AbstractNotificationReceiver):
|
|||||||
self.logger.debug("{0}: {1!s} -> {2!s}".format(topic, payload, r))
|
self.logger.debug("{0}: {1!s} -> {2!s}".format(topic, payload, r))
|
||||||
r.onMessage(payload)
|
r.onMessage(payload)
|
||||||
self.queue.put(r)
|
self.queue.put(r)
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ class MyPriorityQueueItem(object):
|
|||||||
def __gt__(self, other): return self.itemWithPriority.priority > other.itemWithPriority.priority
|
def __gt__(self, other): return self.itemWithPriority.priority > other.itemWithPriority.priority
|
||||||
def __ge__(self, other): return self.itemWithPriority.priority >= other.itemWithPriority.priority
|
def __ge__(self, other): return self.itemWithPriority.priority >= other.itemWithPriority.priority
|
||||||
|
|
||||||
|
|
||||||
class MyPriorityQueue(queue.PriorityQueue):
|
class MyPriorityQueue(queue.PriorityQueue):
|
||||||
def _put(self, itemWithPriority):
|
def _put(self, itemWithPriority):
|
||||||
i = MyPriorityQueueItem(itemWithPriority)
|
i = MyPriorityQueueItem(itemWithPriority)
|
||||||
|
@ -7,6 +7,7 @@ import termios
|
|||||||
|
|
||||||
DE_PIN = 0
|
DE_PIN = 0
|
||||||
|
|
||||||
|
|
||||||
class MyRS485(serial.Serial):
|
class MyRS485(serial.Serial):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
@ -24,4 +25,3 @@ class MyRS485(serial.Serial):
|
|||||||
break
|
break
|
||||||
# wiringpi.digitalWrite(DE_PIN, wiringpi.LOW)
|
# wiringpi.digitalWrite(DE_PIN, wiringpi.LOW)
|
||||||
Pins.pinsWrite('DE', False)
|
Pins.pinsWrite('DE', False)
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ class AbstractNotificationReceiver(object):
|
|||||||
def receiveNotification(self, arg):
|
def receiveNotification(self, arg):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class NotificationForwarder(object):
|
class NotificationForwarder(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.receivers = []
|
self.receivers = []
|
||||||
|
@ -8,7 +8,6 @@ PINS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def pinsInit():
|
def pinsInit():
|
||||||
wiringpi.wiringPiSetup()
|
wiringpi.wiringPiSetup()
|
||||||
for pin in PINS.values():
|
for pin in PINS.values():
|
||||||
@ -21,4 +20,3 @@ def pinsWrite(pinName, v):
|
|||||||
else:
|
else:
|
||||||
pinState = wiringpi.LOW
|
pinState = wiringpi.LOW
|
||||||
wiringpi.digitalWrite(PINS[pinName], pinState)
|
wiringpi.digitalWrite(PINS[pinName], pinState)
|
||||||
|
|
||||||
|
@ -6,7 +6,10 @@ import logging
|
|||||||
import json
|
import json
|
||||||
import Converters
|
import Converters
|
||||||
|
|
||||||
class DatapointException(Exception): pass
|
|
||||||
|
class DatapointException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AbstractModbusDatapoint(object):
|
class AbstractModbusDatapoint(object):
|
||||||
def __init__(self, label=None, unit=None, address=None, count=None, scanRate=None, converter=None):
|
def __init__(self, label=None, unit=None, address=None, count=None, scanRate=None, converter=None):
|
||||||
@ -48,7 +51,6 @@ class AbstractModbusDatapoint(object):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class HoldingRegisterDatapoint(AbstractModbusDatapoint):
|
class HoldingRegisterDatapoint(AbstractModbusDatapoint):
|
||||||
def __init__(self, label=None, unit=None, address=None, count=None, scanRate=None,
|
def __init__(self, label=None, unit=None, address=None, count=None, scanRate=None,
|
||||||
publishTopic=None, subscribeTopic=None, feedbackTopic=None, converter=None):
|
publishTopic=None, subscribeTopic=None, feedbackTopic=None, converter=None):
|
||||||
@ -175,7 +177,8 @@ class CoilDatapoint(AbstractModbusDatapoint):
|
|||||||
|
|
||||||
|
|
||||||
class ReadOnlyDatapoint(AbstractModbusDatapoint):
|
class ReadOnlyDatapoint(AbstractModbusDatapoint):
|
||||||
def __init__(self, label=None, unit=None, address=None, count=None, scanRate=None, updateOnly=None, publishTopic=None, converter=None):
|
def __init__(self, label=None, unit=None, address=None, count=None, scanRate=None, updateOnly=None,
|
||||||
|
publishTopic=None, converter=None):
|
||||||
super().__init__(label, unit, address, count, scanRate, converter)
|
super().__init__(label, unit, address, count, scanRate, converter)
|
||||||
self.argList = self.argList + ['updateOnly', 'publishTopic']
|
self.argList = self.argList + ['updateOnly', 'publishTopic']
|
||||||
self.updateOnly = updateOnly
|
self.updateOnly = updateOnly
|
||||||
@ -188,7 +191,6 @@ class ReadOnlyDatapoint(AbstractModbusDatapoint):
|
|||||||
self.lastValue))
|
self.lastValue))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class InputRegisterDatapoint(ReadOnlyDatapoint):
|
class InputRegisterDatapoint(ReadOnlyDatapoint):
|
||||||
def __init__(self, label=None, unit=None, address=None, count=None, scanRate=None, updateOnly=None,
|
def __init__(self, label=None, unit=None, address=None, count=None, scanRate=None, updateOnly=None,
|
||||||
publishTopic=None, converter=None):
|
publishTopic=None, converter=None):
|
||||||
@ -253,12 +255,11 @@ class DiscreteInputDatapoint(ReadOnlyDatapoint):
|
|||||||
self.lastValues[i] = result.getBit(i)
|
self.lastValues[i] = result.getBit(i)
|
||||||
logger.debug("{0}, {1}: changed: {2!s}".format(self.label, i, result.getBit(i)))
|
logger.debug("{0}, {1}: changed: {2!s}".format(self.label, i, result.getBit(i)))
|
||||||
if self.publishTopic:
|
if self.publishTopic:
|
||||||
pubQueue.put(MqttProcessor.PublishItem("{0}/{1}".format(self.publishTopic, i), str(result.getBit(i))))
|
pubQueue.put(MqttProcessor.PublishItem("{0}/{1}"
|
||||||
|
.format(self.publishTopic, i), str(result.getBit(i))))
|
||||||
self.lastContact = datetime.datetime.now()
|
self.lastContact = datetime.datetime.now()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class JsonifyEncoder(json.JSONEncoder):
|
class JsonifyEncoder(json.JSONEncoder):
|
||||||
def default(self, o):
|
def default(self, o):
|
||||||
res = None
|
res = None
|
||||||
@ -271,6 +272,7 @@ class JsonifyEncoder(json.JSONEncoder):
|
|||||||
res = super().default(o)
|
res = super().default(o)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
def datapointObjectHook(j):
|
def datapointObjectHook(j):
|
||||||
if type(j) == dict and 'type' in j and 'args' in j:
|
if type(j) == dict and 'type' in j and 'args' in j:
|
||||||
klass = eval(j['type'])
|
klass = eval(j['type'])
|
||||||
@ -279,14 +281,15 @@ def datapointObjectHook(j):
|
|||||||
else:
|
else:
|
||||||
return j
|
return j
|
||||||
|
|
||||||
|
|
||||||
def saveRegisterList(registerList, registerListFile):
|
def saveRegisterList(registerList, registerListFile):
|
||||||
js = json.dumps(registerList, cls=JsonifyEncoder, sort_keys=True, indent=4)
|
js = json.dumps(registerList, cls=JsonifyEncoder, sort_keys=True, indent=4)
|
||||||
with open(registerListFile, 'w') as f:
|
with open(registerListFile, 'w') as f:
|
||||||
f.write(js)
|
f.write(js)
|
||||||
|
|
||||||
|
|
||||||
def loadRegisterList(registerListFile):
|
def loadRegisterList(registerListFile):
|
||||||
with open(registerListFile, 'r') as f:
|
with open(registerListFile, 'r') as f:
|
||||||
js = f.read()
|
js = f.read()
|
||||||
registerList = json.loads(js, object_hook=datapointObjectHook)
|
registerList = json.loads(js, object_hook=datapointObjectHook)
|
||||||
return registerList
|
return registerList
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ import datetime
|
|||||||
from NotificationForwarder import AbstractNotificationReceiver
|
from NotificationForwarder import AbstractNotificationReceiver
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
class ScanRateConsideringQueueFeeder(threading.Thread, AbstractNotificationReceiver):
|
class ScanRateConsideringQueueFeeder(threading.Thread, AbstractNotificationReceiver):
|
||||||
def __init__(self, config, registers, queue):
|
def __init__(self, config, registers, queue):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -4,12 +4,14 @@ import pickle
|
|||||||
|
|
||||||
|
|
||||||
datapoints = [
|
datapoints = [
|
||||||
RegisterDatapoint.InputRegisterDatapoint('Temperature', 5, 0x0001, 1, datetime.timedelta(seconds=1.0), False, 'Pub/Temperature'),
|
RegisterDatapoint.InputRegisterDatapoint('Temperature', 5, 0x0001, 1,
|
||||||
RegisterDatapoint.InputRegisterDatapoint('Humidity', 5, 0x0002, 1, datetime.timedelta(seconds=1.0), True, 'Pub/Humidity'),
|
datetime.timedelta(seconds=1.0), False, 'Pub/Temperature'),
|
||||||
RegisterDatapoint.DiscreteInputDatapoint('Switches', 4, 0x0000, 1, datetime.timedelta(seconds=1.0), True, 'Pub/Switches'),
|
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'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
with open('registers.pkl', 'wb') as f:
|
with open('registers.pkl', 'wb') as f:
|
||||||
pickle.dump(datapoints, f)
|
pickle.dump(datapoints, f)
|
||||||
|
|
||||||
|
@ -36,7 +36,6 @@ if __name__ == "__main__":
|
|||||||
nf = NotificationForwarder.NotificationForwarder()
|
nf = NotificationForwarder.NotificationForwarder()
|
||||||
logger.debug('infrastructure prepared')
|
logger.debug('infrastructure prepared')
|
||||||
|
|
||||||
|
|
||||||
datapoints = RegisterDatapoint.loadRegisterList(config.registerFile)
|
datapoints = RegisterDatapoint.loadRegisterList(config.registerFile)
|
||||||
logger.debug('datapoints read')
|
logger.debug('datapoints read')
|
||||||
|
|
||||||
|
@ -15,10 +15,7 @@ for dp in datapoints:
|
|||||||
ndp.__dict__[k] = v
|
ndp.__dict__[k] = v
|
||||||
newDatapoints.append(ndp)
|
newDatapoints.append(ndp)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
js = json.dumps(newDatapoints, cls=RegisterDatapoint.JsonifyEncoder, sort_keys=True, indent=4)
|
js = json.dumps(newDatapoints, cls=RegisterDatapoint.JsonifyEncoder, sort_keys=True, indent=4)
|
||||||
print(js)
|
print(js)
|
||||||
|
|
||||||
|
|
||||||
RegisterDatapoint.saveRegisterList(newDatapoints, 'registers.json')
|
RegisterDatapoint.saveRegisterList(newDatapoints, 'registers.json')
|
Loading…
x
Reference in New Issue
Block a user