def option(option, section): if request.method == "GET": db_option = db.session.query(Option).filter_by( name=option, section=section).first() if db_option: return jsonify(db_option.serialize) if db_option else jsonify(None) return make_response(jsonify({}), 200) elif request.method == "PUT": db_option = db.session.query(Option).filter_by( name=option, section=section).first() if not db_option: # create the new option db_option = Option(name=option, section=section, value="") db.session.add(db_option) # do update changed = db_option.update_value(request.json) db.session.commit() if option == "notifications": if changed: return process_ipc_response(IPCClient().update_configuration()) elif db_option.name == "network" and db_option.section == "dyndns": if os.environ.get("ARGUS_DEVELOPMENT", "0") == "0": return process_ipc_response(IPCClient().update_dyndns()) elif db_option.name == "network" and db_option.section == "access": if os.environ.get("ARGUS_DEVELOPMENT", "0") == "0": return process_ipc_response(IPCClient().update_ssh()) return make_response("", 204) return make_response(jsonify({"error": "Unknown action"}), 400)
def keypad(keypad_id): """ Limited to handle only one keypad! """ if request.method == "GET": keypad = db.session.query(Keypad).first() if keypad: return jsonify(keypad.serialize) return make_response(jsonify({"error": "Option not found"}), 404) elif request.method == "DELETE": keypad = db.session.query(Keypad).get(keypad_id) if keypad: keypad.deleted = True db.session.commit() return process_ipc_response(IPCClient().update_keypad()) return make_response(jsonify({"error": "Option not found"}), 404) elif request.method == "PUT": keypad = db.session.query(Keypad).get(keypad_id) if not keypad: # create the new keypad keypad = Keypad(keypad_type=db.session.query(KeypadType).get( request.json["typeId"])) if not keypad.update(request.json): return make_response("", 204) db.session.commit() return process_ipc_response(IPCClient().update_keypad()) return make_response(jsonify({"error": "Unknown action"}), 400)
def zone(zone_id): if request.method == "GET": zone = db.session.query(Zone).get(zone_id) if zone: return jsonify(zone.serialize) return make_response(jsonify({"error": "Zone not found"}), 404) elif request.method == "DELETE": zone = db.session.query(Zone).get(zone_id) if zone: zone.deleted = True db.session.commit() return process_ipc_response(IPCClient().update_configuration()) return make_response(jsonify({"error": "Zone not found"}), 404) elif request.method == "PUT": zone = db.session.query(Zone).get(zone_id) if not zone: return make_response(jsonify({"error": "Zone not found"}), 404) if not zone.update(request.json): return make_response("", 204) db.session.commit() return process_ipc_response(IPCClient().update_configuration()) return make_response(jsonify({"error": "Unknown action"}), 400)
def register_card(user_id): for user in db.session.query(User).all(): user.card_registration_expiry = None user = db.session.query(User).get(user_id) user.set_card_registration() db.session.commit() return process_ipc_response(IPCClient().register_card())
def create_zone(): zone = Zone() zone.update(request.json) if not zone.description: zone.description = zone.name db.session.add(zone) db.session.commit() IPCClient().update_configuration() return jsonify(zone.serialize)
def sensors_reset_references(): if request.method == "PUT": for sensor in db.session.query(Sensor).all(): sensor.reference_value = None db.session.commit() return process_ipc_response(IPCClient().update_configuration()) return make_response(jsonify({"error": "Unknown action"}), 400)
def sensor(sensor_id): if request.method == "GET": sensor = db.session.query(Sensor).filter_by(id=sensor_id, deleted=False).first() if sensor: return jsonify(sensor.serialize) return jsonify({"error": "Sensor not found"}), (404) elif request.method == "DELETE": sensor = db.session.query(Sensor).get(sensor_id) sensor.deleted = True db.session.commit() return process_ipc_response(IPCClient().update_configuration()) elif request.method == "PUT": sensor = db.session.query(Sensor).get(sensor_id) if not sensor: return jsonify({"error": "Sensor not found"}), (404) if not sensor.update(request.json): return make_response("", 204) db.session.commit() return process_ipc_response(IPCClient().update_configuration()) return make_response(jsonify({"error": "Unknown action"}), 400)
def create_sensor(): data = request.json zone = db.session.query(Zone).get(request.json["zoneId"]) sensor_type = db.session.query(SensorType).get(data["typeId"]) sensor = Sensor( channel=data["channel"], zone=zone, sensor_type=sensor_type, description=data["description"], ) db.session.add(sensor) db.session.commit() return process_ipc_response(IPCClient().update_configuration())
def power_state(): return process_ipc_response(IPCClient().get_power_state())
def disarm(): return process_ipc_response(IPCClient().disarm( request.environ["requester_id"]))
def put_arm(): return process_ipc_response(IPCClient().arm( request.args.get("type"), request.environ["requester_id"]))
def get_arm(): return process_ipc_response(IPCClient().get_arm())
def set_clock(): return process_ipc_response(IPCClient().set_clock(request.json))