Tool assisted code formatting

This commit is contained in:
Odd Stråbø 2021-10-31 18:14:22 +01:00
parent d8f3a16a45
commit c7e97aca84

172
solar_ble.py Normal file → Executable file
View file

@ -1,7 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import time # -*- coding: utf-8 -*-
import struct
import datetime import datetime
import struct
import time
from bluepy import btle from bluepy import btle
from libscrc import modbus from libscrc import modbus
@ -9,43 +10,42 @@ from libscrc import modbus
MAC = "DC:0D:30:9C:61:BA" MAC = "DC:0D:30:9C:61:BA"
INTERVAL = 15 INTERVAL = 15
#write_service = "0000ffd0-0000-1000-8000-00805f9b34fb" # write_service = "0000ffd0-0000-1000-8000-00805f9b34fb"
#read_service = "0000fff0-0000-1000-8000-00805f9b34fb" # read_service = "0000fff0-0000-1000-8000-00805f9b34fb"
write_device = "0000ffd1-0000-1000-8000-00805f9b34fb" write_device = "0000ffd1-0000-1000-8000-00805f9b34fb"
#read_device = "0000fff1-0000-1000-8000-00805f9b34fb" # read_device = "0000fff1-0000-1000-8000-00805f9b34fb"
CMD_GET_1 = b"\xff\x03\x00\x0c\x00\x02"
CMD_GET_1 = b'\xff\x03\x00\x0c\x00\x02'
# > ff 03 04 20 20 20 20 # > ff 03 04 20 20 20 20
CMD_GET_MODEL = b'\xff\x03\x00\x0c\x00\x08' CMD_GET_MODEL = b"\xff\x03\x00\x0c\x00\x08"
# > ff 03 10 20 20 20 20 4d 4c 32 34 32 30 20 20 20 20 20 20 # > ff 03 10 20 20 20 20 4d 4c 32 34 32 30 20 20 20 20 20 20
# Device SKU: ML2420 # Device SKU: ML2420
CMD_GET_VERSION = b'\xff\x03\x00\x14\x00\x04' CMD_GET_VERSION = b"\xff\x03\x00\x14\x00\x04"
# > ff 03 08 00 04 02 00 02 00 00 03 # > ff 03 08 00 04 02 00 02 00 00 03
# CC ?? 11 22 33 ?? 44 55 66 # CC ?? 11 22 33 ?? 44 55 66
# Version: 4.2.0 # Version: 4.2.0
CMD_GET_SERIAL = b'\xff\x03\x00\x18\x00\x03' CMD_GET_SERIAL = b"\xff\x03\x00\x18\x00\x03"
# > ff 03 06 3c 13 02 67 00 01 # > ff 03 06 3c 13 02 67 00 01
# CC 11 22 33 33 ?? ?? # CC 11 22 33 33 ?? ??
# SN: 60-19-0615 # SN: 60-19-0615
CMD_GET_BATTERY_STATE = b'\xff\x03\x01\x00\x00\x07' CMD_GET_BATTERY_STATE = b"\xff\x03\x01\x00\x00\x07"
# > ff 03 0e 00 48 00 7e 00 1d 0e 0d 00 7e 00 1c 00 03 # > ff 03 0e 00 48 00 7e 00 1d 0e 0d 00 7e 00 1c 00 03
# CC 11 11 22 22 33 33 44 55 66 66 77 77 88 88 # CC 11 11 22 22 33 33 44 55 66 66 77 77 88 88
#1: Battery charge: 72 % # 1: Battery charge: 72 %
#2: Battery voltage: 12.6 V # 2: Battery voltage: 12.6 V
#3: Battery current: 0.29 A # 3: Battery current: 0.29 A
#4: Internal temperature? # 4: Internal temperature?
#5: External temperature probe for battery signet 8bit: 13 degC # 5: External temperature probe for battery signet 8bit: 13 degC
#6: Load voltage: 12.6 V # 6: Load voltage: 12.6 V
#7: Load current: 0.28 A # 7: Load current: 0.28 A
#8: Load power: 3 W # 8: Load power: 3 W
CMD_GET_PANEL_STATUS = b'\xff\x03\x01\x07\x00\x04' CMD_GET_PANEL_STATUS = b"\xff\x03\x01\x07\x00\x04"
# > ff 03 08 00 c8 00 14 00 04 00 01 # > ff 03 08 00 c8 00 14 00 04 00 01
# CC 11 11 22 22 33 33 ?? ?? # CC 11 11 22 22 33 33 ?? ??
# 1: Panel voltage: 20.0 V # 1: Panel voltage: 20.0 V
@ -53,14 +53,14 @@ CMD_GET_PANEL_STATUS = b'\xff\x03\x01\x07\x00\x04'
# 3: Panel power: 4 W # 3: Panel power: 4 W
# Charging status? # Charging status?
CMD_GET_2 = b'\xff\x03\x01\x20\x00\x03' CMD_GET_2 = b"\xff\x03\x01\x20\x00\x03"
# > ff 03 06 80 02 00 00 00 00 # > ff 03 06 80 02 00 00 00 00
# CC 11 22 33 33 33 33 # CC 11 22 33 33 33 33
# 1: boolean flag?: 1 # 1: boolean flag?: 1
# 2: ?: 2 # 2: ?: 2
# 3: ?: 0 # 3: ?: 0
CMD_GET_BATTERY_PARAMETERS = b'\xff\x03\xe0\x01\x00\x21' CMD_GET_BATTERY_PARAMETERS = b"\xff\x03\xe0\x01\x00\x21"
# > ff 03 42 07 d0 00 c8 ff 0c 00 02 00 a0 00 9b 00 92 00 90 00 # > ff 03 42 07 d0 00 c8 ff 0c 00 02 00 a0 00 9b 00 92 00 90 00
# > 8a 00 84 00 7e 00 78 00 6f 00 6a 64 32 00 05 00 78 00 78 00 # > 8a 00 84 00 7e 00 78 00 6f 00 6a 64 32 00 05 00 78 00 78 00
# > 1e 00 03 00 41 00 a3 00 4b 00 a3 00 00 00 00 00 00 00 00 00 # > 1e 00 03 00 41 00 a3 00 4b 00 a3 00 00 00 00 00 00 00 00 00
@ -68,16 +68,16 @@ CMD_GET_BATTERY_PARAMETERS = b'\xff\x03\xe0\x01\x00\x21'
# 33 * uint16 # 33 * uint16
# (0xff, 267, 21) # (0xff, 267, 21)
CMD_GET_LOAD_PARAMETERS = b'\xff\x03\x01\x0b\x00\x15' CMD_GET_LOAD_PARAMETERS = b"\xff\x03\x01\x0b\x00\x15"
# > ff 03 2a 00 7c 00 7f 00 51 00 20 00 0a 00 03 00 00 00 00 00 # > ff 03 2a 00 7c 00 7f 00 51 00 20 00 0a 00 03 00 00 00 00 00
# > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# > 00 00 00 00 00 # > 00 00 00 00 00
# 01 03 f000 000a # 01 03 f000 000a
CMD_GET_HISTORICAL_TODAY = b'\x01\x03\xf0\x00\x00\x0a' CMD_GET_HISTORICAL_TODAY = b"\x01\x03\xf0\x00\x00\x0a"
CMD_GET_HISTORICAL_YESTERDAY = b'\x01\x03\xf0\x01\x00\x0a' CMD_GET_HISTORICAL_YESTERDAY = b"\x01\x03\xf0\x01\x00\x0a"
CMD_GET_HISTORICAL_D2 = b'\x01\x03\xf0\x02\x00\x0a' CMD_GET_HISTORICAL_D2 = b"\x01\x03\xf0\x02\x00\x0a"
CMD_GET_HISTORICAL_D3 = b'\x01\x03\xf0\x03\x00\x0a' CMD_GET_HISTORICAL_D3 = b"\x01\x03\xf0\x03\x00\x0a"
# ,- battery_min_voltage # ,- battery_min_voltage
# | ,- battery_max_voltage # | ,- battery_max_voltage
@ -103,24 +103,24 @@ CMD_GET_HISTORICAL_D3 = b'\x01\x03\xf0\x03\x00\x0a'
# production_power = 0 Wh # production_power = 0 Wh
# consumption_power = 0 Wh # consumption_power = 0 Wh
CMD_ENABLE_LOAD = b'\xff\x06\x01\x0a\x00\x01' CMD_ENABLE_LOAD = b"\xff\x06\x01\x0a\x00\x01"
CMD_DISABLE_LOAD = b'\xff\x06\x01\x0a\x00\x00' CMD_DISABLE_LOAD = b"\xff\x06\x01\x0a\x00\x00"
CMD_ = b'\xff\x78\x00\x00\x00\x01' CMD_ = b"\xff\x78\x00\x00\x00\x01"
#CMD_GET_BATTERY_STATE = b'\xff\x03\x01\x00\x00\x07' # CMD_GET_BATTERY_STATE = b'\xff\x03\x01\x00\x00\x07'
# > ff 03 0e 00 48 00 7e 00 1d 0e 0d 00 7e 00 1c 00 03 # > ff 03 0e 00 48 00 7e 00 1d 0e 0d 00 7e 00 1c 00 03
# CC 11 11 22 22 33 33 44 55 66 66 77 77 88 88 # CC 11 11 22 22 33 33 44 55 66 66 77 77 88 88
#1: Battery charge: 72 % # 1: Battery charge: 72 %
#2: Battery voltage: 12.6 V # 2: Battery voltage: 12.6 V
#3: Battery current: 0.29 A # 3: Battery current: 0.29 A
#4: Internal temperature? # 4: Internal temperature?
#5: External temperature probe for battery signed 8bit: 13 degC # 5: External temperature probe for battery signed 8bit: 13 degC
#6: Load voltage: 12.6 V # 6: Load voltage: 12.6 V
#7: Load current: 0.28 A # 7: Load current: 0.28 A
#8: Load power: 3 W # 8: Load power: 3 W
#CMD_GET_PANEL_STATUS = b'\xff\x03\x01\x07\x00\x04' # CMD_GET_PANEL_STATUS = b'\xff\x03\x01\x07\x00\x04'
# > ff 03 08 00 c8 00 14 00 04 00 01 # > ff 03 08 00 c8 00 14 00 04 00 01
# CC 11 11 22 22 33 33 ?? ?? # CC 11 11 22 22 33 33 ?? ??
# > ff 03 08 00 00 00 00 00 00 00 00 # > ff 03 08 00 00 00 00 00 00 00 00
@ -133,86 +133,100 @@ STATUS = {}
def parsePacket(data): def parsePacket(data):
timestamp = datetime.datetime.utcnow().isoformat(' ') timestamp = datetime.datetime.utcnow().isoformat(" ")
prefix = data[0] # prefix = data[0]
operation = data[1] operation = data[1]
cc = data[2] cc = data[2]
res = None res = None
if operation == 3: if operation == 3:
if cc == 0x0e: # GET_BATTERY_STATE if cc == 0x0E: # GET_BATTERY_STATE
res = dict(zip( res = dict(
('battery_charge', 'battery_voltage', 'battery_current', '_internal_temperature?', 'battery_temperature', 'load_voltage', 'load_current', 'load_power'), zip(
struct.unpack('!xxxHHHbbHHHxx', data) (
)) "battery_charge",
res['battery_voltage'] /= 10 "battery_voltage",
res['battery_current'] /= 100 "battery_current",
res['load_voltage'] /= 10 "_internal_temperature?",
res['load_current'] /= 100 "battery_temperature",
"load_voltage",
"load_current",
"load_power",
),
struct.unpack("!xxxHHHbbHHHxx", data),
)
)
res["battery_voltage"] /= 10
res["battery_current"] /= 100
res["load_voltage"] /= 10
res["load_current"] /= 100
STATUS.update(res) STATUS.update(res)
elif cc == 0x08: # GET_PANEL_STATUS (OR version) elif cc == 0x08: # GET_PANEL_STATUS (OR version)
res = dict(zip( res = dict(
('panel_voltage', 'panel_current', 'panel_power', 'load_enabled'), zip(
struct.unpack('!xxxHHHx?xx', data) ("panel_voltage", "panel_current", "panel_power", "load_enabled"),
)) struct.unpack("!xxxHHHx?xx", data),
res['panel_voltage'] /= 10 )
res['panel_current'] /= 100 )
res["panel_voltage"] /= 10
res["panel_current"] /= 100
STATUS.update(res) STATUS.update(res)
elif operation == 6 and cc == 1: elif operation == 6 and cc == 1:
res = dict(zip( res = dict(zip(("load_enabled",), struct.unpack("!xxxxx?xx", data)))
('load_enabled',), STATUS.update(res)
struct.unpack('!xxxxx?xx', data)
))
STATUS.update(res)
if res: if res:
print(timestamp, res) print(timestamp, res)
return res return res
print(timestamp, data) print(timestamp, data)
class Delegate(btle.DefaultDelegate): class Delegate(btle.DefaultDelegate):
data = bytearray() data = bytearray()
def handleNotification(self, cHandle, data): def handleNotification(self, cHandle, data):
dlen = len(data) # print(cHandle, data, dlen)
#print(cHandle, data, dlen)
self.data.extend(data) self.data.extend(data)
c_crc = modbus(bytes(self.data[:-2])) c_crc = modbus(bytes(self.data[:-2]))
d_crc = self.data[-1] << 8 | self.data[-2] # byte order is inverted in regards to libscrc output # byte order is inverted in regards to libscrc output
#print(hex(c_crc), hex(d_crc)) d_crc = self.data[-1] << 8 | self.data[-2]
# print(hex(c_crc), hex(d_crc))
if c_crc == d_crc: if c_crc == d_crc:
parsePacket(self.data) parsePacket(self.data)
self.data.clear() self.data.clear()
def write(fh, data): def write(fh, data):
bdata = bytes(data) bdata = bytes(data)
crc = modbus(bdata) crc = modbus(bdata)
bcrc = bytes([crc & 0xff, (crc & 0xff00) >> 8]) bcrc = bytes([crc & 0xFF, (crc & 0xFF00) >> 8])
fh.write(data + bcrc) fh.write(data + bcrc)
dlgt = Delegate() dlgt = Delegate()
prev = time.time() - INTERVAL prev = time.time() - INTERVAL
with btle.Peripheral(MAC).withDelegate(dlgt) as dev: with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
#for svc in dev.services: # for svc in dev.services:
# print(svc, svc.uuid) # print(svc, svc.uuid)
# print(dir(dev)) # print(dir(dev))
# wd = dev.getServiceByUUID(write_service) # wd = dev.getServiceByUUID(write_service)
# print(wd) # print(wd)
wd = dev.getCharacteristics(uuid=write_device)[0] wd = dev.getCharacteristics(uuid=write_device)[0]
#print(cs) # print(cs)
#print(dir(cs)) # print(dir(cs))
#print(wd.write(b'\xf0\x03\x00\x0c\x00\x08\x91\xd1')) # print(wd.write(b'\xf0\x03\x00\x0c\x00\x08\x91\xd1'))
# rd = dev.getCharacteristics(uuid=read_device)[0] # rd = dev.getCharacteristics(uuid=read_device)[0]
# print(rd.read()) # print(rd.read())
while True: while True:
dev.waitForNotifications(1) dev.waitForNotifications(1)
@ -228,7 +242,7 @@ with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
write(wd, CMD_GET_BATTERY_STATE) write(wd, CMD_GET_BATTERY_STATE)
dev.waitForNotifications(1) dev.waitForNotifications(1)
#if STATUS.get('load_enabled'): # if STATUS.get('load_enabled'):
# write(wd, CMD_DISABLE_LOAD) # write(wd, CMD_DISABLE_LOAD)
#else: # else:
# write(wd, CMD_ENABLE_LOAD) # write(wd, CMD_ENABLE_LOAD)