You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

service.py 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from pulsectl import Pulse
  4. import SocketServer
  5. from threading import Thread
  6. LISTEN_ON = ("10.0.0.4", 2000)
  7. def main():
  8. pulse = Pulse('mic-control')
  9. sources = pulse.source_list()
  10. sourceNumber = getSourceSelection(sources)
  11. try:
  12. server = SocketServer.TCPServer(LISTEN_ON, RequestHandler)
  13. server.serve_forever()
  14. except KeyboardInterrupt:
  15. server.shutdown()
  16. server.server_close()
  17. except:
  18. server.shutdown()
  19. server.server_close()
  20. def getSourceSelection(sources):
  21. sourceCount = len(sources) - 1
  22. error = False
  23. while True:
  24. message = error if error else "Please select a source: "
  25. print(message)
  26. for i, source in enumerate(sources):
  27. print "%i: %s" % (i, source.description)
  28. selection = raw_input("Enter a number[0-%i]:" % sourceCount)
  29. print("")
  30. try :
  31. return getInt(selection, sourceCount)
  32. except InputError as e:
  33. error = e.message
  34. def getInt(value, sourceCount):
  35. number = 0
  36. try:
  37. number = int(value)
  38. except ValueError as e:
  39. raise InputError("Enter only a number...")
  40. if number not in range(0, sourceCount):
  41. raise InputError("Not a valid option...")
  42. return number
  43. class RequestHandler(SocketServer.StreamRequestHandler):
  44. def handle(self):
  45. for line in self.rfile:
  46. message = line.strip()
  47. if message in messageTypes:
  48. messageTypes[message]()
  49. print("message: ".message)
  50. print("Closed!")
  51. self.request.close()
  52. def buttonPressed():
  53. print("down!")
  54. def buttonReleased():
  55. print("up!")
  56. messageTypes = {
  57. 'button_press': buttonPressed,
  58. 'button_release': buttonReleased,
  59. }
  60. class InputError(Exception):
  61. def __init__(self, message):
  62. self.message = message
  63. main()