This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendData.py
65 lines (55 loc) · 2.98 KB
/
sendData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import paho.mqtt.client as mqtt
import settings
import logging
class SendDataViaMQTT(object):
def __init__(self, MQTTc8yConnector, CurrentEvent):
self.logger = logging.getLogger('sendData')
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
self.logger.debug('Logger for sendData was initialised')
self.MQTTc8yConnector = MQTTc8yConnector
self.c8yTopic = CurrentEvent.c8yTopic
self.logger.info('Current Event created following topic for C8Y Mqtt to create measurement: %s', self.c8yTopic)
self.c8yPayload = CurrentEvent.c8yPayload
self.logger.info('Current Event created following payload for C8Y Mqtt to create measurement: %s', self.c8yPayload)
self.publishSeries()
def publishSeries(self):
self.logger.info('Publishing to C8Y Mqtt')
self.MQTTc8yConnector.client.publish(self.c8yTopic,self.c8yPayload)
class MQTTc8yConnector(object):
def __init__(self, ID):
self.logger2 = logging.getLogger('MQTTc8yConnector')
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
self.logger2.debug('Logger for sendData was initialised')
self.ID = ID.translate({ord(':'): None})
self.c8yMqttHost = settings.tenant
self.logger2.info('c8yMqtt host was set to %s', self.c8yMqttHost)
self.c8yMqttPort = int(settings.c8yPort)
self.logger2.info('c8yMqtt port was set to %s', self.c8yMqttPort)
self.tenantID = settings.tenantID
self.logger2.info('The tenant ID is %s', self.tenantID)
self.deviceID = str(self.ID)
self.clientID = self.deviceID
self.logger2.info('The used clientID for C8Y was set to %s', self.clientID)
self.__c8yUser = settings.c8yUser
self.logger2.info('c8y user was set to %s', self.__c8yUser)
self.__c8yPassword = settings.c8yPassword
self.logger2.info('c8y password was set to %s', self.__c8yPassword)
self.baseTopic = 's/us/'
self.logger2.debug('Calling connect method within MQTTc8yConnectorclass')
self.connect()
def connect(self):
self.logger2.debug('Calling connect method within MQTTc8yConnectorclass')
self.logger2.debug('Initialising MQTT client with loaded credentials for MQTTc8yConnector')
self.client = mqtt.Client(client_id=self.deviceID)
self.logger2.info('MQTT client with loaded credentials was initialised')
self.logger2.info('Setting user/Password')
self.client.username_pw_set(username=self.__c8yUser,password=self.__c8yPassword)
self.logger2.info('Connecting')
self.client.connect(self.c8yMqttHost, self.c8yMqttPort,60)
def disconnect(self):
self.logger2.info('Disconnecting')
self.client.disconnect()
self.logger2.debug('Disconnected')
def __del__(self):
self.logger2.debug('Deleting MQTTC8YConnector Object and disconnecting')
self.disconnect()