def serveRead(self, qs): if "port" not in qs: return self.serveError("port parameter expected!") port = qs.get("port")[0] mote = motes.getMote(port) if mote is None: return self.serveError("Mote " + port + " not connected!") if not mote.ensureSerialIsOpen(): return self.serveError("Port " + port + " cannot be opened!") if "clean" in qs: doClean = qs.get("clean")[0].lower() in [ 'true', '1', 't', 'y', 'yes' ] else: doClean = False if "max_data" in qs: try: maxData = int(qs.get("max_data")[0].lower(), 0) except: maxData = None else: maxData = None content = mote.getData(doClean, maxData) self.send_response(200) self.sendDefaultHeaders(content) self.end_headers() self.wfile.write(content)
def serveWrite(self, qs): # print("serveWrite") if "port" not in qs: return self.serveError("port parameter expected!") port = qs.get("port")[0] if self.headers.has_key('content-length'): length = int(self.headers['content-length']) else: return self.serveError("content-length parameter expected!") mote = motes.getMote(port) if mote is None: return self.serveError("Mote " + port + " not connected!") if not mote.ensureSerialIsOpen(): return self.serveError("Port " + port + " cannot be opened!") toWrite = self.rfile.read(length) reply = mote.writeData(toWrite) content = str(reply) self.send_response(200) self.sendDefaultHeaders(content) self.end_headers() self.wfile.write(content)
def serveControl(self, qs): # print("serveControl") # use "default" to set config for all ports without specific config if "port" not in qs: return self.serveError("port parameter expected!") port = qs.get("port")[0] dtr, doSetDtr = qsExtractBool(qs, "dtr") rts, doSetRts = qsExtractBool(qs, "rts") mote = motes.getMote(port) if mote is None: return self.serveError("Mote " + port + " not connected!") if not mote.ensureSerialIsOpen(): return self.serveError("Port " + port + " cannot be opened!") content = "" if doSetDtr: content += "DTR = {}\n".format(dtr) mote.port.setDTR(dtr) if doSetRts: content += "RTS = {}\n".format(rts) mote.port.setRTS(rts) self.send_response(200) self.sendDefaultHeaders(content) self.end_headers() self.wfile.write(content)
def serveRead(self, qs): if "port" not in qs: return self.serveError("port parameter expected!") port = qs.get("port")[0] mote = motes.getMote(port) if mote is None: return self.serveError("Mote " + port + " not connected!") if not mote.ensureSerialIsOpen(): return self.serveError("Port " + port + " cannot be opened!") if "clean" in qs: doClean = qs.get("clean")[0].lower() in ['true', '1', 't', 'y', 'yes'] else: doClean = False if "max_data" in qs: try: maxData = int(qs.get("max_data")[0].lower(),0) except: maxData = None else: maxData = None content = mote.getData(doClean, maxData) self.send_response(200) self.sendDefaultHeaders(content) self.end_headers() self.wfile.write(content)
def serveTalkTo(self, qs): if not self.getLevel() > 1: self.setSession(qs) self.send_response(200) self.sendDefaultHeaders() self.end_headers() self.writeChunk("accessDenied") elif "single" in qs: # listen to a single mote and return motePortName = utils.urlUnescape(qs["single"][0]) print("motePortName = " + motePortName) mote = motes.getMote(motePortName) if mote is None: self.setSession(qs) self.send_response(200) self.sendDefaultHeaders() self.end_headers() try: urllib2.urlopen(motePortName.split('@')[1]) except URLError: self.writeChunk("hostUnreachable") return self.writeChunk("noMotesSelected") return # do not set session info self.send_response(200) self.sendDefaultHeaders() self.end_headers() if "data" in qs: if mote.isLocal(): mote.writeData(qs["data"][0]) self.writeChunk("OK") else: (portname, host) = motePortName.split('@') if os.name == "posix" and not os.path.isabs(portname): fullPortName = "/dev/" + portname else: fullPortName = portname if host.find("://") == -1: host = "http://" + host url = host + "/write?port=" + fullPortName try: req = urllib2.urlopen(url, qs["data"][0]) output = req.read() self.writeChunk(output) except Exception as e: self.writeChunk("hostUnreachable")
def serveProgram(self, qs): global bslLock # print("serveProgram") if "filename" not in qs: return self.serveError("filename parameter expected!") if "args" not in qs: return self.serveError("args parameter expected!") if "port" not in qs: return self.serveError("port parameter expected!") filename = urllib2.unquote(qs.get("filename")[0]) if self.headers.has_key('content-length'): length = int(self.headers['content-length']) else: return self.serveError("content-length parameter expected!") arguments = urllib2.unquote(qs.get("args")[0]) port = urllib2.unquote(qs.get("port")[0]) mote = motes.getMote(port) if mote is None: return self.serveError("Mote " + port + " not connected!") mote.ensureSerialIsClosed() # Note: will not reopen automatically! dirname = os.path.dirname(os.path.join("tmpdir", filename)) if not os.path.exists(dirname): os.makedirs(dirname) with open(os.path.join("tmpdir", filename), "wb") as tmpfile: tmpfile.write(self.rfile.read(length)) bslDir = os.path.abspath( os.path.join(configuration.c.getCfgValue("mansosDirectory"), "mos", "make", "scripts")) if configuration.c.getCfgValueAsBool("slowUpload"): arguments += " --slow" arguments = "python " + bslDir + os.path.sep + arguments with bslLock: os.chdir("tmpdir") (retcode, content) = runSubprocess2(arguments) os.chdir("..") self.send_response(200) self.sendDefaultHeaders(content) self.end_headers() self.wfile.write(content)
def serveProgram(self, qs): global bslLock # print("serveProgram") if "filename" not in qs: return self.serveError("filename parameter expected!") if "args" not in qs: return self.serveError("args parameter expected!") if "port" not in qs: return self.serveError("port parameter expected!") filename = urllib2.unquote(qs.get("filename")[0]) if self.headers.has_key('content-length'): length = int(self.headers['content-length']) else: return self.serveError("content-length parameter expected!") arguments = urllib2.unquote(qs.get("args")[0]) port = urllib2.unquote(qs.get("port")[0]) mote = motes.getMote(port) if mote is None: return self.serveError("Mote " + port + " not connected!") mote.ensureSerialIsClosed() # Note: will not reopen automatically! dirname = os.path.dirname(os.path.join("tmpdir", filename)) if not os.path.exists(dirname): os.makedirs(dirname) with open(os.path.join("tmpdir", filename), "wb") as tmpfile: tmpfile.write(self.rfile.read(length)) bslDir = os.path.abspath(os.path.join(configuration.c.getCfgValue("mansosDirectory"), "mos", "make", "scripts")) if configuration.c.getCfgValueAsBool("slowUpload"): arguments += " --slow" arguments = "python " + bslDir + os.path.sep + arguments with bslLock: os.chdir("tmpdir") (retcode, content) = runSubprocess2(arguments) os.chdir("..") self.send_response(200) self.sendDefaultHeaders(content) self.end_headers() self.wfile.write(content)
def serveRead(self, qs): # print("serveRead") if "port" not in qs: return self.serveError("port parameter expected!") port = qs.get("port")[0] mote = motes.getMote(port) if mote is None: return self.serveError("Mote " + port + " not connected!") if not mote.ensureSerialIsOpen(): return self.serveError("Port " + port + " cannot be opened!") content = mote.getData() self.send_response(200) self.sendDefaultHeaders(content) self.end_headers() self.wfile.write(content)
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 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): 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 serveListenSingle(self, qs): if not self.getLevel() > 1: self.setSession(qs) self.send_response(200) self.sendDefaultHeaders() self.end_headers() self.writeChunk("accessDenied") elif "single" in qs: # listen to a single mote and return motePortName = utils.urlUnescape(qs["single"][0]) print("motePortName = " + motePortName) mote = motes.getMote(motePortName) if mote is None: self.setSession(qs) self.send_response(200) self.sendDefaultHeaders() self.end_headers() try: urllib2.urlopen(motePortName.split('@')[1]) except URLError: self.writeChunk("hostUnreachable") return self.writeChunk("noMotesSelected") return if "max_data" in qs: max_data = qs["max_data"][0] else: max_data = "100" # do not set session info self.send_response(200) self.sendDefaultHeaders() # auto refresh self.send_header('Refresh', '1') self.end_headers() if mote.isLocal(): # Prepare serial port for the first time if "startListen" in qs and qs["startListen"][0] == '1': ht.closeAllSerial() mote.openSerial() ht.openMoteSerial(mote) # Listen for data output = "" for line in sensor_data.moteData.listenTxt: output += line + "\n" self.writeChunk("buffer:" + output) else: # Example URL: "http://*****:*****@') if os.name == "posix" and not os.path.isabs(portname): fullPortName = "/dev/" + portname else: fullPortName = portname if host.find("://") == -1: host = "http://" + host url = host + "/read?max_data=" + max_data + "&port=" + fullPortName + "&clean=1" try: req = urllib2.urlopen(url) output = req.read() self.writeChunk(output) except Exception as e: self.writeChunk("hostUnreachable")