|
4 | 4 | import click
|
5 | 5 | from kc3zvd.iot_state.__about__ import __version__
|
6 | 6 | from kc3zvd.iot_state.publishers import mqtt
|
| 7 | +from .core.mqtt import mqtt |
| 8 | +import logging |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +logger = logging.getLogger('kc3zvd.iot_state') |
| 13 | +handler = logging.StreamHandler(sys.stdout) |
| 14 | +formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
| 15 | +handler.setFormatter(formatter) |
| 16 | +logger.addHandler(handler) |
7 | 17 |
|
8 | 18 | @click.group(context_settings={"help_option_names": ["-h", "--help"], "auto_envvar_prefix": "IOT"}, invoke_without_command=False)
|
9 | 19 | @click.version_option(version=__version__, prog_name="iot-state")
|
| 20 | +@click.option('--log-level', help="Log level", default="INFO", type=str) |
10 | 21 | @click.option('--redis-username', help="Redis instance username", default='', type=str)
|
11 | 22 | @click.option('--redis-password', help="Redis instance password", default='', type=str)
|
12 | 23 | @click.option('--redis-host', help="Redis instance host", default='localhost', type=str)
|
13 | 24 | @click.option('--redis-port', help="Redis instance port", default=6379, type=int)
|
14 | 25 | @click.option('--redis-db', help="Redis instance DB number", default=0, type=int)
|
15 | 26 | @click.pass_context
|
16 |
| -def iot_state(ctx, redis_username, redis_password, redis_host, redis_port, redis_db): |
| 27 | +def iot_state(ctx, log_level, redis_username, redis_password, redis_host, redis_port, redis_db): |
| 28 | + logger.setLevel(level=log_level) |
| 29 | + handler.setLevel(level=log_level) |
| 30 | + |
17 | 31 | ctx.ensure_object(dict)
|
18 | 32 |
|
19 | 33 | if redis_username or redis_password:
|
20 | 34 | if not redis_username or not redis_password:
|
21 |
| - click.echo("Provide both username and password for redis") |
| 35 | + logger.error("Provide both username and password for redis") |
22 | 36 | exit()
|
23 | 37 | ctx.obj['redis_url'] = f"redis://{redis_username}:{redis_password}@{redis_host}:{redis_port}/{redis_db}"
|
24 | 38 | else:
|
25 | 39 | ctx.obj['redis_url'] = f"redis://{redis_host}:{redis_port}/{redis_db}"
|
26 |
| - click.echo("Starting IOT state platform") |
27 | 40 |
|
28 |
| -@iot_state.command() |
29 |
| -@click.option('--platform', help="The platform to publish to", required=True, |
30 |
| - type=click.Choice(['mqtt'], case_sensitive=False)) |
31 |
| -@click.option('--mqtt-host', help="The MQTT host to connect to", default='localhost', type=str) |
32 |
| -@click.option('--mqtt-port', help="The port to use to connect to the MQTT host", default=1883, type=int) |
33 |
| -@click.option('--mqtt-prefix', help="The prefix to use for the MQTT topic", default='', type=str) |
34 |
| -@click.pass_context |
35 |
| -def publisher(ctx, platform, mqtt_host, mqtt_port, mqtt_prefix): |
36 |
| - match platform: |
37 |
| - case 'mqtt': |
38 |
| - click.echo("mqtt platform selected") |
39 |
| - mqtt.run( |
40 |
| - redis_url=ctx.obj['redis_url'], |
41 |
| - ) |
42 |
| - case _: |
43 |
| - exit() |
| 41 | +iot_state.add_command(mqtt) |
0 commit comments