Skip to content

Commit 3992f5e

Browse files
committed
Small Connection.py refactoring due to be according to PEPs.
1 parent 6fae563 commit 3992f5e

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

LiveObjects/Connection.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,26 @@
88
import json
99
import time
1010
import os
11-
INT="i32"
12-
UINT="u32"
13-
BINARY="bin"
14-
STRING="str"
15-
FLOAT="f64"
16-
INFO="INFO"
11+
INT = "i32"
12+
UINT = "u32"
13+
BINARY = "bin"
14+
STRING = "str"
15+
FLOAT = "f64"
16+
INFO = "INFO"
1717
WARNING = "WARNING"
1818
ERROR = "ERROR"
1919

2020
SSL = 8883
2121
NONE = 1883
2222

23+
2324
class LiveObjectsParameter:
24-
def __init__(self,value,type_, cb = None):
25+
def __init__(self, value, type_, cb=None):
2526
self.value = value
2627
self.type = type_
2728
self.callback = cb
2829

30+
2931
class Connection:
3032
def __init__(self, board, deviceID, port, apiKey, debug = True):
3133
try:
@@ -48,7 +50,7 @@ def __init__(self, board, deviceID, port, apiKey, debug = True):
4850
self.__payload = {self.__value: {}}
4951
self.__commands = {}
5052
self.__doLog = debug
51-
self.quit=False
53+
self.quit = False
5254

5355
if self.mode == 1:
5456
self.__mqtt = paho.Client(deviceID)
@@ -60,7 +62,7 @@ def loop(self):
6062
if self.mode == 0:
6163
self.__mqtt.check_msg()
6264

63-
def __onMessage(self,client="", userdata="", msg=""):
65+
def __onMessage(self, client="", userdata="", msg=""):
6466
if self.mode == 1:
6567
if msg.topic == "dev/cfg/upd":
6668
self.__parameterManager(msg)
@@ -72,14 +74,14 @@ def __onMessage(self,client="", userdata="", msg=""):
7274
elif client == b"dev/cmd":
7375
self.__commandManager(userdata)
7476

75-
def __onConnect(self,client="", userdata="", flags="", rc=""):
77+
def __onConnect(self, client="", userdata="", flags="", rc=""):
7678
if self.mode == 1:
77-
if rc ==0:
78-
self.outputDebug(INFO,"Connected!")
79-
if len(self.__commands)>0:
79+
if rc == 0:
80+
self.outputDebug(INFO, "Connected!")
81+
if len(self.__commands) > 0:
8082
self.outputDebug(INFO, "Subscribing commands")
8183
self.__mqtt.subscribe("dev/cmd")
82-
if len(self.__parameters)>0:
84+
if len(self.__parameters) > 0:
8385
self.outputDebug(INFO, "Subscribing parameters")
8486
self.__mqtt.subscribe("dev/cfg/upd")
8587
self.__sendConfig()
@@ -107,8 +109,6 @@ def connect(self):
107109
self.__mqtt.on_connect = self.__onConnect
108110
self.__mqtt.on_message = self.__onMessage
109111
if self.__port == 8883:
110-
# dirname = os.path.dirname(__file__)
111-
# filename = os.path.join(dirname, "./certfile.cer")
112112
filename = "/etc/ssl/certs/ca-certificates.crt"
113113
self.__mqtt.tls_set(filename)
114114
self.__mqtt.connect(self.__server, self.__port, 60)
@@ -133,8 +133,8 @@ def outputDebug(self,info, *args):
133133
def addCommand(self, name, cmd):
134134
self.__commands[name] = cmd
135135

136-
def __commandManager(self,msg):
137-
if self.mode ==1:
136+
def __commandManager(self, msg):
137+
if self.mode == 1:
138138
msgDict = json.loads(msg.payload)
139139
self.outputDebug(INFO, "Received message:\n", json.dumps(msgDict, sort_keys=True, indent=4))
140140
else:
@@ -149,18 +149,18 @@ def __commandManager(self,msg):
149149

150150
def __default(self, req=""):
151151
self.outputDebug(INFO, "Command not found!")
152-
return {"info" : "Command not found"}
152+
return {"info": "Command not found"}
153153

154154
def __sendConfig(self):
155-
outMsg={ "cfg" : {} }
155+
outMsg = { "cfg" : {} }
156156
for param in self.__parameters:
157-
outMsg["cfg"][param]={ "t" : self.__parameters[param].type, "v" : self.__parameters[param].value }
158-
self.__publishMessage("dev/cfg",outMsg)
157+
outMsg["cfg"][param] = {"t": self.__parameters[param].type, "v": self.__parameters[param].value}
158+
self.__publishMessage("dev/cfg", outMsg)
159159

160160
def __parameterManager(self, msg):
161161
if self.mode == 1:
162-
self.outputDebug(INFO,"Received message: ")
163-
self.outputDebug(INFO,json.loads(msg.payload))
162+
self.outputDebug(INFO, "Received message: ")
163+
self.outputDebug(INFO, json.loads(msg.payload))
164164
params = json.loads(msg.payload)
165165
else:
166166
self.outputDebug(INFO, "Received message: ")
@@ -179,9 +179,9 @@ def __parameterManager(self, msg):
179179
self.__parameters[param].type = FLOAT
180180

181181
self.__parameters[param].value = params["cfg"][param]["v"]
182-
if self.__parameters[param].callback != None:
182+
if self.__parameters[param].callback != None:
183183
self.__parameters[param].callback(param, params["cfg"][param]["v"])
184-
self.__publishMessage("dev/cfg",params)
184+
self.__publishMessage("dev/cfg", params)
185185

186186
def addParameter(self, name, val, type_, cb=None):
187187
if type_ == INT:
@@ -196,7 +196,7 @@ def addParameter(self, name, val, type_, cb=None):
196196
val = float(val)
197197
self.__parameters[name] = LiveObjectsParameter(val, type_, cb)
198198

199-
def getParameter(self,name):
199+
def getParameter(self, name):
200200
if self.__parameters[name].type == INT:
201201
return int(self.__parameters[name].value)
202202
elif self.__parameters[name].type == STRING:
@@ -220,12 +220,12 @@ def addModel(self, model):
220220

221221
def addTag(self, tag):
222222
if not "tags" in self.__payload:
223-
self.__payload["tags"]=[]
223+
self.__payload["tags"] = []
224224
self.__payload["tags"].append(tag)
225225

226226
def addTags(self, tags):
227227
if not "tags" in self.__payload:
228-
self.__payload["tags"]=[]
228+
self.__payload["tags"] = []
229229
for tag in tags:
230230
self.__payload["tags"].append(tag)
231231

@@ -234,7 +234,7 @@ def sendData(self):
234234
sys.exit()
235235
self.__publishMessage("dev/data",self.__payload)
236236
self.__payload = {}
237-
self.__payload[self.__value]={}
237+
self.__payload[self.__value] = {}
238238

239239
def __publishMessage(self, topic, msg):
240240
self.outputDebug(INFO, "Publishing message on topic: ", topic)

0 commit comments

Comments
 (0)