Compare commits
No commits in common. "master" and "reconnect" have entirely different histories.
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,4 +0,0 @@
|
||||||
__pycache__/
|
|
||||||
*.pyc
|
|
||||||
|
|
||||||
.*.sw?
|
|
58
README.md
58
README.md
|
@ -1,11 +1,3 @@
|
||||||
# SEM6000 Python Library
|
|
||||||
|
|
||||||
SEM6000 is a energy meter and power switch with Bluetooth 4.0.
|
|
||||||
|
|
||||||
This library provides a Python module for these devices
|
|
||||||
|
|
||||||
## Run the example code
|
|
||||||
|
|
||||||
```
|
```
|
||||||
$ git clone … sem6000
|
$ git clone … sem6000
|
||||||
$ cd sem6000
|
$ cd sem6000
|
||||||
|
@ -13,52 +5,4 @@ $ virtualenv -p python3 python3_venv
|
||||||
$ . ./python3_venv/bin/activate
|
$ . ./python3_venv/bin/activate
|
||||||
$ pip3 install -r requirements.txt
|
$ pip3 install -r requirements.txt
|
||||||
$ python3 example.py
|
$ python3 example.py
|
||||||
```
|
```
|
||||||
|
|
||||||
## Collectd Plugin
|
|
||||||
|
|
||||||
You can find a Plugin for [collectd](https://collectd.org) in the `collectd`
|
|
||||||
subdirectory.
|
|
||||||
|
|
||||||
Installation procedure (the target directory may be changed of course):
|
|
||||||
|
|
||||||
```shell
|
|
||||||
# mkdir -p /usr/local/lib/collectd/python
|
|
||||||
# cp collectd/collectd_sem6000.py /usr/local/lib/collectd/python
|
|
||||||
# cp sem6000.py /usr/local/lib/collectd/python
|
|
||||||
```
|
|
||||||
|
|
||||||
Add or adjust the configuration for your collectd’s Python plugin as follows:
|
|
||||||
|
|
||||||
```
|
|
||||||
<Plugin python>
|
|
||||||
ModulePath "/usr/local/share/collectd/python"
|
|
||||||
LogTraces true
|
|
||||||
Interactive false
|
|
||||||
Import "collectd_sem6000"
|
|
||||||
|
|
||||||
<Module collectd_sem6000>
|
|
||||||
Address "12:34:56:78:90:ab"
|
|
||||||
SocketName "FirstSocket"
|
|
||||||
ReadTimeout 30
|
|
||||||
SuspendTime 300
|
|
||||||
</Module>
|
|
||||||
<Module collectd_sem6000>
|
|
||||||
Address "ab:cd:ef:13:37:42"
|
|
||||||
SocketName "ASecondSocket"
|
|
||||||
</Module>
|
|
||||||
# ...
|
|
||||||
</Plugin>
|
|
||||||
```
|
|
||||||
|
|
||||||
`ReadTimeout` and `SuspendTime` control what’s happening when a device is
|
|
||||||
unavailable. If no value could be retrieved for `ReadTimeout` seconds, the
|
|
||||||
plugin does not retry for `SuspendTime` seconds. After that, normal operation
|
|
||||||
is resumed. This procedure ensures that an unreachable device does not block
|
|
||||||
other devices (too often) in the current single-threaded architecture.
|
|
||||||
|
|
||||||
If not specified, `ReadTimeout` is 30 seconds and `SuspendTime` is 5 minutes.
|
|
||||||
|
|
||||||
Make sure that everything listed in `requirements.txt` is available to the user
|
|
||||||
running collectd.
|
|
||||||
|
|
|
@ -1,144 +0,0 @@
|
||||||
#!/usr/bin/env python3
|
|
||||||
# coding: utf-8
|
|
||||||
# vim: noet ts=2 sw=2 sts=2
|
|
||||||
|
|
||||||
import os
|
|
||||||
import time
|
|
||||||
import collectd
|
|
||||||
|
|
||||||
from sem6000 import SEMSocket
|
|
||||||
from bluepy.btle import BTLEDisconnectError
|
|
||||||
|
|
||||||
instances = []
|
|
||||||
|
|
||||||
def init_func():
|
|
||||||
pass
|
|
||||||
|
|
||||||
def config_func(cfg):
|
|
||||||
global instances
|
|
||||||
|
|
||||||
config = {}
|
|
||||||
|
|
||||||
for node in cfg.children:
|
|
||||||
key = node.key.lower()
|
|
||||||
value = node.values[0]
|
|
||||||
|
|
||||||
if key in ['address', 'socketname']:
|
|
||||||
config[key] = value
|
|
||||||
|
|
||||||
if key == 'readtimeout':
|
|
||||||
config['readtimeout'] = int(value)
|
|
||||||
|
|
||||||
if key == 'suspendtime':
|
|
||||||
config['suspendtime'] = int(value)
|
|
||||||
|
|
||||||
if 'address' not in config.keys():
|
|
||||||
collectd.error('sem6000: address must be set')
|
|
||||||
return
|
|
||||||
|
|
||||||
if 'socketname' not in config.keys():
|
|
||||||
config['socketname'] = config['address'].replace(':', '')
|
|
||||||
|
|
||||||
if 'readtimeout' not in config.keys():
|
|
||||||
config['readtimeout'] = 30
|
|
||||||
|
|
||||||
if 'suspendtime' not in config.keys():
|
|
||||||
config['suspendtime'] = 300
|
|
||||||
|
|
||||||
instances.append( {
|
|
||||||
'config': config,
|
|
||||||
'socket': None,
|
|
||||||
'suspended': False,
|
|
||||||
'lastsuccess': 0,
|
|
||||||
'resumetime': 0
|
|
||||||
} )
|
|
||||||
|
|
||||||
def read_func():
|
|
||||||
global instances
|
|
||||||
|
|
||||||
for inst in instances:
|
|
||||||
config = inst['config']
|
|
||||||
|
|
||||||
if inst['suspended']:
|
|
||||||
if time.time() < inst['resumetime']:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
collectd.info("sem6000: Device {} waking up.".format(config['address']))
|
|
||||||
inst['suspended'] = False
|
|
||||||
inst['lastsuccess'] = time.time()
|
|
||||||
|
|
||||||
try:
|
|
||||||
if inst['socket'] == None:
|
|
||||||
collectd.info("sem6000: Connecting to {}...".format(config['address']))
|
|
||||||
|
|
||||||
inst['socket'] = SEMSocket(config['address'])
|
|
||||||
collectd.info("sem6000: Connected.")
|
|
||||||
|
|
||||||
inst['socket'].getStatus()
|
|
||||||
except (SEMSocket.NotConnectedException, BTLEDisconnectError, BrokenPipeError) as e:
|
|
||||||
collectd.warning("sem6000: Exception caught: {}".format(e))
|
|
||||||
collectd.warning("sem6000: Restarting on next cycle...")
|
|
||||||
|
|
||||||
if inst['lastsuccess'] < time.time() - config['readtimeout']:
|
|
||||||
collectd.error("sem6000: no successful communication with {} for {:.1f} seconds. Suspending device for {:.1f} seconds.".format(
|
|
||||||
config['address'], config['readtimeout'], config['suspendtime']))
|
|
||||||
|
|
||||||
inst['suspended'] = True
|
|
||||||
inst['resumetime'] = time.time() + config['suspendtime']
|
|
||||||
|
|
||||||
if inst['socket'] != None:
|
|
||||||
inst['socket'].disconnect()
|
|
||||||
inst['socket'] = None
|
|
||||||
|
|
||||||
socket = inst['socket']
|
|
||||||
|
|
||||||
if socket != None and socket.voltage != 0:
|
|
||||||
collectd.debug("Uploading values for {}".format(socket.mac_address))
|
|
||||||
|
|
||||||
inst['lastsuccess'] = time.time()
|
|
||||||
|
|
||||||
val = collectd.Values(plugin = 'sem6000-{}'.format(config['socketname']))
|
|
||||||
|
|
||||||
val.type = 'voltage'
|
|
||||||
val.type_instance = 'grid'
|
|
||||||
val.values = [ socket.voltage ]
|
|
||||||
val.dispatch()
|
|
||||||
|
|
||||||
val.type = 'current'
|
|
||||||
val.type_instance = 'load'
|
|
||||||
val.values = [ socket.current ]
|
|
||||||
val.dispatch()
|
|
||||||
|
|
||||||
val.type = 'power'
|
|
||||||
val.type_instance = 'real_power'
|
|
||||||
val.values = [ socket.power ]
|
|
||||||
val.dispatch()
|
|
||||||
|
|
||||||
val.type = 'gauge'
|
|
||||||
val.type_instance = 'power_factor'
|
|
||||||
val.values = [ socket.power_factor ]
|
|
||||||
val.dispatch()
|
|
||||||
|
|
||||||
val.type = 'gauge'
|
|
||||||
val.type_instance = 'load_on'
|
|
||||||
val.values = [ socket.powered ]
|
|
||||||
val.dispatch()
|
|
||||||
|
|
||||||
val.type = 'frequency'
|
|
||||||
val.type_instance = 'grid'
|
|
||||||
val.values = [ socket.frequency ]
|
|
||||||
val.dispatch()
|
|
||||||
|
|
||||||
def shutdown_func():
|
|
||||||
global instances
|
|
||||||
|
|
||||||
for inst in instances:
|
|
||||||
if inst['socket'] != None:
|
|
||||||
inst['socket'].disconnect()
|
|
||||||
|
|
||||||
instances = []
|
|
||||||
|
|
||||||
collectd.register_config(config_func)
|
|
||||||
collectd.register_init(init_func)
|
|
||||||
collectd.register_read(read_func)
|
|
||||||
collectd.register_shutdown(shutdown_func)
|
|
43
example.py
43
example.py
|
@ -1,40 +1,23 @@
|
||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from sem6000 import SEMSocket
|
from sem6000 import SEMSocket
|
||||||
|
|
||||||
import bluepy
|
# auto_reconnect_timeout enabled auto reconnect if sending a command fails. Valid values:
|
||||||
|
# None (default): everything that fails throws NotConnectedException's
|
||||||
|
# -1: infinite retries
|
||||||
|
# integer: seconds before exception is thrown
|
||||||
|
|
||||||
socket = None
|
socket = SEMSocket('f0:c7:7f:0d:e7:17', auto_reconnect_timeout=None)
|
||||||
|
|
||||||
|
#socket.login("1337")
|
||||||
|
#socket.changePassword("1234")
|
||||||
|
#socket.login("1234")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
try:
|
try:
|
||||||
if socket == None:
|
|
||||||
print("Connecting... ", end="")
|
|
||||||
socket = SEMSocket('f0:c7:7f:0d:e7:17')
|
|
||||||
print("Success!")
|
|
||||||
print("You're now connected to: {} (Icon: {})".format(socket.name, socket.icons[0]))
|
|
||||||
if socket.login("1234") and socket.authenticated:
|
|
||||||
print("Login successful!")
|
|
||||||
socket.getSynConfig()
|
|
||||||
print()
|
|
||||||
print("=== Tariff settings ===")
|
|
||||||
print("Default charge:", socket.default_charge)
|
|
||||||
print("Night charge:", socket.night_charge)
|
|
||||||
print("Night tariff from {} to {}".format(socket.night_charge_start_time.tm_hour, socket.night_charge_end_time.tm_hour))
|
|
||||||
print()
|
|
||||||
print("=== Other settings ===")
|
|
||||||
print("Night mode:", "active" if socket.night_mode else "inactive")
|
|
||||||
print("Power protection:", socket.power_protect)
|
|
||||||
print()
|
|
||||||
|
|
||||||
socket.getStatus()
|
socket.getStatus()
|
||||||
|
socket.setStatus(True)
|
||||||
print("=== {} ({}) ===".format(socket.mac_address, "on" if socket.powered else "off"))
|
print("=== {} ({}) ===".format(socket.mac_address, "on" if socket.powered else "off"))
|
||||||
print("\t{}V {}A → {}W@{}Hz (PF: {})".format(socket.voltage, socket.current, socket.power, socket.frequency, socket.power_factor))
|
print("\t{}V {}A → {}W@{}Hz".format(socket.voltage, socket.current, socket.power, socket.frequency))
|
||||||
except (SEMSocket.NotConnectedException, bluepy.btle.BTLEDisconnectError, BrokenPipeError):
|
except SEMSocket.NotConnectedException:
|
||||||
print("Restarting...")
|
socket.reconnect(-1) #infinite reconnect attempts
|
||||||
if socket != None:
|
|
||||||
socket.disconnect()
|
|
||||||
socket = None
|
|
||||||
|
|
||||||
|
|
372
sem6000.py
372
sem6000.py
|
@ -1,205 +1,35 @@
|
||||||
from bluepy import btle
|
from bluepy import btle
|
||||||
|
import struct
|
||||||
import time
|
import time
|
||||||
import datetime
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
class SEMSocket():
|
class SEMSocket():
|
||||||
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"]
|
|
||||||
password = "0000"
|
password = "0000"
|
||||||
|
auto_reconnect_timeout = None
|
||||||
powered = False
|
powered = False
|
||||||
voltage = 0
|
voltage = 0
|
||||||
current = 0
|
current = 0
|
||||||
power = 0
|
power = 0
|
||||||
power_factor = 0
|
|
||||||
total_power = 0
|
|
||||||
frequency = 0
|
frequency = 0
|
||||||
mac_address = ""
|
mac_address = ""
|
||||||
custom_service = None
|
custom_service = None
|
||||||
authenticated = False
|
read_char = None
|
||||||
_icon_idx = None
|
write_char = None
|
||||||
_name = None
|
notify_char = None
|
||||||
_version_char = None
|
connected = False
|
||||||
_write_char = None
|
|
||||||
_name_char = None
|
|
||||||
_btle_device = None
|
|
||||||
|
|
||||||
def __init__(self, mac):
|
def __init__(self, mac, auto_reconnect_timeout = None):
|
||||||
self.mac_address = mac
|
self.mac_address = mac
|
||||||
self._btle_device = btle.Peripheral(None ,addrType=btle.ADDR_TYPE_PUBLIC,iface=0)
|
self.auto_reconnect_timeout = auto_reconnect_timeout
|
||||||
try:
|
try:
|
||||||
self.reconnect()
|
self.reconnect()
|
||||||
except self.NotConnectedException:
|
except self.NotConnectedException:
|
||||||
# initial connection may fail. It is up to the code what to do
|
# initial connection may fail. It is up to the code what to do
|
||||||
pass
|
pass
|
||||||
|
|
||||||
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)
|
|
||||||
return msg.send()
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
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)
|
|
||||||
return msg.send()
|
|
||||||
|
|
||||||
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)
|
|
||||||
return msg.send()
|
|
||||||
|
|
||||||
def login(self, password):
|
|
||||||
self.password = password
|
|
||||||
cmd = bytearray([0x17])
|
|
||||||
payload = bytearray()
|
|
||||||
payload.append(0x00)
|
|
||||||
payload.append(0x00)
|
|
||||||
for i in range(4):
|
|
||||||
payload.append(int(self.password[i]))
|
|
||||||
payload.append(0x00)
|
|
||||||
payload.append(0x00)
|
|
||||||
payload.append(0x00)
|
|
||||||
payload.append(0x00)
|
|
||||||
msg = self.BTLEMessage(self, cmd, payload)
|
|
||||||
success = msg.send()
|
|
||||||
self.authenticated = self.authenticated and success
|
|
||||||
return success
|
|
||||||
|
|
||||||
def changePassword(self, newPassword):
|
|
||||||
cmd = bytearray([0x17])
|
|
||||||
payload = bytearray()
|
|
||||||
payload.append(0x00)
|
|
||||||
payload.append(0x01)
|
|
||||||
for i in range(4):
|
|
||||||
payload.append(int(newPassword[i]))
|
|
||||||
for i in range(4):
|
|
||||||
payload.append(int(self.password[i]))
|
|
||||||
self.password = newPassword
|
|
||||||
msg = self.BTLEMessage(self, cmd, payload)
|
|
||||||
success = msg.send()
|
|
||||||
self.authenticated = self.authenticated and success
|
|
||||||
return success
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
self._name = self._name_char.read().decode("UTF-8")
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@name.setter
|
|
||||||
def name(self, newName):
|
|
||||||
newNameBytes = newName.encode("UTF-8")
|
|
||||||
cmd = bytearray([0x02])
|
|
||||||
payload = bytearray()
|
|
||||||
payload.append(0x02)
|
|
||||||
for i in range(20):
|
|
||||||
if i <= (len(newNameBytes) - 1):
|
|
||||||
payload.append(newNameBytes[i])
|
|
||||||
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)
|
|
||||||
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)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def connected(self):
|
|
||||||
try:
|
|
||||||
return "conn" in self._btle_device.status().get("state")
|
|
||||||
except:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def reconnect(self):
|
|
||||||
self.disconnect()
|
|
||||||
self.connect()
|
|
||||||
if not self.connected:
|
|
||||||
raise self.NotConnectedException
|
|
||||||
|
|
||||||
def connect(self):
|
|
||||||
self.disconnect()
|
|
||||||
self._btle_device.connect(self.mac_address)
|
|
||||||
self._btle_handler = self.BTLEHandler(self)
|
|
||||||
self._btle_device.setDelegate(self._btle_handler)
|
|
||||||
|
|
||||||
self._custom_service = self._btle_device.getServiceByUUID(0xfff0)
|
|
||||||
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
|
|
||||||
self._name_char = self._custom_service.getCharacteristics("0000fff6-0000-1000-8000-00805f9b34fb")[0]
|
|
||||||
|
|
||||||
def disconnect(self):
|
|
||||||
if self.connected == True:
|
|
||||||
self._btle_device.disconnect()
|
|
||||||
|
|
||||||
#def ______RESET(self):
|
|
||||||
# #15, 5, 16, 0, 0, 0, 17, -1, -1 ??? maybe reset?
|
|
||||||
# pass
|
|
||||||
|
|
||||||
class NotConnectedException(Exception):
|
class NotConnectedException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class SendMessageFailed(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class NotLoggedIn(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class BTLEMessage():
|
class BTLEMessage():
|
||||||
MAGIC_START = bytearray([0x0f])
|
MAGIC_START = bytearray([0x0f])
|
||||||
MAGIC_END = bytearray([0xff, 0xff])
|
MAGIC_END = bytearray([0xff, 0xff])
|
||||||
|
@ -218,7 +48,7 @@ class SEMSocket():
|
||||||
|
|
||||||
@cmd.setter
|
@cmd.setter
|
||||||
def cmd(self, cmd):
|
def cmd(self, cmd):
|
||||||
self.__data = self.MAGIC_START + bytearray(1) + cmd + self.__payload + bytearray(1) + self.MAGIC_END
|
self.__data = self.MAGIC_START + bytearray(1) + cmd + self.payload + bytearray(1) + self.MAGIC_END
|
||||||
self.__cmd = cmd
|
self.__cmd = cmd
|
||||||
self.__calc_length()
|
self.__calc_length()
|
||||||
self.__calc_checksum()
|
self.__calc_checksum()
|
||||||
|
@ -229,7 +59,7 @@ class SEMSocket():
|
||||||
|
|
||||||
@payload.setter
|
@payload.setter
|
||||||
def payload(self, payload):
|
def payload(self, payload):
|
||||||
self.__data = self.MAGIC_START + bytearray(1) + self.__cmd + payload + bytearray(1) + self.MAGIC_END
|
self.__data = self.MAGIC_START + bytearray(1) + self.cmd + payload + bytearray(1) + self.MAGIC_END
|
||||||
self.__payload = payload
|
self.__payload = payload
|
||||||
self.__calc_length()
|
self.__calc_length()
|
||||||
self.__calc_checksum()
|
self.__calc_checksum()
|
||||||
|
@ -245,87 +75,141 @@ class SEMSocket():
|
||||||
|
|
||||||
def send(self):
|
def send(self):
|
||||||
if not self.__btle_device.connected:
|
if not self.__btle_device.connected:
|
||||||
self.__btle_device.reconnect()
|
self.__btle_device.reconnect(self.__btle_device.auto_reconnect_timeout)
|
||||||
|
|
||||||
self.__btle_device._write_char.write(self.__data, True)
|
self.__btle_device.write_char.write(self.__data, True)
|
||||||
return self.__btle_device._btle_device.waitForNotifications(5)
|
self.__btle_device.btle_device.waitForNotifications(5)
|
||||||
|
|
||||||
|
|
||||||
class BTLEHandler(btle.DefaultDelegate):
|
class BTLEHandler(btle.DefaultDelegate):
|
||||||
def __init__(self, btle_device):
|
def __init__(self, btle_device):
|
||||||
btle.DefaultDelegate.__init__(self)
|
btle.DefaultDelegate.__init__(self)
|
||||||
self.__btle_device = btle_device
|
self.btle_device = btle_device
|
||||||
|
|
||||||
def handleNotification(self, cHandle, data):
|
def handleNotification(self, cHandle, data):
|
||||||
if len(data) <= 3:
|
|
||||||
print("Notification data seems invalid or incomplete. Could not parse: ", end="")
|
|
||||||
print(data)
|
|
||||||
return
|
|
||||||
|
|
||||||
message_type = data[2]
|
message_type = data[2]
|
||||||
if message_type == 0x00:
|
if message_type == 0x00:
|
||||||
if data[4] == 0x01:
|
if data[4] == 0x01:
|
||||||
print("Checksum error!")
|
print("Checksum error!")
|
||||||
else:
|
else:
|
||||||
print("Unknown error:", data)
|
print("Unknown error:", data)
|
||||||
elif message_type == 0x01: #sync time response
|
elif message_type == 0x03: #switch toggle
|
||||||
if not data[3:] == b'\x00\x00\x02\xff\xff':
|
print("Switch toggled")
|
||||||
print("Time synced failed with unknown data: ", end="")
|
self.btle_device.getStatus()
|
||||||
print(data)
|
elif message_type == 0x04: #status related data
|
||||||
elif message_type == 0x02: #set name response
|
self.btle_device.voltage = data[8]
|
||||||
if not data[3:] == b'\x00\x00\x03\xff\xff':
|
self.btle_device.current = (data[9] << 8 | data[10]) / 1000
|
||||||
print("Set name failed with unknown data: ", end="")
|
self.btle_device.power = (data[5] << 16 | data[6] << 8 | data[7]) / 1000
|
||||||
print(data)
|
self.btle_device.frequency = data[11]
|
||||||
elif message_type == 0x03: #switch toggle response
|
self.btle_device.powered = bool(data[4])
|
||||||
self.__btle_device.getStatus()
|
elif message_type == 0x17:
|
||||||
elif message_type == 0x04: #status related response
|
|
||||||
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
|
|
||||||
|
|
||||||
self.__btle_device.voltage = voltage
|
|
||||||
self.__btle_device.current = current
|
|
||||||
self.__btle_device.power = power
|
|
||||||
self.__btle_device.frequency = data[11]
|
|
||||||
self.__btle_device.powered = bool(data[4])
|
|
||||||
self.__btle_device.total_power = total_power
|
|
||||||
|
|
||||||
# calculated values
|
|
||||||
try:
|
|
||||||
self.__btle_device.power_factor = power / (voltage * current)
|
|
||||||
except ZeroDivisionError:
|
|
||||||
self.__btle_device.power_factor = None
|
|
||||||
elif message_type == 0x10: #plug settings response
|
|
||||||
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])
|
|
||||||
elif message_type == 0x11: #serial number response
|
|
||||||
self.__btle_device._serial = data[-16:].decode("UTF-8")
|
|
||||||
elif message_type == 0x17: #authentication related response
|
|
||||||
if data[5] == 0x00 or data[5] == 0x01:
|
if data[5] == 0x00 or data[5] == 0x01:
|
||||||
# in theory the fifth byte indicates a login attempt response (0) or a response to a password change (1)
|
if not data[4]:
|
||||||
# but since a password change requires a valid login and a successful password changes logs you in,
|
print("Login successful")
|
||||||
# we can just ignore this bit and set the authenticated flag accordingly for both responses
|
else:
|
||||||
self.__btle_device.authenticated = not data[4]
|
print("Login failed")
|
||||||
else:
|
else:
|
||||||
print("5th byte of login-response is > 1:", data)
|
print("5th byte of login-response is > 1:", data)
|
||||||
elif message_type == 0x0f: #set icon response
|
|
||||||
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:
|
|
||||||
print("Unknown response for setting icon: ", end="")
|
|
||||||
print(data[3:])
|
|
||||||
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))
|
||||||
|
|
||||||
|
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)
|
||||||
|
msg.send()
|
||||||
|
|
||||||
|
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)
|
||||||
|
msg.send()
|
||||||
|
|
||||||
|
def login(self, password):
|
||||||
|
self.password = password
|
||||||
|
cmd = bytearray([0x17])
|
||||||
|
payload = bytearray()
|
||||||
|
payload.append(0x00)
|
||||||
|
payload.append(0x00)
|
||||||
|
payload.append(int(self.password[0]))
|
||||||
|
payload.append(int(self.password[1]))
|
||||||
|
payload.append(int(self.password[2]))
|
||||||
|
payload.append(int(self.password[3]))
|
||||||
|
payload.append(0x00)
|
||||||
|
payload.append(0x00)
|
||||||
|
payload.append(0x00)
|
||||||
|
payload.append(0x00)
|
||||||
|
msg = self.BTLEMessage(self, cmd, payload)
|
||||||
|
msg.send()
|
||||||
|
|
||||||
|
def changePassword(self, newPassword):
|
||||||
|
cmd = bytearray([0x17])
|
||||||
|
payload = bytearray()
|
||||||
|
payload.append(0x00)
|
||||||
|
payload.append(0x01)
|
||||||
|
payload.append(int(newPassword[0]))
|
||||||
|
payload.append(int(newPassword[1]))
|
||||||
|
payload.append(int(newPassword[2]))
|
||||||
|
payload.append(int(newPassword[3]))
|
||||||
|
payload.append(int(self.password[0]))
|
||||||
|
payload.append(int(self.password[1]))
|
||||||
|
payload.append(int(self.password[2]))
|
||||||
|
payload.append(int(self.password[3]))
|
||||||
|
self.password = newPassword
|
||||||
|
msg = self.BTLEMessage(self, cmd, payload)
|
||||||
|
msg.send()
|
||||||
|
|
||||||
|
def __reconnect(self):
|
||||||
|
try:
|
||||||
|
self.btle_device = btle.Peripheral(self.mac_address,addrType=btle.ADDR_TYPE_PUBLIC,iface=0)
|
||||||
|
self.btle_handler = self.BTLEHandler(self)
|
||||||
|
|
||||||
|
self.custom_service = self.btle_device.getServiceByUUID(0xfff0)
|
||||||
|
self.read_char = self.custom_service.getCharacteristics("0000fff1-0000-1000-8000-00805f9b34fb")[0]
|
||||||
|
self.write_char = self.custom_service.getCharacteristics("0000fff3-0000-1000-8000-00805f9b34fb")[0]
|
||||||
|
self.notify_char = self.custom_service.getCharacteristics("0000fff4-0000-1000-8000-00805f9b34fb")[0]
|
||||||
|
self.btle_device.setDelegate(self.btle_handler)
|
||||||
|
except btle.BTLEException as e:
|
||||||
|
self.connected = False
|
||||||
|
else:
|
||||||
|
self.connected = True
|
||||||
|
|
||||||
|
def reconnect(self, timeout = None):
|
||||||
|
if timeout == None:
|
||||||
|
self.__reconnect()
|
||||||
|
else:
|
||||||
|
reconnect_start = time.time()
|
||||||
|
while abs(reconnect_start - time.time()) < timeout or timeout == -1:
|
||||||
|
self.__reconnect()
|
||||||
|
|
||||||
|
if not self.connected:
|
||||||
|
raise self.NotConnectedException
|
||||||
|
|
||||||
|
#def SynVer(self):
|
||||||
|
# print("SynVer")
|
||||||
|
# self.read_char.read_value()
|
||||||
|
|
||||||
|
#def GetSynConfig(self):
|
||||||
|
# print("GetSynConfig")
|
||||||
|
# #15, 5, 16, 0, 0, 0, 17, -1, -1
|
||||||
|
# self.write_char.write_value(bytearray(b'\x0f\x05\x10\x00\x00\x00\x11\xff\xff'))
|
||||||
|
|
||||||
|
#def ______RESET(self):
|
||||||
|
# #15, 5, 16, 0, 0, 0, 17, -1, -1 ??? maybe reset?
|
||||||
|
# pass
|
||||||
|
|
||||||
|
#def GetSN(self):
|
||||||
|
# print("GetSN")
|
||||||
|
# #15, 5, 17, 0, 0, 0, 18, -1, -1
|
||||||
|
# self.write_char.write_value(bytearray(b'\x0f\x05\x11\x00\x00\x00\x12\xff\xff'))
|
||||||
|
|
||||||
|
# self.SynVer()
|
||||||
|
# self.notify_char.enable_notifications()
|
||||||
|
# self.Login("1337")
|
||||||
|
# self.GetSynConfig()
|
||||||
|
# #self.GetSN()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue