diff --git a/README.md b/README.md index 77681f6..6bd0c71 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,19 @@ It works with Python 2.7+ and Python 3.5+ environments and it has been tested un gateway/{name}/detach "{id-node}" ``` +* Set node alias + ``` + mosquitto_pub -t 'gateway/usb-dongle/alias/set' -m '{"id": "id-node", "alias": "new-name"}' + ``` + +* Remove node alias + ``` + mosquitto_pub -t 'gateway/usb-dongle/alias/remove' -m '"{id-node}"' + ``` + ``` + mosquitto_pub -t 'gateway/usb-dongle/alias/set' -m '{"id": "id-node", "alias": null}' + ``` + * Scan Start * Start diff --git a/bcg/gateway.py b/bcg/gateway.py index 208a09d..26da134 100755 --- a/bcg/gateway.py +++ b/bcg/gateway.py @@ -51,7 +51,7 @@ def __init__(self, config): self._name = None self._info = None self._info_id = None - self._sub = set(['gateway/ping', 'gateway/all/info/get']) + self._sub = set([config['base_topic_prefix'] + 'gateway/ping', config['base_topic_prefix'] + 'gateway/all/info/get']) self._nodes = set([]) self._ser_error_cnt = 0 @@ -189,11 +189,6 @@ def mqtt_on_message(self, client, userdata, message): logging.debug('mqtt_on_message %s %s', message.topic, message.payload) - if topic.startswith("gateway"): - subtopic = topic[8 + len(self._name):] - else: - subtopic = topic[5:] - if payload == '': payload = 'null' @@ -203,6 +198,19 @@ def mqtt_on_message(self, client, userdata, message): logging.error('parse json ' + str(message.topic) + ' ' + str(message.payload) + ' ' + str(e)) return + if topic.startswith("gateway"): + subtopic = topic[8 + len(self._name):] + if subtopic == '/alias/set': + if "id" in payload and "alias" in payload: + self.node_rename(payload["id"], payload["alias"]) + return + elif subtopic == '/alias/remove': + if payload: + self.node_rename(payload, None) + return + else: + subtopic = topic[5:] + self.write(subtopic, payload) def write(self, topic, payload): @@ -275,15 +283,11 @@ def gateway_message(self, topic, payload): self._info = payload self._rename() - # TODO: remove in the future - if self._info["firmware"].startswith("bcf-usb-gateway"): + if self._info["firmware"].startswith("bcf-gateway-core-module") or self._info["firmware"].startswith("bcf-usb-gateway"): + self._node_rename_id[self._info_id] = self._name + self._node_rename_name[self._name] = self._info_id self.node_add(self._info_id) - if self._info["firmware"].startswith("bcf-gateway-core-module"): - self.node_add(self._info_id) - - self.node_rename(self._info_id, self._name) - self.write("$eeprom/alias/list", 0) elif "/nodes" == topic: @@ -397,14 +401,26 @@ def node_rename(self, address, name): if old_name: self.sub_remove(['node', old_name, '+/+/+/+']) - self._node_rename_id[address] = name - self._node_rename_name[name] = address + if name: + self._node_rename_id[address] = name + self._node_rename_name[name] = address - if address in self._nodes: - self.sub_add(['node', name, '+/+/+/+']) + if address in self._nodes: + self.sub_add(['node', name, '+/+/+/+']) - if address not in self._alias_list or self._alias_list[address] != name: - self.write('$eeprom/alias/add', {'id': address, 'name': name}) + if address not in self._alias_list or self._alias_list[address] != name: + self.write('$eeprom/alias/add', {'id': address, 'name': name}) + + else: + + if old_name: + del self._node_rename_id[address] + del self._node_rename_name[old_name] + + self.sub_add(['node', address, '+/+/+/+']) + + if address in self._alias_list: + self.write('$eeprom/alias/remove', address) # if 'config_file' in self._config: # with open(self._config['config_file'], 'r') as f: