forked from NikolayRag/Yi4kAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyiAPICommand.py
185 lines (130 loc) · 3.61 KB
/
yiAPICommand.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
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
import logging
'''
Public available commands.
'''
commands= []
'''
Class usable to pass to YiAPI.cmd()
.makeCmd() is called then to generate runtime command object.
params
dict of non-changing parameters.
variable
name or list of names to be assigned later with makeCmd().
If variable is defined in params as string, it's appended.
'''
class YiAPICommandGen():
resultReq= None
resultCB= None
commandName= ''
params= None
variable= None
values= None #available values list to assign to .variable. Logging use only.
def __init__(self,
_id,
commandName='',
values=None,
params=None,
variable=[],
resultReq=None,
resultCB=None
):
self.resultReq= resultReq
self.resultCB= resultCB
self.params= {'msg_id':int(_id)}
if params:
self.params.update(params)
if not isinstance(variable, (list, tuple)):
variable= [variable]
self.variable= variable
if values:
self.values= values
if commandName:
self.commandName= commandName
commands.append(self)
'''
Create object representing command at runtime.
Append stored params to provided dict and apply _val to stored .variable respectively
Return YiAPICommand.
'''
def makeCmd(self, _cmdPrep, _val=None):
_cmdPrep.update(self.params)
#assign provided _val[] values to stored .variable[] parameters
if not isinstance(_val, list) and not isinstance(_val, tuple):
_val= [_val]
if self.variable == [] and _val != [None]: #raw
for key in _val[0]:
_cmdPrep[key] = _val[0][key]
else:
for pair in zip(self.variable,_val):
if pair[0] in _cmdPrep and isinstance(_cmdPrep[pair[0]], str):
if isinstance(pair[1], str):
_cmdPrep[pair[0]]+= pair[1]
else:
_cmdPrep[pair[0]]= pair[1]
print('_cmdPrep 2', _cmdPrep)
return YiAPICommand(_cmdPrep, self.resultReq, self.resultCB)
import threading
'''
Runtime command class. Lives from command send to command response.
'''
class YiAPICommand():
cmdSend= None
resultReq= None
resultCB= None
blockingCnt= 1
blockingCB= None
blockingEvent= None
resultDict= None
def __init__(self, _cmdSend, _resultReq, _resultCB):
self.cmdSend= _cmdSend
self.resultDict= {}
self.resultReq= _resultReq
self.resultCB= _resultCB
if _resultReq:
self.blockingCnt= 2
self.blockingCB, self.blockingEvent = self.blockingCBGen()
def result(self):
# print('commandSend', self.cmdSend)
if not 'rval' in self.resultDict:
logging.error('Camera timeout')
return -99998
if self.resultDict['rval']:
logging.warning('Camera error %d' % self.resultDict['rval'])
return self.resultDict['rval']
if callable(self.resultCB):
return self.resultCB(self.resultDict)
if 'msg_id' in self.cmdSend: #settingOptions
print('cmdsend',self.cmdSend)
if self.cmdSend['msg_id'] == 9:
return self.resultDict
if 'param' in self.resultDict:
return self.resultDict['param']
'''
Generate callback suitable for supplying to YiAPIListener.assing()
and Event fired at callback call.
'''
def blockingCBGen(self):
cbEvent= threading.Event()
def func(_res):
self.resultDict.update(_res)
self.blockingCnt-= 1
if ('rval' in _res) and (_res['rval']):
self.blockingCnt= 0
if not self.blockingCnt:
cbEvent.set()
return True
return (func, cbEvent)
'''
Check if Yi response matches command callback conditions
'''
def cbMatch(self, _res):
matchTmpl= [{'msg_id': self.cmdSend['msg_id']}]
if self.resultReq:
matchTmpl.append(self.resultReq)
for cMatch in matchTmpl:
isMatch= True
for cField in cMatch:
if (cField not in _res) or (cMatch[cField]!=_res[cField]):
isMatch= False
if isMatch:
return True