2018-08-11 00:32:33 +02:00
|
|
|
from bluepy import btle
|
|
|
|
import time
|
2020-01-17 22:20:41 +01:00
|
|
|
import datetime
|
2018-08-11 00:32:33 +02:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
class SEMSocket():
|
2020-05-08 19:35:46 +02:00
|
|
|
icons = ["plug", "speaker", "flatscreen", "desk lamp", "oven", "kitchen machine", "canning pot", "stanging lamp", "kettle", "mixer", "hanging lamp", "toaster", "washing machine", "fan", "fridge", "iron", "printer", "monitor", "notebook", "workstation", "video recorder", "curling iron", "heater"]
|
2018-08-13 18:47:46 +02:00
|
|
|
password = "0000"
|
2018-08-11 00:32:33 +02:00
|
|
|
powered = False
|
|
|
|
voltage = 0
|
|
|
|
current = 0
|
|
|
|
power = 0
|
2020-01-19 17:34:18 +01:00
|
|
|
power_factor = 0
|
2020-01-17 22:25:54 +01:00
|
|
|
total_power = 0
|
2018-08-11 00:32:33 +02:00
|
|
|
frequency = 0
|
|
|
|
mac_address = ""
|
|
|
|
custom_service = None
|
2020-05-05 22:58:04 +02:00
|
|
|
authenticated = False
|
2020-05-08 19:35:46 +02:00
|
|
|
_icon_idx = None
|
2020-05-06 11:07:30 +02:00
|
|
|
_name = None
|
2020-05-09 16:27:05 +02:00
|
|
|
_version_char = None
|
2019-02-27 22:26:39 +01:00
|
|
|
_write_char = None
|
2020-05-06 11:07:30 +02:00
|
|
|
_name_char = None
|
2019-02-27 22:26:39 +01:00
|
|
|
_btle_device = None
|
2018-08-11 00:32:33 +02:00
|
|
|
|
2020-01-16 18:55:38 +01:00
|
|
|
def __init__(self, mac):
|
2018-08-11 00:32:33 +02:00
|
|
|
self.mac_address = mac
|
2019-11-13 18:48:46 +01:00
|
|
|
self._btle_device = btle.Peripheral(None ,addrType=btle.ADDR_TYPE_PUBLIC,iface=0)
|
2019-02-27 22:01:46 +01:00
|
|
|
try:
|
|
|
|
self.reconnect()
|
|
|
|
except self.NotConnectedException:
|
|
|
|
# initial connection may fail. It is up to the code what to do
|
|
|
|
pass
|
2018-08-11 00:32:33 +02:00
|
|
|
|
|
|
|
def getStatus(self):
|
|
|
|
#15, 5, 4, 0, 0, 0, 5, -1, -1
|
|
|
|
cmd = bytearray([0x04])
|
|
|
|
payload = bytearray([0x00, 0x00, 0x00])
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
2020-05-05 22:58:04 +02:00
|
|
|
return msg.send()
|
2018-08-11 00:32:33 +02:00
|
|
|
|
2020-01-17 17:12:33 +01:00
|
|
|
def getSynConfig(self):
|
|
|
|
#15, 5, 16, 0, 0, 0, 17, -1, -1
|
|
|
|
cmd = bytearray([0x10])
|
|
|
|
payload = bytearray([0x00, 0x00, 0x00])
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
|
|
|
msg.send()
|
|
|
|
|
2018-08-11 00:32:33 +02:00
|
|
|
def setStatus(self, status):
|
|
|
|
# 0f 06 03 00 01 00 00 05 ff ff -> on
|
|
|
|
# 0f 06 03 00 00 00 00 04 ff ff -> off
|
|
|
|
cmd = bytearray([0x03])
|
|
|
|
payload = bytearray([0x00, status, 0x00, 0x00])
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
2020-05-05 22:58:04 +02:00
|
|
|
return msg.send()
|
2018-08-11 00:32:33 +02:00
|
|
|
|
2020-01-17 22:20:41 +01:00
|
|
|
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
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
cmd = bytearray([0x01])
|
|
|
|
payload = bytearray([0x00])
|
|
|
|
payload += bytearray([now.second, now.minute, now.hour])
|
|
|
|
payload += bytearray([now.day, now.month, int(now.year/256), now.year%256])
|
|
|
|
payload += bytearray([0x00, 0x00])
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
2020-05-05 22:58:04 +02:00
|
|
|
return msg.send()
|
2020-01-17 22:20:41 +01:00
|
|
|
|
2018-08-11 00:32:33 +02:00
|
|
|
def login(self, password):
|
2018-08-13 18:47:46 +02:00
|
|
|
self.password = password
|
2018-08-11 00:32:33 +02:00
|
|
|
cmd = bytearray([0x17])
|
|
|
|
payload = bytearray()
|
|
|
|
payload.append(0x00)
|
|
|
|
payload.append(0x00)
|
2019-06-14 19:51:47 +02:00
|
|
|
for i in range(4):
|
|
|
|
payload.append(int(self.password[i]))
|
2018-08-11 00:32:33 +02:00
|
|
|
payload.append(0x00)
|
|
|
|
payload.append(0x00)
|
|
|
|
payload.append(0x00)
|
|
|
|
payload.append(0x00)
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
2020-05-05 22:58:04 +02:00
|
|
|
success = msg.send()
|
|
|
|
self.authenticated = self.authenticated and success
|
|
|
|
return success
|
2018-08-11 00:32:33 +02:00
|
|
|
|
2018-08-13 18:47:46 +02:00
|
|
|
def changePassword(self, newPassword):
|
|
|
|
cmd = bytearray([0x17])
|
|
|
|
payload = bytearray()
|
|
|
|
payload.append(0x00)
|
|
|
|
payload.append(0x01)
|
2019-06-14 19:51:47 +02:00
|
|
|
for i in range(4):
|
2020-01-28 15:54:36 +01:00
|
|
|
payload.append(int(newPassword[i]))
|
2019-06-14 19:51:47 +02:00
|
|
|
for i in range(4):
|
|
|
|
payload.append(int(self.password[i]))
|
2018-08-13 18:47:46 +02:00
|
|
|
self.password = newPassword
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
2020-05-05 22:58:04 +02:00
|
|
|
success = msg.send()
|
|
|
|
self.authenticated = self.authenticated and success
|
|
|
|
return success
|
2018-08-13 18:47:46 +02:00
|
|
|
|
2020-05-06 21:09:29 +02:00
|
|
|
def setIcon(self, iconIdx):
|
|
|
|
cmd = bytearray([0x0f])
|
|
|
|
payload = bytearray([0x00, 0x03, iconIdx, 0x00, 0x00, 0x00, 0x00])
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
|
|
|
|
|
|
|
success = msg.send()
|
|
|
|
return success
|
|
|
|
|
2020-05-08 20:14:55 +02:00
|
|
|
def enableLED(self, status):
|
|
|
|
cmd = bytearray([0x0f])
|
|
|
|
payload = bytearray([0x00, 0x05, status, 0x00, 0x00, 0x00, 0x00])
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
|
|
|
|
|
|
|
success = msg.send()
|
|
|
|
return success
|
|
|
|
|
2020-05-06 11:07:30 +02:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
self._name = self._name_char.read().decode("UTF-8")
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, newName):
|
2020-05-07 12:17:52 +02:00
|
|
|
newNameBytes = newName.encode("UTF-8")
|
2020-05-06 11:07:30 +02:00
|
|
|
cmd = bytearray([0x02])
|
|
|
|
payload = bytearray()
|
|
|
|
payload.append(0x02)
|
|
|
|
for i in range(20):
|
2020-05-07 12:17:52 +02:00
|
|
|
if i <= (len(newNameBytes) - 1):
|
|
|
|
payload.append(newNameBytes[i])
|
2020-05-06 11:07:30 +02:00
|
|
|
else:
|
|
|
|
payload.append(0x00)
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
|
|
|
success = msg.send()
|
|
|
|
# For some reason the original app sets the first 7 bytes of the payload to zero and sends it again.
|
|
|
|
# However, a first test showed, that this really doesn't change anything. If it becomes neccessary here's a first draft:
|
|
|
|
#for i in range(7):
|
|
|
|
# payload[i+1] = 0x00
|
|
|
|
#msg = self.BTLEMessage(self, cmd, payload)
|
2020-05-09 16:28:14 +02:00
|
|
|
if not success:
|
|
|
|
raise self.SendMessageFailed
|
|
|
|
if self.name != newName:
|
|
|
|
raise self.NotLoggedIn
|
|
|
|
|
|
|
|
@property
|
|
|
|
def serial(self):
|
|
|
|
# 15, 5, 17, 0, 0, 0, 18, -1, -1
|
|
|
|
cmd = bytearray([0x11])
|
|
|
|
payload = bytearray([0x00, 0x00, 0x00])
|
|
|
|
msg = self.BTLEMessage(self, cmd, payload)
|
|
|
|
success = msg.send()
|
|
|
|
if not success:
|
|
|
|
raise self.SendMessageFailed
|
|
|
|
return self._serial
|
|
|
|
|
|
|
|
@property
|
|
|
|
def firmware_version(self):
|
|
|
|
char_value = self._version_char.read()
|
|
|
|
major = char_value[11]
|
|
|
|
minor = char_value[12]
|
|
|
|
return "{}.{}".format(major, minor)
|
2020-05-06 11:07:30 +02:00
|
|
|
|
2019-11-13 17:57:24 +01:00
|
|
|
@property
|
|
|
|
def connected(self):
|
2019-02-27 22:01:46 +01:00
|
|
|
try:
|
2020-05-05 22:58:04 +02:00
|
|
|
return "conn" in self._btle_device.status().get("state")
|
2019-11-13 17:57:24 +01:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2020-01-16 18:55:38 +01:00
|
|
|
def reconnect(self):
|
2019-11-13 17:57:24 +01:00
|
|
|
self.disconnect()
|
|
|
|
self.connect()
|
2019-02-27 22:01:46 +01:00
|
|
|
if not self.connected:
|
|
|
|
raise self.NotConnectedException
|
|
|
|
|
2019-11-13 17:57:24 +01:00
|
|
|
def connect(self):
|
|
|
|
self.disconnect()
|
2019-11-13 18:48:46 +01:00
|
|
|
self._btle_device.connect(self.mac_address)
|
2019-11-13 17:57:24 +01:00
|
|
|
self._btle_handler = self.BTLEHandler(self)
|
2020-05-09 16:27:05 +02:00
|
|
|
self._btle_device.setDelegate(self._btle_handler)
|
2019-11-13 17:57:24 +01:00
|
|
|
|
|
|
|
self._custom_service = self._btle_device.getServiceByUUID(0xfff0)
|
2020-05-09 16:27:05 +02:00
|
|
|
self._version_char = self._custom_service.getCharacteristics("0000fff1-0000-1000-8000-00805f9b34fb")[0] #contains firmware version info
|
|
|
|
self._write_char = self._custom_service.getCharacteristics("0000fff3-0000-1000-8000-00805f9b34fb")[0] #is used to write commands
|
2020-05-06 11:07:30 +02:00
|
|
|
self._name_char = self._custom_service.getCharacteristics("0000fff6-0000-1000-8000-00805f9b34fb")[0]
|
|
|
|
|
2019-11-13 17:57:24 +01:00
|
|
|
def disconnect(self):
|
|
|
|
if self.connected == True:
|
|
|
|
self._btle_device.disconnect()
|
|
|
|
|
2018-08-11 00:32:33 +02:00
|
|
|
#def ______RESET(self):
|
|
|
|
# #15, 5, 16, 0, 0, 0, 17, -1, -1 ??? maybe reset?
|
|
|
|
# pass
|
|
|
|
|
2019-02-27 22:26:39 +01:00
|
|
|
class NotConnectedException(Exception):
|
|
|
|
pass
|
|
|
|
|
2020-05-06 11:07:30 +02:00
|
|
|
class SendMessageFailed(Exception):
|
|
|
|
pass
|
|
|
|
|
2020-05-07 12:17:52 +02:00
|
|
|
class NotLoggedIn(Exception):
|
|
|
|
pass
|
|
|
|
|
2019-02-27 22:26:39 +01:00
|
|
|
class BTLEMessage():
|
|
|
|
MAGIC_START = bytearray([0x0f])
|
|
|
|
MAGIC_END = bytearray([0xff, 0xff])
|
|
|
|
__data = bytearray()
|
|
|
|
__cmd = bytearray(1) # cmd cannot be empty
|
|
|
|
__payload = bytearray()
|
2018-08-11 00:32:33 +02:00
|
|
|
|
2019-02-27 22:26:39 +01:00
|
|
|
def __init__(self, btle_device, cmd=bytearray(), payload=bytearray()):
|
|
|
|
self.__btle_device = btle_device
|
2019-06-14 19:51:47 +02:00
|
|
|
self.cmd = cmd
|
|
|
|
self.payload = payload
|
2019-02-27 22:26:39 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def cmd(self):
|
|
|
|
return self.__cmd
|
|
|
|
|
|
|
|
@cmd.setter
|
|
|
|
def cmd(self, cmd):
|
|
|
|
self.__data = self.MAGIC_START + bytearray(1) + cmd + self.__payload + bytearray(1) + self.MAGIC_END
|
|
|
|
self.__cmd = cmd
|
|
|
|
self.__calc_length()
|
|
|
|
self.__calc_checksum()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def payload(self):
|
|
|
|
return self.__payload
|
|
|
|
|
|
|
|
@payload.setter
|
|
|
|
def payload(self, payload):
|
|
|
|
self.__data = self.MAGIC_START + bytearray(1) + self.__cmd + payload + bytearray(1) + self.MAGIC_END
|
|
|
|
self.__payload = payload
|
|
|
|
self.__calc_length()
|
|
|
|
self.__calc_checksum()
|
|
|
|
|
|
|
|
def __calc_checksum(self):
|
|
|
|
checksum = 1
|
|
|
|
for i in range(2, self.__data[1] + 2):
|
|
|
|
checksum += self.__data[i]
|
|
|
|
self.__data[-3] = checksum & 0xff
|
|
|
|
|
|
|
|
def __calc_length(self):
|
|
|
|
self.__data[1] = 1 + len(self.__payload) + 1 # cmd + payload + checksum
|
|
|
|
|
|
|
|
def send(self):
|
|
|
|
if not self.__btle_device.connected:
|
2020-01-16 18:55:38 +01:00
|
|
|
self.__btle_device.reconnect()
|
2019-02-27 22:26:39 +01:00
|
|
|
|
|
|
|
self.__btle_device._write_char.write(self.__data, True)
|
2020-05-05 22:58:04 +02:00
|
|
|
return self.__btle_device._btle_device.waitForNotifications(5)
|
2019-02-27 22:26:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BTLEHandler(btle.DefaultDelegate):
|
|
|
|
def __init__(self, btle_device):
|
|
|
|
btle.DefaultDelegate.__init__(self)
|
|
|
|
self.__btle_device = btle_device
|
|
|
|
|
|
|
|
def handleNotification(self, cHandle, data):
|
2020-05-06 19:58:50 +02:00
|
|
|
if len(data) <= 3:
|
|
|
|
print("Notification data seems invalid or incomplete. Could not parse: ", end="")
|
|
|
|
print(data)
|
|
|
|
return
|
|
|
|
|
2019-02-27 22:26:39 +01:00
|
|
|
message_type = data[2]
|
|
|
|
if message_type == 0x00:
|
|
|
|
if data[4] == 0x01:
|
|
|
|
print("Checksum error!")
|
|
|
|
else:
|
|
|
|
print("Unknown error:", data)
|
2020-05-06 19:58:50 +02:00
|
|
|
elif message_type == 0x01: #sync time response
|
|
|
|
if not data[3:] == b'\x00\x00\x02\xff\xff':
|
|
|
|
print("Time synced failed with unknown data: ", end="")
|
|
|
|
print(data)
|
2020-05-06 11:07:30 +02:00
|
|
|
elif message_type == 0x02: #set name response
|
|
|
|
if not data[3:] == b'\x00\x00\x03\xff\xff':
|
|
|
|
print("Set name failed with unknown data: ", end="")
|
|
|
|
print(data)
|
|
|
|
elif message_type == 0x03: #switch toggle response
|
2019-02-27 22:26:39 +01:00
|
|
|
self.__btle_device.getStatus()
|
2020-05-06 11:07:30 +02:00
|
|
|
elif message_type == 0x04: #status related response
|
2020-01-17 22:25:54 +01:00
|
|
|
voltage = data[8]
|
|
|
|
current = (data[9] << 8 | data[10]) / 1000
|
|
|
|
power = (data[5] << 16 | data[6] << 8 | data[7]) / 1000
|
|
|
|
total_power = (data[14] << 24 | data[15] << 16 | data[16] << 8 | data[17]) / 1000
|
2020-01-19 17:34:18 +01:00
|
|
|
|
|
|
|
self.__btle_device.voltage = voltage
|
|
|
|
self.__btle_device.current = current
|
|
|
|
self.__btle_device.power = power
|
2019-02-27 22:26:39 +01:00
|
|
|
self.__btle_device.frequency = data[11]
|
|
|
|
self.__btle_device.powered = bool(data[4])
|
2020-01-17 22:25:54 +01:00
|
|
|
self.__btle_device.total_power = total_power
|
2020-01-19 17:34:18 +01:00
|
|
|
|
|
|
|
# calculated values
|
2020-01-20 21:29:20 +01:00
|
|
|
try:
|
|
|
|
self.__btle_device.power_factor = power / (voltage * current)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
self.__btle_device.power_factor = None
|
2020-05-09 16:28:14 +02:00
|
|
|
elif message_type == 0x10: #plug settings response
|
2020-05-08 19:35:46 +02:00
|
|
|
self.__btle_device.default_charge = (data[5] / 100)
|
|
|
|
self.__btle_device.night_charge = (data[6] / 100)
|
|
|
|
night_charge_start_time = int((data[7] << 8 | data[8]) / 60)
|
|
|
|
night_charge_end_time = int((data[9] << 8 | data[10]) / 60)
|
|
|
|
self.__btle_device.night_charge_start_time = time.strptime(str(night_charge_start_time), "%H")
|
|
|
|
self.__btle_device.night_charge_end_time = time.strptime(str(night_charge_end_time), "%H")
|
|
|
|
self.__btle_device.night_mode = not bool(data[11])
|
|
|
|
self.__btle_device.icon_idx = data[12]
|
|
|
|
self.__btle_device.power_protect = (data[13] << 8 | data[14])
|
2020-05-09 16:28:14 +02:00
|
|
|
elif message_type == 0x11: #serial number response
|
|
|
|
self.__btle_device._serial = data[-16:].decode("UTF-8")
|
2020-05-06 11:07:30 +02:00
|
|
|
elif message_type == 0x17: #authentication related response
|
2019-02-27 22:26:39 +01:00
|
|
|
if data[5] == 0x00 or data[5] == 0x01:
|
2020-05-05 22:58:04 +02:00
|
|
|
# in theory the fifth byte indicates a login attempt response (0) or a response to a password change (1)
|
|
|
|
# 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]
|
2019-02-27 22:26:39 +01:00
|
|
|
else:
|
|
|
|
print("5th byte of login-response is > 1:", data)
|
2020-05-06 19:58:50 +02:00
|
|
|
elif message_type == 0x0f: #set icon response
|
2020-05-06 21:09:29 +02:00
|
|
|
if data[3:6] == b'\x00\x03\x00':
|
|
|
|
# LED set successfully
|
|
|
|
pass
|
|
|
|
elif data[3:6] == b'\x00\x05\x00':
|
|
|
|
# Icon set successfully
|
|
|
|
pass
|
|
|
|
else:
|
2020-05-06 19:58:50 +02:00
|
|
|
print("Unknown response for setting icon: ", end="")
|
|
|
|
print(data[3:])
|
2019-02-27 22:26:39 +01:00
|
|
|
else:
|
|
|
|
print ("Unknown message from Handle: 0x" + format(cHandle,'02X') + " Value: "+ format(data))
|
2020-05-05 22:58:04 +02:00
|
|
|
|