1 import cgifrom google.appengine.ext import webapp 2 from google.appengine.ext.webapp.util import run_wsgi_app 3 from google.appengine.ext import db 4 5 class DeviceState(db.Model): 6 device = db.StringProperty() 7 device_state = db.StringProperty() 8 class MainPage(webapp.RequestHandler): def get(self): 9 self.response.out.write('<html><body>') 10 states=db.GqlQuery("SELECT * FROM DeviceState ORDER BY device") 11 for state in states: 12 self.response.out.write("<div>"+cgi.escape(state.device)+":"+ 13 cgi.escape(state.device_state)+"</div>") 14 self.response.out.write(""" 15 <form action="/humanSet" method="get"> 16 <div><input type="text" name="d"></div> 17 <div><input type="text" name="s"></div> 18 <div><input type="submit" value="submit"></div> 19 </form> 20 </body> 21 </html>""") 22 23 class UpdateState(webapp.RequestHandler): 24 def get(self): 25 if(self.request.get('d')): 26 states=db.GqlQuery("SELECT * FROM DeviceState WHERE device = :1", 27 self.request.get('d')) 28 if(states.count()>0): 29 for state in states: 30 state.device_state=self.request.get('s') 31 state.put() 32 else: 33 deviceState = DeviceState() 34 deviceState.device = self.request.get('d') 35 deviceState.device_state = self.request.get('s') 36 deviceState.put() 37 if(self.request.get('q')): 38 if(self.request.get('q')=='*'): 39 states=db.GqlQuery("SELECT * FROM DeviceState ORDER BY device") 40 else: 41 states=db.GqlQuery("SELECT * FROM DeviceState WHERE device='"+ 42 self.request.get('q')+"'") 43 for state in states: 44 self.response.out.write(cgi.escape(state.device)+":"+ 45 cgi.escape(state.device_state)+"\r\n") 46 47 class HumanSet(webapp.RequestHandler): 48 def get(self): 49 states=db.GqlQuery("SELECT * FROM DeviceState WHERE device = :1", 50 self.request.get('d')) 51 if(states.count()>0): 52 for state in states: 53 state.device_state=self.request.get('s') 54 state.put() 55 else: 56 deviceState = DeviceState() 57 deviceState.device = self.request.get('d') 58 deviceState.device_state = self.request.get('s') 59 deviceState.put() 60 self.redirect('/') 61 62 class Reset(webapp.RequestHandler): 63 def get(self): 64 states=DeviceState.all() 65 for state in states: 66 state.delete() 67 self.response.out.write("All Data Deleted\n") 68 69 application = webapp.WSGIApplication([('/',MainPage), 70 ('/humanSet',HumanSet), 71 ('/update',UpdateState), 72 ('/reset',Reset)],debug=True) 73 74 def main(): 75 run_wsgi_app(application) 76 77 if __name__ == "__main__": 78 main()