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 import os
16 import time
17 import socket
18 
19 from webiopi.utils.config import Config
20 from webiopi.utils import loader
21 from webiopi.utils import logger
22 from webiopi.utils import crypto
23 from webiopi.devices import manager
24 from webiopi.protocols import rest
25 from webiopi.protocols import http
26 from webiopi.protocols import coap
27 from webiopi.devices.digital.gpio import NativeGPIO
28 
29 def getLocalIP():
30  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
31  try:
32  s.connect(('8.8.8.8', 53))
33  host = s.getsockname()[0]
34  s.close()
35  return host
36  except:
37  return "localhost"
38 
39 class Server():
40  def __init__(self, port=8000, coap_port=5683, login=None, password=None, passwdfile=None, configfile=None, scriptfile=None):
41  self.host = getLocalIP()
42  self.gpio = NativeGPIO()
43  self.restHandler = rest.RESTHandler()
44  manager.addDeviceInstance("GPIO", self.gpio, [])
45 
46  if configfile != None:
47  logger.info("Loading configuration from %s" % configfile)
48  config = Config(configfile)
49  else:
50  config = Config()
51 
52  self.gpio.addSetups(config.items("GPIO"))
53  self.gpio.addResets(config.items("~GPIO"))
54  self.gpio.setup()
55 
56  devices = config.items("DEVICES")
57  for (name, params) in devices:
58  values = params.split(" ")
59  driver = values[0];
60  args = {}
61  i = 1
62  while i < len(values):
63  (arg, val) = values[i].split(":")
64  args[arg] = val
65  i+=1
66  manager.addDevice(name, driver, args)
67 
68 
69  if scriptfile != None:
70  scriptname = scriptfile.split("/")[-1].split(".")[0]
71  loader.loadScript(scriptname, scriptfile, self.restHandler)
72 
73  scripts = config.items("SCRIPTS")
74  for (name, source) in scripts:
75  loader.loadScript(name, source, self.restHandler)
76 
77  self.restHandler.device_mapping = config.getboolean("REST", "device-mapping", True)
78  self.gpio.post_value = config.getboolean("REST", "gpio-post-value", True)
79  self.gpio.post_function = config.getboolean("REST", "gpio-post-function", True)
80  exports = config.get("REST", "gpio-export", None)
81  if exports != None:
82  self.gpio.export = [int(s) for s in exports.split(",")]
83  self.restHandler.export = self.gpio.export
84 
85  http_port = config.getint("HTTP", "port", port)
86  http_enabled = config.getboolean("HTTP", "enabled", http_port > 0)
87  http_passwdfile = config.get("HTTP", "passwd-file", passwdfile)
88  context = config.get("HTTP", "context", None)
89  docroot = config.get("HTTP", "doc-root", None)
90  index = config.get("HTTP", "welcome-file", None)
91 
92  coap_port = config.getint("COAP", "port", coap_port)
93  coap_enabled = config.getboolean("COAP", "enabled", coap_port > 0)
94  coap_multicast = config.getboolean("COAP", "multicast", coap_enabled)
95 
96  routes = config.items("ROUTES")
97  for (source, destination) in routes:
98  self.restHandler.addRoute(source, destination)
99 
100  auth = None
101  if http_passwdfile != None:
102  if os.path.exists(http_passwdfile):
103  f = open(http_passwdfile)
104  auth = f.read().strip(" \r\n")
105  f.close()
106  if len(auth) > 0:
107  logger.info("Access protected using %s" % http_passwdfile)
108  else:
109  logger.info("Passwd file %s is empty" % http_passwdfile)
110  else:
111  logger.error("Passwd file %s not found" % http_passwdfile)
112 
113  elif login != None or password != None:
114  auth = crypto.encryptCredentials(login, password)
115  logger.info("Access protected using login/password")
116 
117  if auth == None or len(auth) == 0:
118  logger.warn("Access unprotected")
119 
120  realm = config.get("HTTP", "prompt", None)
121 
122  if http_enabled:
123  self.http_server = http.HTTPServer(self.host, http_port, self.restHandler, context, docroot, index, auth, realm)
124  else:
125  self.http_server = None
126 
127  if coap_enabled:
128  self.coap_server = coap.COAPServer(self.host, coap_port, self.restHandler)
129  if coap_multicast:
130  self.coap_server.enableMulticast()
131  else:
132  self.coap_server = None
133 
134  def addMacro(self, macro):
135  self.restHandler.addMacro(macro)
136 
137  def stop(self):
138  if self.http_server:
139  self.http_server.stop()
140  if self.coap_server:
141  self.coap_server.stop()
142  loader.unloadScripts()
143  manager.closeDevices()
144 
145 
146 
def getLocalIP
Definition: __init__.py:29