-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrestremote.py
executable file
·99 lines (84 loc) · 3.44 KB
/
restremote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
import click
from flask import abort
from flask import Flask
import importlib
import json
import logging
import sys
import utils
import yaml
app = Flask(__name__)
devices = {}
logger = logging.getLogger('RESTRemote')
@app.route('/<string:deviceName>/<string:commandName>', methods=['PUT'])
@app.route('/<string:deviceName>/<string:commandName>/<string:args>', methods=['PUT'])
def execute_command(deviceName, commandName, args=None):
try:
return json.dumps(getattr(devices[deviceName], 'executeCommand')(
commandName, args))
except Exception as e:
logger.exception('Exception: %s', e)
abort(400)
@app.route('/<string:deviceName>/<string:commandName>', methods=['GET'])
@app.route('/<string:deviceName>/<string:commandName>/<string:args>', methods=['GET'])
def get_data(deviceName, commandName, args=None):
try:
return json.dumps(getattr(devices[deviceName], 'getData')(
commandName, args))
except Exception as e:
logger.exception('Exception: %s', e)
abort(400)
@app.route('/devices', methods=['GET'])
def get_device_list():
try:
deviceList = []
for deviceName, device in devices.items():
deviceList.append({
'name': deviceName,
'is_connected': device.is_connected()
})
return json.dumps({
'devices': deviceList
})
except Exception as e:
logger.exception('Exception: %s', e)
abort(400)
@click.command()
@click.option('-c', '--config', help='Config file', type=click.File('r'), required=True)
@click.option('-sc', '--serverConfig', help='Server config file', type=click.File('r'), required=True)
@click.option('-d', '--debug', help='Run server in debug mode', default=False, is_flag=True)
def RESTRemote(config, serverconfig, debug):
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s')
logger.setLevel('DEBUG')
logger.info('Starting with config file %s', config.name)
configData = yaml.safe_load(config)
configData.update(yaml.safe_load(serverconfig))
sys.path.append(configData['driversPath'])
drivers = {}
devicesConfig = configData.get('devices', {})
for driverName, driverData in configData['drivers'].items():
module = importlib.import_module('drivers.' + driverName)
driver = getattr(module, driverData.get(
'moduleName', driverName.capitalize()))
drivers[driverName] = driver
deviceData = driver.discoverDevices(logger)
if deviceData is not None:
devicesConfig.update(deviceData)
configData['devices'] = devicesConfig
for deviceName, deviceData in configData['devices'].items():
if deviceData.get('enable', True):
driverName = deviceData['driver']
logger.info('Loading device %s using driver %s',
deviceName, driverName)
driverData = configData['drivers'][driverName]
deviceData.update(driverData)
deviceData.get('values', {}).update(configData.get(
'driverConfig', {}).get(driverName, {}).get('values', {}))
utils.flatten_commands(deviceData)
devices[deviceName] = drivers[driverName](deviceData, logger)
devices[deviceName].start()
app.run(host=configData.get('bindHost', '0.0.0.0'), port=configData.get('port', 5000),
debug=debug)
if __name__ == '__main__':
RESTRemote()