basic/display_stream/websocket_client.py

See examples/basic/display-stream

1 #
2 # ZentriOS SDK LICENSE AGREEMENT | Zentri.com, 2015.
3 #
4 # Use of source code and/or libraries contained in the ZentriOS SDK is
5 # subject to the Zentri Operating System SDK license agreement and
6 # applicable open source license agreements.
7 
8 # Python is available from http://python.org
9 # Tested with Python 2.7
10 #
11 # This example uses the python websocket client
12 # http://pypi.python.org/pypi/websocket-client
13 
14 # Name of the ZentriOS device
15 SERVER_ADDRESS = 'display_stream.local'
16 
17 from websocket import create_connection
18 import json
19 
20 # Open a connection to the device
21 ws = create_connection("ws://%s/zapstreams" % SERVER_ADDRESS)
22 
23 # Create, format and send some data to the device
24 data = [128, 64, 32, 16, 8, 4, 2, 1, 100] # Frame is a diagonal line top left to bottom right, displayed for 1 second
25 # 1:10000000
26 # 2:01000000
27 # 3:00100000
28 # 4:00010000
29 # 5:00001000
30 # 6:00000100
31 # 7:00000010
32 # 8:00000001
33 # 9:display frame for 100*1/100ths of a second
34 p = {
35  'stream' : 'frame',
36  'method' : 'put',
37  'data' : json.dumps(data)
38  }
39 ws.send(json.dumps(p))
40 
41 
42 # Retrieve data from the device
43 p = {
44  'stream' : 'frame',
45  'method' : 'get',
46  }
47 ws.send(json.dumps(p))
48 result = ws.recv()
49 print "Received '%s'" % json.loads(result)
50 
51 
52 # Flush data on the device
53 p = {
54  'stream' : 'flush',
55  'method' : 'put',
56  'data' : ''
57  }
58 ws.send(json.dumps(p))
59 
60 # Close the connection to the device
61 ws.close()