__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #! /usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of BeeWatch.
  5. #
  6. # BeeWatch is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # BeeWatch is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with BeeWatch. If not, see <http://www.gnu.org/licenses/>.
  18. import curio
  19. import blinker
  20. import logging
  21. import logging.handlers
  22. import digilib.network
  23. import os
  24. import queue
  25. import select
  26. import socket
  27. import sys
  28. import threading
  29. import time
  30. import traceback
  31. import digilib.pin
  32. import digilib.network
  33. import beewatch
  34. import beewatch.pinapi
  35. log = logging.getLogger(__name__+"")
  36. # lapi = logging.getLogger(__name__+".api")
  37. # lschat = logging.getLogger(__name__+".schat")
  38. lch = logging.getLogger(__name__+".chandler")
  39. lserver = logging.getLogger(__name__+".server")
  40. class BeeWatchServer(digilib.network.Server):
  41. """
  42. BeeWatchServer is a subclass of digilib.network.Server. It connects to
  43. the gpio control socket and adds protetction by requireing user
  44. authentification. This isn't implemented yet.
  45. """
  46. def __init__(self,*args,**kwargs):
  47. super(BeeWatchServer, self).__init__(
  48. *args,
  49. handler_class=beewatch.server.ConnHandlerBeeWatch,
  50. **kwargs,
  51. )
  52. def make_socket(self):
  53. s = super(BeeWatchServer,self).make_socket()
  54. # only for debugging because I'm impatient and don't want to wait
  55. lch.warning("setting sokopt SO_REUSEADDR to 1. DEBUGGING ONLY")
  56. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  57. return s
  58. class ConnHandlerBeeWatch(digilib.network.ConnHandler):
  59. """
  60. Connection handler for the beewatch server. Applies permission system,
  61. parses commands and tells apis what to do.
  62. """
  63. def __init__(self, socket, addr, server):
  64. super(ConnHandlerBeeWatch, self).__init__(socket, addr, server)
  65. async def handle(self, data):
  66. """
  67. The handle method parses commands and responds if a command was not
  68. found. It executes synchronous and asynchronous methods correctly and
  69. logs the traceback if an exception occured.
  70. """
  71. data = data.strip()
  72. data = data.split(" ")
  73. cmd,*args = data
  74. func = beewatch._commands.get(cmd,False)
  75. if not func:
  76. await self.respond("Unknown command")
  77. return
  78. kwargs = {"args":args,"command":cmd,"respond":self.respond}
  79. task = None
  80. try:
  81. coro = func(**kwargs)
  82. if hasattr(coro,"__await__"):
  83. task = await coro
  84. except Exception as e:
  85. lch.error("api_func raised an error:",exc_info=e)
  86. tb = traceback.format_exc()
  87. await self.respond(tb,log_msg="traceback of '{}'"
  88. .format(e.__cause__))
  89. finally:
  90. pass
  91. if task:
  92. lch.debug("exec: "+task.exception.__cause__)
  93. # task joins iself to suppress the "task not joined" warning
  94. cur_task = await curio.current_task()
  95. await curio.ignore_after(0,cur_task.wait)
  96. async def respond(self,text,*args,log_msg=False):
  97. """
  98. this method is passed to apis so they can give feedback to the user
  99. """
  100. await self.send(text,log_msg)
  101. #