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