Debian Codegraph @

expeyes-web/cgi-bin/expeyes_status.cgi

raw
1 #!/usr/bin/python3
2 """
3 CGI script for reading status of input pins of expEYES Junior connected to server
4 Copyright 2016, Authors : Manoj.S.Nair([email protected]), Jishnu R([email protected]), Rakesh K M([email protected]) [Amrita School of Engineering, Amritapuri Campus, Kollam 690525, Kerala]
5 License : GNU GPL version 3
6 """
7 import sys,os,json
8
9 import expeyes.eyesj as eyes
10 from contextlib import contextmanager
11 @contextmanager # prevent header crash because of connection errors etc
12 def suppress_stdout():
13 with open(os.devnull, "w") as devnull:
14 old_stdout = sys.stdout
15 sys.stdout = devnull
16 try:
17 yield
18 finally:
19 sys.stdout = old_stdout
20
21 with suppress_stdout():#open expEYES
22 p=eyes.open()
23
24 a1 = p.get_voltage(1)#get status of A1
25 a2 = p.get_voltage(2)#get status of A2
26 in1 = p.get_voltage(3)#get status of IN1
27 in2 = p.get_voltage(4)#get status of IN2
28
29 data = {}#creating results
30 data['a1'] = float("{0:.4f}".format(a1))
31 data['a2'] = float("{0:.4f}".format(a2))
32 data['in1'] = float("{0:.4f}".format(in1))
33 data['in2'] = float("{0:.4f}".format(in2))
34 json_data = json.dumps(data)#parsing to JSON format
35
36 sys.stdout.write('Content-Type: application/json \r\n\r\n')#header info
37 sys.stdout.write(unicode('%s\r\n\r\n'%(json_data)))#print data
38 sys.stdout.flush()#flush buffers