Fixing log format, adding commit support

This commit is contained in:
Ryan Summers 2021-03-01 13:47:44 +01:00
parent c943e8d136
commit 9a5de507d1
1 changed files with 15 additions and 9 deletions

View File

@ -55,20 +55,20 @@ class MiniconfApi:
self.inflight_settings[topic].set_result(payload.decode('ascii')) self.inflight_settings[topic].set_result(payload.decode('ascii'))
async def set_setting(self, setting, value): async def command(self, path, value):
""" Change the provided setting with the provided data. """ Write the provided data to the specified path.
Args: Args:
setting: The setting to update setting: The path to write the message to.
value: The value to configure the setting to (in JSON-serialized format). value: The value to write to the path.
Returns: Returns:
The received response to the command. The received response to the command.
""" """
setting_topic = f'{self.identifier}/settings/{setting}' setting_topic = f'{self.identifier}/{path}'
response_topic = f'{self.identifier}/feedback/{setting}' response_topic = f'{self.identifier}/feedback/{path}'
assert response_topic not in self.inflight_settings, \ assert response_topic not in self.inflight_settings, \
'Only one in-flight setting is supported' 'Only one in-flight message per topic is supported'
self.logger.debug('Sending %s to "%s"', value, setting_topic) self.logger.debug('Sending %s to "%s"', value, setting_topic)
self.inflight_settings[response_topic] = asyncio.get_running_loop().create_future() self.inflight_settings[response_topic] = asyncio.get_running_loop().create_future()
@ -105,9 +105,13 @@ async def configure_settings(args):
request[str(key)] = json.loads(value) request[str(key)] = json.loads(value)
logger.debug('Parsed request: %s', request) logger.debug('Parsed request: %s', request)
response = await interface.set_setting(args.setting, json.dumps(request)) response = await interface.command(f'settings/{args.setting}', json.dumps(request))
logger.info(response) logger.info(response)
if args.commit:
response = await interface.command('commit', 'commit')
logger.info(response)
def main(): def main():
""" Main program entry point. """ """ Main program entry point. """
@ -118,6 +122,8 @@ def main():
parser.add_argument('--broker', default='10.34.16.1', type=str, help='The MQTT broker address') parser.add_argument('--broker', default='10.34.16.1', type=str, help='The MQTT broker address')
parser.add_argument('values', nargs='+', type=str, parser.add_argument('values', nargs='+', type=str,
help='The value of settings. key=value list or a single value is accepted.') help='The value of settings. key=value list or a single value is accepted.')
parser.add_argument('--commit', action='store_true',
help='Specified true to commit after updating settings.')
parser.add_argument('-v', '--verbose', action='store_true', help='Enable verbose logging') parser.add_argument('-v', '--verbose', action='store_true', help='Enable verbose logging')
args = parser.parse_args() args = parser.parse_args()
@ -128,7 +134,7 @@ def main():
if args.verbose: if args.verbose:
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
logging.basicConfig() logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s')
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(configure_settings(args)) loop.run_until_complete(configure_settings(args))