he,
front-end web developer bit familiar python new writing python scripts send data through ajax webserver. explain probeem possible hope understand
have python script open garage door https://github.com/andrewshilliday/gara ... controller. data displayed in web server made twised using code below. add php / sql login www files.
have created apache web server , transferred codes /var/www/html folder. i'm able see website through apache not know how can run script in apache server en send pyhton data instead of using twisted.
please @ github or in code below
https://github.com/maxswinkels/garage-controller
controller.py config.json
front-end web developer bit familiar python new writing python scripts send data through ajax webserver. explain probeem possible hope understand
have python script open garage door https://github.com/andrewshilliday/gara ... controller. data displayed in web server made twised using code below. add php / sql login www files.
code: select all
def run(self): task.loopingcall(self.status_check).start(0.5) root = file('www') root.putchild('upd', self.updatehandler) root.putchild('cfg', confighandler(self)) root.putchild('clk', clickhandler(self)) site = server.site(root) reactor.listentcp(self.config['site']['port'], site) reactor.run()
please @ github or in code below
https://github.com/maxswinkels/garage-controller
controller.py
code: select all
import time, syslog, uuid import rpi.gpio gpio import json import httplib twisted.internet import task twisted.internet import reactor twisted.web import static, twcgi twisted.web import server twisted.web.static import file twisted.web.resource import resource, iresource zope.interface import implements twisted.cred import checkers, portal twisted.web.guard import digestcredentialfactory, basiccredentialfactory class door(object): last_action = none last_action_time = none msg_sent = false pb_iden = none def __init__(self, doorid, config): self.id = doorid self.name = config['name'] self.relay_pin = config['relay_pin'] self.state_pin = config['state_pin'] self.time_to_close = config.get('time_to_close', 10) self.time_to_open = config.get('time_to_open', 10) self.open_time = time.time() gpio.setup(self.relay_pin, gpio.out) gpio.setup(self.state_pin, gpio.in, pull_up_down=gpio.pud_up) gpio.output(self.relay_pin, true) def get_state(self): if gpio.input(self.state_pin) == 0: return 'gesloten' elif self.last_action == 'geopend': if time.time() - self.last_action_time >= self.time_to_open: return 'geopend' else: return 'openen' elif self.last_action == 'sluiten': if time.time() - self.last_action_time >= self.time_to_close: return 'geopend' # state indicates problem else: return 'sluiten' else: return 'geopend' def toggle_relay(self): state = self.get_state() if (state == 'geopend'): self.last_action = 'sluiten' self.last_action_time = time.time() elif state == 'gesloten': self.last_action = 'geopend' self.last_action_time = time.time() else: self.last_action = none self.last_action_time = none gpio.output(self.relay_pin, false) time.sleep(0.2) gpio.output(self.relay_pin, true) class controller(): def __init__(self, config): gpio.setwarnings(false) gpio.cleanup() gpio.setmode(gpio.bcm) self.config = config self.doors = [door(n,c) (n,c) in config['doors'].items()] self.updatehandler = updatehandler(self) door in self.doors: door.last_state = 'unknown' door.last_state_time = time.time() def status_check(self): door in self.doors: new_state = door.get_state() if (door.last_state != new_state): syslog.syslog('%s: %s => %s' % (door.name, door.last_state, new_state)) door.last_state = new_state door.last_state_time = time.time() self.updatehandler.handle_updates() if new_state == 'gesloten': door.open_time = time.time() def toggle(self, doorid): d in self.doors: if d.id == doorid: syslog.syslog('%s: toggled' % d.name) d.toggle_relay() return def get_updates(self, lastupdate): updates = [] d in self.doors: if d.last_state_time >= lastupdate: updates.append((d.id, d.last_state, d.last_state_time)) return updates def run(self): task.loopingcall(self.status_check).start(0.5) root = file('www') root.putchild('upd', self.updatehandler) root.putchild('cfg', confighandler(self)) root.putchild('clk', clickhandler(self)) site = server.site(root) reactor.listentcp(self.config['site']['port'], site) # @undefinedvariable reactor.run() # @undefinedvariable class clickhandler(resource): isleaf = true def __init__ (self, controller): resource.__init__(self) self.controller = controller def render(self, request): door = request.args['id'][0] self.controller.toggle(door) return 'ok' class confighandler(resource): isleaf = true def __init__ (self, controller): resource.__init__(self) self.controller = controller def render(self, request): request.setheader('content-type', 'application/json') return json.dumps([(d.id, d.name, d.last_state, d.last_state_time) d in controller.doors]) class updatehandler(resource): isleaf = true def __init__(self, controller): resource.__init__(self) self.delayed_requests = [] self.controller = controller def handle_updates(self): request in self.delayed_requests: updates = self.controller.get_updates(request.lastupdate) if updates != []: self.send_updates(request, updates) self.delayed_requests.remove(request); def format_updates(self, request, update): response = json.dumps({'timestamp': int(time.time()), 'update':update}) if hasattr(request, 'jsonpcallback'): return request.jsonpcallback +'('+response+')' else: return response def send_updates(self, request, updates): request.write(self.format_updates(request, updates)) request.finish() def render(self, request): # set request content type request.setheader('content-type', 'application/json') # set args args = request.args # set jsonp callback handler name if exists if 'callback' in args: request.jsonpcallback = args['callback'][0] # set lastupdate if exists if 'lastupdate' in args: request.lastupdate = float(args['lastupdate'][0]) else: request.lastupdate = 0 #print "request received " + str(request.lastupdate) # can accommodate request now? updates = controller.get_updates(request.lastupdate) if updates != []: return self.format_updates(request, updates) request.notifyfinish().adderrback(lambda x: self.delayed_requests.remove(request)) self.delayed_requests.append(request) # tell client we're not done yet return server.not_done_yet def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=false, separator=' '): """ takes amount of seconds , turns human-readable amount of time. """ # formatted time string returned time = [] # pieces of time iterate on (days, hours, minutes, etc) # - first piece in each tuple suffix (d, h, w) # - second piece length in seconds (a day 60s * 60m * 24h) parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52), (suffixes[1], 60 * 60 * 24 * 7), (suffixes[2], 60 * 60 * 24), (suffixes[3], 60 * 60), (suffixes[4], 60), (suffixes[5], 1)] # each time piece, grab value , remaining seconds, , add # time string suffix, length in parts: value = seconds / length if value > 0: seconds = seconds % length time.append('%s%s' % (str(value), (suffix, (suffix, suffix + 's')[value > 1])[add_s])) if seconds < 1: break return separator.join(time) if __name__ == '__main__': syslog.openlog('garage_controller') config_file = open('config.json') controller = controller(json.load(config_file)) config_file.close() controller.run()
code: select all
{ "site" : { "port":8081, "username":"", "password":"" }, "doors" : { "main" : { "name" : "garage deur", "relay_pin" : 23, "state_pin" : 17, "approx_time_to_close" : 17, "approx_time_to_open" : 17 } } }
instead of mixing , confusing life apache, php, python etc. simplify things using javascript , node.js
see simple example here: https://bitbucket.org/zicog/pigpio2html
see simple example here: https://bitbucket.org/zicog/pigpio2html
raspberrypi
Comments
Post a Comment