Advertisement
Guest User

eth_test4.py

a guest
Jan 9th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. """
  4. Simple example to show how to use cherrypy with jquery and jquery mobile.
  5. """
  6.  
  7. import cherrypy
  8.  
  9. class HelloWorld:
  10. """ Sample request handler class. """
  11. @cherrypy.expose
  12. def index(self):
  13. return '''<!DOCTYPE html>
  14. <html>
  15. <head>
  16. <meta charset="utf-8" />
  17. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0;" />
  18. <meta name="viewport" content="width=device-width"/>
  19. <meta name="apple-mobile-web-app-capable" content="yes" />
  20. <title>
  21. </title>
  22. <link rel="stylesheet" href="static/jquery.mobile-1.2.0.min.css" />
  23. <style>
  24. /* App custom styles */
  25. </style>
  26. <script src="static/jquery.min.js">
  27. </script>
  28. <script src="static/jquery.mobile-1.2.0.min.js">
  29. </script>
  30. <script type="text/javascript">
  31. $(document).ready(function(){
  32. var login_code = "";
  33. $("#login_div").hide();
  34. $('#myInput').click(function(){
  35. $('#n_keypad').fadeToggle('fast');
  36. });
  37. $('.numero').click(function(){
  38. if (!isNaN($('#myInput').val())) {
  39. if (parseInt($('#myInput').val()) == 0) {
  40. $('#myInput').val($(this).text());
  41. } else {
  42. $('#myInput').val($('#myInput').val() + $(this).text());
  43. }
  44. }
  45. });
  46.  
  47. $("#login").click(function(){
  48. $("#login_div").fadeToggle('fast');
  49. });
  50. $('.done').click(function(){
  51. $('#n_keypad').hide('fast');
  52. login_code = $('#myInput').val();
  53. $.post( '/start' , {passcode:login_code.toString()});
  54. $("#login_div").hide('fast');
  55. });
  56. $('.del').click(function(){
  57. $('#myInput').val($('#myInput').val().substring(0,$('#myInput').val().length - 1));
  58. });
  59. $('.clear').click(function(){
  60. $('#myInput').val('');
  61. });
  62. $('.zero').click(function(){
  63. if (!isNaN($('#myInput').val())) {
  64. if (parseInt($('#myInput').val()) != 0) {
  65. $('#myInput').val($('#myInput').val() + $(this).text());
  66. }
  67. }
  68. });
  69. $("#start").click(function () {$.post('/request',{key_pressed:"start"})});
  70. $("#esc").click(function () {$.post('/request',{key_pressed:"esc"})});
  71. $("#enter").click(function () {$.post('/request',{key_pressed:"enter"})});
  72. $("#power").change(function () {$.post('/request',{key_pressed:"power_"+$(this).val()})});
  73.  
  74. });
  75. </script>
  76. </head>
  77. <body style="overflow: hidden;overflow-x:hidden;">
  78. <div data-role="page" data-theme="a" id="page1">
  79. <div data-theme="a" data-role="header" data-position="">
  80. <h5>
  81. Comfort Web Remote
  82. </h5>
  83. </div>
  84. <div data-role="content">
  85. <div class="ui-grid-b">
  86. <div class="ui-block-a">
  87. <button type="button" id="start" data-role="button" data-transition="fade" >
  88. Start
  89. </button>
  90. </div>
  91. <div class="ui-block-b">
  92. <button type="button" id="enter" data-role="button" data-transition="fade">
  93. Enter
  94. </button>
  95. </div>
  96. <div class="ui-block-c">
  97. <button type="button" id="esc" data-role="button" data-transition="fade">
  98. Esc
  99. </button>
  100. </div>
  101. <div class="ui-block-a">
  102.  
  103. </div>
  104. <div data-role="fieldcontain">
  105. <fieldset data-role="controlgroup">
  106. <label for="power">
  107. </label>
  108. <select name="power" id="power" data-theme="a" data-role="slider">
  109. <option value="off">
  110. Off
  111. </option>
  112. <option value="on">
  113. On
  114. </option>
  115. </select>
  116. </fieldset>
  117. </div>
  118. </div>
  119. </div>
  120. <div class="ui-grid-c">
  121. <div class="ui-block-a">
  122. <button type="button" id="login" data-role="button" data-transition="fade" >
  123. LOGIN
  124. </button>
  125. </div>
  126. <div class="ui-block-a" id="login_div">
  127. <input style="background: white; color: black;" type="text" readonly="readonly" id="myInput"/>
  128.  
  129. <table class="ui-bar-a" id="n_keypad" style="display: none; -khtml-user-select: none;">
  130. <tr>
  131. <td><a data-role="button" data-theme="b" class="numero">7</a></td>
  132. <td><a data-role="button" data-theme="b" class="numero">8</a></td>
  133. <td><a data-role="button" data-theme="b" class="numero">9</a></td>
  134. <td><a data-role="button" data-theme="e" class="del">Del</a></td>
  135. </tr>
  136. <tr>
  137. <td><a data-role="button" data-theme="b" class="numero">4</a></td>
  138. <td><a data-role="button" data-theme="b" class="numero">5</a></td>
  139. <td><a data-role="button" data-theme="b" class="numero">6</a></td>
  140. <td><a data-role="button" data-theme="e" class="clear">Clear</a></td>
  141. </tr>
  142. <tr>
  143. <td><a data-role="button" data-theme="b" class="numero">1</a></td>
  144. <td><a data-role="button" data-theme="b" class="numero">2</a></td>
  145. <td><a data-role="button" data-theme="b" class="numero">3</a></td>
  146. <td><a data-role="button" data-theme="e">&nbsp;</a></td>
  147. </tr>
  148. <tr>
  149. <td><a data-role="button" data-theme="e" class="numero">*</a></td>
  150. <td><a data-role="button" data-theme="b" class="zero">0</a></td>
  151. <td><a data-role="button" data-theme="e" class="numero">#</a></td>
  152. <td><a data-role="button" data-theme="e" class="done">Done</a></td>
  153. </tr>
  154. </table>
  155. </div>
  156. </div>
  157. <script>
  158. //App custom javascript
  159. </script>
  160. </body>
  161. </html>
  162. '''
  163. @cherrypy.expose
  164. def request(self, **data):
  165. key = data['key_pressed'].lower()
  166. import socket
  167. import time
  168. def netcat(content):
  169. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  170. s.connect(('10.0.0.70', 1001))
  171. s.sendall(content)
  172. time.sleep(0.5)
  173. s.shutdown(socket.SHUT_WR)
  174.  
  175. while 1:
  176. data = s.recv(1024)
  177. if data == "":
  178. break
  179. print "Received:", repr(data)
  180. print "Connection closed."
  181. s.close()
  182. # Then to access the data do the following
  183. #print data
  184.  
  185.  
  186. if key == "start":
  187. print "start"
  188. elif key == "enter":
  189. #arm
  190. netcat ("\x03O!0101\x0D")
  191. elif key == "esc":
  192. #disarm
  193. netcat ("\x03O!0100\x0D")
  194.  
  195. elif key == "power_on":
  196. #switch on light
  197. netcat ("\x03O!0201\x0D")
  198.  
  199. elif key == "power_off":
  200. #switch off light
  201. netcat ("\x03O!0200\x0D")
  202.  
  203. else:
  204. print key
  205. time.sleep(1)
  206.  
  207.  
  208. @cherrypy.expose
  209. def start (self, **data):
  210. passcode = data.get("passcode", None)
  211. print "logging in using passcode %s"%passcode
  212. # this is where you should add your device send code
  213. ## import serial
  214. ## import time
  215. ##
  216. ## serialport=serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
  217. ## serialport.write("\x03LI%s\x0D"%passcode)
  218. ## #i would like to write "\x03LI1234\x0D"
  219. ## reply=serialport.readlines(1)
  220. ## print reply
  221. ## time.sleep(1)
  222.  
  223. import socket
  224. import time
  225.  
  226. def netcat(content):
  227. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  228. s.connect(('10.0.0.70', 1001))
  229. s.sendall(content)
  230. time.sleep(0.5)
  231. s.shutdown(socket.SHUT_WR)
  232.  
  233. while 1:
  234. data = s.recv(1024)
  235. if data == "":
  236. break
  237. print "Received:", repr(data)
  238. print "Connection closed."
  239. s.close()
  240.  
  241. netcat ("\x03LI%s\x0D"%passcode)
  242.  
  243.  
  244.  
  245. import os.path
  246. tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
  247.  
  248. if __name__ == '__main__':
  249. # CherryPy always starts with app.root when trying to map request URIs
  250. # to objects, so we need to mount a request handler root. A request
  251. # to '/' will be mapped to HelloWorld().index().
  252. cherrypy.quickstart(HelloWorld(), config=tutconf)
  253. else:
  254. # This branch is for the test suite; you can ignore it.
  255. cherrypy.tree.mount(HelloWorld(), config=tutconf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement