satisfy pycodestyle checker

This commit is contained in:
Wolfgang Hottgenroth
2022-02-22 14:58:11 +01:00
parent 9e28ffacde
commit 4b8a15b454
8 changed files with 27 additions and 27 deletions

View File

@ -1,12 +1,14 @@
import re
import json
class InvalidDataObjectException(Exception):
def __init__(self, message):
super().__init__(message)
class AbstractDataObject(object):
invalidChars = re.compile("[#+\s]")
invalidChars = re.compile(r'[#+\s]')
def __init__(self, topicPart):
self.topicPart = topicPart
@ -17,4 +19,4 @@ class AbstractDataObject(object):
return self.topicPart
def getPayload(self):
raise NotImplementedError()
raise NotImplementedError()

View File

@ -4,14 +4,13 @@ from AbstractDataObject import AbstractDataObject
class FlatDataObject(AbstractDataObject):
def __init__(self, serverName, nameSpaceIndex, variableName, value):
super().__init__(serverName + '/' + str(nameSpaceIndex) + '/' + variableName)
self.serverName = serverName
self.nameSpaceIndex = nameSpaceIndex
self.variableName = variableName
self.value = value
def getPayload(self):
payload = {
"serverName": self.serverName,
@ -19,4 +18,4 @@ class FlatDataObject(AbstractDataObject):
"variableName": self.variableName,
"value": self.value
}
return json.dumps(payload)
return json.dumps(payload)

View File

@ -4,13 +4,14 @@ import threading
import ssl
def mqttOnConnectCallback(client, userdata, flags, rc):
userdata.onConnect()
def mqttOnMessageCallback(client, userdata, message):
userdata.onMessage(message.topic, message.payload)
def mqttOnDisconnectCallback(client, userdata, rc):
userdata.onDisconnect(rc)
@ -34,38 +35,37 @@ class AbstractMqttPublisher(threading.Thread):
if ("login" in self.config) and ("password" in self.config):
self.client.username_pw_set(self.config["login"], self.config["password"])
if ("ca" in self.config) and ("cert" in self.config) and ("key" in self.config):
self.client.tls_set(
ca_certs=self.config["ca"],
certfile=self.config["cert"],
keyfile=self.config["key"],
cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None # this does not mean "no cipher" but it means "default ciphers"
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None # this does not mean "no cipher" but it means "default ciphers"
)
elif ("ca" in self.config):
self.client.tls_set(
ca_certs=self.config["ca"],
cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None # this does not mean "no cipher" but it means "default ciphers"
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None # this does not mean "no cipher" but it means "default ciphers"
)
self.client.connect(self.config["broker"], int(self.config["port"]))
self.client.loop_start()
logger.info("mqtt loop started")
self.localLoop()
def localLoop(self):
raise NotImplementedError()
def stop(self):
self.client.loop_stop()
logger.info("mqtt loop stopped")
self.killBill = True
logger.info("kill flag set")
@ -74,10 +74,9 @@ class AbstractMqttPublisher(threading.Thread):
def onConnect(self):
logger.info("mqtt connected")
def onDisconnect(self, rc):
logger.warning("mqtt disconnect, rc: {}".format(rc))
def onMessage(self, topic, payload):
logger.warning("mqtt unexpected message received: {} -> {}".format(topic, str(payload)))

View File

@ -7,6 +7,7 @@ import json
LOOP_SLICE = 0.1 # seconds
class MqttPublish(AbstractMqttPublisher):
def __init__(self, config, stats, queue):
super().__init__(config)
@ -32,4 +33,3 @@ class MqttPublish(AbstractMqttPublisher):
except Exception as e:
self.stats.incMqttErrors()
logger.error(f"Exception {type(e)} received in MQTT local loop: {e}")

View File

@ -5,6 +5,7 @@ from loguru import logger
from FlatDataObject import FlatDataObject
from StructuredDataObject import StructuredDataObject
class OpcUaRequester(threading.Thread):
def __init__(self, config, stats, queue):
super().__init__()
@ -25,7 +26,6 @@ class OpcUaRequester(threading.Thread):
self.killBill = False
self.killEvent = threading.Event()
async def opcUaRequesterInnerLoop(self):
while not self.killBill:
try:
@ -69,4 +69,3 @@ class OpcUaRequester(threading.Thread):
self.killEvent.set()
logger.info("kill events triggered")

View File

@ -4,11 +4,12 @@ from AbstractDataObject import AbstractDataObject
import json
import datetime
class StatisticsDataObject(AbstractDataObject):
def __init__(self, topic, payload):
super().__init__(topic)
self.payload = payload
def getPayload(self):
return json.dumps(self.payload)
@ -27,7 +28,7 @@ class StatisticsCollector(threading.Thread):
self.stats = {
'opcUaRequests': 0,
'opcUaErrors' : 0,
'opcUaErrors': 0,
'opcUaTimeouts': 0,
'mqttRequests': 0,
'mqttErrors': 0,
@ -36,10 +37,10 @@ class StatisticsCollector(threading.Thread):
def incOpcUaRequests(self):
self.stats['opcUaRequests'] += 1
def incOpcUaErrors(self):
self.stats['opcUaErrors'] += 1
def incOpcUaTimeouts(self):
self.stats['opcUaTimeouts'] += 1
@ -63,4 +64,3 @@ class StatisticsCollector(threading.Thread):
self.stats['uptime'] = int((currentTime - startTime).total_seconds())
self.queue.put(StatisticsDataObject(self.topic, self.stats))
self.killEvent.wait(timeout=float(self.period))

View File

@ -4,13 +4,12 @@ from AbstractDataObject import AbstractDataObject
class StructuredDataObject(AbstractDataObject):
def __init__(self, topicPart):
super().__init__(topicPart)
self.keyValuePairs = []
def add(self, key, value):
self.keyValuePairs.append({key: value})
def getPayload(self):
return json.dumps(self.keyValuePairs)
return json.dumps(self.keyValuePairs)

View File

@ -12,12 +12,14 @@ import signal
deathBell = threading.Event()
def exceptHook(args):
global deathBell
logger.error("Exception in thread caught: {}".format(args))
deathBell.set()
logger.error("rang the death bell")
def terminateHook(sig, frame):
global deathBell
logger.error("SIGINT received")