Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
__init__.py
Go to the documentation of this file.
1 # Copyright 2012-2013 Eric Ptak - trouch.com
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 from webiopi.utils.logger import LOGGER
16 from webiopi.utils.version import PYTHON_MAJOR
17 from webiopi.utils.crypto import encodeCredentials
18 from webiopi.protocols.coap import COAPClient, COAPGet, COAPPost, COAPPut, COAPDelete
19 
20 if PYTHON_MAJOR >= 3:
21  import http.client as httplib
22 else:
23  import httplib
24 
25 class PiMixedClient():
26  def __init__(self, host, port=8000, coap=5683):
27  self.host = host
28  if coap > 0:
29  self.coapport = coap
31  else:
32  self.coapclient = None
33  if port > 0:
34  self.httpclient = httplib.HTTPConnection(host, port)
35  else:
36  self.httpclient = None
37  self.forceHttp = False
38  self.coapfailure = 0
39  self.maxfailure = 2
40  self.auth= None;
41 
42  def setCredentials(self, login, password):
43  self.auth = "Basic " + encodeCredentials(login, password)
44 
45  def sendRequest(self, method, uri):
46  if self.coapclient != None and not self.forceHttp:
47  if method == "GET":
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)))
51 
52  if response:
53  return str(response.payload)
54  elif self.httpclient != None:
55  self.coapfailure += 1
56  print("No CoAP response, fall-back to HTTP")
57  if (self.coapfailure > self.maxfailure):
58  self.forceHttp = True
59  self.coapfailure = 0
60  print("Too many CoAP failure forcing HTTP")
61 
62  if self.httpclient != None:
63  headers = {}
64  if self.auth != None:
65  headers["Authorization"] = self.auth
66 
67  self.httpclient.request(method, uri, None, headers)
68  response = self.httpclient.getresponse()
69  if response.status == 200:
70  data = response.read()
71  return data
72  elif response.status == 401:
73  raise Exception("Missing credentials")
74  else:
75  raise Exception("Unhandled HTTP Response %d %s" % (response.status, response.reason))
76 
77  raise Exception("No data received")
78 
80  def __init__(self, host, port=8000):
81  PiMixedClient.__init__(self, host, port, -1)
82 
84  def __init__(self, host, port=5683):
85  PiMixedClient.__init__(self, host, -1, port)
86 
88  def __init__(self, port=5683):
89  PiMixedClient.__init__(self, "224.0.1.123", -1, port)
90 
91 class RESTAPI():
92  def __init__(self, client, path):
93  self.client = client
94  self.path = path
95 
96  def sendRequest(self, method, path):
97  return self.client.sendRequest(method, self.path + path)
98 
99 class Macro(RESTAPI):
100  def __init__(self, client, name):
101  RESTAPI.__init__(self, client, "/macros/" + name + "/")
102 
103  def call(self, *args):
104  values = ",".join(["%s" % i for i in args])
105  if values == None:
106  values = ""
107  return self.sendRequest("POST", values)
108 
110  def __init__(self, client, name, category):
111  RESTAPI.__init__(self, client, "/devices/" + name + "/" + category)
112 
113 class GPIO(Device):
114  def __init__(self, client, name):
115  Device.__init__(self, client, name, "digital")
116 
117  def getFunction(self, channel):
118  return self.sendRequest("GET", "/%d/function" % channel)
119 
120  def setFunction(self, channel, func):
121  return self.sendRequest("POST", "/%d/function/%s" % (channel, func))
122 
123  def digitalRead(self, channel):
124  return int(self.sendRequest("GET", "/%d/value" % channel))
125 
126  def digitalWrite(self, channel, value):
127  return int(self.sendRequest("POST", "/%d/value/%d" % (channel, value)))
128 
129  def portRead(self):
130  return int(self.sendRequest("GET", "/integer"))
131 
132  def portWrite(self, value):
133  return int(self.sendRequest("POST", "/integer/%d" % value))
134 
136  def __init__(self, client):
137  RESTAPI.__init__(self, client, "/GPIO")
138 
139 class ADC(Device):
140  def __init__(self, client, name):
141  Device.__init__(self, client, name, "analog")
142 
143  def read(self, channel):
144  return float(self.sendRequest("GET", "/%d/integer" % channel))
145 
146  def readFloat(self, channel):
147  return float(self.sendRequest("GET", "/%d/float" % channel))
148 
149  def readVolt(self, channel):
150  return float(self.sendRequest("GET", "/%d/volt" % channel))
151 
152 class DAC(ADC):
153  def __init__(self, client, name):
154  Device.__init__(self, client, name, "analog")
155 
156  def write(self, channel, value):
157  return float(self.sendRequest("POST", "/%d/integer/%d" % (channel, value)))
158 
159  def writeFloat(self, channel, value):
160  return float(self.sendRequest("POST", "/%d/float/%f" % (channel, value)))
161 
162  def writeVolt(self, channel, value):
163  return float(self.sendRequest("POST", "/%d/volt/%f" % (channel, value)))
164 
165 class PWM(DAC):
166  def __init__(self, client, name):
167  Device.__init__(self, client, name, "pwm")
168 
169  def readAngle(self, channel, value):
170  return float(self.sendRequest("GET", "/%d/angle" % (channel)))
171 
172  def writeAngle(self, channel, value):
173  return float(self.sendRequest("POST", "/%d/angle/%f" % (channel, value)))
174 
175 class Sensor(Device):
176  def __init__(self, client, name):
177  Device.__init__(self, client, name, "sensor")
178 
180  def getKelvin(self):
181  return float(self.sendRequest("GET", "/temperature/k"))
182 
183  def getCelsius(self):
184  return float(self.sendRequest("GET", "/temperature/c"))
185 
186  def getFahrenheit(self):
187  return float(self.sendRequest("GET", "/temperature/f"))
188 
190  def getPascal(self):
191  return float(self.sendRequest("GET", "/pressure/pa"))
192 
193  def getHectoPascal(self):
194  return float(self.sendRequest("GET", "/pressure/hpa"))
195 
197  def getLux(self):
198  return float(self.sendRequest("GET", "/luminosity/lux"))
199 
201  def getMillimeter(self):
202  return float(self.sendRequest("GET", "/distance/mm"))
203 
204  def getCentimeter(self):
205  return float(self.sendRequest("GET", "/distance/cm"))
206 
207  def getInch(self):
208  return float(self.sendRequest("GET", "/distance/in"))
209 
def encodeCredentials
Definition: crypto.py:5