Skip to content

Commit 053c6b4

Browse files
committed
build docker image and deploy
1 parent 15aa86f commit 053c6b4

File tree

3 files changed

+53
-64
lines changed

3 files changed

+53
-64
lines changed

Dockerfile

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
FROM python:3.10
1+
FROM python:3.9-slim-buster
22

3-
RUN mkdir -p /opt/broadlink
4-
RUN mkdir -p /var/log/broadlink
3+
RUN mkdir -p /etc/broadlink
4+
WORKDIR /etc/broadlink
55

6-
WORKDIR /opt/broadlink
7-
COPY . /opt/broadlink
6+
RUN groupadd -r -g 1000 broadlink && useradd -r -u 1000 -g broadlink broadlink
7+
RUN chown -R broadlink:broadlink /etc/broadlink
88

9-
RUN pip install -r /opt/broadlink/requirements.txt
10-
11-
RUN groupadd -r broadlink && useradd -r -g broadlink broadlink
12-
RUN chown -R broadlink:broadlink /opt/broadlink
13-
RUN chown -R broadlink:broadlink /var/log/broadlink
9+
COPY . /src
10+
RUN pip install -r /src/requirements.txt
1411

1512
USER broadlink
1613

17-
VOLUME ["/opt/broadlink"]
18-
ENV DATA_DIR="/opt/broadlink"
14+
VOLUME ["/etc/broadlink"]
15+
16+
ENV DATA_DIR="/etc/broadlink"
1917

20-
CMD python app.py
18+
CMD python /src/app.py

app.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
# get the data directory
1515
DATA_DIR = os.getenv('DATA_DIR', os.path.dirname(os.path.abspath(__file__)))
1616

17+
# logging config
18+
LOG_LEVEL = os.getenv('LOG_LEVEL', 'DEBUG')
19+
LOG_FORMAT = os.getenv('LOG_FORMAT', '%(asctime)-15s %(levelname)-5s [%(module)s] %(message)s')
20+
1721
# read initial config files
18-
LOGGING_CONF = os.path.join(DATA_DIR, 'logging.conf')
1922
APP_CONFIG = os.path.join(DATA_DIR, 'app.conf')
2023

2124
class Config(object):
2225
def __init__(self, filename):
2326
self.config = {}
24-
exec(compile(open(filename, "rb").read(), filename, 'exec'), self.config)
27+
if os.path.exists(filename):
28+
exec(compile(open(filename, "rb").read(), filename, 'exec'), self.config)
2529

2630
def get(self, key, default='MISSING_CONFIG'):
2731
# env var has precendence
@@ -35,7 +39,7 @@ def get(self, key, default='MISSING_CONFIG'):
3539
return value
3640

3741
# initialise logging as early as possible
38-
logging.config.fileConfig(LOGGING_CONF)
42+
logging.basicConfig(stream=sys.stderr, level=LOG_LEVEL, format=LOG_FORMAT)
3943

4044
# load application config
4145
try:
@@ -59,45 +63,49 @@ def on_message(client, devices, msg):
5963
device = devices[host]
6064

6165
if not device:
62-
logging.warning('No device found for payload: %s' % msg.payload)
66+
logging.warning('No device found for message: %s' % (msg.payload))
6367
return
6468

65-
logging.debug('Message received for %s device at %s (MAC %s)' % (device.type, device.host[0], ':'.join(format(s, '02x') for s in device.mac)))
66-
6769
if 'command' in payload:
6870
command = payload.get('command')
6971
action = payload.get('action', '')
7072

73+
logging.debug('Command received for %s device at %s (MAC %s): %s -> %s' % (device.type, device.host[0], ':'.join(format(s, '02x') for s in device.mac), command, action))
74+
7175
if device.type in ['RM2', 'RM4', 'RM4PRO', 'RMMINI', 'RM4MINI', 'RMMINIB', 'RMPRO']:
7276
file = os.path.join(DATA_DIR, 'commands', command)
7377
handy_file = os.path.join(file, action)
7478

75-
if command == 'macro':
76-
file = os.path.join(DATA_DIR, 'macros', action)
77-
macro(device, file)
78-
elif action == '' or action == 'auto':
79-
record_or_replay(device, file)
80-
elif action == 'autorf':
81-
record_or_replay_rf(device, file)
82-
elif os.path.isfile(handy_file):
83-
replay(device, handy_file)
84-
elif action == 'record':
85-
record(device, file)
86-
elif action == 'recordrf':
87-
record_rf(device, file)
88-
elif action == 'replay':
89-
replay(device, file)
90-
elif action == 'macro':
91-
file = os.path.join(DATA_DIR, 'macros', command)
92-
macro(device, file)
93-
else:
94-
logging.warning("Unrecognized MQTT message " + action)
95-
79+
try:
80+
if command == 'macro':
81+
file = os.path.join(DATA_DIR, 'macros', action)
82+
macro(device, file)
83+
elif action == '' or action == 'auto':
84+
record_or_replay(device, file)
85+
elif action == 'autorf':
86+
record_or_replay_rf(device, file)
87+
elif os.path.isfile(handy_file):
88+
replay(device, handy_file)
89+
elif action == 'record':
90+
record(device, file)
91+
elif action == 'recordrf':
92+
record_rf(device, file)
93+
elif action == 'replay':
94+
replay(device, file)
95+
elif action == 'macro':
96+
file = os.path.join(DATA_DIR, 'macros', command)
97+
macro(device, file)
98+
else:
99+
logging.warning('Unrecognized MQTT message: %s' % (msg.payload))
100+
101+
except Exception as e:
102+
logging.error('Error handling command %s: %s' % (command, e))
103+
return
96104

97105
# noinspection PyUnusedLocal
98106
def on_connect(client, devices, flags, result_code):
99107
if result_code == 0:
100-
logging.debug("MQTT connected")
108+
logging.info("MQTT connected")
101109

102110
birth_topic = cf.get('mqtt_birth_topic', 'stat/broadlink/lwt')
103111
birth_payload = payload=cf.get('mqtt_birth_payload', '{"online":true}')
@@ -250,13 +258,17 @@ def configure_device(device):
250258

251259

252260
if __name__ == '__main__':
261+
logging.info("Scanning for Broadlink devices...")
253262
devices = get_devices(cf)
254263

255264
if len(devices) == 0:
256265
logging.warning('No devices found, exiting')
257266
sys.exit(2)
258267

268+
broker = cf.get('mqtt_broker', 'localhost')
269+
port = int(cf.get('mqtt_port', 1883))
259270
clientid = cf.get('mqtt_clientid', 'broadlink')
271+
260272
mqttc = paho.Client(clientid, clean_session=False, userdata=devices)
261273

262274
mqttc.on_message = on_message
@@ -271,8 +283,9 @@ def configure_device(device):
271283
mqttc.username_pw_set(cf.get('mqtt_username'), cf.get('mqtt_password'))
272284

273285
while True:
286+
logging.info('Attempting to connect to MQTT broker at %s:%d...' % (broker, port))
274287
try:
275-
mqttc.connect(cf.get('mqtt_broker', 'localhost'), cf.get('mqtt_port', 1883), 60)
288+
mqttc.connect(broker, port, 60)
276289
mqttc.loop_forever()
277290
except socket.error:
278291
logging.warning("Failed to connect to MQTT broker, will try to reconnect in 5 seconds")

logging.conf

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)