-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
86 lines (64 loc) · 1.61 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from bottle import route
from bottle import run
from bottle import static_file
_EXPORT_PIN_STREAM = '/sys/class/gpio/export'
_UNEXPORT_PIN_STREAM = '/sys/class/gpio/unexport'
_VALUE_ON = '1'
_VALUE_OFF = '0'
_DIRECTION_IN = 'in'
_DIRECTION_OUT = 'out'
def ExportPin(pin):
with open(_EXPORT_PIN_STREAM, 'w') as f:
f.write(str(pin))
def UnexportPin(pin):
with open(_UNEXPORT_PIN_STREAM, 'w') as f:
f.write(str(pin))
def GetPinPath(pin):
return '/sys/class/gpio/gpio%s' % pin
def GetDirectionFile(pin):
return '%s/direction' % GetPinPath(pin)
def GetValueFile(pin):
return '%s/value' % GetPinPath(pin)
def SetDirection(pin, direction):
file_name = GetDirectionFile(pin)
with open(file_name, 'w') as f:
f.write(direction)
def SetValue(pin, value):
file_name = GetValueFile(pin)
with open(file_name, 'w') as f:
f.write(value)
def GetValue(pin):
file_name = GetValueFile(pin)
with open(file_name, 'r') as f:
return f.read()
@route('/')
def index():
return static_file('static/html/index.html', root='.')
@route('/static/<path:path>')
def static_files(path):
return static_file(path, './static')
@route('/api/pin/get/<pin>')
def GetPinValue(pin=None):
if pin is None:
return ''
return GetValue(pin)
@route('/api/pin/on/<pin>')
def SetPinOn(pin=None):
if pin is None:
return
SetValue(pin, _VALUE_ON)
@route('/api/pin/off/<pin>')
def SetPinOff(pin=None):
if pin is None:
return
SetValue(pin, _VALUE_OFF)
def setup():
ExportPin(25)
SetDirection(25, _DIRECTION_OUT)
def exit():
UnexportPin(25)
try:
setup()
run(host='192.168.1.177', port=80)
finally:
exit()