def compileAndUpload(self, code, config, fileContents, isSEAL): global lastUploadCode global lastUploadConfig global lastUploadFile global maybeIsInSubprocess if not os.path.exists("build"): os.mkdir("build") # do this synchronously subprocessLock.acquire() global isListening ht.closeAllSerial() isListening = False maybeIsInSubprocess = True try: if fileContents: lastUploadFile = form["file"].filename filename = os.path.join("build", "tmp-file.ihex") with open(filename, "w") as outFile: outFile.write(fileContents) outFile.close() retcode = 0 for m in motes.getMotes(): if not m.tryToOpenSerial(False): continue r = m.tryToUpload(self, filename) if r != 0: retcode = r elif code: lastUploadCode = code filename = "main." + ("sl" if isSEAL else "c") with open(os.path.join("build", filename), "w") as outFile: outFile.write(code) outFile.close() with open(os.path.join("build", "config"), "w") as outFile: if config is None: config = "" outFile.write(config) outFile.close() with open(os.path.join("build", "Makefile"), "w") as outFile: if isSEAL: outFile.write("SEAL_SOURCES = main.sl\n") else: outFile.write("SOURCES = main.c\n") outFile.write("APPMOD = App\n") outFile.write("PROJDIR = $(CURDIR)\n") outFile.write("ifndef MOSROOT\n") mansosPath = configuration.c.getCfgValue("mansosDirectory") if not os.path.isabs(mansosPath): # one level up - because we are in build directory mansosPath = os.path.join(mansosPath, "..") outFile.write(" MOSROOT = " + mansosPath + "\n") outFile.write("endif\n") outFile.write("include ${MOSROOT}/mos/make/Makefile\n") outFile.close() retcode = 0 for m in motes.getMotes(): if not m.tryToOpenSerial(False): continue r = m.tryToCompileAndUpload(self, filename) if r != 0: retcode = r finally: maybeIsInSubprocess = False global isListening sensor_data.moteData.reset() ht.openAllSerial() isListening = True subprocessLock.release() return retcode
def serveConfig(self, qs): global isListening self.send_response(200) self.sendDefaultHeaders() self.end_headers() if "platform_set" in qs: for m in motes.getMotes(): motename = "sel_mote" + utils.urlEscape(m.getFullBasename()) if motename in qs: m.platform = qs[motename][0] motes.storeSelected() text = '<strong>Mote platforms updated!</strong>\n' self.writeChunk(self.serveAnyPage("config", qs, content = text, generatedContentOnly = True)) return else: sensor_data.moteData.reset() ht.openAllSerial() isListening = True motePortName = None filesRequired = False for s in qs: if s[:4] == "mote": pair = s[4:].split('_') try: motePortName = pair[0] filesRequired = pair[1] == "files" except: pass break if motePortName is None: # or moteIndex >= len(motes.getMotes()): self.writeChunk("<h4 class='err'>Config page requested, but mote not specified!</h4>") return motePortNameEsc = utils.urlEscape(motePortName) platform = None dropdownName = "sel_mote" + motePortNameEsc if dropdownName in qs: platform = qs[dropdownName][0] if platform not in moteconfig.supportedPlatforms: self.writeChunk("<h4 class='err'>Config page requested, but platform not specified or unknown!</h4>") return moteconfig.instance.setMote(motes.getMote(motePortName), platform) (errmsg, ok) = moteconfig.instance.updateConfigValues(qs) if not ok: self.writeChunk(errmsg) return # fill config values from the mote / send new values to the mote if "get" in qs: reply = moteconfig.instance.getConfigValues() elif "set" in qs: reply = moteconfig.instance.setConfigValues() if filesRequired: if "filename" in qs: (text, ok) = moteconfig.instance.getFileContentsHTML(qs) else: (text, ok) = moteconfig.instance.getFileListHTML(motePortNameEsc) else: (text, ok) = moteconfig.instance.getConfigHTML() if not ok: self.writeChunk("\n<h4 class='err'>Error: " + text + "</h4>\n") return moteidQS = "?sel_mote" + motePortNameEsc + "=" + platform + "&" + "mote" + motePortNameEsc replaceValues = { "MOTEID_CONFIG" : moteidQS + "_cfg=1", "MOTEID_FILES" : moteidQS + "_files=1" } if "update_sensors" in qs: configuration.c.selectSection("graph") graphConfig = [] storeConfig = [] try: graphSensors = json.loads(configuration.c.getCfgValue("graphsensors")) except: graphSensors = {} for s in moteconfig.instance.activePlatform.sensors: storeFlag = "store_" + s.varname graphFlag = "graph_" + s.varname if storeFlag in qs and qs[storeFlag][0] == 'on': storeConfig.append(s.varname) if graphFlag in qs and qs[graphFlag][0] == 'on': graphConfig.append(s.varname) configuration.c.selectSection("graph") graphSensors[motePortNameEsc] = graphConfig configuration.c.setCfgValue("graphsensors", json.dumps(graphSensors)) configuration.c.save() else: try: configuration.c.selectSection("graph") graphSensors = json.loads(configuration.c.getCfgValue("graphsensors")) graphConfig = graphSensors[motePortNameEsc] except: graphConfig = [] for s in moteconfig.instance.activePlatform.sensors: if s.varname in graphConfig: replaceValues["graph_" + s.varname] = "checked" else: replaceValues["graph_" + s.varname] = "" self.writeChunk(self.serveAnyPage("config", qs, isGeneric = False, content = text, replaceValues = replaceValues, generatedContentOnly = True))
def serveConfig(self, qs): self.setSession(qs) if not self.getLevel() > 1: self.serveDefault(qs, True) return self.send_response(200) self.sendDefaultHeaders() self.end_headers() if "platform_set" in qs: for m in motes.getMotes(): motename = "sel_mote" + m.getPortBasename() if motename in qs: m.platform = qs[motename][0] motes.storeSelected() text = '<strong>Mote platforms updated!</strong>\n' self.serveAnyPage("config", qs, content = text) return else: global isListening sensor_data.moteData.reset() ht.openAllSerial() isListening = True motePortName = None filesRequired = False for s in qs: if s[:4] == "mote": pair = s[4:].split('_') try: motePortName = pair[0] filesRequired = pair[1] == "files" except: pass break if motePortName is None: # or moteIndex >= len(motes.getMotes()): self.serveAnyPage("error:critical", qs, errorMsg = "Config page requested, but mote not specified!") return platform = None dropdownName = "sel_mote" + motePortName if dropdownName in qs: platform = qs[dropdownName][0] if platform not in moteconfig.supportedPlatforms: self.serveAnyPage("error:critical", qs, errorMsg = "Config page requested, but platform not specified or unknown!") return if os.name == "posix": fullMotePortName = "/dev/" + motePortName else: fullMotePortName = motePortName moteconfig.instance.setMote(motes.getMote(fullMotePortName), platform) (errmsg, ok) = moteconfig.instance.updateConfigValues(qs) if not ok: self.serveAnyPage("error:critical", qs, errorMsg = errmsg) return # fill config values from the mote / send new values to the mote if "get" in qs: reply = moteconfig.instance.getConfigValues() elif "set" in qs: reply = moteconfig.instance.setConfigValues() if filesRequired: if "filename" in qs: (text, ok) = moteconfig.instance.getFileContentsHTML(qs) else: (text, ok) = moteconfig.instance.getFileListHTML(motePortName) else: (text, ok) = moteconfig.instance.getConfigHTML() if not ok: self.serveAnyPage("error:critical", qs, errorMsg = text) return moteidQS = "?sel_mote" + motePortName + "=" + platform + "&" + "mote" + motePortName self.serveAnyPage("config", qs, isGeneric = False, content = text, replaceValues = { "MOTEID_CONFIG" : moteidQS + "_cfg=1", "MOTEID_FILES" : moteidQS + "_files=1"})
def compileAndUpload(self, code, config, fileName, fileContents, codeType): # global lastUploadCode # global lastUploadConfig # global lastUploadFile global maybeIsInSubprocess global isListening if not os.path.exists("build"): os.mkdir("build") # do this synchronously subprocessLock.acquire() ht.closeAllSerial() isListening = False maybeIsInSubprocess = True try: if fileContents: lastUploadFile = fileName filename = os.path.join("build", "tmp-file.ihex") with open(filename, "w") as outFile: outFile.write(fileContents) outFile.close() retcode = 0 for m in motes.getMotes(): if not m.tryToOpenSerial(False): continue r = m.tryToUpload(self, filename) if r != 0: retcode = r elif code: lastUploadCode = code if config == None: config = "" if codeType == "c": emitCodeMansOS(code, config) elif codeType == "plain_c": emitCodePlainC(code, config) elif codeType == "nesc": (componentName, appName) = detectTinyOSAppName(code, config) emitCodeTinyOS(code, config, componentName, appName) elif codeType == "contiki_c": emitCodeContiki(code) elif codeType == "seal": emitCodeSEAL(code, config) else: print("compileAndUpload: unknow code type: " + codeType) return 1 retcode = 0 for m in motes.getMotes(): if m.isLocal(): if not m.tryToOpenSerial(False): continue r = m.tryToCompileAndUpload(self, codeType) if r != 0: retcode = r finally: maybeIsInSubprocess = False sensor_data.moteData.reset() ht.openAllSerial() isListening = True subprocessLock.release() return retcode
def serveConfig(self, qs): global isListening self.send_response(200) self.sendDefaultHeaders() self.end_headers() if "platform_set" in qs: for m in motes.getMotes(): motename = "sel_mote" + utils.urlEscape(m.getFullBasename()) if motename in qs: m.platform = qs[motename][0] motes.storeSelected() text = '<strong>Mote platforms updated!</strong>\n' self.writeChunk( self.serveAnyPage("config", qs, content=text, generatedContentOnly=True)) return else: sensor_data.moteData.reset() ht.openAllSerial() isListening = True motePortName = None filesRequired = False for s in qs: if s[:4] == "mote": pair = s[4:].split('_') try: motePortName = pair[0] filesRequired = pair[1] == "files" except: pass break if motePortName is None: # or moteIndex >= len(motes.getMotes()): self.writeChunk( "<h4 class='err'>Config page requested, but mote not specified!</h4>" ) return motePortNameEsc = utils.urlEscape(motePortName) platform = None dropdownName = "sel_mote" + motePortNameEsc if dropdownName in qs: platform = qs[dropdownName][0] if platform not in moteconfig.supportedPlatforms: self.writeChunk( "<h4 class='err'>Config page requested, but platform not specified or unknown!</h4>" ) return moteconfig.instance.setMote(motes.getMote(motePortName), platform) (errmsg, ok) = moteconfig.instance.updateConfigValues(qs) if not ok: self.writeChunk(errmsg) return # fill config values from the mote / send new values to the mote if "get" in qs: reply = moteconfig.instance.getConfigValues() elif "set" in qs: reply = moteconfig.instance.setConfigValues() if filesRequired: if "filename" in qs: (text, ok) = moteconfig.instance.getFileContentsHTML(qs) else: (text, ok) = moteconfig.instance.getFileListHTML(motePortNameEsc) else: (text, ok) = moteconfig.instance.getConfigHTML() if not ok: self.writeChunk("\n<h4 class='err'>Error: " + text + "</h4>\n") return moteidQS = "?sel_mote" + motePortNameEsc + "=" + platform + "&" + "mote" + motePortNameEsc replaceValues = { "MOTEID_CONFIG": moteidQS + "_cfg=1", "MOTEID_FILES": moteidQS + "_files=1" } if "update_sensors" in qs: configuration.c.selectSection("graph") graphConfig = [] storeConfig = [] try: graphSensors = json.loads( configuration.c.getCfgValue("graphsensors")) except: graphSensors = {} for s in moteconfig.instance.activePlatform.sensors: storeFlag = "store_" + s.varname graphFlag = "graph_" + s.varname if storeFlag in qs and qs[storeFlag][0] == 'on': storeConfig.append(s.varname) if graphFlag in qs and qs[graphFlag][0] == 'on': graphConfig.append(s.varname) configuration.c.selectSection("graph") graphSensors[motePortNameEsc] = graphConfig configuration.c.setCfgValue("graphsensors", json.dumps(graphSensors)) configuration.c.save() else: try: configuration.c.selectSection("graph") graphSensors = json.loads( configuration.c.getCfgValue("graphsensors")) graphConfig = graphSensors[motePortNameEsc] except: graphConfig = [] for s in moteconfig.instance.activePlatform.sensors: if s.varname in graphConfig: replaceValues["graph_" + s.varname] = "checked" else: replaceValues["graph_" + s.varname] = "" self.writeChunk( self.serveAnyPage("config", qs, isGeneric=False, content=text, replaceValues=replaceValues, generatedContentOnly=True))
def serveListen(self, qs): self.setSession(qs) self.send_response(200) self.sendDefaultHeaders() self.end_headers() motesText = self.serveMotes("listen", "Listen", qs, None) errorMsg = None global isListening if "action" in qs and self.getLevel() > 1: if qs["action"][0] == "Start": if not motes.anySelected(): errorMsg = "\n<h4 class='err'>Error: No motes selected!</h4>\n" if isListening: errorMsg = "\n<h4 class='err'>Already listening!</h4>\n" else: sensor_data.moteData.reset() ht.openAllSerial() isListening = True # Open DB connection data_utils.openDBConnection() else: ht.closeAllSerial() isListening = False # Close DB connection data_utils.closeDBConnection() txt = "" for line in sensor_data.moteData.listenTxt: txt += line + "<br/>" action = "Stop" if isListening else "Start" dataFilename = configuration.c.getCfgValue("saveToFilename") saveProcessedData = configuration.c.getCfgValueAsBool("saveProcessedData") if self.getLevel() > 1: if "dataFile" in qs: dataFilename = qs["dataFile"][0] if len(dataFilename) and dataFilename.find(".") == -1: dataFilename += ".csv" if "dataType" in qs: saveProcessedData = not qs["dataType"][0] == "raw" saveMultipleFiles = qs["dataType"][0] == "mprocessed" configuration.c.setCfgValue("saveToFilename", dataFilename) configuration.c.setCfgValue("saveProcessedData", bool(saveProcessedData)) configuration.c.save() rawdataChecked = not saveProcessedData mprocessedChecked = saveProcessedData div_height = "0" if "div_height" in qs: div_height = qs["div_height"][0] self.serveAnyPage("listen", qs, True, {"MOTES_TXT" : motesText, "LISTEN_TXT" : txt, "MOTE_ACTION": action, "DATA_FILENAME" : dataFilename, "RAWDATA_CHECKED" : 'checked="checked"' if rawdataChecked else "", "MPROCDATA_CHECKED" : 'checked="checked"' if mprocessedChecked else "", "DIV_HEIGHT" : div_height}, errorMsg = errorMsg)
def serveListen(self, qs): self.setSession(qs) self.send_response(200) self.sendDefaultHeaders() self.end_headers() motesText = self.serveMotes("listen", "Listen", qs, None, True) errorStyle = "none" errorMsg = "" global isListening if "action" in qs and self.getLevel() > 1: if qs["action"][0] == "Start": if not motes.anySelected(): errorMsg = "\n<h4 class='err'>Error: No motes selected!</h4>\n" errorStyle = "block" elif isListening: errorMsg = "\n<h4 class='err'>Already listening!</h4>\n" errorStyle = "block" else: sensor_data.moteData.reset() ht.openAllSerial() isListening = True # Open DB connection data_utils.openDBConnection() else: ht.closeAllSerial() isListening = False # Close DB connection data_utils.closeDBConnection() txt = "" for line in sensor_data.moteData.listenTxt: txt += line + "<br/>" if errorMsg == "": action = "Stop" if isListening else "Start" else: action = "Start" dataFilename = configuration.c.getCfgValue("saveToFilename") saveProcessedData = configuration.c.getCfgValueAsBool("saveProcessedData") if self.getLevel() > 1: if "dataFile" in qs: dataFilename = qs["dataFile"][0] if len(dataFilename) and dataFilename.find(".") == -1: dataFilename += ".csv" if "dataType" in qs: saveProcessedData = not qs["dataType"][0] == "raw" saveMultipleFiles = qs["dataType"][0] == "mprocessed" configuration.c.setCfgValue("saveToFilename", dataFilename) configuration.c.setCfgValue("saveProcessedData", bool(saveProcessedData)) configuration.c.save() rawdataChecked = not saveProcessedData mprocessedChecked = saveProcessedData self.serveAnyPage("listen", qs, True, {"MOTES_TXT" : motesText, "LISTEN_TXT" : txt, "MOTE_ACTION": action, "DATA_FILENAME" : dataFilename, "RAWDATA_CHECKED" : 'checked="checked"' if rawdataChecked else "", "MPROCDATA_CHECKED" : 'checked="checked"' if mprocessedChecked else "", "ERROR_MSG" : errorMsg, "ERROR_STATUS" : errorStyle})