def _org_chart_add(name, duty, warrant): with Writer(config.DB_PATH) as db_write: db_write.write(( "INSERT INTO Officers (Name, Duty, Warrant) " "VALUES (" "?," "?," "?" ")" ), parameters=(name, duty, json.dumps(warrant)))
def message_cb(topic, message): try: data = json.loads(message) # station, name, duty, warrant station = data.get("station", None) name = data.get("name", None) duty = data.get("duty", None) warrant = data.get("warrant", None) if station is None or name is None: raise Exception("Invalid message: {0}".format(message)) # TODO: check if station is valid # TODO: check if duty is valid # TODO: check if warrant is valid # send the message to the specified station station_topic = config.topic_name_station_get(station) del data["station"] self._publish(station_topic, json.dumps(data)) # remove the agent that the station should call back with Writer(config.DB_PATH) as db_write: db_write.write(("DELETE FROM Agents " "WHERE (Station = ?) " "AND (Name = ?) "), parameters=(station, name)) if duty is not None: # save the agent that the station should dispatch with Writer(config.DB_PATH) as db_write: db_write.write(( "INSERT INTO Agents (Station, Name, Duty, Warrant) " "VALUES (?, ?, ?, ?)"), parameters=(station, name, duty, json.dumps(warrant))) except Exception as e: l.error("Unable to process message '{0}': {1}".format( message, str(e)))
def officer(): content_str = request.data.decode('utf-8') station = request.args.get("station", None) # if station is None: # return "" ret_str = "" if request.method == "POST": content_json = request.get_json(True) mqtt_client = MQTTWrap("ceva") mqtt_client.init() mqtt_client.publish(station, content_str) mqtt_client.uninit() print(content_json) name = content_json["name"] assignment = content_json["assignment"] with Writer(DB_PATH) as db_write: db_write.write(( "INSERT INTO Officers (Name, Assignment) " "VALUES (" "?," "?" ")" ), parameters=(name, json.dumps(assignment))) if request.method == "GET": ret = [] with Reader(DB_PATH) as db_read: ret = db_read.read(( "SELECT Name, Assignment " "FROM Officers " )) ret_str = json.dumps([{"name": row[0], "assignment": json.loads(row[1])} for row in ret]) print(ret_str) return ret_str
import sys import config from db import Writer if len(sys.argv) != 2: sys.stderr.write("Invalid number of arguments") sys.exit(1) station_name = sys.argv[1] with Writer(config.DB_PATH) as db_write: db_write.write(("CREATE TABLE IF NOT EXISTS " "Station(ID INT PRIMARY KEY, PID STRING, Name STRING)")) db_write.write(("INSERT OR REPLACE INTO Station (ID, Name) " "VALUES (" "0, " "'" + station_name + "'" ") "))
import json from flask import Flask, render_template, request from messaging import MQTTWrap from db import Reader, Writer app = Flask(__name__) DB_PATH = "test_orig.db" with Writer(DB_PATH) as db_write: db_write.write(( "CREATE TABLE IF NOT EXISTS " "Officers(Name String PRIMARY KEY, Assignment STRING)")) @app.route("/") def index(): name = "haha" return render_template('hello.html', name=name) #@app.route("/officer/<station>/", methods=["POST", "GET", "PUT", "DELETE"]) @app.route("/officer/", methods=["POST", "GET", "PUT", "DELETE"]) @app.route("/officer", methods=["POST", "GET", "PUT", "DELETE"]) #@app.route("/officer/", methods=["GET"]) #@app.route("/officer/", methods=["GET"]) def officer(): content_str = request.data.decode('utf-8')
def _org_chart_remove(name): with Writer(config.DB_PATH) as db_write: db_write.write(( "DELETE FROM Officers " "WHERE Name = ? " ), parameters=(name,))
def _org_chart_init(): with Writer(config.DB_PATH) as db_write: db_write.write(( "CREATE TABLE IF NOT EXISTS " "Officers(Name String PRIMARY KEY, Duty STRING, Warrant STRING)"))