1
0
mirror of https://github.com/Adam-Ant/mqtt-dash synced 2024-12-20 14:44:34 +00:00

Added ability to define config directory

This commit is contained in:
Adam Dodman 2016-12-28 14:12:19 +00:00
parent d92135a02e
commit 39564cbf35
2 changed files with 20 additions and 6 deletions

View File

@ -4,9 +4,8 @@ MAINTAINER "Adam Dodman <adam.dodman@gmx.com>"
RUN apk add --no-cache python py-pip \ RUN apk add --no-cache python py-pip \
&& pip install --upgrade pip \ && pip install --upgrade pip \
&& pip install paho-mqtt scapy configparser \ && pip install paho-mqtt scapy configparser \
&& mkdir dash \ && mkdir dash
&& ln -s /config/dash.cfg /dash/dash.cfg
ADD main.py /dash/main.py ADD main.py /dash/main.py
CMD ["python","/dash/main.py"] CMD ["python","/dash/main.py","-c","/config"]

21
main.py
View File

@ -2,6 +2,7 @@ from backports import configparser
import os import os
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import logging import logging
import argparse
from time import sleep from time import sleep
logging.getLogger("scapy.runtime").setLevel(logging.ERROR) logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
@ -42,15 +43,29 @@ def arp_display(pkt):
if __name__ == '__main__': if __name__ == '__main__':
if not (os.path.isfile('./dash.cfg')): argparser = argparse.ArgumentParser(prog='MQTT-Dash',description='Send MQTT events when dash nuttons are pressed.')
argparser.add_argument('-c','--config',help='Specify directory for config file and database')
cmdargs = argparser.parse_args()
if not (cmdargs.config):
filepath = '.'
else:
filepath = cmdargs.config
if not (os.path.isdir(filepath)):
print('Error: Config directory does not exist. Exiting...')
exit(1)
if not (os.path.isfile(filepath + '/dash.cfg')):
print('Warn: Config file does not exist, writing example config. Please configure and try again.') print('Warn: Config file does not exist, writing example config. Please configure and try again.')
c = open('./dash.cfg', 'w') c = open(filepath + '/dash.cfg', 'w')
c.write(defaultconfig) c.write(defaultconfig)
c.close() c.close()
exit(1) exit(1)
config = configparser.ConfigParser(delimiters=('=')) config = configparser.ConfigParser(delimiters=('='))
config.read('./dash.cfg') config.read(filepath + '/dash.cfg')
hostname = config['MQTT'].get('host') hostname = config['MQTT'].get('host')
port = config['MQTT'].get('port') port = config['MQTT'].get('port')
authrequired = config['MQTT'].getboolean('auth', False) authrequired = config['MQTT'].getboolean('auth', False)