feat: move sensitive configuration to separate config.json
This commit is contained in:
parent
469cc2966f
commit
370fb4d3c5
4 changed files with 18 additions and 11 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
config.json
|
|
@ -8,7 +8,7 @@ with other subscribers as well.
|
|||
|
||||
## 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. Reboot the flow3r and start the `OpenHAB` app
|
||||
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
|
||||
import gc
|
||||
import json
|
||||
import network
|
||||
import time
|
||||
import os
|
||||
import socket
|
||||
import time
|
||||
|
||||
from st3m.application import Application, ApplicationContext
|
||||
|
||||
from umqtt.robust import MQTTClient
|
||||
|
||||
WIFI_SSID = b'...'
|
||||
WIFI_PSK = b'...'
|
||||
|
||||
MQTT_BROKER_IP = '...'
|
||||
MQTT_CLIENT_ID = b'flow3r-{}'
|
||||
MQTT_BASE_TOPIC = b'flow3r-{}/event'
|
||||
with open(os.path.join(os.path.dirname(__file__), 'config.json'), 'r') as f:
|
||||
CONFIG = json.load(f)
|
||||
|
||||
|
||||
class Flow3rOpenhabMqtt(Application):
|
||||
|
@ -22,9 +21,9 @@ class Flow3rOpenhabMqtt(Application):
|
|||
super().__init__(app_ctx)
|
||||
self.state = 0
|
||||
self.nic = network.WLAN(network.STA_IF)
|
||||
client_id = MQTT_CLIENT_ID.format(self.nic.config('mac').hex())
|
||||
self.base_topic = MQTT_BASE_TOPIC.format(self.nic.config('mac').hex())
|
||||
self.mqtt = MQTTClient(client_id, MQTT_BROKER_IP)
|
||||
client_id = CONFIG['mqtt_client_id'].format(self.nic.config('mac').hex())
|
||||
self.base_topic = CONFIG['mqtt_topic'].format(self.nic.config('mac').hex())
|
||||
self.mqtt = MQTTClient(client_id, CONFIG['mqtt_broker_ip'])
|
||||
self.touch_time = [None]*10
|
||||
self.touch_begin = [None]*10
|
||||
self.touch_last = [None]*10
|
||||
|
@ -44,7 +43,7 @@ class Flow3rOpenhabMqtt(Application):
|
|||
self.nic.active(True)
|
||||
if not self.nic.isconnected():
|
||||
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():
|
||||
pass
|
||||
print('network config:', self.nic.ifconfig())
|
||||
|
|
7
flow3r_openhab/config.example.json
Normal file
7
flow3r_openhab/config.example.json
Normal 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"
|
||||
}
|
Loading…
Reference in a new issue