67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
![]() |
from loguru import logger
|
||
|
import threading
|
||
|
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)
|
||
|
|
||
|
|
||
|
class StatisticsCollector(threading.Thread):
|
||
|
def __init__(self, config, queue):
|
||
|
super().__init__()
|
||
|
|
||
|
self.config = config['stats']
|
||
|
self.period = self.config['period']
|
||
|
self.topic = self.config['topic']
|
||
|
self.queue = queue
|
||
|
|
||
|
self.killBill = False
|
||
|
self.killEvent = threading.Event()
|
||
|
|
||
|
self.stats = {
|
||
|
'opcUaRequests': 0,
|
||
|
'opcUaErrors' : 0,
|
||
|
'opcUaTimeouts': 0,
|
||
|
'mqttRequests': 0,
|
||
|
'mqttErrors': 0,
|
||
|
'uptime': 0
|
||
|
}
|
||
|
|
||
|
def incOpcUaRequests(self):
|
||
|
self.stats['opcUaRequests'] += 1
|
||
|
|
||
|
def incOpcUaErrors(self):
|
||
|
self.stats['opcUaErrors'] += 1
|
||
|
|
||
|
def incOpcUaTimeouts(self):
|
||
|
self.stats['opcUaTimeouts'] += 1
|
||
|
|
||
|
def incMqttRequests(self):
|
||
|
self.stats['mqttRequests'] += 1
|
||
|
|
||
|
def incMqttErrors(self):
|
||
|
self.stats['mqttErrors'] += 1
|
||
|
|
||
|
def stop(self):
|
||
|
self.killBill = True
|
||
|
logger.info("kill flag set")
|
||
|
|
||
|
self.killEvent.set()
|
||
|
logger.info("kill events triggered")
|
||
|
|
||
|
def run(self):
|
||
|
startTime = datetime.datetime.now()
|
||
|
while not self.killBill:
|
||
|
currentTime = datetime.datetime.now()
|
||
|
self.stats['uptime'] = int((currentTime - startTime).total_seconds())
|
||
|
self.queue.put(StatisticsDataObject(self.topic, self.stats))
|
||
|
self.killEvent.wait(timeout=float(self.period))
|
||
|
|