def get_latest_data(): ''' 1. Take input as token and device id. 2. Query InfluxDB for latest entry. 3. return this data in json format. ''' data = request.json print(data) if (validate_token(data)): try: from server import SQLSession session = SQLSession() d = session.query(Device).filter_by(id=data['id']).first() session.close() if not d: return jsonify({"message": "No such device"}), 400 if d.monitoring == False: return jsonify({"message": "Monitoring not allowed"}), 404 from influxdb import InfluxDBClient dbname = "ihome" host = "influx" port = 8086 influx_client = InfluxDBClient(host=host, port=port) influx_client.switch_database(dbname) q = 'SELECT LAST(value) FROM {}'.format(d.measurement) a = influx_client.query(q) print(list(a.get_points(measurement=d.measurement))) return jsonify( {"data": list(a.get_points(measurement=d.measurement))}), 200 except Exception as e: return jsonify({"message": str(e)}), 500 else: return jsonify({"message": "Unauthorize"}), 300
def get_all_users(): if request.method == 'POST': data = request.json if validate_token(data): from server import SQLSession session = SQLSession() users_ = session.query(User).all() users_ = [{ "name": i.username, "email": i.email, } for i in users_] if len(users_) == 0: response_object = { 'status': 'success', 'message': 'No user yet' } else: response_object = { 'status': 'success', 'message': 'giving all users. use it wisely', 'payload': users_ } return jsonify(response_object), 200 else: response_object = {'status': 'fail', 'message': 'not a admin'} return jsonify(response_object), 300 else: response_object = {'status': 'fail', 'message': 'method not allowed'} return jsonify(response_object), 400
def devices(): ''' 1. take input as the token in url 2. return all devices and there information. Query the status of the device too. ''' try: auth = request.args.get('auth', None, type=str) print("auth = ", auth) except: response = {"status": "fail", "messahe": "No argument"} return jsonify(response), 400 if (validate_token({"token": auth})): from server import SQLSession session = SQLSession() devices = session.query(Device).all() session.close() res = [ { "id": device.id, # id of device, for further query "name": device.name, # name of the device # port of the pi, from which device is connected "port": device.port, "location": device.location, # location of device e.g. kitchen "desc": device.desc, # description of the device # status of the device, wether it is on or off "status": device.status, "monitoring": device.monitoring, # wheather device allow monitoring or not # if monitoring is on then which measurement it uses if not then null "measurement": device.measurement, "min_intensity": device.min_intensity, # min intensity of the device "max_intensity": device.max_intensity, # max intensity of the device "cur_intensity": device.cur_intensity # current intensity of the device } for device in devices ] response = { "status": "success", "message": "device information", "payload": res } return jsonify(response), 200 else: response = {"status": "fail", "message": "authorization fail"} return jsonify(response), 300
def toggle_device(): ''' 1. take input as token and name of device and future status to perform. 2. publish this request as a topic for 'cmds/toggle/device_name'. 3. return success or failure along with new status of device. ''' data = request.json if (validate_token(data)): try: dev_name = data['device_name'] operation = data['operation'] from server import client client.publish("cmd/toggle/{}".format(dev_name), operation).wait_for_publish() return jsonify({"message": "published"}), 200 except Exception as e: print(str(e)) return jsonify({"msmessage": str(e)}), 500 else: return jsonify({"message": "Unauthorize"}), 300
def change_intensity(): ''' 1. take input as token and name of device and future intensity. 2. publish this request as a topic for 'cmds/intensity/device_name'. 3. return success or failure along with new intensity of device. ''' data = request.json if (validate_token(data)): try: dev_name = data['device_name'] intensity = data['intensity'] from server import client client.publish("cmd/intensity/{}".format(dev_name), intensity).wait_for_publish() return jsonify({"message": "published"}), 200 except Exception as e: print(str(e)) return jsonify({"msmessage": str(e)}), 500 else: return jsonify({"message": "Unauthorize"}), 300