Web based Jalousie control.

simple_server.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. '''
  2. Created on 24.06.2014
  3. @author: volkma_s
  4. '''
  5. import os, sys
  6. import urllib
  7. sys.path.append(os.path.join(os.path.dirname(__file__), "./../"))
  8. #import daemon
  9. from http.server import BaseHTTPRequestHandler, HTTPServer
  10. from socketserver import ThreadingMixIn
  11. import time
  12. from urllib.parse import urlparse
  13. from utils.web import handle_file_request, get_my_ip
  14. from mc_control.mc_statemachine import mc_state_machine, mc_event
  15. HOST_NAME = '' #'localhost'
  16. PORT_NUMBER = 8000
  17. def HandleQeury(query):
  18. global sm
  19. print("HandleQuery query={}".format(query))
  20. query_components = urllib.parse.parse_qs(query)
  21. port = int(query_components['port'][0])
  22. action = query_components['action'][0]
  23. if action == 'up':
  24. print("UP Request")
  25. sm[port].MailBox.put(mc_event.Up)
  26. elif action == 'stop':
  27. print("Stop Request")
  28. sm[port].MailBox.put(mc_event.Stop)
  29. elif action == 'down':
  30. print("Down Request")
  31. sm[port].MailBox.put(mc_event.Down)
  32. else:
  33. print("Unknown query: {}".format(query))
  34. def GetState():
  35. global sm
  36. state = "{}".format(sm[0].position.position)
  37. return state
  38. def GetStates():
  39. global sm
  40. state = "{},{},{},{}".format( sm[0].position.position,
  41. sm[1].position.position,
  42. sm[2].position.position,
  43. sm[3].position.position )
  44. return state
  45. class myhandler(BaseHTTPRequestHandler):
  46. def do_HEAD(self):
  47. self.send_response(200)
  48. self.send_header('Content-type','text/html')
  49. self.end_headers()
  50. def do_GET(self):
  51. parsed_path = urlparse(self.path)
  52. path = parsed_path[2]
  53. query = parsed_path[4]
  54. if(query):
  55. HandleQeury(query)
  56. print("do_GET path=" + path)
  57. if(path.endswith('.html')):
  58. self.send_response(200)
  59. self.send_header('Content-type','text/html')
  60. self.end_headers()
  61. data = handle_file_request(path,'utf-8')
  62. elif(path.endswith('.css')):
  63. self.send_response(200)
  64. self.send_header('Content-type','text/css')
  65. self.end_headers()
  66. data = handle_file_request(path,'utf-8')
  67. elif(path.endswith('.js')):
  68. self.send_response(200)
  69. self.send_header('Content-type','text/javascript')
  70. self.end_headers()
  71. data = handle_file_request(path,'utf-8')
  72. elif(path.endswith('cmd')):
  73. self.send_response(200)
  74. self.send_header('Content-type','text/html')
  75. self.end_headers()
  76. out = GetStates()
  77. data = out.encode('utf-8')
  78. else:
  79. self.send_response(200)
  80. self.send_header('Content-type','text/html')
  81. self.end_headers()
  82. data = handle_file_request('index.html','utf-8')
  83. self.wfile.write(data)
  84. return
  85. def do_POST(self):
  86. self.send_response(200)
  87. self.send_header('Content-type','text/html')
  88. self.end_headers()
  89. parsed_path = urlparse(self.path)
  90. path = parsed_path[2]
  91. query = parsed_path[4]
  92. if(query):
  93. HandleQeury(query)
  94. class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
  95. ''' '''
  96. def StartStateMachines():
  97. global sm
  98. sm = list()
  99. sm.append( mc_state_machine(0, 1))
  100. sm.append( mc_state_machine(2, 3))
  101. sm.append( mc_state_machine(4, 5))
  102. sm.append( mc_state_machine(6, 7))
  103. def CloseStateMachines():
  104. global sm
  105. sm[0].Stop()
  106. sm[1].Stop()
  107. sm[2].Stop()
  108. sm[3].Stop()
  109. if __name__ == '__main__':
  110. global sm
  111. StartStateMachines()
  112. httpd = ThreadedHTTPServer((HOST_NAME,PORT_NUMBER),myhandler)
  113. print('{} - http Server started - http://{}:{}'.format(time.asctime(), get_my_ip(), PORT_NUMBER))
  114. try:
  115. httpd.serve_forever()
  116. except KeyboardInterrupt:
  117. print("end")
  118. pass
  119. CloseStateMachines()
  120. print('{}http Server stopped '.format(time.asctime()))
  121. import unittest
  122. class WebServer_UnitTest(unittest.TestCase):
  123. def setUp(self):
  124. StartStateMachines()
  125. def tearDown(self):
  126. CloseStateMachines()
  127. def test_HandleQuery(self):
  128. # state changed from idle over move down to down
  129. HandleQeury('action=up&port=0')
  130. HandleQeury('action=down&port=0')
  131. HandleQeury('action=stop&port=0')
  132. HandleQeury('action=up&port=1')
  133. HandleQeury('action=down&port=1')
  134. HandleQeury('action=stop&port=1')
  135. HandleQeury('action=up&port=2')
  136. HandleQeury('action=down&port=2')
  137. HandleQeury('action=stop&port=2')
  138. HandleQeury('action=up&port=3')
  139. HandleQeury('action=down&port=3')
  140. HandleQeury('action=stop&port=3')
  141. pass