Controller.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. from WTimer import WTimer
  3. class Controller(object):
  4. GOOD = 0
  5. SOSO = 1
  6. BAD = 2
  7. TERRIBLE = 3
  8. def __init__(self):
  9. self._map = {}
  10. self._timer = WTimer(self._check, 1800)
  11. self._timer.start()
  12. def add(self, city, led):
  13. self._map[city] = led
  14. self._check()
  15. def _check(self):
  16. for city in self._map:
  17. level = self._air_level(city)
  18. self._turn_light(self._map[city], level)
  19. def _air_level(self, city):
  20. aqi = city.aqi
  21. if aqi <= 150:
  22. return self.GOOD
  23. elif aqi <= 200:
  24. return self.SOSO
  25. elif aqi <= 250:
  26. return self.BAD
  27. elif aqi >= 300:
  28. return self.TERRIBLE
  29. else:
  30. assert False
  31. def _turn_light(self, light, level):
  32. if level == self.GOOD:
  33. light.off()
  34. elif level == self.SOSO:
  35. light.on()
  36. elif level == self.BAD:
  37. light.on(0.6)
  38. elif level == self.TERRIBLE:
  39. light.on(0.1)
  40. else:
  41. assert False