Add proper responses to all functions #15

Manually merged
sqozz merged 1 commits from feature/return_codes into master 2020-05-06 11:05:01 +02:00
1 changed files with 17 additions and 13 deletions

View File

@ -14,6 +14,7 @@ class SEMSocket():
frequency = 0 frequency = 0
mac_address = "" mac_address = ""
custom_service = None custom_service = None
authenticated = False
_read_char = None _read_char = None
_write_char = None _write_char = None
_notify_char = None _notify_char = None
@ -33,7 +34,7 @@ class SEMSocket():
cmd = bytearray([0x04]) cmd = bytearray([0x04])
payload = bytearray([0x00, 0x00, 0x00]) payload = bytearray([0x00, 0x00, 0x00])
msg = self.BTLEMessage(self, cmd, payload) msg = self.BTLEMessage(self, cmd, payload)
msg.send() return msg.send()
def setStatus(self, status): def setStatus(self, status):
# 0f 06 03 00 01 00 00 05 ff ff -> on # 0f 06 03 00 01 00 00 05 ff ff -> on
@ -41,7 +42,7 @@ class SEMSocket():
cmd = bytearray([0x03]) cmd = bytearray([0x03])
payload = bytearray([0x00, status, 0x00, 0x00]) payload = bytearray([0x00, status, 0x00, 0x00])
msg = self.BTLEMessage(self, cmd, payload) msg = self.BTLEMessage(self, cmd, payload)
msg.send() return msg.send()
def syncTime(self): def syncTime(self):
#15, 12, 1, 0, SECOND, MINUTE, HOUR_OF_DAY, DAY_OF_MONTH, MONTH (+1), int(YEAR/256), YEAR%256, 0, 0, CHKSUM, 255, 255 #15, 12, 1, 0, SECOND, MINUTE, HOUR_OF_DAY, DAY_OF_MONTH, MONTH (+1), int(YEAR/256), YEAR%256, 0, 0, CHKSUM, 255, 255
@ -52,7 +53,7 @@ class SEMSocket():
payload += bytearray([now.day, now.month, int(now.year/256), now.year%256]) payload += bytearray([now.day, now.month, int(now.year/256), now.year%256])
payload += bytearray([0x00, 0x00]) payload += bytearray([0x00, 0x00])
msg = self.BTLEMessage(self, cmd, payload) msg = self.BTLEMessage(self, cmd, payload)
msg.send() return msg.send()
def login(self, password): def login(self, password):
self.password = password self.password = password
@ -67,7 +68,9 @@ class SEMSocket():
payload.append(0x00) payload.append(0x00)
payload.append(0x00) payload.append(0x00)
msg = self.BTLEMessage(self, cmd, payload) msg = self.BTLEMessage(self, cmd, payload)
msg.send() success = msg.send()
self.authenticated = self.authenticated and success
Review

Meinten Sie: or statt and?

So kann man authenticated nur auf true setzen, wenn es schon true ist :D

Gibt es weiter unten nochmal.

Meinten Sie: `or` statt `and`? So kann man `authenticated` nur auf `true` setzen, wenn es schon `true` ist :D Gibt es weiter unten nochmal.
Review

Tatsächlich ist das so beabsichtigt weil der response handler weiter unten in 7a9a2949ff/sem6000.py (L235) authenticated auf true setzt. Das passiert nach dem msg.send(). Beides muss erfolgreich sein damit man auch wirklich eingeloggt ist. Denn failed msg.send() kam die Nachricht nie an, setzt der response handler self.authenticated auf False war der Pin falsch.

Tatsächlich ist das so beabsichtigt weil der response handler weiter unten in https://git.geekify.de/sqozz/sem6000/src/commit/7a9a2949ffafa3a671d598cc0b60544c0f518988/sem6000.py#L235 `authenticated` auf `true` setzt. Das passiert nach dem `msg.send()`. Beides muss erfolgreich sein damit man auch wirklich eingeloggt ist. Denn failed `msg.send()` kam die Nachricht nie an, setzt der response handler `self.authenticated` auf `False` war der Pin falsch.
Review

und ich hab mich dazu entschlossen hier den Status von msg.send() zurück zu geben damit das alles konsistent ist. Der caller von login() kann dann nochmal die Funktion callen falls False zurück kam bzw. den User informieren der Pin sei falsch wenn zwar login() True zurück gibt danach aber immer noch socket.authenticated == False ist

und ich hab mich dazu entschlossen hier den Status von `msg.send()` zurück zu geben damit das alles konsistent ist. Der caller von `login()` kann dann nochmal die Funktion callen falls `False` zurück kam bzw. den User informieren der Pin sei falsch wenn zwar `login()` `True` zurück gibt danach aber immer noch `socket.authenticated == False` ist
Review

Ah, alles klar. Danke für die Erklärung. Trotzdem wäre ein kurzer Kommentar wohl hilfreich für zukünftige Leser des Codes :)

Ah, alles klar. Danke für die Erklärung. Trotzdem wäre ein kurzer Kommentar wohl hilfreich für zukünftige Leser des Codes :)
return success
def changePassword(self, newPassword): def changePassword(self, newPassword):
cmd = bytearray([0x17]) cmd = bytearray([0x17])
@ -80,15 +83,14 @@ class SEMSocket():
payload.append(int(self.password[i])) payload.append(int(self.password[i]))
self.password = newPassword self.password = newPassword
msg = self.BTLEMessage(self, cmd, payload) msg = self.BTLEMessage(self, cmd, payload)
msg.send() success = msg.send()
self.authenticated = self.authenticated and success
return success
@property @property
def connected(self): def connected(self):
try: try:
if "conn" in self._btle_device.status().get("state"): return "conn" in self._btle_device.status().get("state")
Review

just a minor cleanup btw

just a minor cleanup btw
return True
else:
return False
except: except:
return False return False
@ -188,7 +190,7 @@ class SEMSocket():
self.__btle_device.reconnect() self.__btle_device.reconnect()
self.__btle_device._write_char.write(self.__data, True) self.__btle_device._write_char.write(self.__data, True)
self.__btle_device._btle_device.waitForNotifications(5) return self.__btle_device._btle_device.waitForNotifications(5)
class BTLEHandler(btle.DefaultDelegate): class BTLEHandler(btle.DefaultDelegate):
@ -206,7 +208,6 @@ class SEMSocket():
elif message_type == 0x01: elif message_type == 0x01:
print("Time synced") print("Time synced")
elif message_type == 0x03: #switch toggle elif message_type == 0x03: #switch toggle
print("Switch toggled")
self.__btle_device.getStatus() self.__btle_device.getStatus()
elif message_type == 0x04: #status related data elif message_type == 0x04: #status related data
voltage = data[8] voltage = data[8]
@ -228,9 +229,12 @@ class SEMSocket():
self.__btle_device.power_factor = None self.__btle_device.power_factor = None
elif message_type == 0x17: elif message_type == 0x17:
if data[5] == 0x00 or data[5] == 0x01: if data[5] == 0x00 or data[5] == 0x01:
if data[4]: # in theory the fifth byte indicates a login attempt response (0) or a response to a password change (1)
print("Login failed") # but since a password change requires a valid login and a successful password changes logs you in,
# we can just ignore this bit and set the authenticated flag accordingly for both responses
self.__btle_device.authenticated = not data[4]
else: else:
print("5th byte of login-response is > 1:", data) print("5th byte of login-response is > 1:", data)
else: else:
print ("Unknown message from Handle: 0x" + format(cHandle,'02X') + " Value: "+ format(data)) print ("Unknown message from Handle: 0x" + format(cHandle,'02X') + " Value: "+ format(data))