diff --git a/picoreflowd.py b/picoreflowd.py index 8d899fc..599371b 100644 --- a/picoreflowd.py +++ b/picoreflowd.py @@ -78,8 +78,15 @@ def handle_storage(): elif msgdict.get("cmd") == "PUT": log.info("PUT command received") profile_obj = msgdict.get('profile') + force = msgdict.get('force',False) if profile_obj: - save_profile(profile_obj) + #del msgdict["cmd"] + if save_profile(profile_obj,force): + msgdict["resp"]="OK" + else: + msgdict["resp"]="FAIL" + print "sending:" +str(msgdict) + wsock.send(json.dumps(msgdict)) except WebSocketError: break log.info("websocket (storage) closed") @@ -111,14 +118,19 @@ def get_profiles(): profiles.append(json.load(f)) return json.dumps(profiles) -def save_profile(profile): +def save_profile(profile, force=False): profile_json = json.dumps(profile) filename = profile['name']+".json" filepath = os.path.join(profile_path,filename) + if not force and os.path.exists(filepath): + print "Didnt write" + return False with open(filepath, 'w+') as f: print filepath f.write(profile_json) f.close() + print "Did write" + return True def main(): ip = "0.0.0.0" diff --git a/public/index.html b/public/index.html index dcff58d..54b0d47 100644 --- a/public/index.html +++ b/public/index.html @@ -150,8 +150,8 @@ function enterEditMode() { graph.series[0].options.draggableX=true; graph.series[0].options.draggableY=true; graph.render(); -} +} function leaveEditMode() { state="IDLE"; $('#saveas').hide(); @@ -240,11 +240,31 @@ ws_storage.onopen = function() ws_storage.onmessage = function(e) { console.log('Storage MSG:' + e.data); + + message = JSON.parse(e.data); - profiles = JSON.parse(e.data); - - console.log("Parsed profile:" + profiles); - + console.log("Parsed message:" + message); + + if(message.resp) + { + console.log("RESP"); + if(message.resp == "FAIL") + { + console.log("FAIL"); + if (confirm('Overwrite?')) { + //message.cmd="PUT"; + message.force=true; + console.log("Sending: " + JSON.stringify(message)); + ws_storage.send(JSON.stringify(message)); + } else { + ws_storage.send("GET"); + } + } + return; + } + //the message is an array of profiles + //FIXME: this should be better, maybe a {"profiles": ...} container? + profiles = message; //delete old options in select $('#e2') .find('option')