1
1
"""ownet protocol implementation
2
2
3
- This module provides classes to query an owserver via the ownet protocol.
4
- It is a pure python implementation, with no external dependencies .
3
+ This module is a pure python, low level implementation of the ownet
4
+ protocol .
5
5
6
- A proxy object is provided that implements methods for the protocol
7
- commands .
6
+ OwnetProxy instances are proxy objects whose methods correspond to ownet
7
+ protocol messages .
8
8
9
9
>>> owproxy = OwnetProxy(host="owserver.example.com", port=4304)
10
10
>>> owproxy.ping()
18
18
>>> owproxy.dir()
19
19
['/sensA/', '/05.4AEC29CDBAAB/']
20
20
21
- The lowlevel OwnetConnection encapsulates all socket operations and
22
- interactions with the server.
21
+ The OwnetConnection class encapsulates all socket operations and
22
+ interactions with the server and is mean for internal use .
23
23
24
24
"""
25
25
26
+ #
27
+ # Copyright 2013, 2014 Stefano Miccoli
28
+ #
29
+ # This python package is free software: you can redistribute it and/or modify
30
+ # it under the terms of the GNU General Public License as published by
31
+ # the Free Software Foundation, either version 3 of the License, or
32
+ # (at your option) any later version.
33
+ #
34
+ # This program is distributed in the hope that it will be useful,
35
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
36
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37
+ # GNU General Public License for more details.
38
+ #
39
+ # You should have received a copy of the GNU General Public License
40
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
41
+ #
42
+
26
43
from __future__ import print_function
27
44
28
45
import struct
29
46
import socket
30
- import collections
47
+ # import collections
31
48
32
49
# see msg_classification from ow_message.h
33
50
MSG_ERROR = 0
86
103
87
104
def str2bytez (s ):
88
105
"transform string to zero-terminated bytes"
89
- return s .encode () + b'\x00 '
106
+ return s .encode ('ascii' ) + b'\x00 '
90
107
91
- class _dummy (collections .Sequence ):
92
- # dummy list, every item is an empty string
93
- __len__ = lambda self : 0
94
- __getitem__ = lambda self , i : ''
108
+ def bytes2str (b ):
109
+ "transform bytes to string"
110
+ return b .decode ('ascii' )
111
+
112
+
113
+ #class _dummy(collections.Sequence):
114
+ # # dummy list, every item is an empty string
115
+ # __len__ = lambda self: 0
116
+ # __getitem__ = lambda self, i: ''
95
117
96
118
#
97
119
# exceptions
@@ -329,10 +351,11 @@ def __init__(self, host='localhost', port=4304, flags=0,
329
351
self .ping ()
330
352
331
353
#self.errmess = _dummy()
354
+
332
355
# fetch errcodes array from owserver
333
356
errcodes = '/settings/return_codes/text.ALL'
334
357
assert self .present (errcodes )
335
- self .errmess = self .read (errcodes ). decode ( ).split (',' )
358
+ self .errmess = bytes2str ( self .read (errcodes )).split (',' )
336
359
337
360
def sendmess (self , type , payload , flags = 0 , size = 0 , offset = 0 ):
338
361
""" retcode, data = sendmess(type, payload)
@@ -358,7 +381,7 @@ def ping(self):
358
381
raise OwnetError (- ret , self .errmess [- ret ])
359
382
360
383
def present (self , path ):
361
- "returns True if there is an entity as path"
384
+ "returns True if there is an entity at path"
362
385
363
386
ret , data = self .sendmess (MSG_PRESENCE , str2bytez (path ))
364
387
assert ret <= 0
@@ -384,7 +407,7 @@ def dir(self, path='/', slash=True, bus=False):
384
407
if ret < 0 :
385
408
raise OwnetError (- ret , self .errmess [- ret ], path )
386
409
if data :
387
- return data . decode ( ).split (',' )
410
+ return bytes2str ( data ).split (',' )
388
411
else :
389
412
return []
390
413
@@ -413,19 +436,19 @@ def write(self, path, data):
413
436
if ret < 0 :
414
437
raise OwnetError (- ret , self .errmess [- ret ], path )
415
438
416
- def _test ():
417
- proxy = OwnetProxy ()
418
- proxy . ping ()
419
- assert not proxy . present ( '/nonexistent' )
420
- sensors = proxy . dir ( '/' , bus = False )
421
- for j , i in enumerate ( sensors ):
422
- assert proxy . present ( i ), i
423
- stype = proxy . read ( i + 'type' )
424
- if proxy . present ( i + 'temperature' ):
425
- temp = proxy .read ( i + 'temperature' )
426
- else :
427
- temp = ''
428
- print ( i , stype , temp )
439
+ def _main ():
440
+ # print sensors on localhost owserver
441
+ try :
442
+ proxy = OwnetProxy ( )
443
+ except ConnError :
444
+ print ( "No owserver on localhost" )
445
+ return 1
446
+ print ( "owserver directory on localhost:" )
447
+ print ( "id" . center ( 17 ), "type" . center ( 7 ))
448
+ for sensor in proxy .dir ( slash = False , bus = False ):
449
+ stype = proxy . read ( sensor + '/type' )
450
+ print ( sensor . ljust ( 17 ), stype . ljust ( 7 ))
451
+ return 0
429
452
430
453
if __name__ == '__main__' :
431
- _test ()
454
+ _main ()
0 commit comments