@@ -0,0 +1,163 | |||
|
1 | """ | |
|
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. | |
|
4 | It needs the following scripts: abs_gpio.py and bottle.py | |
|
5 | """ | |
|
6 | from bottle import route, run, request | |
|
7 | from bottle import error | |
|
8 | from abs_gpio import abs_read | |
|
9 | ||
|
10 | #Sockets | |
|
11 | import socket | |
|
12 | import sys | |
|
13 | ||
|
14 | import json | |
|
15 | ||
|
16 | module_ip = '192.168.1.xx' | |
|
17 | module_num = 'xx' | |
|
18 | module_port = 5500 | |
|
19 | module = (module_ip,5500) | |
|
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 | ||
|
31 | @route('/') | |
|
32 | @route('/hello') | |
|
33 | def hello(): | |
|
34 | return "Hello World!" | |
|
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 | """ | |
|
72 | ||
|
73 | #------ Get Status ------- | |
|
74 | @route('/status', method='GET') | |
|
75 | def module_status(): | |
|
76 | """ | |
|
77 | This function returns: | |
|
78 | 0 : No Connected | |
|
79 | 1 : Connected | |
|
80 | ||
|
81 | """ | |
|
82 | message_tx = 'JROABSClnt_01CeCnMod000001MNTR10' | |
|
83 | #Create the datagram socket | |
|
84 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
|
85 | try: | |
|
86 | sock.connect(module) | |
|
87 | sock.send(message_tx) | |
|
88 | sock.close() | |
|
89 | except: | |
|
90 | sock = None | |
|
91 | return {"status":0, "message": "TCP Control Module not detected."} | |
|
92 | return {"status": 1, "message": "Module "+module_num+" is running"} | |
|
93 | ||
|
94 | ||
|
95 | #---- Get Bits Function ---- | |
|
96 | @route('/read', method='GET') | |
|
97 | def readbits(): | |
|
98 | ||
|
99 | """ | |
|
100 | This function reads the real values from the embedded system pins | |
|
101 | with gpio class | |
|
102 | """ | |
|
103 | ||
|
104 | #This function reads sent bits. | |
|
105 | ||
|
106 | #------Get Monitoring Values------ | |
|
107 | #----------------UP--------------- | |
|
108 | um2_value = abs_read(80) #(++) | |
|
109 | um1_value = abs_read(82) | |
|
110 | um0_value = abs_read(84) #(--) | |
|
111 | #--------------DOWN--------------- | |
|
112 | dm2_value = abs_read(94) #(++) | |
|
113 | dm1_value = abs_read(88) | |
|
114 | dm0_value = abs_read(86) #(--) | |
|
115 | ||
|
116 | allbits = [um2_value, um1_value, um0_value, dm2_value, dm1_value, dm0_value] | |
|
117 | if "" not in allbits: | |
|
118 | 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)} | |
|
119 | #allbits = {"ub0":0, "ub1":0, "ub2":0, "db0":0, "db1":0, "db2":0} | |
|
120 | return {"status": 1, "message": "Bits were successfully read", "allbits" : allbits} | |
|
121 | else: | |
|
122 | return {"status": 0, "message": "There's a problem reading bits", "allbits" : ""} | |
|
123 | ||
|
124 | @route('/write', method='POST') | |
|
125 | def writebits(): | |
|
126 | """ | |
|
127 | This function sends configurations to the module tcp_ | |
|
128 | """ | |
|
129 | header_rx = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' | |
|
130 | module_rx = 'ABS_'+module_num | |
|
131 | try: | |
|
132 | #header_rx = request.forms.get('header') | |
|
133 | #module_rx = request.forms.get('module') | |
|
134 | #beams_rx = request.forms.get('beams') | |
|
135 | #beams_rx = json.loads(beams_rx) | |
|
136 | beams_rx = json.loads(request.body.readline()) | |
|
137 | beams_rx = beams_rx['beams'] | |
|
138 | except: | |
|
139 | return {"status":0, "message": "Could not accept configuration"} | |
|
140 | #print beams_rx | |
|
141 | message_tx = header_rx+"\n"+module_rx+"\n" | |
|
142 | for i in range(0,len(beams_rx)): #(1,len(beams_rx)+1) | |
|
143 | try: | |
|
144 | message_tx = message_tx+fromChar2Binary(beams_rx[i])+"\n" | |
|
145 | except: | |
|
146 | return {"status":0, "message": "Error in parameters from Beams List"} | |
|
147 | message_tx = message_tx+"0" | |
|
148 | ||
|
149 | ||
|
150 | # Create the datagram socket | |
|
151 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
|
152 | print >>sys.stderr, 'sending "%s"' % message_tx | |
|
153 | sock.connect(module) | |
|
154 | sock.send(message_tx) | |
|
155 | sock.close() | |
|
156 | ||
|
157 | return {"status":1, "message": "Configuration has been successfully sent"} | |
|
158 | ||
|
159 | @error(404) | |
|
160 | def error404(error): | |
|
161 | return "^^' Nothing here, try again." | |
|
162 | ||
|
163 | run(host=module_ip, port=8080, debug=True) |
General Comments 0
You need to be logged in to leave comments.
Login now