21 import http.client
as httplib
26 def __init__(self, host, port=8000, coap=5683):
48 response = self.coapclient.sendRequest(
COAPGet(
"coap://%s:%d%s" % (self.
host, self.
coapport, uri)))
49 elif method ==
"POST":
50 response = self.coapclient.sendRequest(
COAPPost(
"coap://%s:%d%s" % (self.
host, self.
coapport, uri)))
53 return str(response.payload)
56 print(
"No CoAP response, fall-back to HTTP")
60 print(
"Too many CoAP failure forcing HTTP")
65 headers[
"Authorization"] = self.
auth
67 self.httpclient.request(method, uri,
None, headers)
68 response = self.httpclient.getresponse()
69 if response.status == 200:
70 data = response.read()
72 elif response.status == 401:
73 raise Exception(
"Missing credentials")
75 raise Exception(
"Unhandled HTTP Response %d %s" % (response.status, response.reason))
77 raise Exception(
"No data received")
81 PiMixedClient.__init__(self, host, port, -1)
85 PiMixedClient.__init__(self, host, -1, port)
89 PiMixedClient.__init__(self,
"224.0.1.123", -1, port)
97 return self.client.sendRequest(method, self.
path + path)
101 RESTAPI.__init__(self, client,
"/macros/" + name +
"/")
104 values =
",".join([
"%s" % i
for i
in args])
111 RESTAPI.__init__(self, client,
"/devices/" + name +
"/" + category)
115 Device.__init__(self, client, name,
"digital")
118 return self.
sendRequest(
"GET",
"/%d/function" % channel)
121 return self.
sendRequest(
"POST",
"/%d/function/%s" % (channel, func))
124 return int(self.
sendRequest(
"GET",
"/%d/value" % channel))
127 return int(self.
sendRequest(
"POST",
"/%d/value/%d" % (channel, value)))
133 return int(self.
sendRequest(
"POST",
"/integer/%d" % value))
137 RESTAPI.__init__(self, client,
"/GPIO")
141 Device.__init__(self, client, name,
"analog")
144 return float(self.
sendRequest(
"GET",
"/%d/integer" % channel))
147 return float(self.
sendRequest(
"GET",
"/%d/float" % channel))
150 return float(self.
sendRequest(
"GET",
"/%d/volt" % channel))
154 Device.__init__(self, client, name,
"analog")
157 return float(self.
sendRequest(
"POST",
"/%d/integer/%d" % (channel, value)))
160 return float(self.
sendRequest(
"POST",
"/%d/float/%f" % (channel, value)))
163 return float(self.
sendRequest(
"POST",
"/%d/volt/%f" % (channel, value)))
167 Device.__init__(self, client, name,
"pwm")
170 return float(self.
sendRequest(
"GET",
"/%d/angle" % (channel)))
173 return float(self.
sendRequest(
"POST",
"/%d/angle/%f" % (channel, value)))
177 Device.__init__(self, client, name,
"sensor")
181 return float(self.
sendRequest(
"GET",
"/temperature/k"))
184 return float(self.
sendRequest(
"GET",
"/temperature/c"))
187 return float(self.
sendRequest(
"GET",
"/temperature/f"))
191 return float(self.
sendRequest(
"GET",
"/pressure/pa"))
194 return float(self.
sendRequest(
"GET",
"/pressure/hpa"))
198 return float(self.
sendRequest(
"GET",
"/luminosity/lux"))
202 return float(self.
sendRequest(
"GET",
"/distance/mm"))
205 return float(self.
sendRequest(
"GET",
"/distance/cm"))
208 return float(self.
sendRequest(
"GET",
"/distance/in"))