Skip to content

Commit

Permalink
Added log level as input argument. (#12)
Browse files Browse the repository at this point in the history
Fixed input argument --insecure. It was not
possible to set it False explicitly.

Co-authored-by: Tapani Hyvämäki <[email protected]>
  • Loading branch information
tahyx and Tapani Hyvämäki authored Sep 9, 2024
1 parent 96722db commit c7f0f9e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ To run it with docker type:
-e WM_SERVICES_MQTT_PORT=8883 \
-e WM_SERVICES_MQTT_USERNAME=username \
-e WM_SERVICES_MQTT_PASSWORD=password \
-e WM_PROV_LOG_LEVEL=INFO \
wirepas/provisioning-server
```

Expand Down
46 changes: 38 additions & 8 deletions wirepas_provisioning_server/provisioning_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
from wirepas_mesh_messaging.received_data import ReceivedDataEvent

from wirepas_provisioning_server.session import ProvisioningSession, ProvisioningStatus
from wirepas_provisioning_server.message import ProvisioningMessageFactory, ProvisioningMessageException
from wirepas_provisioning_server.message import (
ProvisioningMessageFactory,
ProvisioningMessageException,
)
from wirepas_provisioning_server.data import ProvisioningData
from wirepas_provisioning_server.events import ProvisioningEventPacketReceived

Expand All @@ -42,7 +45,11 @@ def __init__(
interface.register_data_cb(self.on_data_received, src_ep=246, dst_ep=255)

def on_session_finish(self, key: tuple[int, bytes, int], status: ProvisioningStatus) -> None:
logging.info("Provisioning Session %s terminated with result: %s.", self.sessions[key], status)
logging.info(
"Provisioning Session %s terminated with result: %s.",
self.sessions[key],
status,
)
del self.sessions[key]

def on_data_received(self, data: ReceivedDataEvent) -> None:
Expand All @@ -60,7 +67,10 @@ def on_data_received(self, data: ReceivedDataEvent) -> None:
except KeyError:
logging.info("Create new SM with id: %s.", msg_data)
self.sessions[msg_data.msg_id] = ProvisioningSession(
self.interface.send_message, msg_data.msg_id, self.data, self.on_session_finish
self.interface.send_message,
msg_data.msg_id,
self.data,
self.on_session_finish,
)
self.sessions[msg_data.msg_id].event_q.put(ev)

Expand Down Expand Up @@ -101,14 +111,22 @@ def main() -> None:
"""

parser = argparse.ArgumentParser(fromfile_prefix_chars="@")
parser.add_argument("--host", default=get_default_value_from_env("WM_SERVICES_MQTT_HOSTNAME"), help="MQTT broker address")
parser.add_argument(
"--port", default=get_default_value_from_env("WM_SERVICES_MQTT_PORT", 8883), type=int, help="MQTT broker port"
"--host",
default=get_default_value_from_env("WM_SERVICES_MQTT_HOSTNAME"),
help="MQTT broker address",
)
parser.add_argument(
"--port",
default=get_default_value_from_env("WM_SERVICES_MQTT_PORT", 8883),
type=int,
help="MQTT broker port",
)
parser.add_argument(
"--insecure",
default=get_default_value_from_env("WM_SERVICES_MQTT_INSECURE", False),
type=bool,
type=lambda x: x.lower() == "true",
choices=[True, False],
help="MQTT security option",
)
parser.add_argument(
Expand All @@ -117,17 +135,29 @@ def main() -> None:
help="MQTT broker username",
)
parser.add_argument(
"--password", default=get_default_value_from_env("WM_SERVICES_MQTT_PASSWORD"), help="MQTT broker password"
"--password",
default=get_default_value_from_env("WM_SERVICES_MQTT_PASSWORD"),
help="MQTT broker password",
)
parser.add_argument(
"--config",
default=get_default_value_from_env("WM_PROV_CONFIG", "/home/wirepas/wm-provisioning/vars/settings.yml"),
type=str,
help='The path to your .yml config file: "examples/provisioning_config.yml"',
)
parser.add_argument(
"--loglevel",
default=get_default_value_from_env("WM_PROV_LOG_LEVEL", "INFO"),
type=str,
help=f'Log level, choose one of {", ".join(logging._nameToLevel.keys())} ',
)

args = parser.parse_args()

logging.basicConfig(format="%(levelname)s %(asctime)s %(message)s", level=logging.INFO)
logging.basicConfig(
format="%(levelname)s %(asctime)s %(message)s",
level=logging._nameToLevel[args.loglevel] or logging.INFO,
)

wni = WirepasNetworkInterface(args.host, args.port, args.username, args.password, args.insecure)

Expand Down

0 comments on commit c7f0f9e

Please sign in to comment.