2023-03-05 10:46:51 -07:00
|
|
|
import importlib
|
2023-03-14 16:17:36 -06:00
|
|
|
from contextlib import suppress
|
2023-03-05 10:46:51 -07:00
|
|
|
|
2023-04-11 14:14:36 -06:00
|
|
|
from pyhon import helper
|
2023-02-12 19:36:09 -07:00
|
|
|
from pyhon.commands import HonCommand
|
2023-03-14 07:29:25 -06:00
|
|
|
from pyhon.parameter import HonParameterFixed
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
|
2023-04-09 10:13:50 -06:00
|
|
|
class HonAppliance:
|
2023-04-09 12:50:28 -06:00
|
|
|
def __init__(self, api, info):
|
|
|
|
if attributes := info.get("attributes"):
|
|
|
|
info["attributes"] = {v["parName"]: v["parValue"] for v in attributes}
|
|
|
|
self._info = info
|
|
|
|
self._api = api
|
2023-02-12 17:41:38 -07:00
|
|
|
self._appliance_model = {}
|
|
|
|
|
|
|
|
self._commands = {}
|
|
|
|
self._statistics = {}
|
2023-03-04 13:27:10 -07:00
|
|
|
self._attributes = {}
|
2023-02-12 17:41:38 -07:00
|
|
|
|
2023-03-07 16:58:25 -07:00
|
|
|
try:
|
2023-04-09 12:55:36 -06:00
|
|
|
self._extra = importlib.import_module(
|
|
|
|
f"pyhon.appliances.{self.appliance_type.lower()}"
|
|
|
|
).Appliance()
|
2023-03-07 16:58:25 -07:00
|
|
|
except ModuleNotFoundError:
|
|
|
|
self._extra = None
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
if "." in item:
|
|
|
|
result = self.data
|
|
|
|
for key in item.split("."):
|
2023-03-08 14:18:44 -07:00
|
|
|
if all([k in "0123456789" for k in key]) and type(result) is list:
|
2023-03-07 16:58:25 -07:00
|
|
|
result = result[int(key)]
|
|
|
|
else:
|
|
|
|
result = result[key]
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
if item in self.data:
|
|
|
|
return self.data[item]
|
2023-03-08 14:18:44 -07:00
|
|
|
if item in self.attributes["parameters"]:
|
|
|
|
return self.attributes["parameters"].get(item)
|
2023-04-09 12:50:28 -06:00
|
|
|
return self.info[item]
|
2023-02-12 17:41:38 -07:00
|
|
|
|
2023-03-08 13:53:53 -07:00
|
|
|
def get(self, item, default=None):
|
|
|
|
try:
|
|
|
|
return self[item]
|
2023-03-08 14:18:44 -07:00
|
|
|
except (KeyError, IndexError):
|
2023-03-08 13:53:53 -07:00
|
|
|
return default
|
|
|
|
|
2023-02-12 17:41:38 -07:00
|
|
|
@property
|
|
|
|
def appliance_model_id(self):
|
2023-04-09 12:50:28 -06:00
|
|
|
return self._info.get("applianceModelId")
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
@property
|
2023-03-07 16:58:25 -07:00
|
|
|
def appliance_type(self):
|
2023-04-09 12:50:28 -06:00
|
|
|
return self._info.get("applianceTypeName")
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def mac_address(self):
|
2023-04-09 12:50:28 -06:00
|
|
|
return self._info.get("macAddress")
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def model_name(self):
|
2023-04-09 12:50:28 -06:00
|
|
|
return self._info.get("modelName")
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def nick_name(self):
|
2023-04-09 12:50:28 -06:00
|
|
|
return self._info.get("nickName")
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def commands_options(self):
|
|
|
|
return self._appliance_model.get("options")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def commands(self):
|
|
|
|
return self._commands
|
|
|
|
|
|
|
|
@property
|
|
|
|
def attributes(self):
|
2023-03-04 13:27:10 -07:00
|
|
|
return self._attributes
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def statistics(self):
|
|
|
|
return self._statistics
|
|
|
|
|
2023-03-05 10:46:51 -07:00
|
|
|
@property
|
2023-04-09 12:50:28 -06:00
|
|
|
def info(self):
|
|
|
|
return self._info
|
2023-03-05 10:46:51 -07:00
|
|
|
|
2023-03-10 18:31:56 -07:00
|
|
|
async def _recover_last_command_states(self, commands):
|
2023-04-09 12:50:28 -06:00
|
|
|
command_history = await self._api.command_history(self)
|
2023-03-10 18:31:56 -07:00
|
|
|
for name, command in commands.items():
|
2023-04-09 12:55:36 -06:00
|
|
|
last = next(
|
|
|
|
(
|
|
|
|
index
|
|
|
|
for (index, d) in enumerate(command_history)
|
|
|
|
if d.get("command", {}).get("commandName") == name
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
2023-03-10 18:31:56 -07:00
|
|
|
if last is None:
|
|
|
|
continue
|
|
|
|
parameters = command_history[last].get("command", {}).get("parameters", {})
|
|
|
|
if command._multi and parameters.get("program"):
|
|
|
|
command.set_program(parameters.pop("program").split(".")[-1].lower())
|
|
|
|
command = self.commands[name]
|
|
|
|
for key, data in command.settings.items():
|
2023-04-09 12:55:36 -06:00
|
|
|
if (
|
|
|
|
not isinstance(data, HonParameterFixed)
|
|
|
|
and parameters.get(key) is not None
|
|
|
|
):
|
2023-03-14 16:17:36 -06:00
|
|
|
with suppress(ValueError):
|
|
|
|
data.value = parameters.get(key)
|
2023-03-10 18:31:56 -07:00
|
|
|
|
2023-02-12 17:41:38 -07:00
|
|
|
async def load_commands(self):
|
2023-04-09 12:50:28 -06:00
|
|
|
raw = await self._api.load_commands(self)
|
2023-02-12 17:41:38 -07:00
|
|
|
self._appliance_model = raw.pop("applianceModel")
|
|
|
|
for item in ["settings", "options", "dictionaryId"]:
|
|
|
|
raw.pop(item)
|
|
|
|
commands = {}
|
|
|
|
for command, attr in raw.items():
|
|
|
|
if "parameters" in attr:
|
2023-04-09 12:50:28 -06:00
|
|
|
commands[command] = HonCommand(command, attr, self._api, self)
|
2023-02-12 17:41:38 -07:00
|
|
|
elif "parameters" in attr[list(attr)[0]]:
|
|
|
|
multi = {}
|
2023-03-10 17:10:27 -07:00
|
|
|
for program, attr2 in attr.items():
|
|
|
|
program = program.split(".")[-1].lower()
|
2023-04-09 12:55:36 -06:00
|
|
|
cmd = HonCommand(
|
|
|
|
command, attr2, self._api, self, multi=multi, program=program
|
|
|
|
)
|
2023-03-10 17:10:27 -07:00
|
|
|
multi[program] = cmd
|
2023-02-12 17:41:38 -07:00
|
|
|
commands[command] = cmd
|
|
|
|
self._commands = commands
|
2023-03-10 18:31:56 -07:00
|
|
|
await self._recover_last_command_states(commands)
|
2023-02-12 17:41:38 -07:00
|
|
|
|
2023-02-18 14:25:51 -07:00
|
|
|
@property
|
|
|
|
def settings(self):
|
|
|
|
result = {}
|
2023-02-19 11:43:41 -07:00
|
|
|
for name, command in self._commands.items():
|
|
|
|
for key, setting in command.settings.items():
|
|
|
|
result[f"{name}.{key}"] = setting
|
2023-04-07 20:06:36 -06:00
|
|
|
if self._extra:
|
|
|
|
return self._extra.settings(result)
|
2023-02-19 11:43:41 -07:00
|
|
|
return result
|
|
|
|
|
|
|
|
@property
|
|
|
|
def parameters(self):
|
|
|
|
result = {}
|
|
|
|
for name, command in self._commands.items():
|
|
|
|
for key, parameter in command.parameters.items():
|
2023-03-07 16:58:25 -07:00
|
|
|
result.setdefault(name, {})[key] = parameter.value
|
2023-02-18 14:25:51 -07:00
|
|
|
return result
|
|
|
|
|
2023-02-12 17:41:38 -07:00
|
|
|
async def load_attributes(self):
|
2023-04-09 12:50:28 -06:00
|
|
|
self._attributes = await self._api.load_attributes(self)
|
2023-03-07 16:58:25 -07:00
|
|
|
for name, values in self._attributes.pop("shadow").get("parameters").items():
|
|
|
|
self._attributes.setdefault("parameters", {})[name] = values["parNewVal"]
|
2023-02-12 17:41:38 -07:00
|
|
|
|
|
|
|
async def load_statistics(self):
|
2023-04-09 12:50:28 -06:00
|
|
|
self._statistics = await self._api.load_statistics(self)
|
2023-02-12 19:36:09 -07:00
|
|
|
|
|
|
|
async def update(self):
|
|
|
|
await self.load_attributes()
|
2023-03-03 10:24:19 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def data(self):
|
2023-04-09 12:55:36 -06:00
|
|
|
result = {
|
|
|
|
"attributes": self.attributes,
|
|
|
|
"appliance": self.info,
|
|
|
|
"statistics": self.statistics,
|
|
|
|
**self.parameters,
|
|
|
|
}
|
2023-03-07 16:58:25 -07:00
|
|
|
if self._extra:
|
2023-04-07 20:06:36 -06:00
|
|
|
return self._extra.data(result)
|
2023-03-07 16:58:25 -07:00
|
|
|
return result
|
2023-04-11 14:14:36 -06:00
|
|
|
|
|
|
|
@property
|
|
|
|
def diagnose(self):
|
|
|
|
data = self.data.copy()
|
2023-04-14 15:24:31 -06:00
|
|
|
for sensible in ["PK", "SK", "serialNumber", "code", "coords"]:
|
2023-04-11 14:14:36 -06:00
|
|
|
data["appliance"].pop(sensible, None)
|
|
|
|
result = helper.pretty_print({"data": self.data}, whitespace="\u200B \u200B ")
|
|
|
|
result += helper.pretty_print(
|
|
|
|
{"commands": helper.create_command(self.commands)},
|
|
|
|
whitespace="\u200B \u200B ",
|
|
|
|
)
|
|
|
|
return result.replace(self.mac_address, "12-34-56-78-90-ab")
|