-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
executable file
·216 lines (195 loc) · 7.92 KB
/
camera.py
File metadata and controls
executable file
·216 lines (195 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python
#
# Install:
# sudo pip install ipaddr
# Issues:
# - Once the node is registered, it's name will not change.
#
# Auth Issues:
# - Needs basic auth: 11.37.2.52
# - Uses digest auth: 11.37.2.59
#
""" Camera Node Server for ISY """
try:
from httplib import BadStatusLine # Python 2.x
except ImportError:
from http.client import BadStatusLine # Python 3.x
from polyglot.nodeserver_api import SimpleNodeServer, PolyglotConnector
from collections import defaultdict, OrderedDict
import os, json, logging, requests, threading, SocketServer, re, socket, yaml
from requests.auth import HTTPDigestAuth,HTTPBasicAuth
# Is there a better way to import these? I'd rather import just this:
import camera_nodes
# And have all camera_nodes defined at the current level? But have to import them all?
from camera_nodes import CameraServer
from camera_polyglot_version import VERSION_MAJOR,VERSION_MINOR
global _REST_HANDLER
class CameraNodeServer(SimpleNodeServer):
""" ISY Helper Camera Node Server """
global _REST_HANDLER
def setup(self):
""" Initial node setup. """
super(SimpleNodeServer, self).setup()
self.logger.info('CameraNodeServer: Version=%s.%s starting up.' % (VERSION_MAJOR,VERSION_MINOR))
self.logger.info("CameraNodeServer: Sandbox=%s" % (self.poly.sandbox))
self.logger.info("CameraNodeServer: Config=%s" % (self.config))
# Setup the config data.
self.get_cam_config()
# define nodes for settings
# Start a simple server for cameras to ping
self.server = start_server(self)
self.manifest = self.config.get('manifest', {})
# Add the top level camera server node.
CameraServer(self, "cams", "Camera Server", self.manifest)
self.update_config()
def get_cam_config(self):
"""
Read the sandbox/config.yaml.
If it does not exist, create a blank template
Make sure necessary settings are set
"""
# The config file.
self.config_file = self.poly.sandbox + "/config.yaml"
# Default configuration paramaters.
default_config = dict(
user = 'YourCameraUserName',
password = 'YourCameraPassword'
)
if not os.path.isfile(self.config_file):
with open(self.config_file, 'w') as outfile:
outfile.write( yaml.dump(default_config, default_flow_style=False) )
msg = 'Created default config file, please edit and set the proper values "%s"' % (self.config_file)
self.logger.error(msg)
raise IOError(msg)
try:
config_h = open(self.config_file, 'r')
except IOError as e:
# Does not exist OR no read permissions, so show error in both logs.
msg = 'Error Unable to open config file "%s"' % (self.config_file)
self.logger.error(msg)
raise IOError(msg)
# TODO: Ok to just let load errors throw an exception?
self.cam_config = yaml.load(config_h)
config_h.close
# Check that user and password are defined.
errors = 0
for param in ('user', 'password'):
if not param in self.cam_config:
self.logger.error("%s not defined in %s" % (param,self.config_file))
errors += 1
elif self.cam_config[param] is None:
self.logger.error("%s is %s in %s" % (param,self.cam_config[param],self.config_file))
errors += 1
if errors > 0:
raise ValueError('Error in config file "%s", see log "%s"' % (self.config_file, self.poly.log_filename))
def poll(self):
""" Poll Camera's """
for node_addr, node in self.nodes.items():
node.poll()
return True
def long_poll(self):
""" Call long_poll on all nodes and Save configuration every 30 seconds. """
self.logger.debug("CameraNodeServer:long_poll")
self.update_config()
for node_addr, node in self.nodes.items():
node.long_poll()
return True
def on_exit(self, **kwargs):
self.server.socket.close()
return True
def send_error(self,error_str):
self.logger.error(error_str)
self.poly.send_error(error_str);
def motion(self,address,value):
""" Poll Camera's """
self.logger.info("Got Motion for node %s '%s'" % (address, value) )
if address in self.nodes:
return self.nodes[address].motion(value)
else:
self.send_error("No node for motion on address %s" % (address));
return False
def http_get(self,ip,port,user,password,path,payload,auth_mode=0):
url = "http://{}:{}/{}".format(ip,port,path)
self.logger.debug("http_get: Sending: %s %s auth_mode=%d" % (url, payload, auth_mode) )
if auth_mode == 0:
auth = HTTPBasicAuth(user,password)
elif auth_mode == 1:
auth = HTTPDigestAuth(user,password)
else:
self.send_error("Unknown auth_mode '%s' for request '%s'. Must be 0 for 'digest' or 1 for 'basic'." % (auth_mode, url) )
return False
try:
response = requests.get(
url,
auth=auth,
params=payload,
timeout=5
)
# This is supposed to catch all request excpetions.
except requests.exceptions.RequestException as e:
self.send_error("Connection error for %s: %s" % (url, e))
return False
self.logger.debug("http_get: Got: code=%s", response.status_code)
if response.status_code == 200:
#self.logger.debug("http_get: Got: text=%s", response.text)
return response.text
elif response.status_code == 400:
self.send_error("Bad request: %s" % (url) )
elif response.status_code == 404:
self.send_error("Not Found: %s" % (url) )
elif response.status_code == 401:
# Authentication error
self.send_error(
"Failed to authenticate, please check your username and password")
else:
self.send_error("Unknown response %s: %s" % (response.status_code, url) )
return False
class EchoRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
try:
# Echo the back to the client
data = self.request.recv(1024)
# Don't worry about a status for now, just echo back.
self.request.sendall(data)
# Then parse it.
myhandler(data)
except:
_SERVER.poly.send_error("echo_request_handler failed")
return
def get_network_ip(rhost):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((rhost, 0))
return s.getsockname()[0]
except:
_SERVER.poly.send_error("get_network_ip: Failed to open socket to " + rhost)
return False
def start_server(handler):
global _REST_HANDLER
myip = get_network_ip('8.8.8.8')
address = (myip, 0) # let the kernel give us a port
_REST_HANDLER = handler;
server = SocketServer.TCPServer(address, EchoRequestHandler)
#print "Running on: %s:%s" % server.server_address
t = threading.Thread(target=server.serve_forever)
#t.setDaemon(True) # don't hang on exit
t.start()
return server
def myhandler(data):
#print "got: {}".format(data.strip())
match = re.match( r'GET /motion/(.*) ', data, re.I)
if match:
address = match.group(1)
_REST_HANDLER.motion(address,1)
else:
_REST_HANDLER.poly.send_error("Unrecognized socket server command: " + data)
def main():
""" setup connection, node server, and nodes """
poly = PolyglotConnector()
nserver = CameraNodeServer(poly, shortpoll=30, longpoll=300)
poly.connect()
poly.wait_for_config()
nserver.setup()
nserver.run()
if __name__ == "__main__":
main()