2019-06-05 13:32:10 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
A hub for controlling IOT devices.
|
|
|
|
"""
|
2019-06-13 20:28:47 -04:00
|
|
|
import os
|
2019-06-05 13:32:10 -04:00
|
|
|
import re
|
2019-06-08 03:56:30 -04:00
|
|
|
import copy
|
2019-06-05 13:32:10 -04:00
|
|
|
import json
|
|
|
|
|
|
|
|
import requests
|
2019-06-13 10:46:59 -04:00
|
|
|
from flask import Flask, render_template, request, abort, jsonify
|
2019-06-13 20:28:47 -04:00
|
|
|
from flask import Blueprint
|
|
|
|
|
|
|
|
import config
|
2019-06-16 18:22:47 -04:00
|
|
|
import auth
|
|
|
|
from auth import auth_required
|
2019-06-05 13:32:10 -04:00
|
|
|
|
|
|
|
class RelayDevice:
|
|
|
|
"""
|
|
|
|
Represents a relay receptacle device controlled by an ESP-01. The
|
|
|
|
ESP-01 acts a simple switch to turn each receptable on or off via
|
|
|
|
a relay. Each device only has two outputs, `OUT0` and `OUT2`.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.id = ""
|
2019-06-20 09:00:15 -04:00
|
|
|
self.type = "RelayDevice"
|
2019-06-05 13:32:10 -04:00
|
|
|
self.description = ""
|
|
|
|
self.location = ""
|
|
|
|
self.ip_address = ""
|
2019-06-18 13:44:35 -04:00
|
|
|
self.sub_devices = []
|
2019-06-20 07:49:29 -04:00
|
|
|
self.sub_devices.append(RelayOutlet())
|
|
|
|
self.sub_devices.append(RelayOutlet())
|
2019-06-20 10:09:40 -04:00
|
|
|
self.sub_devices[0].id = 'OUT0'
|
2019-06-20 07:49:29 -04:00
|
|
|
self.sub_devices[0].gpio = '0'
|
2019-06-20 10:09:40 -04:00
|
|
|
self.sub_devices[1].id = 'OUT2'
|
2019-06-20 07:49:29 -04:00
|
|
|
self.sub_devices[1].gpio = '2'
|
2019-06-05 13:32:10 -04:00
|
|
|
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""
|
|
|
|
Queries the physical device and updates the internal model as
|
|
|
|
appropriate.
|
|
|
|
"""
|
2019-06-20 07:49:29 -04:00
|
|
|
if not self.ip_address:
|
|
|
|
return
|
2019-06-18 13:44:35 -04:00
|
|
|
for sub_dev in self.sub_devices:
|
2019-06-05 13:32:10 -04:00
|
|
|
res = requests.get(self.ip_address)
|
|
|
|
gpio0 = re.search(r"GPIO0: (\bLow|\bHigh)", res.text).groups()[0]
|
2019-06-18 13:44:35 -04:00
|
|
|
sub_dev.state = gpio0 == 'High'
|
2019-06-05 13:32:10 -04:00
|
|
|
|
2019-06-06 13:16:59 -04:00
|
|
|
def toggle(self, sub_dev_id):
|
|
|
|
"""
|
|
|
|
Toggles the state of a sub device.
|
|
|
|
"""
|
2019-06-18 13:44:35 -04:00
|
|
|
for sub_dev in self.sub_devices:
|
2019-06-06 13:16:59 -04:00
|
|
|
if sub_dev.id == sub_dev_id:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
params = {sub_dev.gpio: 'toggle'}
|
|
|
|
res = requests.get('http://' + self.ip_address + '/gpio',params=params)
|
|
|
|
res.raise_for_status()
|
|
|
|
|
|
|
|
state = re.search(
|
|
|
|
rf"GPIO{sub_dev.gpio}: (\bLow|\bHigh)",
|
|
|
|
res.text
|
|
|
|
).groups()[0] == 'High'
|
|
|
|
sub_dev.state = state
|
|
|
|
|
2019-06-13 10:46:59 -04:00
|
|
|
return json.dumps({'ok': True, sub_dev_id: state})
|
2019-06-06 13:16:59 -04:00
|
|
|
|
2019-06-05 13:32:10 -04:00
|
|
|
def from_dict(self, data):
|
|
|
|
"""
|
|
|
|
Initializes self with data from JSON dict.
|
|
|
|
"""
|
2019-06-08 03:12:03 -04:00
|
|
|
for key, value in data.items():
|
|
|
|
setattr(self, key, value)
|
2019-06-18 13:44:35 -04:00
|
|
|
self.sub_devices = []
|
|
|
|
for sub_dev_dict in data.get('sub_devices'):
|
|
|
|
if sub_dev_dict.get('type') == 'RelayOutlet':
|
|
|
|
sub_dev = RelayOutlet().from_dict(sub_dev_dict)
|
|
|
|
self.sub_devices.append(sub_dev)
|
2019-06-05 13:32:10 -04:00
|
|
|
else:
|
2019-06-18 13:44:35 -04:00
|
|
|
typ = sub_dev.get('type')
|
|
|
|
raise ValueError(f"Unknown sub_device type {typ}")
|
2019-06-05 13:32:10 -04:00
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
class RelayOutlet:
|
|
|
|
"""
|
|
|
|
Represents a single outlet on a RelayDevice.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.id = ""
|
2019-06-20 09:00:15 -04:00
|
|
|
self.type = "RelayOutlet"
|
2019-06-05 13:32:10 -04:00
|
|
|
self.description = ""
|
2019-06-06 13:16:59 -04:00
|
|
|
self.gpio = ""
|
2019-06-05 13:32:10 -04:00
|
|
|
self.state = False
|
|
|
|
|
|
|
|
def from_dict(self, data):
|
|
|
|
"""
|
|
|
|
Initializes self with data from JSON dict.
|
|
|
|
"""
|
2019-06-08 03:12:03 -04:00
|
|
|
for key, value in data.items():
|
|
|
|
setattr(self, key, value)
|
2019-06-05 13:32:10 -04:00
|
|
|
return self
|
|
|
|
|
|
|
|
|
2019-06-08 03:56:30 -04:00
|
|
|
class DeviceEncoder(json.JSONEncoder):
|
|
|
|
"""
|
|
|
|
A custom json encoder for dumping devices to file.
|
|
|
|
"""
|
|
|
|
def default(self, o):
|
|
|
|
return vars(o)
|
|
|
|
|
|
|
|
|
|
|
|
def init_network(filepath="devices.json"):
|
2019-06-05 13:32:10 -04:00
|
|
|
"""
|
|
|
|
Initializes the network of IOT devices.
|
|
|
|
"""
|
2019-06-08 03:56:30 -04:00
|
|
|
with open(filepath, 'r') as file:
|
2019-06-05 13:32:10 -04:00
|
|
|
data = file.read()
|
|
|
|
data = json.loads(data)
|
2019-06-18 13:44:35 -04:00
|
|
|
network = []
|
|
|
|
for device_dict in data:
|
|
|
|
if device_dict.get('type') == 'RelayDevice':
|
|
|
|
device = RelayDevice().from_dict(device_dict)
|
|
|
|
network.append(device)
|
2019-06-05 13:32:10 -04:00
|
|
|
else:
|
2019-06-18 13:44:35 -04:00
|
|
|
raise ValueError(f"Unknown device type {device.get('type')}")
|
2019-06-05 13:32:10 -04:00
|
|
|
return network
|
|
|
|
|
|
|
|
|
2019-06-08 03:56:30 -04:00
|
|
|
def save_network(filepath="devices.json"):
|
|
|
|
"""
|
|
|
|
Dumps the network to file.
|
|
|
|
"""
|
|
|
|
with open(filepath, 'w') as file:
|
|
|
|
file.write(json.dumps(network, cls=DeviceEncoder, indent='\t'))
|
|
|
|
|
2019-06-13 20:28:47 -04:00
|
|
|
app_views = Blueprint('app_views', __name__)
|
2019-06-08 03:56:30 -04:00
|
|
|
|
2019-06-13 20:28:47 -04:00
|
|
|
@app_views.route('/')
|
2019-06-16 18:22:47 -04:00
|
|
|
@auth_required
|
2019-06-05 13:32:10 -04:00
|
|
|
def index():
|
|
|
|
"""
|
|
|
|
The index page.
|
|
|
|
"""
|
2019-06-19 10:15:08 -04:00
|
|
|
init_state = {}
|
|
|
|
for device in network:
|
|
|
|
device_state = {}
|
|
|
|
for sub_dev in device.sub_devices:
|
|
|
|
device_state[sub_dev.id] = sub_dev.state
|
|
|
|
init_state[device.id] = device_state
|
|
|
|
return render_template('index.html', network=network,init_state=init_state)
|
2019-06-05 13:32:10 -04:00
|
|
|
|
|
|
|
|
2019-06-13 20:28:47 -04:00
|
|
|
@app_views.route('/toggle')
|
2019-06-16 18:22:47 -04:00
|
|
|
@auth_required
|
2019-06-06 13:16:59 -04:00
|
|
|
def toggle():
|
|
|
|
"""
|
|
|
|
Toggles the state of a RelayDevice.
|
|
|
|
"""
|
|
|
|
device_id = request.args.get('device_id')
|
|
|
|
sub_dev_id = request.args.get('sub_dev_id')
|
|
|
|
|
2019-06-18 13:44:35 -04:00
|
|
|
for device in network:
|
2019-06-06 13:16:59 -04:00
|
|
|
if device.id == device_id:
|
|
|
|
break
|
|
|
|
else:
|
2019-06-13 10:46:59 -04:00
|
|
|
return make_error(404, "device_id not found")
|
2019-06-06 13:16:59 -04:00
|
|
|
res = device.toggle(sub_dev_id)
|
|
|
|
if not res:
|
2019-06-13 10:46:59 -04:00
|
|
|
return make_error(404, "sub_dev_id not found")
|
2019-06-08 03:56:30 -04:00
|
|
|
save_network()
|
2019-06-06 13:16:59 -04:00
|
|
|
return res
|
|
|
|
|
2019-06-13 20:28:47 -04:00
|
|
|
@app_views.route('/edit')
|
2019-06-16 18:22:47 -04:00
|
|
|
@auth_required
|
2019-06-13 10:46:59 -04:00
|
|
|
def edit():
|
|
|
|
"""
|
|
|
|
Edits the text of a particular field.
|
|
|
|
"""
|
|
|
|
device_id = request.args.get('device_id')
|
|
|
|
sub_dev_id = request.args.get('sub_dev_id')
|
|
|
|
field = request.args.get('field')
|
|
|
|
value = request.args.get('value')
|
|
|
|
|
2019-06-18 13:44:35 -04:00
|
|
|
for device in network:
|
2019-06-13 10:46:59 -04:00
|
|
|
if device.id == device_id:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return make_error(404, "device_id not found")
|
|
|
|
|
|
|
|
if sub_dev_id:
|
2019-06-18 13:44:35 -04:00
|
|
|
for sub_dev in device.sub_devices:
|
2019-06-13 10:46:59 -04:00
|
|
|
if sub_dev.id == sub_dev_id:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return make_error(404, "sub_dev_id not found")
|
|
|
|
if hasattr(sub_dev, field):
|
|
|
|
setattr(sub_dev, field, value)
|
|
|
|
else:
|
|
|
|
return make_error(404, "sub_device field not found")
|
|
|
|
else:
|
|
|
|
if hasattr(device, field):
|
|
|
|
setattr(device, field, value)
|
|
|
|
else:
|
|
|
|
return make_error(404, "device field not found")
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'ok': True,
|
|
|
|
'field': field,
|
|
|
|
'value': value
|
|
|
|
}
|
|
|
|
save_network()
|
|
|
|
return json.dumps(data)
|
|
|
|
|
|
|
|
|
2019-06-20 07:49:29 -04:00
|
|
|
@app_views.route('/new_device')
|
|
|
|
def new_device():
|
|
|
|
"""
|
|
|
|
Allows adding a new device. Accepts device_type parameter, returns
|
|
|
|
the device_id.
|
|
|
|
"""
|
|
|
|
device_type = request.args.get('device_type')
|
|
|
|
|
|
|
|
if device_type == 'RelayDevice':
|
|
|
|
device = RelayDevice()
|
|
|
|
else:
|
|
|
|
return make_error(400, "Unknown device type")
|
|
|
|
|
|
|
|
devices = [dev for dev in network if dev.type == device_type]
|
|
|
|
devices.sort(key=lambda dev: dev.type)
|
|
|
|
if not devices:
|
|
|
|
device.id = device_type + '01'
|
|
|
|
else:
|
|
|
|
num = re.search(r'(\d*)$', devices[-1].id).groups()
|
|
|
|
if not num:
|
|
|
|
device.id = device_type + '01'
|
|
|
|
else:
|
|
|
|
num = str(int(num[0]) + 1).zfill(2)
|
|
|
|
device.id = device_type + num
|
|
|
|
network.append(device)
|
|
|
|
save_network()
|
|
|
|
data = {'device_id': device.id}
|
|
|
|
return json.dumps(data)
|
|
|
|
|
|
|
|
|
2019-06-13 10:46:59 -04:00
|
|
|
def make_error(code, message):
|
|
|
|
"""
|
|
|
|
Returns a JSON error.
|
|
|
|
"""
|
|
|
|
res = jsonify(ok=False, status=code, message=message)
|
|
|
|
res.status_code = code
|
|
|
|
return res
|
|
|
|
|
2019-06-06 13:16:59 -04:00
|
|
|
|
2019-06-13 20:28:47 -04:00
|
|
|
app = Flask(__name__)
|
|
|
|
app.register_blueprint(app_views, url_prefix=config.url_prefix)
|
2019-06-16 18:22:47 -04:00
|
|
|
app.register_blueprint(auth.auth_views, url_prefix=config.url_prefix)
|
|
|
|
if os.path.isfile('secret_key'):
|
|
|
|
with open('secret_key', 'rb') as file:
|
|
|
|
app.secret_key = file.read()
|
|
|
|
else:
|
|
|
|
secret_key = os.urandom(32)
|
|
|
|
app.secret_key = secret_key
|
|
|
|
with open('secret_key', 'wb') as file:
|
|
|
|
file.write(secret_key)
|
2019-06-13 20:28:47 -04:00
|
|
|
network = init_network()
|
|
|
|
|
|
|
|
|
2019-06-05 13:32:10 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host='0.0.0.0', port=5300)
|