Compare commits

...
12 Commits
11 changed files with 209 additions and 58 deletions
+11 -2
View File
@@ -1,4 +1,13 @@
# Python environment, setuptools, build
venv venv
.idea
/test/clients.json
*.pyc *.pyc
*.egg-info
deb_dist
dist
# PyCharm
.idea
# Project generated test files
/test/clients.json
*.json
+3
View File
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
+9 -1
View File
@@ -1,2 +1,10 @@
pip~=20.2.4
setuptools~=50.3.2
stdeb3~=0.9.0.post2
paho-mqtt~=1.5.1 paho-mqtt~=1.5.1
setuptools~=50.3.1 Jinja2~=2.11.2
Werkzeug~=1.0.1
itsdangerous~=1.1.0
click~=7.1.2
MarkupSafe~=1.1.1
Flask~=1.1.2
+29
View File
@@ -0,0 +1,29 @@
[metadata]
name = torchsub
version = 0.0.3
author = Benjamin Dweck
author_email = [email protected]
description = TORch Simple Subscriber
url = https://git.rudefox.io/bj/torch-subscriber-simple
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
[options]
packages = find:
install_requires =
paho-mqtt~=1.5.1
setuptools~=50.3.1
pip~=20.2.3
Jinja2~=2.11.2
Werkzeug~=1.0.1
itsdangerous~=1.1.0
click~=7.1.2
MarkupSafe~=1.1.1
Flask~=1.1.1
[options.entry_points]
console_scripts = torch-sub=torchsub.torch_sub:main
[options.packages.find]
exclude=test
+2 -15
View File
@@ -1,16 +1,3 @@
from setuptools import setup import setuptools
setup( setuptools.setup()
name='torch-subscriber-simple',
version='0.0.1',
packages=['torch_sub', 'torch_sub.test'],
url='http://git.rudefox.io/bj/torch-subscriber-simple',
license='MIT',
author='Benjamin Dweck',
author_email='[email protected]',
description='',
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
],
)
+1 -1
View File
@@ -10,7 +10,7 @@ from unittest.case import TestCase
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
from torch_sub import torch_sub from torchsub import torch_sub
broker_hostname = "mqtt.example.com" broker_hostname = "mqtt.example.com"
broker_port = 8883 broker_port = 8883
+15
View File
@@ -0,0 +1,15 @@
##############################################################################
# Sample torch-sub.conf file
##############################################################################
[mqtt]
#Topic is required
Topic=torch/+/wake
[database]
#Filename is optional and defaults to 'clients.json'
Filename=torch_clients.json
[web]
#Port is optional and defaults to 8080
Port=3434
-39
View File
@@ -1,39 +0,0 @@
import json
import os
import threading
import paho.mqtt.subscribe as mqtt
database_filename = "clients.json"
database_lock = threading.Lock()
# noinspection PyUnusedLocal
def update_client_record(client, userdata, message):
database_lock.acquire()
if not os.path.exists(database_filename):
with open(database_filename, 'w') as database_blank:
json.dump({}, database_blank)
with open(database_filename, 'r') as infile:
database = json.load(infile)
payload = message.payload.decode('utf-8')
response = json.loads(payload)
database[response['clientId']] = response
with open(database_filename, 'w') as outfile:
json.dump(database, outfile)
database_lock.release()
def subscribe(broker_hostname, broker_port, topic="torch", tls=None, auth=None):
mqtt.callback(update_client_record,
topic,
hostname=broker_hostname,
port=broker_port,
tls=tls,
auth=auth)
+124
View File
@@ -0,0 +1,124 @@
import argparse
import configparser
import json
import os
import sys
import threading
import paho.mqtt.subscribe as mqtt
from torchsub import torch_sub_webserver
database_filename = "clients.json"
database_lock = threading.Lock()
# noinspection PyUnusedLocal
def update_client_record(client, userdata, message):
database_lock.acquire()
with open(database_filename, 'r') as infile:
database = json.load(infile)
payload = message.payload.decode('utf-8')
response = json.loads(payload)
database[response['clientId']] = response
with open(database_filename, 'w') as outfile:
json.dump(database, outfile)
database_lock.release()
def subscribe(broker_hostname, broker_port, topic="torch", tls=None):
mqtt.callback(update_client_record,
topic,
hostname=broker_hostname,
port=broker_port,
tls=tls)
class Config:
def __init__(self):
self.broker_hostname = None
self.broker_port = None
self.topic = None
self.tls = None
self.database_file = database_filename
self.web_port = 8080
parser = self.do_cli_argument_parsing()
(config_path, config_filename) = self.get_config_path(parser.parse_args())
print("Using torch configuration path: " + config_path)
self.read_configuration_file(config_path, config_filename)
@staticmethod
def do_cli_argument_parsing():
parser = argparse.ArgumentParser(description='Simple Torch Flat-file Database Subscriber')
parser.add_argument('--config-dir',
nargs='?',
dest='configPath',
default='/etc/torch',
help='configuration directory (default: /etc/torch)')
parser.add_argument('--config-filename',
nargs='?',
dest='configFilename',
default='torch-sub.conf',
help='configuration filename (default: torch-sub.conf)')
return parser
@staticmethod
def get_config_path(args):
config_path = args.configPath
if "TORCH_CONFIG_DIR" in os.environ:
config_path = os.environ.get("TORCH_CONFIG_DIR")
if not config_path.endswith("/"):
config_path = config_path + "/"
return config_path, args.configFilename
def read_configuration_file(self, config_dir, config_filename):
config_file_path = os.path.join(config_dir, config_filename)
if not os.path.exists(config_file_path):
print("Unable to locate config file at '%s'" % config_file_path)
sys.exit(1)
config = configparser.ConfigParser()
config.read(config_file_path)
mqtt_config = config['mqtt']
self.broker_hostname = mqtt_config.get('BrokerHost', fallback="localhost")
self.broker_port = mqtt_config.getint('BrokerPort', fallback=1883)
self.topic = mqtt_config.get('Topic', fallback="torch/+/onion_url")
require_certificate = mqtt_config.getboolean('RequireCertificate', fallback=False)
ca_file = config_dir + mqtt_config.get('CaFile', "")
cert_file = config_dir + mqtt_config.get('CertFile', "")
key_file = config_dir + mqtt_config.get('KeyFile', "")
self.tls = None
if require_certificate:
self.tls = {
'ca_certs': ca_file,
'certfile': cert_file,
'keyfile': key_file
}
if config.has_section('database'):
self.database_file = config['database'].get('Filename', fallback=database_filename)
if config.has_section('web'):
self.web_port = config['web'].getint('Port', fallback=self.web_port)
def main():
config = Config()
global database_filename
database_filename = config.database_file
if not os.path.exists(database_filename):
with open(database_filename, 'w') as database_blank:
json.dump({}, database_blank)
threading.Thread(target=torch_sub_webserver.app.run,
args=("0.0.0.0", config.web_port),
daemon=True).start()
subscribe(config.broker_hostname,
config.broker_port,
config.topic,
config.tls)
+15
View File
@@ -0,0 +1,15 @@
from flask import Flask
from torchsub import torch_sub
app = Flask(__name__)
@app.route('/clients', methods=['GET'])
def clients():
with open(torch_sub.database_filename, 'r') as database:
return app.response_class(
response=database.read(),
status=200,
mimetype='application/json'
)