feat: move sensitive configuration to separate config.json

This commit is contained in:
s3lph 2023-09-10 16:02:36 +02:00
parent 469cc2966f
commit 370fb4d3c5
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
4 changed files with 18 additions and 11 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
config.json

View file

@ -8,7 +8,7 @@ with other subscribers as well.
## Installation ## Installation
1. Edit `flow3r_openhab/__init__.py` and set the variables `WIFI_SSID`, `WIFI_PSK` and `MQTT_BROKER_IP`. 1. Copy `flow3r_openhab/config.example.json` to `flow3r_openhab/config.json` and enter your configuration details.
1. Copy the `flow3r_openhab` directory to `/sys/apps/flow3r_openhab` on the flow3r's flash filesystem. 1. Copy the `flow3r_openhab` directory to `/sys/apps/flow3r_openhab` on the flow3r's flash filesystem.
1. Reboot the flow3r and start the `OpenHAB` app 1. Reboot the flow3r and start the `OpenHAB` app

View file

@ -1,19 +1,18 @@
import gc import gc
import json
import network import network
import time import os
import socket import socket
import time
from st3m.application import Application, ApplicationContext from st3m.application import Application, ApplicationContext
from umqtt.robust import MQTTClient from umqtt.robust import MQTTClient
WIFI_SSID = b'...'
WIFI_PSK = b'...'
MQTT_BROKER_IP = '...' with open(os.path.join(os.path.dirname(__file__), 'config.json'), 'r') as f:
MQTT_CLIENT_ID = b'flow3r-{}' CONFIG = json.load(f)
MQTT_BASE_TOPIC = b'flow3r-{}/event'
class Flow3rOpenhabMqtt(Application): class Flow3rOpenhabMqtt(Application):
@ -22,9 +21,9 @@ class Flow3rOpenhabMqtt(Application):
super().__init__(app_ctx) super().__init__(app_ctx)
self.state = 0 self.state = 0
self.nic = network.WLAN(network.STA_IF) self.nic = network.WLAN(network.STA_IF)
client_id = MQTT_CLIENT_ID.format(self.nic.config('mac').hex()) client_id = CONFIG['mqtt_client_id'].format(self.nic.config('mac').hex())
self.base_topic = MQTT_BASE_TOPIC.format(self.nic.config('mac').hex()) self.base_topic = CONFIG['mqtt_topic'].format(self.nic.config('mac').hex())
self.mqtt = MQTTClient(client_id, MQTT_BROKER_IP) self.mqtt = MQTTClient(client_id, CONFIG['mqtt_broker_ip'])
self.touch_time = [None]*10 self.touch_time = [None]*10
self.touch_begin = [None]*10 self.touch_begin = [None]*10
self.touch_last = [None]*10 self.touch_last = [None]*10
@ -44,7 +43,7 @@ class Flow3rOpenhabMqtt(Application):
self.nic.active(True) self.nic.active(True)
if not self.nic.isconnected(): if not self.nic.isconnected():
print('connecting to network...') print('connecting to network...')
self.nic.connect(WIFI_SSID, WIFI_PSK) self.nic.connect(CONFIG['wifi_ssid'], CONFIG['wifi_psk'])
while not self.nic.isconnected(): while not self.nic.isconnected():
pass pass
print('network config:', self.nic.ifconfig()) print('network config:', self.nic.ifconfig())

View file

@ -0,0 +1,7 @@
{
"wifi_ssid": "example",
"wifi_psk": "supersecurepassword",
"mqtt_broker_ip": "127.0.0.1",
"mqtt_client_id": "flow3r-{}",
"mqtt_topic": "flow3r-{}/event"
}