-
Notifications
You must be signed in to change notification settings - Fork 2
/
sjcam.py
144 lines (124 loc) · 4.15 KB
/
sjcam.py
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
"""
SJCAM camera library
Send command to the camera, via WIFI, to configure it and to read parameters
Basic functions are:
- sync camera date/time with the computer
- load presets of settings to the camera
- download media files from camera
"""
import simpleyaml as yaml
import requests
class sjcamCommand(object):
"""command for sjcam4000"""
def __init__(self, id, description="unknown", parameters=[],
readOnly=False, format=""):
super(sjcamCommand, self).__init__()
self.id = int(id)
self.description = description
self.parameters = parameters
self.readOnly = readOnly
self.format = format
fnParts = self.description.split(' ')
fnParts[0] = fnParts[0].lower()
fnParts[1:] = [f.capitalize() for f in fnParts[1:]]
self.fn = ''.join(fnParts)
def paramInt(self, paramStr):
"""
Find the ID of a parameter, given the text description
"""
for p in self.parameters:
if paramStr.lower() == p.lower():
return self.parameters.index(p)
return None
def paramString(self, paramInt):
"""
Find the text description of a parameter, given the ID
"""
if len(self.parameters) > paramInt:
return self.parameters[paramInt]
return None
def __repr__(self):
return r"<%s(id=%d, description='%s')>" % (self.__class__.__name__,
self.id,
self.description)
def __str__(self):
return "%d: %s" % (self.id,
self.description)
class sjcam(object):
"""sjcam connection library"""
address = "192.168.1.254"
url = "http://%s/" % address
cmdUrl = "%s?custom=1&cmd=%%d&par=%%d" % url
rtsp = "rtsp://%s/sjcam.mov" % address
def __init__(self):
super(sjcam, self).__init__()
self.commands = []
print "parsing yaml"
with open('sjcam.yaml', 'r') as yamlFile:
commands = yaml.load(yamlFile)
print commands
for id, values in commands['commands'].items():
self.commands.append(sjcamCommand(id=id,
description=values.get('description'),
parameters=values.get('parameter', []),
readOnly=values.get('readOnly', False),
format=values.get('format', '')))
def getCommand(self, command, parameter=None):
"""
Prepare a command to execute
"""
cmd = [c for c in self.commands if c.description == command or c.fn == command] # NOQA
if cmd > 0:
cmd = cmd[0]
if parameter in cmd.parameters:
paramId = cmd.parameters.index(parameter)
else:
paramId = None
def httpCall(self, method="GET", cmdId=None, paramId=None):
"""
Generic method to do an HTTP call
@cmdId - numeric id of the command to issue
@parameter - numeric id of the parameter
"""
if cmdId is not None:
data = {"custom": 1, "cmd": cmdId} # , "par": parameter}
if paramId is not None:
data["par"] = paramId
print method, self.url, data
try:
r = requests.request(method=method, url=self.url,
data=data, timeout=5)
except requests.exceptions.ConnectTimeout:
return None
return self.parseResponse(r)
def httpPing(self):
"""
Check if camera is available
"""
r = self.httpCall(method='HEAD')
print r
if r is not None:
return r.ok
def parseResponse(self, response):
"""
<?xml version="1.0" encoding="UTF-8" ?>
<Function>
<Cmd>2003</Cmd>
<Status>0</Status>
</Function>
returns
{'Cmd': 2003, 'Status': 0}
"""
root = ET.fromstring(response)
response = {}
for child in root:
response[child.tag] = child.text
return response
if __name__ == "__main__":
print "SJcam lib"
cam = sjcam()
# print "Ping"
# if cam.httpPing():
# print "Camera available"
# else:
# print "Check connection to camera"