def blinds_id_delete(blind_id): db, cursor = dbutil.get_db_objects() # delete blind with matching id: try: cursor.execute("DELETE FROM blinds WHERE id=?", (blind_id, )) db.commit() except sqlite3.OperationalError: return jsonify(dict(status="table not found", error=True)), 200 return jsonify(dict(status="blind gone")), 200
def blinds_id_get(blind_id): db, cursor = dbutil.get_db_objects() # check for blinds: try: cursor.execute("SELECT * FROM blinds WHERE id=?", (blind_id, )) except sqlite3.OperationalError: return jsonify(dict(status="table not found", error=True)), 200 r = cursor.fetchone() if r == None: return jsonify(dict(status="blind not found", error=True)), 200 return jsonify(r), 200
def blinds_put(): db, cursor = dbutil.get_db_objects() post_data = request.get_json(silent=True) if post_data == None: return jsonify(dict(status="invalid json", error=True)), 200 try: cursor.execute("UPDATE blinds SET status=?", (int(post_data["status"]), )) except sqlite3.OperationalError: return jsonify(dict(status="table not found", error=True)), 200 except KeyError: return jsonify(dict(status="json incomplete", error=True)), 200 db.commit() return jsonify(status="ok"), 200
def scenes_id_put(scene_id): db, cursor = dbutil.get_db_objects() # check for scenes: try: cursor.execute("SELECT * FROM scenes WHERE scene_id=?", (scene_id, )) except sqlite3.OperationalError: return jsonify(dict(status="table not found", error=True)), 200 try: for i in cursor.fetchall(): print(i["light_id"], i["brightness"], i["status"], flush=True) cursor.execute( "UPDATE lights SET status=?, brightness=? WHERE id=?", (i["status"], i["brightness"], i["light_id"]), ) except sqlite3.OperationalError: return jsonify(dict(status="table not found", error=True)), 200 db.commit() return jsonify(status="ok"), 200
def blinds_delete(): db, cursor = dbutil.get_db_objects() with db: cursor.execute("DROP TABLE IF EXISTS blinds") return jsonify(dict(status="ok")), 200