Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/work'
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayRag committed May 21, 2017
2 parents 9df40fb + c4b302a commit 611c172
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ tmp.py
__pycache__
build
dist
*.do
*.do.cfg
*.do.state
.gitignore
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Differences from original API:
- 'stopRecording' and 'capturePhoto' waits till operation is really ended and return produced file name.
- Values provided to commands should be correct strings, compared to integers in original API.
- 'adapter', 'adapter_status' and 'battery_status' callbacks are available in addition to all other. .setCB() used to set/get callbacks.
- 'deleteFile' and 'getFileList' commands require path relative to DCIM folder.

Try not to call same commands simultaneously from different threads: camera response is not clearly identified with caller and response can be mixed.

Expand All @@ -15,9 +16,6 @@ Try not to call same commands simultaneously from different threads: camera resp
- formatSDCard
NSFW

- deleteFile
Considered vulnerable, maybe later

- downloadFile
- cancelDownload
Redundant, available by http
Expand All @@ -29,4 +27,4 @@ Try not to call same commands simultaneously from different threads: camera resp
Maybe later

- startRecording datetime
Lazy to implement
Lazy to implement due to specific input value format
27 changes: 27 additions & 0 deletions Yi4kAPI.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-feature 1: +0 "yiClasses.py" kii 17/01/12 17:35:58
add error constants

feature 2: +0 "yiClasses.py" kii 17/01/12 21:23:35
implement nonblocking execution, use callbacks

!feature 3: +0 "yiClasses.py" kii 17/05/21 04:42:41
predefined limit of supplied variables

!feature 4: +0 "" kii 17/01/15 05:17:26
redundant

!feature 5: +0 "yiClasses.py" kii 17/04/02 15:11:56
continously recieve result

+ux 6: +0 "__init__.py" kii 17/05/21 04:41:47
add sample usecase

+ 8: +0 "" kii 17/05/21 04:41:19
detect disconnect

+check 9: +0 "yiAPI.py" kii 17/05/21 04:41:21
test if disconnected

+fix 10: +0 "__init__.py" kii 17/05/21 04:41:44
getFileList not listing all files

24 changes: 12 additions & 12 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
'''
Lightweighted version of Yi4k API, reverse-engineered from official Java API.
It is limited so:
- values provided to commands should be correct strings,
- camera data exchanging operations are blocking,
- no callbacks are supported.
Lightweighted version of Yi4k API, based on official Java API.
Values provided to commands should be correct strings.
deleteFile and getFileList commands require path relative to DCIM folder.
deleteFile though could be unsafe by supplying '..' path.
Commands not implemented:
formatSDCard
NSFW
deleteFile
Considered vulnerable, maybe later
downloadFile
cancelDownload
Redundant, available by http
Expand All @@ -24,8 +21,7 @@
Maybe later
startRecording datetime
Lazy to implement
Lazy to implement due to specific input value format
'''


Expand All @@ -48,11 +44,15 @@
)

getFileList= YiAPICommandGen(1282, 'getFileList',
params= {'param':'/tmp/fuse_d'},
params= {'param':'/tmp/fuse_d/DCIM/'},
variable= 'param',
resultCB= lambda res: res['listing']
)

#deleteFile= YiAPICommandGen(1281, 'deleteFile', {}, {'param': '/tmp/fuse_d/DCIM'}, resultCB= lambda res: res['listing'])
deleteFile= YiAPICommandGen(1281, 'deleteFile',
params= {'param':'/tmp/fuse_d/DCIM/'},
variable= 'param'
)

startViewFinder= YiAPICommandGen(259, 'startViewFinder')
stopViewFinder= YiAPICommandGen(260, 'stopViewFinder')
Expand Down
7 changes: 7 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v1.4
add:
- handle connection loss
- deleteFile command, path is relative to DCIM folder
fix:
- getFileList accepts path relative to DCIM folder

v1.3
add:
- 'start_album' and 'stop_album' events.
Expand Down
6 changes: 4 additions & 2 deletions test/testApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def set_ok(_res):
print('getSettings: %s' % str(res))
res= a.cmd(stopRecording)
print('(err:stopped) stopRecording: %s' % str(res))
time.sleep(40)
print('=============Paused to test manual recording tart/stop and shange settings')
print('=============Paused to test manual recording start/stop and shange settings')
time.sleep(20)
print('')


Expand All @@ -49,6 +49,8 @@ def set_ok(_res):

res= a.cmd(stopRecording)
print('stopRecording: %s' % str(res))
res= a.cmd(deleteFile, str(res).split('/tmp/fuse_d/DCIM/')[1])
print('deleted: %s' % str(res))
res= a.cmd(setVideoExposureValue, resEv)
print('setVideoExposureValue: %s' % str(res))
time.sleep(4)
Expand Down
13 changes: 11 additions & 2 deletions yiAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def cmd(self, _command, _val=None):
timeoutCmd= threading.Timer(self.commandTimeout, runCmd.blockingEvent.set)
timeoutCmd.start()

self.cmdSend(runCmd.cmdSend)
if not self.cmdSend(runCmd.cmdSend):
logging.critical('Socket error while sending')

runCmd.blockingEvent.set()
self.sock= None

runCmd.blockingEvent.wait()
timeoutCmd.cancel()
Expand All @@ -108,9 +112,14 @@ def cmd(self, _command, _val=None):
def cmdSend(self, _cmdDict):
logging.debug("Send %s" % _cmdDict)

self.sock.sendall( bytes(json.dumps(_cmdDict),'ascii') )
try:
self.sock.sendall( bytes(json.dumps(_cmdDict),'ascii') )
except:
return

self.tick+= 1
return True



def setCB(self, _type=None, _cb=None):
Expand Down
11 changes: 8 additions & 3 deletions yiAPICommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
dict of non-changing parameters.
variable
name or list of names to be assigned later with apply()
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
Expand Down Expand Up @@ -44,7 +45,7 @@ def __init__(self,
if params:
self.params.update(params)

if not isinstance(variable, list) and not isinstance(variable, tuple):
if not isinstance(variable, (list, tuple)):
variable= [variable]

self.variable= variable
Expand Down Expand Up @@ -75,7 +76,11 @@ def makeCmd(self, _cmdPrep, _val=None):
_val= [_val]

for pair in zip(self.variable,_val):
_cmdPrep[pair[0]]= pair[1]
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]


return YiAPICommand(_cmdPrep, self.resultReq, self.resultCB)
Expand Down
2 changes: 1 addition & 1 deletion yiAPIListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


'''
Constantly listen to opened Yi4k camera for sultable response.
Constantly listen to opened Yi4k camera for suitable response.
Most of response will be the result of commands sent to camera, while some of the responce
is camera state, pushed periodically.
'''
Expand Down

0 comments on commit 611c172

Please sign in to comment.