@@ -1,87 +1,141 | |||||
1 | """ |
|
1 | """ | |
2 | This script should run in the abs module embedded system |
|
2 | This script should run in the abs module embedded system | |
3 | It creates a Web Application with API Restful Bottle to connect to SIR server. |
|
3 | It creates a Web Application with API Restful Bottle to connect to SIR server. | |
4 | It needs the following scripts: abs_gpio.py and bottle.py |
|
4 | It needs the following scripts: abs_gpio.py and bottle.py | |
5 | """ |
|
5 | """ | |
6 | from bottle import route, run, request |
|
6 | from bottle import route, run, request | |
7 | from bottle import error |
|
7 | from bottle import error | |
8 | from abs_gpio import abs_read |
|
8 | from abs_gpio import abs_read | |
9 |
|
9 | |||
10 | #Sockets |
|
10 | #Sockets | |
11 | import socket |
|
11 | import socket | |
12 | import sys |
|
12 | import sys | |
13 |
|
13 | |||
14 | import json |
|
14 | import json | |
15 |
|
15 | |||
16 | module_ip = '192.168.1.xx' |
|
16 | module_ip = '192.168.1.xx' | |
17 | module_num = 'xx' |
|
17 | module_num = 'xx' | |
18 | module_port = 5500 |
|
18 | module_port = 5500 | |
19 | module_xx = ('192.168.1.xx',5500) |
|
19 | module_xx = ('192.168.1.xx',5500) | |
20 |
|
20 | |||
|
21 | #This function decode sent characters | |||
|
22 | def fromChar2Binary(char): | |||
|
23 | #To get the real value (-33) | |||
|
24 | number = ord(char) - 33 | |||
|
25 | bits = bin(number)[2:] | |||
|
26 | #To ensure we have a string with 6bits | |||
|
27 | if len(bits) < 6: | |||
|
28 | bits = bits.zfill(6) | |||
|
29 | return bits | |||
|
30 | ||||
21 | @route('/') |
|
31 | @route('/') | |
22 | @route('/hello') |
|
32 | @route('/hello') | |
23 | def hello(): |
|
33 | def hello(): | |
24 | return "Hello World!" |
|
34 | return "Hello World!" | |
25 |
|
35 | |||
|
36 | """ | |||
|
37 | #---- Send Bits Function ---- | |||
|
38 | @route('/write', method='POST') | |||
|
39 | def writebits(): | |||
|
40 | ||||
|
41 | #This funcion configure ABS sending bits. | |||
|
42 | ||||
|
43 | try: | |||
|
44 | #------------Get Values----------- | |||
|
45 | #----------------UP--------------- | |||
|
46 | ub2 = request.forms.get('ub2') | |||
|
47 | ub1 = request.forms.get('ub1') | |||
|
48 | ub0 = request.forms.get('ub0') | |||
|
49 | #--------------DOWN--------------- | |||
|
50 | db2 = request.forms.get('db2') | |||
|
51 | db1 = request.forms.get('db1') | |||
|
52 | db0 = request.forms.get('db0') | |||
|
53 | ||||
|
54 | #-----------Send Values----------- | |||
|
55 | #----------------UP--------------- | |||
|
56 | ub2 = abs_write(126,ub2) | |||
|
57 | ub1 = abs_write(124,ub1) | |||
|
58 | ub0 = abs_write(122,ub0) | |||
|
59 | #--------------DOWN--------------- | |||
|
60 | db2 = abs_write(120,db2) | |||
|
61 | db1 = abs_write(118,db1) | |||
|
62 | db0 = abs_write(116,db0) | |||
|
63 | ||||
|
64 | if (ub2+ub1+ub0+db2+db1+db0) == 6: | |||
|
65 | return {"status": 1, "message": "Bits were successfully adjusted"} | |||
|
66 | else: | |||
|
67 | return {"status": 0, "message": "Couldn't configure ABS"} | |||
|
68 | ||||
|
69 | except: | |||
|
70 | return {"status": 0, "message": "Couldn't configure ABS"} | |||
|
71 | """ | |||
26 |
|
72 | |||
27 | #---- Get Bits Function ---- |
|
73 | #---- Get Bits Function ---- | |
28 | @route('/read', method='GET') |
|
74 | @route('/read', method='GET') | |
29 | def readbits(): |
|
75 | def readbits(): | |
30 |
|
76 | |||
31 | """ |
|
77 | """ | |
32 | This function reads the real values from the embedded system pins |
|
78 | This function reads the real values from the embedded system pins | |
33 | with gpio class |
|
79 | with gpio class | |
34 | """ |
|
80 | """ | |
35 |
|
81 | |||
36 | #This function reads sent bits. |
|
82 | #This function reads sent bits. | |
37 |
|
83 | |||
38 | #------Get Monitoring Values------ |
|
84 | #------Get Monitoring Values------ | |
39 | #----------------UP--------------- |
|
85 | #----------------UP--------------- | |
40 | um2_value = abs_read(80) #(++) |
|
86 | um2_value = abs_read(80) #(++) | |
41 | um1_value = abs_read(82) |
|
87 | um1_value = abs_read(82) | |
42 | um0_value = abs_read(84) #(--) |
|
88 | um0_value = abs_read(84) #(--) | |
43 | #--------------DOWN--------------- |
|
89 | #--------------DOWN--------------- | |
44 | dm2_value = abs_read(94) #(++) |
|
90 | dm2_value = abs_read(94) #(++) | |
45 | dm1_value = abs_read(88) |
|
91 | dm1_value = abs_read(88) | |
46 | dm0_value = abs_read(86) #(--) |
|
92 | dm0_value = abs_read(86) #(--) | |
47 |
|
93 | |||
48 | allbits = [um2_value, um1_value, um0_value, dm2_value, dm1_value, dm0_value] |
|
94 | allbits = [um2_value, um1_value, um0_value, dm2_value, dm1_value, dm0_value] | |
49 | if "" not in allbits: |
|
95 | if "" not in allbits: | |
50 | allbits = {"um2":int(um2_value), "um1": int(um1_value), "um0": int(um0_value), "dm2": int(dm2_value), "dm1": int(dm1_value), "dm0": int(dm0_value)} |
|
96 | allbits = {"um2":int(um2_value), "um1": int(um1_value), "um0": int(um0_value), "dm2": int(dm2_value), "dm1": int(dm1_value), "dm0": int(dm0_value)} | |
51 | #allbits = {"ub0":0, "ub1":0, "ub2":0, "db0":0, "db1":0, "db2":0} |
|
97 | #allbits = {"ub0":0, "ub1":0, "ub2":0, "db0":0, "db1":0, "db2":0} | |
52 | return {"status": 1, "message": "Bits were successfully read", "allbits" : allbits} |
|
98 | return {"status": 1, "message": "Bits were successfully read", "allbits" : allbits} | |
53 | else: |
|
99 | else: | |
54 | return {"status": 0, "message": "There's a problem reading bits", "allbits" : ""} |
|
100 | return {"status": 0, "message": "There's a problem reading bits", "allbits" : ""} | |
55 |
|
101 | |||
56 | @route('/write', method='POST') |
|
102 | @route('/write', method='POST') | |
57 | def writebits(): |
|
103 | def writebits(): | |
58 | """ |
|
104 | """ | |
59 | This function sends configurations to the module tcp_ |
|
105 | This function sends configurations to the module tcp_ | |
60 | """ |
|
106 | """ | |
|
107 | header_rx = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' | |||
|
108 | module_rx = 'ABS_'+module_num | |||
61 | try: |
|
109 | try: | |
62 | header_rx = request.forms.get('header') |
|
110 | #header_rx = request.forms.get('header') | |
63 | module_rx = request.forms.get('module') |
|
111 | #module_rx = request.forms.get('module') | |
64 | beams_rx = request.forms.get('beams') |
|
112 | #beams_rx = request.forms.get('beams') | |
65 | beams_rx = json.loads(beams_rx) |
|
113 | #beams_rx = json.loads(beams_rx) | |
|
114 | beams_rx = json.loads(request.body.readline()) | |||
|
115 | beams_rx = beams_rx['beams'] | |||
66 | except: |
|
116 | except: | |
67 | return {"status":0, "message": "Could not accept configuration"} |
|
117 | return {"status":0, "message": "Could not accept configuration"} | |
68 |
#print |
|
118 | #print beams_rx | |
69 | message_tx = header_rx+"\n"+module_rx+"\n" |
|
119 | message_tx = header_rx+"\n"+module_rx+"\n" | |
70 |
for i in range(1,len(beams_rx) |
|
120 | for i in range(1,len(beams_rx)): #(1,len(beams_rx)+1) | |
71 | message_tx = message_tx+beams_rx[str(i)]+"\n" |
|
121 | try: | |
|
122 | message_tx = message_tx+fromChar2Binary(beams_rx[i])+"\n" | |||
|
123 | except: | |||
|
124 | return {"status":0, "message": "Error in parameters from Beams List" | |||
72 | message_tx = message_tx+"0" |
|
125 | message_tx = message_tx+"0" | |
73 |
|
126 | |||
|
127 | ||||
74 | # Create the datagram socket |
|
128 | # Create the datagram socket | |
75 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
129 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
76 | print >>sys.stderr, 'sending "%s"' % message_tx |
|
130 | print >>sys.stderr, 'sending "%s"' % message_tx | |
77 |
sock.connect(module |
|
131 | sock.connect(module) | |
78 | sock.send(message_tx) |
|
132 | sock.send(message_tx) | |
79 | sock.close() |
|
133 | sock.close() | |
80 |
|
134 | |||
81 | return {"status":1, "message": "Configuration has been successfully sent"} |
|
135 | return {"status":1, "message": "Configuration has been successfully sent"} | |
82 |
|
136 | |||
83 | @error(404) |
|
137 | @error(404) | |
84 | def error404(error): |
|
138 | def error404(error): | |
85 | return "^^' Nothing here, try again." |
|
139 | return "^^' Nothing here, try again." | |
86 |
|
140 | |||
87 | run(host=module_ip, port=8080, debug=True) |
|
141 | run(host=module_ip, port=8080, debug=True) |
General Comments 0
You need to be logged in to leave comments.
Login now