def getJSON(self, compact=False): if compact: f = 'f' v = 'v' else: f = 'function' v = 'value' json = {} for (bus, value) in BUSLIST.items(): json[bus] = int(value["enabled"]) gpios = {} if len(self.export) > 0: export = self.export else: export = range(GPIO.GPIO_COUNT) for gpio in export: gpios[gpio] = {} if compact: gpios[gpio][f] = GPIO.getFunction(gpio) else: gpios[gpio][f] = GPIO.getFunctionString(gpio) gpios[gpio][v] = int(GPIO.input(gpio)) if GPIO.getFunction(gpio) == GPIO.PWM: (pwmType, value) = GPIO.getPulse(gpio).split(':') gpios[gpio][pwmType] = value json['GPIO'] = gpios return types.jsonDumps(json)
def getJSON(self): json = "{" first = True for (alt, value) in FUNCTIONS.items(): if not first: json += ", " json += '"%s": %d' % (alt, value["enabled"]) first = False json += ', "GPIO":{\n' first = True for gpio in range(GPIO.GPIO_COUNT): if not first: json += ", \n" function = GPIO.getFunctionString(gpio) value = GPIO.input(gpio) json += '"%d": {"function": "%s", "value": %d' % (gpio, function, value) if GPIO.getFunction(gpio) == GPIO.PWM: (type, value) = GPIO.getPulse(gpio).split(':') json += ', "%s": %s' % (type, value) json += '}' first = False json += "\n}}" return json
def do_GET(self, relativePath): # JSON full state if relativePath == "*": return (200, self.getJSON(), M_JSON) # RPi header map elif relativePath == "map": json = "%s" % MAPPING[GPIO.BOARD_REVISION] json = json.replace("'", '"') return (200, json, M_JSON) # server version elif relativePath == "version": return (200, SERVER_VERSION, M_PLAIN) # board revision elif relativePath == "revision": revision = "%s" % GPIO.BOARD_REVISION return (200, revision, M_PLAIN) # get temperature elif relativePath == "TEMP": strPath="/home/pi/test/thermostate/lastTemp.txt" f = open(strPath) strText = f.read() return (200, strText, M_PLAIN) # Single GPIO getter elif relativePath.startswith("GPIO/"): (mode, s_gpio, operation) = relativePath.split("/") gpio = int(s_gpio) value = None if operation == "value": if GPIO.input(gpio): value = "1" else: value = "0" elif operation == "function": value = GPIO.getFunctionString(gpio) elif operation == "pwm": if GPIO.isPWMEnabled(gpio): value = "enabled" else: value = "disabled" elif operation == "pulse": value = GPIO.getPulse(gpio) else: return (404, operation + " Not Found", M_PLAIN) return (200, value, M_PLAIN) else: return (0, None, None)
def do_GET(self, relativePath): # JSON full state if relativePath == "*": return (200, self.getJSON(), M_JSON) # RPi header map elif relativePath == "map": json = "%s" % MAPPING[GPIO.BOARD_REVISION] json = json.replace("'", '"') return (200, json, M_JSON) # server version elif relativePath == "version": return (200, SERVER_VERSION, M_PLAIN) # board revision elif relativePath == "revision": revision = "%s" % GPIO.BOARD_REVISION return (200, revision, M_PLAIN) # get temperature elif relativePath == "TEMP": strPath = "/home/pi/test/thermostate/lastTemp.txt" f = open(strPath) strText = f.read() return (200, strText, M_PLAIN) # Single GPIO getter elif relativePath.startswith("GPIO/"): (mode, s_gpio, operation) = relativePath.split("/") gpio = int(s_gpio) value = None if operation == "value": if GPIO.input(gpio): value = "1" else: value = "0" elif operation == "function": value = GPIO.getFunctionString(gpio) elif operation == "pwm": if GPIO.isPWMEnabled(gpio): value = "enabled" else: value = "disabled" elif operation == "pulse": value = GPIO.getPulse(gpio) else: return (404, operation + " Not Found", M_PLAIN) return (200, value, M_PLAIN) else: return (0, None, None)
def do_GET(self): if not self.checkAuthentication(): return relativePath = self.path.replace(self.server.context, "/") if (relativePath.startswith("/")): relativePath = relativePath[1:] # JSON full state if relativePath == "*": self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.server.writeJSON(self.wfile) elif relativePath == "map": json = "%s" % MAPPING[GPIO.BOARD_REVISION] self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(json.replace("'", '"').encode()) # version elif relativePath == "version": self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(SERVER_VERSION.encode()) # Single GPIO getter elif (relativePath.startswith("GPIO/")): (mode, s_gpio, operation) = relativePath.split("/") gpio = int(s_gpio) value = None if (operation == "value"): if GPIO.input(gpio): value = "1" else: value = "0" elif (operation == "function"): value = GPIO.getFunctionString(gpio) elif (operation == "pwm"): if GPIO.isPWMEnabled(gpio): value = "enabled" else: value = "disabled" elif (operation == "pulse"): value = GPIO.getPulse(gpio) else: self.send_error(404, operation + " Not Found") return self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(value.encode()) # handle files else: if relativePath == "": relativePath = self.server.index realPath = relativePath if not os.path.exists(realPath): realPath = self.server.docroot + os.sep + relativePath if not os.path.exists(realPath): self.send_error(404, "Not Found") return realPath = os.path.realpath(realPath) if realPath.endswith(".py"): self.send_error(403, "Not Authorized") return if not (realPath.startswith(self.server.docroot) or realPath.startswith(os.getcwd())): self.send_error(403, "Not Authorized") return if (os.path.isdir(realPath)): realPath += os.sep + self.server.index if not os.path.exists(realPath): self.send_error(403, "Not Authorized") return (type, encoding) = mime.guess_type(realPath) f = open(realPath) data = f.read() f.close() self.send_response(200) self.send_header("Content-type", type) # self.send_header("Content-length", os.path.getsize(realPath)) self.end_headers() try: self.wfile.write(data.encode()) except UnicodeDecodeError: self.wfile.write(data)
def pulseAngle(self, channel, value): self.checkDigitalChannelExported(channel) self.checkPostingValueAllowed() self.checkDigitalChannel(channel) GPIO.pulseAngle(channel, value) return GPIO.getPulse(channel)
def getPulse(self, channel): self.checkDigitalChannelExported(channel) self.checkDigitalChannel(channel) return GPIO.getPulse(channel)
def do_GET(self): if not self.checkAuthentication(): return relativePath = self.path.replace(self.server.context, "/") if (relativePath.startswith("/")): relativePath = relativePath[1:]; # JSON full state if relativePath == "*": self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.server.writeJSON(self.wfile) elif relativePath == "map": json = "%s" % MAPPING[GPIO.BOARD_REVISION] self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(json.replace("'", '"').encode()) # version elif relativePath == "version": self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(SERVER_VERSION.encode()) # Single GPIO getter elif (relativePath.startswith("GPIO/")): (mode, s_gpio, operation) = relativePath.split("/") gpio = int(s_gpio) value = None if (operation == "value"): if GPIO.input(gpio): value = "1" else: value = "0" elif (operation == "function"): value = GPIO.getFunctionString(gpio) elif (operation == "pwm"): if GPIO.isPWMEnabled(gpio): value = "enabled" else: value = "disabled" elif (operation == "pulse"): value = GPIO.getPulse(gpio) else: self.send_error(404, operation + " Not Found") return self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(value.encode()) # handle files else: if relativePath == "": relativePath = self.server.index realPath = relativePath; if not os.path.exists(realPath): realPath = self.server.docroot + os.sep + relativePath if not os.path.exists(realPath): self.send_error(404, "Not Found") return realPath = os.path.realpath(realPath) if realPath.endswith(".py"): self.send_error(403, "Not Authorized") return if not (realPath.startswith(self.server.docroot) or realPath.startswith(os.getcwd())): self.send_error(403, "Not Authorized") return if (os.path.isdir(realPath)): realPath += os.sep + self.server.index; if not os.path.exists(realPath): self.send_error(403, "Not Authorized") return (type, encoding) = mime.guess_type(realPath) f = open(realPath) data = f.read() f.close() self.send_response(200) self.send_header("Content-type", type); # self.send_header("Content-length", os.path.getsize(realPath)) self.end_headers() try: self.wfile.write(data.encode()) except UnicodeDecodeError: self.wfile.write(data)