2021-02-19 19:07:09 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
"""
|
|
|
|
Author: Vertigo Designs, Ryan Summers
|
|
|
|
|
2021-02-19 19:09:35 +08:00
|
|
|
Description: Provides an API for controlling Stabilizer over Miniconf (MQTT).
|
2021-02-19 19:07:09 +08:00
|
|
|
"""
|
|
|
|
import argparse
|
|
|
|
import asyncio
|
|
|
|
import json
|
2021-02-19 20:04:10 +08:00
|
|
|
import logging
|
2021-02-19 19:07:09 +08:00
|
|
|
|
|
|
|
from gmqtt import Client as MqttClient
|
|
|
|
|
|
|
|
|
|
|
|
class MiniconfApi:
|
|
|
|
""" An asynchronous API for controlling Miniconf devices using the MQTT control interface. """
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create(cls, identifier, broker):
|
2021-02-19 19:09:35 +08:00
|
|
|
""" Create a connection to MQTT for communication with the device. """
|
2021-02-19 19:07:09 +08:00
|
|
|
client = MqttClient(client_id='')
|
|
|
|
await client.connect(broker)
|
|
|
|
return cls(client, identifier)
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, client, identifier):
|
|
|
|
""" Consructor.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
client: A connected MQTT5 client.
|
|
|
|
identifier: The ID of the device to control.
|
|
|
|
"""
|
|
|
|
self.client = client
|
|
|
|
self.identifier = identifier
|
|
|
|
self.client.on_message = self._handle_response
|
2021-03-01 20:32:53 +08:00
|
|
|
self.inflight_settings = dict()
|
2021-02-19 20:04:10 +08:00
|
|
|
self.logger = logging.getLogger('stabilizer.miniconf')
|
2021-02-19 19:07:09 +08:00
|
|
|
|
2021-03-01 20:32:53 +08:00
|
|
|
self.client.subscribe(f'{identifier}/feedback/#')
|
2021-02-19 19:07:09 +08:00
|
|
|
|
|
|
|
|
2021-03-01 20:32:53 +08:00
|
|
|
def _handle_response(self, _client, topic, payload, *_args, **_kwargs):
|
2021-02-19 19:07:09 +08:00
|
|
|
""" Callback function for when messages are received over MQTT.
|
|
|
|
|
|
|
|
Args:
|
2021-03-01 20:32:53 +08:00
|
|
|
_client: The MQTT client.
|
2021-02-19 19:07:09 +08:00
|
|
|
topic: The topic that the message was received on.
|
|
|
|
payload: The payload of the message.
|
|
|
|
"""
|
2021-03-01 20:32:53 +08:00
|
|
|
if topic not in self.inflight_settings:
|
|
|
|
self.logger.warning('Unknown response topic: %s', topic)
|
|
|
|
return
|
2021-02-19 19:07:09 +08:00
|
|
|
|
2021-03-01 20:32:53 +08:00
|
|
|
# Indicate a response was received for the provided topic.
|
|
|
|
self.inflight_settings[topic].set_result(payload.decode('ascii'))
|
2021-02-19 19:07:09 +08:00
|
|
|
|
|
|
|
|
2021-03-01 20:47:44 +08:00
|
|
|
async def command(self, path, value):
|
|
|
|
""" Write the provided data to the specified path.
|
2021-02-19 19:07:09 +08:00
|
|
|
|
|
|
|
Args:
|
2021-03-01 20:47:44 +08:00
|
|
|
setting: The path to write the message to.
|
|
|
|
value: The value to write to the path.
|
2021-02-19 19:07:09 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
The received response to the command.
|
|
|
|
"""
|
2021-03-01 20:47:44 +08:00
|
|
|
setting_topic = f'{self.identifier}/{path}'
|
|
|
|
response_topic = f'{self.identifier}/feedback/{path}'
|
2021-03-01 20:32:53 +08:00
|
|
|
assert response_topic not in self.inflight_settings, \
|
2021-03-01 20:47:44 +08:00
|
|
|
'Only one in-flight message per topic is supported'
|
2021-02-19 19:07:09 +08:00
|
|
|
|
2021-03-01 20:32:53 +08:00
|
|
|
self.logger.debug('Sending %s to "%s"', value, setting_topic)
|
|
|
|
self.inflight_settings[response_topic] = asyncio.get_running_loop().create_future()
|
|
|
|
self.client.publish(setting_topic, payload=value, qos=0, retain=False,
|
|
|
|
response_topic=response_topic)
|
2021-02-19 19:07:09 +08:00
|
|
|
|
2021-03-01 20:32:53 +08:00
|
|
|
response = await self.inflight_settings[response_topic]
|
|
|
|
del self.inflight_settings[response_topic]
|
2021-02-19 19:07:09 +08:00
|
|
|
|
2021-03-01 20:32:53 +08:00
|
|
|
return response
|
2021-02-19 19:07:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
async def configure_settings(args):
|
|
|
|
""" Configure an RF channel. """
|
2021-02-19 20:04:10 +08:00
|
|
|
logger = logging.getLogger('stabilizer')
|
2021-02-19 19:07:09 +08:00
|
|
|
|
2021-02-19 19:09:35 +08:00
|
|
|
# Establish a communication interface with stabilizer.
|
2021-02-19 19:07:09 +08:00
|
|
|
interface = await MiniconfApi.create(args.stabilizer, args.broker)
|
|
|
|
|
|
|
|
request = None
|
|
|
|
|
|
|
|
# In the exceptional case that this is a terminal value, there is no key available and only a
|
|
|
|
# single value.
|
|
|
|
if len(args.values) == 1 and '=' not in args.values[0]:
|
2021-03-01 20:32:53 +08:00
|
|
|
if args.values[0][0].isalpha():
|
|
|
|
request = args.values[0]
|
|
|
|
else:
|
|
|
|
request = json.loads(args.values[0])
|
2021-02-19 19:07:09 +08:00
|
|
|
else:
|
|
|
|
# Convert all of the values into a key-value list.
|
|
|
|
request = dict()
|
|
|
|
for pair in args.values:
|
|
|
|
key, value = pair.split('=')
|
2021-02-19 20:04:10 +08:00
|
|
|
request[str(key)] = json.loads(value)
|
|
|
|
logger.debug('Parsed request: %s', request)
|
2021-02-19 19:07:09 +08:00
|
|
|
|
2021-03-01 20:47:44 +08:00
|
|
|
response = await interface.command(f'settings/{args.setting}', json.dumps(request))
|
2021-02-19 20:04:10 +08:00
|
|
|
logger.info(response)
|
2021-02-19 19:07:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
""" Main program entry point. """
|
2021-02-19 19:09:35 +08:00
|
|
|
parser = argparse.ArgumentParser(description='Stabilizer settings modification utility')
|
2021-02-19 19:07:09 +08:00
|
|
|
parser.add_argument('--stabilizer', type=str, default='stabilizer',
|
|
|
|
help='The identifier of the stabilizer to configure')
|
|
|
|
parser.add_argument('--setting', required=True, type=str, help='The setting path to configure')
|
|
|
|
parser.add_argument('--broker', default='10.34.16.1', type=str, help='The MQTT broker address')
|
|
|
|
parser.add_argument('values', nargs='+', type=str,
|
|
|
|
help='The value of settings. key=value list or a single value is accepted.')
|
2021-02-19 20:04:10 +08:00
|
|
|
parser.add_argument('-v', '--verbose', action='store_true', help='Enable verbose logging')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
logger = logging.getLogger('stabilizer')
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
if args.verbose:
|
|
|
|
logger.setLevel(logging.DEBUG)
|
2021-03-01 20:32:53 +08:00
|
|
|
|
2021-03-01 20:47:44 +08:00
|
|
|
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s')
|
2021-02-19 19:07:09 +08:00
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
2021-02-19 20:04:10 +08:00
|
|
|
loop.run_until_complete(configure_settings(args))
|
2021-02-19 19:07:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|