controller.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright 2017 Digital
  2. #
  3. # This file is part of DigiLib.
  4. #
  5. # DigiLib is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # DigiLib is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with DigiLib. If not, see <http://www.gnu.org/licen
  17. import logging
  18. log = logging.getLogger(__name__+"")
  19. lctrl = logging.getLogger(__name__+".ctrl")
  20. import curio
  21. import digilib.network
  22. import digilib.misc
  23. import digilib.gpio.wrapper
  24. class ControllerBase(object):
  25. """
  26. ControllerBase is the baseclass for Controller. All collectors need to inherit from CollectorBase or provide the same methods.
  27. A collector collects information form sensors and puts them in a pipe, so the CtrlManager can access it. The minutely, hourly and daily methods are easy to use but there execution time depends on when the core was started. use curio's execute_at feature to execute a function at a specific time.
  28. Parameters
  29. ----------
  30. """
  31. def __init__(self):
  32. """
  33. during initilazion, a Controller should register its methods.
  34. """
  35. super().__init__()
  36. async def collect_loadcell_data(self):
  37. """
  38. This method collects data from a sensor.
  39. """
  40. async def daily(self):
  41. """
  42. This method is called once every day by the core.
  43. """
  44. async def hourly(self):
  45. """
  46. This method is called once every hour by the core.
  47. """
  48. async def main_loop(self):
  49. """
  50. This is the main loop of the Controller class.
  51. """
  52. async def minutely(self):
  53. """
  54. This method is called once every minute by the core.
  55. """
  56. async def on_startup(self):
  57. """
  58. This method is called by the core when it starts. This is a good entry point for a Controller class, however the main_loop should be in a different method wich can be called here.
  59. """
  60. # self.main_loop()
  61. async def on_shutdown(self):
  62. """
  63. This method is called by the core when it shuts down.
  64. """
  65. class ButtonController(object):
  66. """
  67. ButtonController can be used with a hardware push button. It provides events you can register a callback to, join it or test the buttons state.
  68. .. A collector collects information form sensors and puts them in a pipe, so the CtrlManager can access it. The minutely, hourly and daily methods are easy to use but there execution time depends on when the core was started. use curio's execute_at feature to execute a function at a specific time.
  69. Parameters
  70. ----------
  71. pin_num: int
  72. the number of the pin wich is connected to the button and to logical HIGH through an appropriate resistor.
  73. """
  74. def __init__(self,pin_num):
  75. """
  76. """
  77. super().__init__()
  78. self.pin_num = pin_num
  79. digilib.gpio.wrapper.setup(self.pin_num,digilib.gpio.wrapper.OUT)
  80. async def read_button_state(self):
  81. """
  82. This method reads the current state from the button's gpio pin.
  83. """
  84. return digilib.gpio.wrapper.read(self.pin_num)
  85. async def daily(self):
  86. """
  87. This method is called once every day by the core.
  88. """
  89. async def hourly(self):
  90. """
  91. This method is called once every hour by the core.
  92. """
  93. async def main_loop(self):
  94. """
  95. This is the main loop of the Controller class.
  96. """
  97. async def minutely(self):
  98. """
  99. This method is called once every minute by the core.
  100. """
  101. async def on_startup(self):
  102. """
  103. This method is called by the core when it starts. This is a good entry point for a Controller class, however the main_loop should be in a different method wich can be called here.
  104. """
  105. # self.main_loop()
  106. async def on_shutdown(self):
  107. """
  108. This method is called by the core when it shuts down.
  109. """
  110. #