Skip to content

Commit

Permalink
Add set alias over mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
blavka committed Jan 31, 2018
1 parent 2976b8f commit 692b332
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 35 additions & 19 deletions bcg/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'

Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 692b332

Please sign in to comment.