123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- from dataclasses import dataclass
- from enum import Enum
- import struct
- from typing import List, Dict, Callable, Any
- from .crc import crc16
- type Value = str|int|float
- class FunctionCodes(Enum):
- # Read
- READ_STATUS_REGISTER = 0x02 # Read the switch input status
- READ_PARAMETER = 0x03 # Read multiple hold registers
- READ_MEMORY = 0x04 # Read input register
- # Write
- WRITE_STATUS_REGISTER = 0x05 # Write single register
- WRITE_MEMORY_SINGLE = 0x06 # Write single hold register
- WRITE_MEMORY_RANGE = 0x10 # Write multiple hold registers
- @dataclass
- class Variable():
- address: int
- is_32_bit: bool
- is_signed: bool
- function_codes: List[int]
- unit: str
- multiplier: int
- name: str
- friendly_name: str
- func: Callable[int, str]
- class LumiaxClient:
- def _get_functional_status_registers(self, function_codes: list[int], offset: int):
- return [
- # Controller functional status 1
- Variable(offset, False, False, function_codes, "", 0, "maximum_system_voltage_level", "Maximum system voltage level",
- lambda x: ["", "12V", "24V", "36V", "48V"][(x >> 12) & 0xF]),
- Variable(offset, False, False, function_codes, "", 0, "minimum_system_voltage_level", "Minimum system voltage level",
- lambda x: ["", "12V", "24V", "36V", "48V"][(x >> 8) & 0xF]),
- Variable(offset, False, False, function_codes, "", 0, "controller_series", "Controller Series",
- lambda x: ["MT series", "DC series", "SMR series"][(x >> 4) & 0xF]),
- Variable(offset, False, False, function_codes, "", 0, "battery_type", "Battery type",
- lambda x: ["Lithium battery", "Non Lithium battery"][(x >> 0) & 0xF]),
- # Controller functional status 2
- Variable(offset + 1, False, False, function_codes, "", 0, "infrared_function_available", "Is infrared function available",
- lambda x: (x >> 15) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "automatic_power_reduction_available", "Is automatic power reduction setting available(only in 365 mode)",
- lambda x: (x >> 14) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "charging_at_zero_celsius_available", "Is 0°C prohibit charging setting available",
- lambda x: (x >> 13) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "grade_of_rated_voltage_available", "Is grade of rated voltage setting available",
- lambda x: (x >> 12) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "overcharge_recovery_voltage_available", "Is overcharge recovery voltage setting available (only lithium battery)",
- lambda x: (x >> 11) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "overcharge_protection_available", "Is overcharge protection setting available (only lithium battery)",
- lambda x: (x >> 10) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "floating_charge_voltage_available", "Is floating charge voltage setting available",
- lambda x: (x >> 9) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "equilibrium_charge_voltage_available", "Is equilibrium charge voltage setting available",
- lambda x: (x >> 8) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "strong_charging_voltage_available", "Is strong charging voltage setting available",
- lambda x: (x >> 7) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "low_voltage_recovery_voltage_available", "Is low voltage recovery setting available",
- lambda x: (x >> 6) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "low_voltage_protection_voltage_available", "Is low voltage protection setting available",
- lambda x: (x >> 5) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "battery_type_available", "Is Battery Type setting available",
- lambda x: (x >> 4) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "backlight_time_available", "Is Backlight Time setting available",
- lambda x: (x >> 3) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "device_time_available", "Is Device Time setting available",
- lambda x: (x >> 2) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "device_id_available", "Is Device ID setting available",
- lambda x: (x >> 1) & 1 == 1),
- Variable(offset + 1, False, False, function_codes, "", 0, "device_password_available", "Is Device password setting available",
- lambda x: (x >> 0) & 1 == 1),
- # Controller functional status 3
- Variable(offset + 2, False, False, function_codes, "", 0, "six_time_frame_mode_available", "Is Six Time Frame Mode available",
- lambda x: (x >> 7) & 1 == 1),
- Variable(offset + 2, False, False, function_codes, "", 0, "five_time_frame_mode_available", "Is Five Time Frame Mode available",
- lambda x: (x >> 6) & 1 == 1),
- Variable(offset + 2, False, False, function_codes, "", 0, "timing_control_mode_available", "Is Timing Control available",
- lambda x: (x >> 5) & 1 == 1),
- Variable(offset + 2, False, False, function_codes, "", 0, "t0t_mode_available", "Is T0T Mode available",
- lambda x: (x >> 4) & 1 == 1),
- Variable(offset + 2, False, False, function_codes, "", 0, "fixed_duration_mode_available", "Is Fixed Light Up Duration Mode available",
- lambda x: (x >> 3) & 1 == 1),
- Variable(offset + 2, False, False, function_codes, "", 0, "d2d_mode_available", "Is D2D Mode available",
- lambda x: (x >> 2) & 1 == 1),
- Variable(offset + 2, False, False, function_codes, "", 0, "24h_mode_available", "Is 24H Mode available",
- lambda x: (x >> 1) & 1 == 1),
- Variable(offset + 2, False, False, function_codes, "", 0, "manual_operation_mode_available", "Is Manual Operation Mode available",
- lambda x: (x >> 0) & 1 == 1),
- # Controller functional status 4 (reserved)
- # Variable(offset + 3, False, False, function_codes, "", 0, "controller_functional_status_4", "Controller functional status 4", None),
- ]
- def _get_status_registers(self, offset: int):
- return [
- # Battery status
- Variable(offset, False, False, [0x04], "", 0, "battery_temperature_protection_status", "Battery temperature protection status",
- lambda x: ["Normal", "High temperature protection"][(x >> 4) & 0x1]),
- Variable(offset, False, False, [0x04], "", 0, "battery_voltage_protection_status", "Battery voltage protection status",
- lambda x: ["Normal", "Over voltage protection", "Voltage is low", "Low voltage protection"][(x >> 0) & 0xF]),
- # Charge status
- Variable(offset + 1, False, False, [0x04], "", 0, "solar_panel_charge_disabled", "Is charging manually disabled",
- lambda x: (x >> 6) & 0x1 == 1),
- Variable(offset + 1, False, False, [0x04], "", 0, "solar_panel_is_night", "Is solar panel night",
- lambda x: (x >> 5) & 0x1 == 1),
- Variable(offset + 1, False, False, [0x04], "", 0, "solar_panel_charge_over_temperature", "Is charge over temperature",
- lambda x: (x >> 4) & 0x1 == 1),
- Variable(offset + 1, False, False, [0x04], "", 0, "solar_panel_charge_state", "Solar panel charge status",
- lambda x: ["Not charging", "Float charge", "Boost charge", "Equal charge"][(x >> 2) & 0x3]),
- Variable(offset + 1, False, False, [0x04], "", 0, "solar_panel_charge_state", "Is charge fault",
- lambda x: (x >> 1) & 0x1 == 1),
- Variable(offset + 1, False, False, [0x04], "", 0, "solar_panel_is_charging", "Solar panel is charging",
- lambda x: (x >> 0) & 0x1 == 1),
- # Discharge status
- Variable(offset + 2, False, False, [0x04], "", 0, "load_state", "Load status",
- lambda x: ["Light load", "Moderate load", "Rated load", "Overload"][(x >> 12) & 0x3]),
- Variable(offset + 2, False, False, [0x04], "", 0, "output_short_circuit", "Is output short circuit",
- lambda x: (x >> 11) & 0x1 == 1),
- Variable(offset + 2, False, False, [0x04], "", 0, "output_hardware_protection", "Is output hardware protection",
- lambda x: (x >> 4) & 0x1 == 1),
- Variable(offset + 2, False, False, [0x04], "", 0, "output_open_circuit_protection", "Is output open circuit protection",
- lambda x: (x >> 3) & 0x1 == 1),
- Variable(offset + 2, False, False, [0x04], "", 0, "output_over_temperature", "Is output over temperature",
- lambda x: (x >> 2) & 0x1 == 1),
- Variable(offset + 2, False, False, [0x04], "", 0, "output_fault", "Is output fault",
- lambda x: (x >> 1) & 0x1 == 1),
- Variable(offset + 2, False, False, [0x04], "", 0, "load_is_enabled", "Is load enabled",
- lambda x: (x >> 0) & 0x1 == 1),
- ]
-
- def __init__(self):
- # List of addresses to variable information
- self.variables = [
- Variable(0x2000, False, False, [0x02], "", 0, "equipment_internal_over_temperature", "Equipment internal over temperature",
- lambda x: ["Normal", "Over temperature"][x]),
- Variable(0x200C, False, False, [0x02], "", 0, "day_or_night", "Day or night",
- lambda x: ["Day", "Night"][x]),
- ] + self._get_functional_status_registers([0x04], 0x3011) + [
- Variable(0x3015, False, False, [0x04], "V", 100, "lvd_min_setting_value", "Low voltage detect min setting value", None),
- Variable(0x3016, False, False, [0x04], "V", 100, "lvd_max_setting_value", "Low voltage detect max setting value", None),
- Variable(0x3017, False, False, [0x04], "V", 100, "lvd_default_setting_value", "Low voltage detect default setting value", None),
- Variable(0x3018, False, False, [0x04], "V", 100, "lvr_min_setting_value", "Low voltage recovery min setting value", None),
- Variable(0x3019, False, False, [0x04], "V", 100, "lvr_max_setting_value", "Low voltage recovery max setting value", None),
- Variable(0x301A, False, False, [0x04], "V", 100, "lvr_default_setting_value", "Low voltage recovery default setting value", None),
- Variable(0x301B, False, False, [0x04], "V", 100, "cvt_min_setting_value", "Charge target voltage min setting value for Li Series controller", None),
- Variable(0x301C, False, False, [0x04], "V", 100, "cvt_max_setting_value", "Charge target voltage max setting value for Li Series controller", None),
- Variable(0x301D, False, False, [0x04], "V", 100, "cvt_default_setting_value", "Charge target voltage default setting value Li Series controller", None),
- Variable(0x301E, False, False, [0x04], "V", 100, "cvr_min_setting_value", "Charge recovery voltage min setting value Li Series controller", None),
- Variable(0x301F, False, False, [0x04], "V", 100, "cvr_max_setting_value", "Charge recovery voltage max setting value Li Series controller", None),
- Variable(0x3020, False, False, [0x04], "V", 100, "cvr_default_setting_value", "Charge recovery voltage default setting value Li Series controller", None),
- Variable(0x3021, False, False, [0x04], "V", 100, "day_night_threshold_voltage_min", "Day/Night threshold voltage min setting value", None),
- Variable(0x3022, False, False, [0x04], "V", 100, "day_night_threshold_voltage_max", "Day/Night threshold voltage max setting value", None),
- Variable(0x3023, False, False, [0x04], "V", 100, "day_night_threshold_voltage_default", "Day/Night threshold voltage default setting value", None),
- Variable(0x3024, False, False, [0x04], "V", 100, "dimming_voltage_min", "Dimming voltage min setting value", None),
- Variable(0x3025, False, False, [0x04], "V", 100, "dimming_voltage_max", "Dimming voltage max setting value", None),
- Variable(0x3026, False, False, [0x04], "V", 100, "dimming_voltage_default", "Dimming voltage default setting value", None),
- Variable(0x3027, False, False, [0x04], "A", 100, "load_current_min", "Load current min setting value", None),
- Variable(0x3028, False, False, [0x04], "A", 100, "load_current_max", "Load current max setting value", None),
- Variable(0x3029, False, False, [0x04], "V", 100, "cvt_cvr_max_dropout_voltage", "Charge target and recovery voltage max allow dropout voltage for Li-series controller", None),
- Variable(0x302A, False, False, [0x04], "V", 100, "cvt_cvr_min_dropout_voltage", "Charge target and recovery voltage min allow dropout voltage for Li-series controller", None),
- Variable(0x302B, False, False, [0x04], "V", 100, "lvd_lvr_min_dropout_voltage", "Low voltage detect and recovery min allow dropout voltage", None),
- Variable(0x302C, False, False, [0x04], "V", 100, "min_allow_dropout_voltage", "CVR and LVD & CVT and LVR Min allow dropout voltage", None),
- Variable(0x3030, False, False, [0x04], "", 0, "equipment_id", "Equipment ID", None),
- Variable(0x3031, False, False, [0x04], "", 0, "run_days", "Number of running days", None),
- Variable(0x3032, False, False, [0x04], "V", 100, "battery_voltage_level", "Current battery voltage level", None),
- ] + self._get_status_registers(0x3033) + [
- Variable(0x3036, False, False, [0x04], "℃", 100, "environment_temperature", "Environment temperature", None),
- Variable(0x3037, False, False, [0x04], "℃", 100, "device_built_in_temperature", "Device built-intemperature", None),
- Variable(0x3038, False, False, [0x04], "", 0, "battery_empty_times", "Battery empty times", None),
- Variable(0x3039, False, False, [0x04], "", 0, "battery_full_times", "Battery full times", None),
- Variable(0x303A, False, False, [0x04], "", 0, "over_voltage_protection_times", "Over-voltage protection times", None),
- Variable(0x303B, False, False, [0x04], "", 0, "over_current_protection_times", "Over-current protection times", None),
- Variable(0x303C, False, False, [0x04], "", 0, "short_circuit_protection_times", "short-circuit protection times", None),
- Variable(0x303D, False, False, [0x04], "", 0, "open_circuit_protection_times", "Open-circuit protection times", None),
- Variable(0x303E, False, False, [0x04], "", 0, "hardware_protection_times", "Hardware protection times", None),
- Variable(0x303F, False, False, [0x04], "", 0, "charge_over_temperature_protection_times", "Charge over-temperature protection times", None),
- Variable(0x3040, False, False, [0x04], "", 0, "discharge_over_temperature_protection_times", "Discharge over-temperature protection time", None),
- Variable(0x3045, False, False, [0x04], "%", 1, "battery_percentage", "Battery remaining capacity", None),
- Variable(0x3046, False, False, [0x04], "V", 100, "battery_voltage", "Battery voltage", None),
- Variable(0x3047, False, True, [0x04], "A", 100, "battery_current", "Battery current", None),
- Variable(0x3048, True, True, [0x04], "W", 100, "battery_power", "Battery power", None),
- Variable(0x304A, False, False, [0x04], "V", 100, "load_voltage", "Load voltage", None),
- Variable(0x304B, False, False, [0x04], "A", 100, "load_current", "Load current", None),
- Variable(0x304C, True, False, [0x04], "W", 100, "load_power", "Load power", None),
- Variable(0x304E, False, False, [0x04], "V", 100, "solar_panel_voltage", "Solar panel voltage", None),
- Variable(0x304F, False, False, [0x04], "A", 100, "solar_panel_current", "Solar panel current", None),
- Variable(0x3050, True, False, [0x04], "W", 100, "solar_panel_power", "Solar panel power", None),
- Variable(0x3052, False, False, [0x04], "kWh", 100, "solar_panel_daily_energy", "The charging capacity of the day", None),
- Variable(0x3053, True, False, [0x04], "kWh", 100, "solar_panel_total_energy", "Total charging capacity", None),
- Variable(0x3055, True, False, [0x04], "kWh", 100, "load_daily_energy", "The electricity consumption of the day", None),
- Variable(0x3056, True, False, [0x04], "kWh", 100, "load_total_energy", "Total electricity consumption", None),
- Variable(0x3058, False, False, [0x04], "Min", 0, "total_light_time_during_the_day", "Total light time during the day", None),
- Variable(0x309D, False, False, [0x04], "", 0, "run_days", "The number of running days", None),
- Variable(0x30A0, False, False, [0x04], "V", 100, "battery_voltage", "Battery voltage", None),
- Variable(0x30A1, False, True, [0x04], "A", 100, "battery_current", "Battery current", None),
- Variable(0x30A2, False, False, [0x04], "℃", 100, "environment_temperature", "Environment temperature", None),
- ] + self._get_status_registers(0x30A3) + [
- Variable(0x30A6, False, False, [0x04], "", 0, "battery_empty_times", "Battery empty times", None),
- Variable(0x30A7, False, False, [0x04], "", 0, "battery_full_times", "Battery full times", None),
- Variable(0x30A8, False, False, [0x04], "V", 100, "battery_daily_voltage_maximum", "The highest battery voltage today", None),
- Variable(0x30A9, False, False, [0x04], "V", 100, "battery_daily_voltage_minimum", "The lowest battery voltage today", None),
- Variable(0x3125, False, False, [0x04], "V", 100, "load_voltage", "Load voltage", None),
- Variable(0x3126, False, False, [0x04], "A", 100, "load_current", "Load current", None),
- Variable(0x3127, True, False, [0x04], "W", 100, "load_power", "Load power", None),
- Variable(0x3129, False, False, [0x04], "kWh", 100, "load_daily_energy", "The electricity consumption of the day", None),
- Variable(0x312E, True, False, [0x04], "kWh", 100, "load_total_energy", "Total electricity consumption", None),
- Variable(0x316C, False, False, [0x04], "", 0, "run_days", "The number of running days", None),
-
- # Factory settings
- Variable(0x3000, False, False, [0x04], "V", 100, "solar_panel_rated_voltage", "Solar panel rated voltage", None),
- Variable(0x3001, False, False, [0x04], "A", 100, "solar_panel_rated_current", "Solar panel rated current", None),
- Variable(0x3002, True, False, [0x04], "W", 100, "solar_panel_rated_power", "Solar panel rated power", None),
- Variable(0x3004, False, False, [0x04], "V", 100, "battery_rated_voltage", "Battery rated voltage", None),
- Variable(0x3005, False, False, [0x04], "A", 100, "battery_rated_current", "Battery rated current", None),
- Variable(0x3006, True, False, [0x04], "W", 100, "battery_rated_power", "Battery rated power", None),
- Variable(0x3008, False, False, [0x04], "V", 100, "load_rated_voltage", "Load rated voltage", None),
- Variable(0x3009, False, False, [0x04], "A", 100, "load_rated_current", "Load rated current", None),
- Variable(0x300A, True, False, [0x04], "W", 100, "load_rated_power", "Load rated power", None),
- ] + self._get_functional_status_registers([0x03], 0x8FF0) + [
- Variable(0x8FF4, False, False, [0x03], "V", 100, "lvd_min_setting_value", "Low voltage detect min setting value", None),
- Variable(0x8FF5, False, False, [0x03], "V", 100, "lvd_max_setting_value", "Low voltage detect max setting value", None),
- Variable(0x8FF6, False, False, [0x03], "V", 100, "lvd_default_setting_value", "Low voltage detect default setting value", None),
- Variable(0x8FF7, False, False, [0x03], "V", 100, "lvr_min_setting_value", "Low voltage recovery min setting value", None),
- Variable(0x8FF8, False, False, [0x03], "V", 100, "lvr_max_setting_value", "Low voltage recovery max setting value", None),
- Variable(0x8FF9, False, False, [0x03], "V", 100, "lvr_default_setting_value", "Low voltage recovery default setting value", None),
- Variable(0x8FFA, False, False, [0x03], "V", 100, "cvt_min_setting_value", "Charge target voltage min setting value for Li Series controller", None),
- Variable(0x8FFB, False, False, [0x03], "V", 100, "cvt_max_setting_value", "Charge target voltage max setting value Li Series controller", None),
- Variable(0x8FFC, False, False, [0x03], "V", 100, "cvt_default_setting_value", "Charge target voltage default setting value Li Series controller", None),
- Variable(0x8FFD, False, False, [0x03], "V", 100, "cvr_min_setting_value", "Charge recovery voltage min setting value Li Series controller", None),
- Variable(0x8FFE, False, False, [0x03], "V", 100, "cvr_max_setting_value", "Charge recovery voltage max setting value Li Series controller", None),
- Variable(0x8FFF, False, False, [0x03], "V", 100, "cvr_default_setting_value", "Charge recovery voltage default setting value Li Series controller", None),
- Variable(0x9000, False, False, [0x03], "V", 100, "day_night_threshold_voltage_min", "Day/Night threshold voltage min setting value", None),
- Variable(0x9001, False, False, [0x03], "V", 100, "day_night_threshold_voltage_max", "Day/Night threshold voltage max setting value", None),
- Variable(0x9002, False, False, [0x03], "V", 100, "day_night_threshold_voltage_default", "Day/Night threshold voltage default setting value", None),
- Variable(0x9003, False, False, [0x03], "V", 100, "dimming_voltage_min", "Dimming voltage min setting value", None),
- Variable(0x9004, False, False, [0x03], "V", 100, "dimming_voltage_max", "Dimming voltage max setting value", None),
- Variable(0x9005, False, False, [0x03], "V", 100, "dimming_voltage_default", "Dimming voltage default setting value", None),
- Variable(0x9006, False, False, [0x03], "A", 100, "load_current_min", "Load current min setting value", None),
- Variable(0x9007, False, False, [0x03], "A", 100, "load_current_max", "Load current max setting value", None),
- Variable(0x9008, False, False, [0x03], "V", 100, "battery_voltage_level", "Current battery voltage level", None),
- Variable(0x9009, False, False, [0x03], "V", 100, "cvt_cvr_max_dropout_voltage", "Charge target and recovery voltage max allow dropout voltage for Li-series controller", None),
- Variable(0x900A, False, False, [0x03], "V", 100, "cvt_cvr_min_dropout_voltage", "Charge target and recovery voltage min allow dropout voltage for Li-series controller", None),
- Variable(0x900B, False, False, [0x03], "V", 100, "lvd_lvr_min_dropout_voltage", "Low voltage detect and recovery min allow dropout voltage", None),
- Variable(0x900C, False, False, [0x03], "V", 100, "min_allow_dropout_voltage", "CVR and LVD & CVT and LVR Min allow dropout voltage", None),
- Variable(0x9017, False, False, [0x03, 0x06, 0x10], "ss", 0, "real_time_clock_second", "Real-time clock second", None),
- Variable(0x9018, False, False, [0x03, 0x06, 0x10], "mm", 0, "real_time_clock_minute", "Real-time clock minute", None),
- Variable(0x9019, False, False, [0x03, 0x06, 0x10], "hh", 0, "real_time_clock_hour", "Real-time clock hour", None),
- Variable(0x901A, False, False, [0x03, 0x06, 0x10], "dd", 0, "real_time_clock_day", "Real-time clock day", None),
- Variable(0x901B, False, False, [0x03, 0x06, 0x10], "MM", 0, "real_time_clock_month", "Real-time clock month", None),
- Variable(0x901C, False, False, [0x03, 0x06, 0x10], "yy", 0, "real_time_clock_year", "Real-time clock year (00-99)", None),
- Variable(0x901D, False, False, [0x03, 0x06, 0x10], "", 0, "baud_rate", "Baud rate",
- lambda x: [4800, 9600, 19200, 57600, 115200][x & 0xF]),
- Variable(0x901E, False, False, [0x03, 0x06, 0x10], "s", 0, "backlight_time", "Backlight time", None),
- Variable(0x901F, False, False, [0x03, 0x06, 0x10], "", 0, "device_password", "Device password",
- lambda x: str(max((x>>12) & 0xF, 9)) +
- str(max((x>> 8) & 0xF, 9)) +
- str(max((x>> 4) & 0xF, 9)) +
- str(max((x>> 0) & 0xF, 9))),
- Variable(0x9020, False, False, [0x03, 0x06, 0x10], "", 0, "slave_id", "Slave ID", None),
- Variable(0x9021, False, False, [0x03, 0x06, 0x10], "", 0, "battery_type", "Battery type",
- lambda x: ["Lithium", "Liquid", "GEL", "AGM"][(x >> 0) & 0xF]),
- Variable(0x9022, False, False, [0x03, 0x06, 0x10], "V", 100, "low_voltage_protection_voltage", "Low voltage protection", None),
- Variable(0x9023, False, False, [0x03, 0x06, 0x10], "V", 100, "low_voltage_recovery_voltage", "Low voltage recovery", None),
- Variable(0x9024, False, False, [0x03, 0x06, 0x10], "V", 100, "boost_voltage", "Boost voltage", None),
- Variable(0x9025, False, False, [0x03, 0x06, 0x10], "V", 100, "equalizing_voltage", "Equalizing voltage", None),
- Variable(0x9026, False, False, [0x03, 0x06, 0x10], "V", 100, "float_voltage", "Float voltage", None),
- Variable(0x9027, False, False, [0x03, 0x06, 0x10], "", 0, "system_rated_voltage_level", "System rated voltage level",
- lambda x: ["Auto", "12V", "24V", "36V", "48V", "60V", "110V", "120V", "220V", "240V"][x]),
- Variable(0x9028, False, False, [0x03, 0x06, 0x10], "V", 100, "charge_target_voltage_for_lithium", "Charge target voltage for lithium", None),
- Variable(0x9029, False, False, [0x03, 0x06, 0x10], "V", 100, "charge_recovery_voltage_for_lithium", "Charge recovery voltage for lithium", None),
- Variable(0x902A, False, False, [0x03, 0x06, 0x10], "", 0, "charging_at_zero_celsius", "0°C charging",
- lambda x: ["Normal charging", "No charging", "Slow charging"][x & 0xF]),
- Variable(0x902B, False, False, [0x03, 0x06, 0x10], "", 0, "mt_series_load_mode", "Load mode for MT series controller",
- lambda x: (["Always on", "Dusk to dawn"] +
- [f"Night light on time {n} hours" for n in range(2, 10)] +
- ["Manual", "T0T", "Timing switch"])[x]),
- Variable(0x902C, False, False, [0x03, 0x06, 0x10], "", 0, "mt_series_manual_control_default", "MT Series manual control mode default setting",
- lambda x: ["On", "Off"][x]),
- Variable(0x902D, False, False, [0x03, 0x06, 0x10], "Min", 0, "mt_series_timing_period_1", "MT Series timing opening period 1",
- lambda x: ((x >> 8) & 0xFF) * 60 + max(x & 0xFF, 59)),
- Variable(0x902E, False, False, [0x03, 0x06, 0x10], "", 0, "mt_series_timing_period_2", "MT Series timing opening period 2",
- lambda x: ((x >> 8) & 0xFF) * 60 + max(x & 0xFF, 59)),
- Variable(0x902F, False, False, [0x03, 0x06, 0x10], "sec", 0, "timed_start_time_1_seconds", "Timed start time 1-seconds", None),
- Variable(0x9030, False, False, [0x03, 0x06, 0x10], "Min", 0, "timed_start_time_1_minutes", "Timed start time 1-minute", None),
- Variable(0x9031, False, False, [0x03, 0x06, 0x10], "hour", 0, "timed_start_time_1_hours", "Timed start time 1-hour", None),
- Variable(0x9032, False, False, [0x03, 0x06, 0x10], "sec", 0, "timed_off_time_1_seconds", "Timed off time 1-seconds", None),
- Variable(0x9033, False, False, [0x03, 0x06, 0x10], "Min", 0, "timed_off_time_1_minutes", "Timed off time 1-minute", None),
- Variable(0x9034, False, False, [0x03, 0x06, 0x10], "hour", 0, "timed_off_time_1_hours", "Timed off time 1-hour", None),
- Variable(0x9035, False, False, [0x03, 0x06, 0x10], "sec", 0, "timed_start_time_2_seconds", "Timed start time 2-seconds", None),
- Variable(0x9036, False, False, [0x03, 0x06, 0x10], "Min", 0, "timed_start_time_2_minutes", "Timed start time 2-minute", None),
- Variable(0x9037, False, False, [0x03, 0x06, 0x10], "hour", 0, "timed_start_time_2_hours", "Timed start time 2-hour", None),
- Variable(0x9038, False, False, [0x03, 0x06, 0x10], "sec", 0, "timed_off_time_2_seconds", "Timed off time 2-seconds", None),
- Variable(0x9039, False, False, [0x03, 0x06, 0x10], "Min", 0, "timed_off_time_2_minutes", "Timed off time 2-minute", None),
- Variable(0x903A, False, False, [0x03, 0x06, 0x10], "hour", 0, "timed_off_time_2_hours", "Timed off time 2-hour", None),
- Variable(0x903B, False, False, [0x03, 0x06, 0x10], "", 0, "time_control_period_selection", "Time control period selection",
- lambda x: ["1 period", "2 periods"][x]),
- Variable(0x903C, False, False, [0x03, 0x06, 0x10], "V", 100, "light_controlled_dark_voltage", "Light controlled dark voltage", None),
- Variable(0x903D, False, False, [0x03, 0x06, 0x10], "Min", 0, "day_night_delay_time", "Day/Night delay time", None),
- Variable(0x903E, False, False, [0x03, 0x06, 0x10], "%", 0.1, "dc_series_timing_control_time_1_dimming", "DC series timing control time 1 dimming", None),
- Variable(0x903F, False, False, [0x03, 0x06, 0x10], "%", 0.1, "dc_series_timing_control_time_2_dimming", "DC series timing control time 2 dimming", None),
- Variable(0x9040, False, False, [0x03, 0x06, 0x10], "Min", 1.0/30, "dc_series_time_1", "DC Series time 1", None),
- Variable(0x9041, False, False, [0x03, 0x06, 0x10], "%", 0.1, "dc_series_time_1_dimming", "DC Series the time 1 dimming", None),
- Variable(0x9042, False, False, [0x03, 0x06, 0x10], "Min", 1.0/30, "dc_series_time_2", "DC Series time 2", None),
- Variable(0x9043, False, False, [0x03, 0x06, 0x10], "%", 0.1, "dc_series_time_2_dimming", "DC Series the time 2 dimming", None),
- Variable(0x9044, False, False, [0x03, 0x06, 0x10], "Sec", 1.0/30, "dc_series_time_3", "DC Series time 3", None),
- Variable(0x9045, False, False, [0x03, 0x06, 0x10], "%", 0.1, "dc_series_time_3_dimming", "DC Series the time 3 dimming", None),
- Variable(0x9046, False, False, [0x03, 0x06, 0x10], "Sec", 1.0/30, "dc_series_time_4", "DC Series time 4", None),
- Variable(0x9047, False, False, [0x03, 0x06, 0x10], "%", 0.1, "dc_series_time_4_dimming", "DC Series the time 4 dimming", None),
- Variable(0x9048, False, False, [0x03, 0x06, 0x10], "Sec", 1.0/30, "dc_series_time_5", "DC Series time 5", None),
- Variable(0x9049, False, False, [0x03, 0x06, 0x10], "%", 0.1, "dc_series_time_5_dimming", "DC Series the time 5 dimming", None),
- Variable(0x904A, False, False, [0x03, 0x06, 0x10], "A", 100, "dc_series_load_current_limit", "DC Series load current limit", None),
- Variable(0x904B, False, False, [0x03, 0x06, 0x10], "", 0, "dc_series_auto_dimming", "DC Series auto dimming",
- lambda x: ["Auto dimming", "365 mode", "No dimming", "No dimming"][x & 0xF]),
- Variable(0x904C, False, False, [0x03, 0x06, 0x10], "V", 100, "dc_series_dimming_voltage", "DC Series dimming voltage", None),
- Variable(0x904D, False, False, [0x03, 0x06, 0x10], "%", 0, "dc_series_dimming_percentage", "DC Series dimming percentage", None),
- Variable(0x904E, False, False, [0x03, 0x06, 0x10], "Sec", 0.1, "sensing_delay_off_time", "Sensing delay off time", None),
- Variable(0x904F, False, False, [0x03, 0x06, 0x10], "%", 0.1, "infrared_dimming_when_no_people", "Dimming of Infrared Series controller when no people", None),
- Variable(0x9052, False, False, [0x03, 0x06, 0x10], "", 0, "light_controlled_switch", "Light controlled switch",
- lambda x: ["Off", "On"][x]),
- Variable(0x9053, False, False, [0x03, 0x06, 0x10], "V", 100, "light_controlled_daybreak_voltage", "Light-control led daybreak voltage", None),
- Variable(0x9054, False, False, [0x03, 0x06, 0x10], "%", 0, "dimming_percentage", "Dimming percentage for load test", None),
- Variable(0x9069, False, False, [0x03, 0x06, 0x10], "A", 100, "maximum_charging_current_setting", "Maximum charging current setting", None),
- Variable(0x906A, False, False, [0x03, 0x06, 0x10], "℃", 100, "over_temperature_protection", "Over temperature protection", None),
- Variable(0x0000, False, False, [0x05], "", 0, "manual_control_switch", "Manual control switch",
- lambda x: ["Off", "On"][x]),
- Variable(0x0001, False, False, [0x05], "", 0, "test_key_trigger", "Test key on/off",
- lambda x: ["Off", "On"][x]),
- Variable(0x0002, False, False, [0x05], "", 0, "dc_series_timing_control_mode_switch", "DC Series timing control mode switch",
- lambda x: ["Off", "On"][x]),
- Variable(0x0003, False, False, [0x05], "", 0, "manual_control_charging_switch", "Manual control charging switch",
- lambda x: ["Off", "On"][x]),
- Variable(0x0004, False, False, [0x05], "", 0, "manual_control_switch", "Manual control switch",
- lambda x: ["Off", "On"][x]),
- Variable(0x0005, False, False, [0x05], "", 0, "restore_system_default_values", "Restore system default values",
- lambda x: ["No", "Yes"][x]),
- Variable(0x0006, False, False, [0x05], "", 0, "clear_device_statistics", "Clear running days, Power generation or consumption WH and historical minimum/maximum voltage",
- lambda x: ["", "Clear"][x]),
- Variable(0x0007, False, False, [0x05], "", 0, "clear_counters", "Clear all protection and fully charged times",
- lambda x: ["", "Clear"][x]),
- Variable(0x0008, False, False, [0x05], "", 0, "Clear_charge_discharge_ah", "Clear charge/discharge AH",
- lambda x: ["", "Clear"][x]),
- Variable(0x0009, False, False, [0x05], "", 0, "clear_all", "Clear all of the above historical data",
- lambda x: ["", "Clear"][x]),
- ]
- def bytes_to_value(self, variable: Variable, buffer: bytes, offset: int) -> Value:
- if variable.is_32_bit and variable.is_signed:
- raw_value = struct.unpack_from(">H", buffer, offset)[0] | struct.unpack_from(">h", buffer, offset + 2)[0] << 16
- elif variable.is_32_bit:
- raw_value = struct.unpack_from(">H", buffer, offset)[0] | struct.unpack_from(">H", buffer, offset + 2)[0] << 16
- elif variable.is_signed:
- raw_value = struct.unpack_from(">h", buffer, offset)[0]
- else:
- raw_value = struct.unpack_from(">H", buffer, offset)[0]
- if variable.multiplier:
- value = raw_value / variable.multiplier
- elif variable.func:
- try:
- value = variable.func(raw_value)
- except IndexError as e:
- raise Exception(f"unexpected value for {variable.name} ({hex(variable.address)}): '{raw_value}'")
- else:
- value = raw_value
- return value
- def value_to_bytes(self, variable: Variable, buffer: bytearray, offset: int, value: Value) -> int:
- if variable.multiplier:
- raw_value = round(float(value) * variable.multiplier)
- elif variable.func:
- raw_value = self._find_raw_value_by_brute_force(variable, value)
- if raw_value == None:
- raise Exception(f"invalid value for {variable.name}: '{value}'")
- else:
- raw_value = int(value)
- if variable.is_32_bit and variable.is_signed:
- struct.pack_into(">H", buffer, offset, raw_value & 0xFFFF)
- struct.pack_into(">h", buffer, offset + 2, raw_value >> 16)
- elif variable.is_32_bit:
- struct.pack_into(">H", buffer, offset, raw_value & 0xFFFF)
- struct.pack_into(">H", buffer, offset + 2, raw_value >> 16)
- elif variable.is_signed:
- struct.pack_into(">h", buffer, offset, raw_value)
- else:
- struct.pack_into(">H", buffer, offset, raw_value)
- length = 4 if variable.is_32_bit else 2
- return offset + length
- def get_read_command(self, device_id: int, start_address: int, count: int) -> bytes:
- variables = [v for v in self.variables if v.address >= start_address and v.address < start_address + count]
- if not variables:
- raise Exception(f"the range {hex(start_address)}-{hex(start_address+count-1)} contains no variables")
-
- function_code = variables[0].function_codes[0]
- if not all(function_code in v.function_codes for v in variables):
- raise Exception(f"the range {hex(start_address)}-{hex(start_address+count-1)} spans multiple function codes")
- result = bytes([
- device_id,
- function_code,
- start_address >> 8,
- start_address & 0xFF,
- count >> 8,
- count & 0xFF
- ])
- return result + crc16(result)
- def get_write_command(self, device_id: int, values: list[(Variable, Any)]) -> bytes:
- if not values:
- raise Exception(f"values list is empty")
- values.sort(key=lambda x: x[0].address)
- address = values[0][0].address
- for variable, value in values:
- if value is None:
- raise Exception(f"value of {variable.name} ({hex(variable.address)}) is empty")
- if address < variable.address:
- raise Exception(f"variables are not continuous at {hex(variable.address)}")
- address = variable.address + (2 if variable.is_32_bit else 1)
- start_variable = values[0][0]
- end_variable = values[-1][0]
- start_address = start_variable.address
- end_address = end_variable.address + (1 if end_variable.is_32_bit else 0)
- count = end_address - start_address + 1
- byte_count = count * 2
- if byte_count > 255:
- raise Exception(f"address range is too large")
- if count > 1:
- function_code = FunctionCodes.WRITE_MEMORY_RANGE
- header = bytes([
- device_id,
- function_code.value,
- start_address >> 8,
- start_address & 0xFF,
- count >> 8,
- count & 0xFF,
- byte_count,
- ])
- else:
- if FunctionCodes.WRITE_STATUS_REGISTER.value in values[0][0].function_codes:
- function_code = FunctionCodes.WRITE_STATUS_REGISTER
- else:
- function_code = FunctionCodes.WRITE_MEMORY_SINGLE
- header = bytes([
- device_id,
- function_code.value,
- start_address >> 8,
- start_address & 0xFF,
- ])
- if not all(function_code.value in x[0].function_codes for x in values):
- raise Exception(f"function code {function_code.name} is not supported for all addresses")
- data = bytearray(byte_count)
- for variable, value in values:
- offset = (variable.address - start_address) * 2
- self.value_to_bytes(variable, data, offset, value)
- result = header + bytes(data)
- return result + crc16(result)
- def parse(self, start_address: int, buffer: bytes) -> list[(Variable, Value)]:
- device_id = buffer[0]
- function_code = FunctionCodes(buffer[1])
- if function_code in [FunctionCodes.READ_MEMORY, FunctionCodes.READ_PARAMETER, FunctionCodes.READ_STATUS_REGISTER]:
- data_length = buffer[2]
- received_crc = buffer[3+data_length:3+data_length+2]
- calculated_crc = crc16(buffer[:3+data_length])
- if received_crc != calculated_crc:
- raise Exception(f"CRC mismatch ({calculated_crc} != {received_crc})")
- results = []
- address = start_address
- cursor = 3
- while cursor < data_length + 3:
- variables = [v for v in self.variables if address == v.address and function_code.value in v.function_codes]
- for variable in variables:
- value = self.bytes_to_value(variable, buffer, cursor)
- results.append((variable, value))
- cursor += 2
- address += 1
- return results
- else:
- address = struct.unpack_from('>H', buffer, 2)[0]
- if address != start_address:
- raise Exception(f"Write result address mismatch ({hex(address)} != {hex(start_address)})")
- received_crc = buffer[6:8]
- calculated_crc = crc16(buffer[:6])
- if received_crc != calculated_crc:
- raise Exception(f"CRC mismatch ({calculated_crc} != {received_crc})")
- return []
- def _find_raw_value_by_brute_force(self, variable: Variable, value):
- n_bits = 32 if variable.is_32_bit else 16
- if variable.is_signed:
- for i in range(0, 2**(n_bits-1) + 1):
- try:
- if variable.func(i) == value:
- return i
- except IndexError:
- pass
- for i in range(0, -2**(n_bits-1) - 2, -1):
- try:
- if variable.func(i) == value:
- return i
- except IndexError:
- pass
- else:
- for i in range(0, 2**n_bits + 1):
- try:
- if variable.func(i) == value:
- return i
- except IndexError:
- pass
- return None
|