From 1a15b3caf1606aecbe34bc532fe859389df8cd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Balzarotti?= Date: Wed, 4 Jan 2023 14:52:28 +0100 Subject: [PATCH] edit recipes --- actuators.py | 6 +-- app.py | 19 +++++-- dist/server.js | 101 +++++++++++++++++++++++++++++++++-- phasectrl.py | 44 ++++++++++----- recipes.json | 66 ++++++++++++----------- templates/recipe-editor.html | 74 +++++++------------------ templates/recipes-list.html | 37 +++++++++++++ 7 files changed, 234 insertions(+), 113 deletions(-) create mode 100644 templates/recipes-list.html diff --git a/actuators.py b/actuators.py index 3820e4f..982d93e 100644 --- a/actuators.py +++ b/actuators.py @@ -30,7 +30,7 @@ class GPIOPin(Actuator): def enable(self, enable=True): GPIO.output(self.pin, self.onstate) if enable else self.disable() - + def disable(self): GPIO.output(self.pin, self.offstate) @@ -53,7 +53,5 @@ class MockPIN(Actuator): # print('FAKE DISABLE') actuators = { - 'heater': GPIOPin( - pin=22, initial=GPIO.HIGH, onstate=GPIO.LOW, offstate=GPIO.HIGH) if ( - rpi) else MockPIN(pin=22, initial=False, onstate=True, offstate=False), + 'heater': (GPIOPin if rpi else MockPIN)(pin=22, initial=GPIO.HIGH, onstate=GPIO.LOW, offstate=GPIO.HIGH), } diff --git a/app.py b/app.py index 211dd1b..5935d32 100644 --- a/app.py +++ b/app.py @@ -51,9 +51,14 @@ def make_state(): def index(): return render_template('index.html') -@app.route('/recipe-editor') -def recipe_editor(): - return render_template('recipe-editor.html') +@app.route('/edit-recipe/') +def recipe_editor(id): + return render_template('recipe-editor.html', + recipe=statemachine.recipeById(id)) + +@app.route('/edit-recipe/') +def recipes_list(): + return render_template('recipes-list.html', recipes=statemachine.recipes) @app.route('/status') def status(): @@ -88,6 +93,14 @@ def handle_manual_response(response): def load_recipe_idx(idx): statemachine.loadByIdx(idx) +@socketio.on('update recipe phase') +def update_recipe_phase(recipeid, phaseid, content): + recipe = statemachine.recipeById(recipeid) + recipe.phases[phaseid] = phasectrl.load_phase(content) + statemachine.recipes[recipeid] = recipe + phasectrl.store_recipes("recipes.json", + [r.getState() for r in statemachine.recipes]) + @socketio.on('stop recipe') def stop_recipe(): statemachine.stop() diff --git a/dist/server.js b/dist/server.js index 9498915..f82c585 100644 --- a/dist/server.js +++ b/dist/server.js @@ -1,7 +1,8 @@ var state = {}; var localstate = { manual: {}, recipe: 0, page: ['General', 'Dashboard'], - maxpoints: 1000 + maxpoints: 1000, + editing: {} }; var menu_items = [ {name: "General", @@ -12,7 +13,7 @@ var menu_items = [ }, {name: "Recipes", subitems:[ - {name: "Recipes Editor", link: 'recipe-editor'}, + {name: "Recipes Editor", link: 'edit-recipe'}, {name: "New Recipe"} ] }, @@ -54,6 +55,8 @@ function applyState(newstate) { current_recipe(state.recipe); render_load_recipe(state); render_plot(); + localstate.editing.recipe = state.recipes.findIndex( + function (el) {return el.name == localstate.editing.recipe_name;}); } var enable_draw = true; @@ -124,7 +127,7 @@ function render_sensors(sensordata) { function navigate(path, link) { if (link !== undefined) { - window.location.href = "./" + link + window.location.href = "/" + link return; } localstate.page = path; @@ -160,6 +163,93 @@ function load_recipe() { socket.emit('load recipe idx', localstate.recipe); } +function edit_recipe(recipeid) { + let recipe = recipeid === undefined ? localstate.recipe : recipeid; + window.location.href = '/edit-recipe/'+recipe; +} + +function currently_editing_recipe(name) { + localstate.editing.recipe_name = name; +} + +function update_field(fieldname, newvalue) { + state.recipes[localstate.editing.recipe].phases[localstate.editing.phase][fieldname] = newvalue; + console.log(state.recipes[localstate.editing.recipe].phases[localstate.editing.phase]); + console.log(fieldname, newvalue); +} + +function save_phase() { + let edited_phase = state.recipes[localstate.editing.recipe].phases[ + localstate.editing.phase]; + console.log(edited_phase); + socket.emit('update recipe phase', + localstate.editing.recipe, localstate.editing.phase, edited_phase) +} + +function edit_phase(phaseid) { + console.log(localstate); + let html = document.getElementById("phase-editor"); + if (html === null) return; + localstate.editing.phase = phaseid; + data = state.recipes[localstate.editing.recipe].phases[phaseid]; + console.log(data.text); + let template = `
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
`; + html.innerHTML = Mustache.render(template, data); +} + function stop_recipe() { socket.emit('stop recipe'); } @@ -193,6 +283,7 @@ function render_load_recipe(data) { {{#selected_recipe}} {{description}} +

Controllers: