24 import _webiopi.GPIO
as GPIO
39 self.
macros[macro.__name__] = macro
44 if destination[0] ==
"/":
45 destination = destination[1:]
46 self.
routes[source] = destination
47 logger.info(
"Added Route /%s => /%s" % (source, destination))
51 if path.startswith(source):
52 route = path.replace(source, self.
routes[source])
53 logger.info(
"Routing /%s => /%s" % (path, route))
57 def extract(self, fmtArray, pathArray, args):
58 if len(fmtArray) != len(pathArray):
60 if len(fmtArray) == 0:
65 return self.
extract(fmtArray[1:], pathArray[1:], args)
66 if fmt.startswith(
"%"):
77 raise Exception(
"Missing closing brace")
84 args[name] = types.str2bool(path)
86 args[name] = types.toint(path)
88 args[name] = int(path, 16)
90 args[name] = float(path)
92 raise Exception(
"Unknown format type : %s" % t)
94 return self.
extract(fmtArray[1:], pathArray[1:], args)
99 pathArray = path.split(
"/")
100 deviceName = pathArray[0]
101 device = instance.DEVICES[deviceName]
103 return (
None, deviceName +
" Not Found")
104 pathArray = pathArray[1:]
105 funcs = device[
"functions"][method]
106 functionName =
"/".join(pathArray)
107 if functionName
in funcs:
108 return (funcs[functionName], {})
112 funcPathArray = func.path.split(
"/")
114 if self.
extract(funcPathArray, pathArray, args):
117 return (
None, functionName +
" Not Found")
122 return (404, args, M_PLAIN)
124 if func.data !=
None:
125 args[func.data] = data
127 result = func(**args)
131 if hasattr(func,
"contentType"):
132 contentType = func.contentType
133 if contentType == M_JSON:
134 response = types.jsonDumps(result)
136 response = func.format % result
140 return (200, response, contentType)
142 def do_GET(self, relativePath, compact=False):
143 relativePath = self.
findRoute(relativePath)
146 if relativePath ==
"*":
147 return (200, self.
getJSON(compact), M_JSON)
150 elif relativePath ==
"map":
151 json =
"%s" % MAPPING
152 json = json.replace(
"'",
'"')
153 return (200, json, M_JSON)
156 elif relativePath ==
"version":
157 return (200, VERSION_STRING, M_PLAIN)
160 elif relativePath ==
"revision":
161 revision =
"%s" % BOARD_REVISION
162 return (200, revision, M_PLAIN)
165 elif relativePath.startswith(
"GPIO/"):
168 elif relativePath ==
"devices/*":
169 return (200, manager.getDevicesJSON(compact), M_JSON)
171 elif relativePath.startswith(
"devices/"):
173 return (404,
None,
None)
174 path = relativePath.replace(
"devices/",
"")
178 return (0,
None,
None)
180 def do_POST(self, relativePath, data, compact=False):
181 relativePath = self.
findRoute(relativePath)
183 if relativePath.startswith(
"GPIO/"):
186 elif relativePath.startswith(
"macros/"):
187 paths = relativePath.split(
"/")
195 macro = self.
macros[mname]
198 args = value.split(
',')
199 result =
macro(*args)
201 result =
macro(value)
207 response =
"%s" % result
208 return (200, response, M_PLAIN)
211 return (404, mname +
" Not Found", M_PLAIN)
213 elif relativePath.startswith(
"devices/"):
215 return (404,
None,
None)
216 path = relativePath.replace(
"devices/",
"")
220 return (0,
None,
None)
231 for (bus, value)
in BUSLIST.items():
232 json[bus] = int(value[
"enabled"])
238 export = range(GPIO.GPIO_COUNT)
243 gpios[gpio][f] = GPIO.getFunction(gpio)
245 gpios[gpio][f] = GPIO.getFunctionString(gpio)
246 gpios[gpio][v] = int(GPIO.input(gpio))
248 if GPIO.getFunction(gpio) == GPIO.PWM:
249 (pwmType, value) = GPIO.getPulse(gpio).split(
':')
250 gpios[gpio][pwmType] = value
253 return types.jsonDumps(json)