monitor.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2012-2013 AGR Audio, Industria e Comercio LTDA. <contato@moddevices.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import socket
  17. try:
  18. from tornado.tcpserver import TCPServer
  19. except ImportError:
  20. # tornado 2.x
  21. from tornado.netutil import TCPServer
  22. from mod.settings import TUNER
  23. class MonitorServer(TCPServer):
  24. def _process_msg(self, msg):
  25. from mod.session import SESSION
  26. try:
  27. cmd, instance, port, value = msg.replace("\x00", "").split()
  28. assert cmd == "monitor"
  29. instance = int(instance)
  30. value = float(value)
  31. except (ValueError, AssertionError) as e:
  32. # TODO: tratar error
  33. pass
  34. else:
  35. if instance == TUNER:
  36. SESSION.tuner(value)
  37. self._handle_conn()
  38. def handle_stream(self, s, addr):
  39. self._stream = s
  40. self._handle_conn()
  41. def _handle_conn(self):
  42. self._stream.read_until("\x00", self._process_msg)