Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #!/usr/bin/python
  2. import tornado.ioloop
  3. import tornado.web
  4.  
  5. MIN_TEMP = 0
  6. ERROR_TEMP = -999.99
  7.  
  8. TEMP_SENSORS = {
  9. "28-00043a4518ff" : {"label":"A","location":"Server-1-Front","temperature":None},
  10. "28-00043b61b4ff" : {"label":"B","location":"Server-1-Back","temperature":None},
  11. "28-00043e827cff" : {"label":"C","location":"Server-2-Front","temperature":None},
  12. "28-00043a52a1ff" : {"label":"D","location":"Server-2-Back","temperature":None},
  13. "28-00043b6803ff" : {"label":"E","location":"Server Room Ambient","temperature":None},
  14. }
  15.  
  16. def read_temperature(sensor):
  17. tfile = open("/sys/bus/w1/devices/{0}/w1_slave".format(sensor))
  18. text = tfile.read()
  19. tfile.close()
  20. lines = text.split("\n")
  21. if lines[0].find("YES") > 0:
  22. temp = float((lines[1].split(" ")[9])[2:])
  23. temp /= 1000
  24. return temp
  25. return ERROR_TEMP
  26.  
  27. class MainHandler(tornado.web.RequestHandler):
  28. def get(self):
  29. self.write("Hello, world")
  30.  
  31. class TempHandler(tornado.web.RequestHandler):
  32. def get(self):
  33. temps = {}
  34. for rrd in TEMP_SENSORS:
  35. #temp = read_temperature(TEMP_SENSORS[rrd])
  36. TEMP_SENSORS[rrd]['temperature'] = read_temperature(rrd)
  37. response = { 'controller':{'location': 'Server Room','hardware':'raspberrypib+'},
  38. 'sensors': TEMP_SENSORS,}
  39. self.write(response)
  40.  
  41. application = tornado.web.Application([
  42. (r"/", MainHandler),
  43. (r"/temperatures", TempHandler),
  44. ])
  45.  
  46. if __name__ == "__main__":
  47. application.listen(8888)
  48. tornado.ioloop.IOLoop.instance().start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement