def request_live_data(): router_id = request.json['router_id'] if DatabaseHandler().user_has_router(router_id, request.json['token']) is False: return jsonify(result=False) topic_name = str(uuid.uuid4()) message = MessageCreator(MessageCreator.NEW_CHANNEL, topic_name) send_message(router_id, message) send_message_admin("API", "New Live data link by user " + DatabaseHandler().get_user_from_id(request.json['token']) + " to router " + str(router_id)) return jsonify(topic_name=topic_name), 200
def set_config(): sensors = request.json["sensors"] payload = [] for sensor in sensors[:]: DatabaseHandler().set_sensor_config(sensor['id'], sensor['config']) payload.append({"id": sensor['id'], "config": sensor['config']}) message = MessageCreator(MessageCreator.UPDATE_SENSORS, payload) send_message(request.json['router_id'], message) send_message_admin( "POST SENSOR", "By user " + DatabaseHandler().get_user_from_id(request.json['token']) + " to router " + str(request.json['router_id'])) return jsonify(True), 200
def set_script(): router_id = request.json['router_id'] script = request.json['script'] res = DatabaseHandler().add_script(router_id, script) if res is True: scripts = DatabaseHandler().get_script(router_id=router_id) scripts_list = [] for x in scripts[:]: scripts_list.append(x.script) message = MessageCreator(MessageCreator.UPDATE_SCRIPT, scripts_list) send_message(router_id, message) send_message_admin( "POST SCRIPT", "New script added by user " + DatabaseHandler().get_user_from_id(request.json['token']) + " to router " + str(router_id)) return jsonify(res), 200
def RouterSleep(id=None): send_message(id, "SLPPHN") return jsonify({'status': 'Sleeping Photons'}), 200
def RouterRestart(id=None): send_message(id, "RSTART") return jsonify({'status': 'Restarting'}), 200
def RouterStop(id=None): send_message(id, "SHTDWN") return jsonify({'status': 'Shutting Down'}), 200
def set_internet_button(): router_id = request.json['router_id'] message = MessageCreator(MessageCreator.CONFIG_BUTTON, request.json['buttons']) send_message(router_id, message) return jsonify(True), 200
def control_actuator(): router_id = request.json['router_id'] message = MessageCreator(MessageCreator.COMMAND, {'MAC': request.json['MAC'], "command": request.json['command']}) send_message(router_id, message) send_message_admin("API", "Actuator control by user " + DatabaseHandler().get_user_from_id(request.json['token']) + " to router " + str(router_id)) return jsonify(True), 200
def arm_system(): router_id = request.json['router_id'] arm = request.json['armed'] message = MessageCreator(MessageCreator.ARM_SYSTEM, arm) send_message(router_id, message) return jsonify(True), 200
def phone_location(): router_id = request.json['router_id'] message = MessageCreator(MessageCreator.PHONE_LOCATION, request.json['sensor_id']) send_message(router_id, message) return jsonify(True), 200