This repository was archived by the owner on Apr 25, 2019. It is now read-only.
forked from derekstavis/python-sysfs-gpio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
142 lines (106 loc) · 4.02 KB
/
main.py
File metadata and controls
142 lines (106 loc) · 4.02 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import sys
import signal
import logging
import configparser
import argparse
import json
from functools import partial
from tornado import ioloop, httpclient
import sysfs
__all__ = ('DEFAULT_CONFIG_LOCATION', 'DEFAULT_CONFIG_STUB',
'main','run','io_callback','write_config')
DEFAULT_CONFIG_LOCATION='config.ini'
DEFAULT_CONFIG_STUB= {
'post_url': 'http://ops.voltserver.dev/policy_input/state',
'this_host': 'ioboard0',
'enabled_pins': '408 409', # see: http://docs.getchip.com/#how-the-system-sees-gpio
}
if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s %(levelname)s %(message)s',
level=logging.DEBUG )
logger = logging.getLogger(__name__)
def io_callback(config, pin, value):
'''
Send an HTTP POST to C{config['post_url']} with JSON body in the form:
{
host: 'ioboard0', // name identifying this device, from config['this_host']
input_id: 'd408', // pin ID
value: 1 // changed value
}
'''
logger.debug('PIN[%d] %d', pin.number, int(value) )
post_body = json.dumps({
'host': config['this_host'],
'input_id': 'd%d' % pin.number, # d0, d1, etc. TODO analog input support
'value': value,
})
# POST data to the server
http = httpclient.AsyncHTTPClient()
http.fetch(config['post_url'],
callback=partial(handle_http_response, config, pin),
headers={
'content-type':'application/json',
'accept':'application/json',
},
method='POST',
body=post_body )
logger.debug('HTTP >> [%s] %s', config['post_url'], post_body)
def handle_http_response(config, pin, response):
if response.error:
logger.warn('HTTP << %d %s', response.code, response.error)
return
logger.debug('HTTP << %d %s', response.code, response.body)
def read_config( location=DEFAULT_CONFIG_LOCATION ):
config = configparser.ConfigParser()
with open(location,'r') as file:
config.read_file(file)
return config
def write_default_config( location=DEFAULT_CONFIG_LOCATION ):
'''
Write a default config file to C{DEFAULT_CONFIG_LOCATION}.
Default config will look like:
[main]
post_url: http://foo.com/policy_input/state
this_host: ioboard0
enabled_pins: 1 2 3
See C{DEFAULT_CONFIG_STUB}
'''
config = configparser.ConfigParser()
config['main'] = DEFAULT_CONFIG_STUB
with open(location,'w') as file:
config.write(file)
def main(args):
'''
Parse cmd line opts, read config & call C{run()}
'''
if args.write_config:
print('Writing default config file to %s' % args.config)
write_default_config(args.config)
sys.exit(1)
# else parse config and run main loop:
config = read_config(args.config)
run(config['main']) # TODO option to select alternate config section?
def run(config):
io_loop = ioloop.IOLoop.current()
def quit(sig,frame):
logger.info("Caught signal (%d), quitting...", sig)
io_loop.stop()
for sig in ('HUP','INT','QUIT','TERM'):
signal.signal( getattr(signal, 'SIG'+sig), quit )
# assume all enabled pins are inputs.
pins = list(map(int, config['enabled_pins'].split()))
gpio = sysfs.Gpio( poll_queue=io_loop,
available_pins=pins )
callback = partial(io_callback, config)
for pin in pins:
logger.debug('PIN %d enabled as input', pin)
gpio.alloc_pin(pin, sysfs.INPUT, callback, sysfs.BOTH)
io_loop.start()
parser = argparse.ArgumentParser(description='SysFS GPIO poller/ HTTP forwarder')
parser.add_argument('-c', '--config', default=DEFAULT_CONFIG_LOCATION,
help='Config file to use (absolute path or relative to working dir)' )
parser.add_argument('--write-config', action='store_const', const=True,
help='write a config file stub and exit')
if __name__ == '__main__':
main(parser.parse_args())