Merging multiple versions of Xbee python driver
[iot2.git] / benchmarks / other / XbeePythonDriver / xbee_test.py
1
2 from xbee import ZigBee
3 import serial
4 import time
5 import collections
6 import sys
7 import getopt
8 from socket import *
9 import traceback
10 from threading import Thread, Lock
11 import random
12
13 # Communication Parameters
14 ZIGBEE_SERIAL_PORT = "/dev/cu.usbserial-DN01DJIP"
15 ZIGBEE_SERIAL_BAUD = 115200
16 UDP_RECEIVE_PORT = 5005
17
18 # Radio Parameters
19 ZIGBEE_DEVICE_ADDRESS = "0000000000000000"
20 didzigbeeAddressLSBs = False
21 didzigbeeAddressMSBs = False
22
23
24 def parseCommandLineArgs(argv):
25     global ZIGBEE_SERIAL_PORT
26     global ZIGBEE_SERIAL_BAUD
27     try:
28         opts, args = getopt.getopt(
29             argv, "hp:b:u:", ["port=", "baud=", "udpport="])
30
31     except getopt.GetoptError:
32         print 'test.py -p <serial_port> -b <baud_rate> -u <udp_port>'
33         sys.exit(2)
34
35     for opt, arg in opts:
36         if opt == '-h':
37             print 'test.py -i <inputfile> -o <outputfile>'
38             sys.exit()
39
40         elif opt in ("-p", "--port"):
41             ZIGBEE_SERIAL_PORT = arg
42
43         elif opt in ("-b", "--baud"):
44             try:
45                 ZIGBEE_SERIAL_BAUD = int(arg)
46             except ValueError:
47                 print "Buad rate must be an integer"
48                 sys.exit()
49
50         # elif opt in ("-u", "--udpport"):
51         #     try:
52         #         UDP_PORT = int(arg)
53         #     except ValueError:
54         #         print "Udp port must be an integer"
55         #         sys.exit()
56
57
58 def printMessageData(data):
59     for d in data:
60         print d, ' : ',
61         for e in data[d]:
62             print "{0:02x}".format(ord(e)),
63         if (d == 'id'):
64             print "({})".format(data[d]),
65         print
66
67
68 def convertMessageToString(data):
69     retString = ""
70
71     for d in data:
72         retString += d + ' : '
73
74         for e in data[d]:
75             retString += "{0:02x}".format(ord(e))
76
77         if (d == 'id'):
78             retString += "({})".format(data[d])
79
80         retString += "\n"
81
82     return retString
83
84
85 def splitByN(seq, n):
86     """A generator to divide a sequence into chunks of n units."""
87     while seq:
88         yield seq[:n]
89         seq = seq[n:]
90
91
92 def hexListToChar(hexList):
93     retString = ""
94     for h in hexList:
95         retString += chr(int(h, 16))
96     return retString
97
98
99
100
101 def zigbeeMessageCallbackHandler(data):
102     global ZIGBEE_DEVICE_ADDRESS
103     global didzigbeeAddressLSBs
104     global didzigbeeAddressMSBs
105
106     print "================================================================================"
107     printMessageData(data)
108     print "================================================================================"
109
110
111
112 if __name__ == "__main__":
113
114     # zigbeeClientCallbackDict["000d6f0003ebf2ee"] = [("127.0.0.1", 5556)]
115
116     # parse the command line arguments
117     parseCommandLineArgs(sys.argv[1:])
118
119     # create serial object used for communication to the zigbee radio
120     serialConnection = serial.Serial(ZIGBEE_SERIAL_PORT, ZIGBEE_SERIAL_BAUD)
121     
122     # create a zigbee object that handles all zigbee communication
123     # we use this to do all communication to and from the radio
124     # when data comes from the radio it will get a bit of unpacking
125     # and then a call to the callback specified will be done with the 
126     # unpacked data
127     zigbeeConnection = ZigBee(
128         serialConnection, callback=zigbeeMessageCallbackHandler)
129
130
131
132     print "Starting main loop..."
133     try:
134         # zigbeeConnection.send('tx_explicit',
135         #                       frame_id='\x01',
136         #                       dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
137         #                       dest_addr='\xff\xfd',
138         #                       src_endpoint='\x00',
139         #                       dest_endpoint='\x00',
140         #                       cluster='\x00\x00',
141         #                       profile='\x00\x00',
142         #                       data='\xb4' + '\xee\xf2\xeb\x03\x00\x6f\x0d\x00' + '\x00'
143         #                       )
144
145         # zigbeeConnection.send('tx_explicit',
146         #                       frame_id='\x01',
147         #                       dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
148         #                       dest_addr='\xff\xfd',
149         #                       src_endpoint='\x00',
150         #                       dest_endpoint='\x00',
151         #                       cluster='\x00\x21',
152         #                       profile='\x00\x00',
153         #                       data='\xb4' + '\xee\xf2\xeb\x03\x00\x6f\x0d\x00' + '\x01' + '\x05\x04' +
154         #                       '\x03' + '\xda\x9a\xd9\x40\x00\xa2\x13\x00' + '\x00'
155         #                       )
156
157         # zigbeeConnection.send('tx_explicit',
158         #                       frame_id='\x01',
159         #                       dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
160         #                       dest_addr='\x65\x04',
161         #                       src_endpoint='\x00',
162         #                       dest_endpoint='\x01',
163         #                       cluster='\x04\x05',
164         #                       profile='\x01\x04',
165         #                       data='\x00' + '\xa1' + '\x06' + '\x00' + '\x00\x00' +
166         #                       '\x21' + '\x01\x00' + '\x3c\x00' + '\x00\x00'
167         #                       )
168         # time.sleep(0.1)
169
170
171         print "looping"
172         while True:
173             print "sending"
174             # zigbeeConnection.send('tx_explicit',
175             #           frame_id='\x01',
176             #           dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
177             #           dest_addr='\x9a\xb7',
178             #           src_endpoint='\x00',
179             #           dest_endpoint='\x00',
180             #           cluster='\x00\x00',
181             #           profile='\x00\x00',
182             #           data='\xb4' + '\xee\xf2\xeb\x03\x00\x6f\x0d\x00' + '\x00'
183             #           )
184
185
186             zigbeeConnection.send('tx_explicit',
187                       frame_id='\x01',
188                       dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
189                       dest_addr='\x9a\xb7',
190                       src_endpoint='\x00',
191                       dest_endpoint='\x00',
192                       cluster='\x00\x21',
193                       profile='\x00\x00',
194                       data='\xb4' + '\xee\xf2\xeb\x03\x00\x6f\x0d\x00' + '\x01' + '\x05\x04' +
195                       '\x03' + '\xda\x9a\xd9\x40\x00\xa2\x13\x00' + '\x00'
196                       )
197
198             time.sleep(1)
199
200     except KeyboardInterrupt:
201         pass
202     except:
203         traceback.print_exc()
204
205
206     zigbeeConnection.halt()
207     serialConnection.close()
208
209
210
211