Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- """
- Simple example to show how to use cherrypy with jquery and jquery mobile.
- """
- import cherrypy
- class HelloWorld:
- """ Sample request handler class. """
- @cherrypy.expose
- def index(self):
- return '''<!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0;" />
- <meta name="viewport" content="width=device-width"/>
- <meta name="apple-mobile-web-app-capable" content="yes" />
- <title>
- </title>
- <link rel="stylesheet" href="static/jquery.mobile-1.2.0.min.css" />
- <style>
- /* App custom styles */
- </style>
- <script src="static/jquery.min.js">
- </script>
- <script src="static/jquery.mobile-1.2.0.min.js">
- </script>
- <script type="text/javascript">
- $(document).ready(function(){
- var login_code = "";
- $("#login_div").hide();
- $('#myInput').click(function(){
- $('#n_keypad').fadeToggle('fast');
- });
- $('.numero').click(function(){
- if (!isNaN($('#myInput').val())) {
- if (parseInt($('#myInput').val()) == 0) {
- $('#myInput').val($(this).text());
- } else {
- $('#myInput').val($('#myInput').val() + $(this).text());
- }
- }
- });
- $("#login").click(function(){
- $("#login_div").fadeToggle('fast');
- });
- $('.done').click(function(){
- $('#n_keypad').hide('fast');
- login_code = $('#myInput').val();
- $.post( '/start' , {passcode:login_code.toString()});
- $("#login_div").hide('fast');
- });
- $('.del').click(function(){
- $('#myInput').val($('#myInput').val().substring(0,$('#myInput').val().length - 1));
- });
- $('.clear').click(function(){
- $('#myInput').val('');
- });
- $('.zero').click(function(){
- if (!isNaN($('#myInput').val())) {
- if (parseInt($('#myInput').val()) != 0) {
- $('#myInput').val($('#myInput').val() + $(this).text());
- }
- }
- });
- $("#start").click(function () {$.post('/request',{key_pressed:"start"})});
- $("#esc").click(function () {$.post('/request',{key_pressed:"esc"})});
- $("#enter").click(function () {$.post('/request',{key_pressed:"enter"})});
- $("#power").change(function () {$.post('/request',{key_pressed:"power_"+$(this).val()})});
- });
- </script>
- </head>
- <body style="overflow: hidden;overflow-x:hidden;">
- <div data-role="page" data-theme="a" id="page1">
- <div data-theme="a" data-role="header" data-position="">
- <h5>
- Comfort Web Remote
- </h5>
- </div>
- <div data-role="content">
- <div class="ui-grid-b">
- <div class="ui-block-a">
- <button type="button" id="start" data-role="button" data-transition="fade" >
- Start
- </button>
- </div>
- <div class="ui-block-b">
- <button type="button" id="enter" data-role="button" data-transition="fade">
- Enter
- </button>
- </div>
- <div class="ui-block-c">
- <button type="button" id="esc" data-role="button" data-transition="fade">
- Esc
- </button>
- </div>
- <div class="ui-block-a">
- </div>
- <div data-role="fieldcontain">
- <fieldset data-role="controlgroup">
- <label for="power">
- </label>
- <select name="power" id="power" data-theme="a" data-role="slider">
- <option value="off">
- Off
- </option>
- <option value="on">
- On
- </option>
- </select>
- </fieldset>
- </div>
- </div>
- </div>
- <div class="ui-grid-c">
- <div class="ui-block-a">
- <button type="button" id="login" data-role="button" data-transition="fade" >
- LOGIN
- </button>
- </div>
- <div class="ui-block-a" id="login_div">
- <input style="background: white; color: black;" type="text" readonly="readonly" id="myInput"/>
- <table class="ui-bar-a" id="n_keypad" style="display: none; -khtml-user-select: none;">
- <tr>
- <td><a data-role="button" data-theme="b" class="numero">7</a></td>
- <td><a data-role="button" data-theme="b" class="numero">8</a></td>
- <td><a data-role="button" data-theme="b" class="numero">9</a></td>
- <td><a data-role="button" data-theme="e" class="del">Del</a></td>
- </tr>
- <tr>
- <td><a data-role="button" data-theme="b" class="numero">4</a></td>
- <td><a data-role="button" data-theme="b" class="numero">5</a></td>
- <td><a data-role="button" data-theme="b" class="numero">6</a></td>
- <td><a data-role="button" data-theme="e" class="clear">Clear</a></td>
- </tr>
- <tr>
- <td><a data-role="button" data-theme="b" class="numero">1</a></td>
- <td><a data-role="button" data-theme="b" class="numero">2</a></td>
- <td><a data-role="button" data-theme="b" class="numero">3</a></td>
- <td><a data-role="button" data-theme="e"> </a></td>
- </tr>
- <tr>
- <td><a data-role="button" data-theme="e" class="numero">*</a></td>
- <td><a data-role="button" data-theme="b" class="zero">0</a></td>
- <td><a data-role="button" data-theme="e" class="numero">#</a></td>
- <td><a data-role="button" data-theme="e" class="done">Done</a></td>
- </tr>
- </table>
- </div>
- </div>
- <script>
- //App custom javascript
- </script>
- </body>
- </html>
- '''
- @cherrypy.expose
- def request(self, **data):
- key = data['key_pressed'].lower()
- import socket
- import time
- def netcat(content):
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect(('10.0.0.70', 1001))
- s.sendall(content)
- time.sleep(0.5)
- s.shutdown(socket.SHUT_WR)
- while 1:
- data = s.recv(1024)
- if data == "":
- break
- print "Received:", repr(data)
- print "Connection closed."
- s.close()
- # Then to access the data do the following
- #print data
- if key == "start":
- print "start"
- elif key == "enter":
- #arm
- netcat ("\x03O!0101\x0D")
- elif key == "esc":
- #disarm
- netcat ("\x03O!0100\x0D")
- elif key == "power_on":
- #switch on light
- netcat ("\x03O!0201\x0D")
- elif key == "power_off":
- #switch off light
- netcat ("\x03O!0200\x0D")
- else:
- print key
- time.sleep(1)
- @cherrypy.expose
- def start (self, **data):
- passcode = data.get("passcode", None)
- print "logging in using passcode %s"%passcode
- # this is where you should add your device send code
- ## import serial
- ## import time
- ##
- ## serialport=serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
- ## serialport.write("\x03LI%s\x0D"%passcode)
- ## #i would like to write "\x03LI1234\x0D"
- ## reply=serialport.readlines(1)
- ## print reply
- ## time.sleep(1)
- import socket
- import time
- def netcat(content):
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect(('10.0.0.70', 1001))
- s.sendall(content)
- time.sleep(0.5)
- s.shutdown(socket.SHUT_WR)
- while 1:
- data = s.recv(1024)
- if data == "":
- break
- print "Received:", repr(data)
- print "Connection closed."
- s.close()
- netcat ("\x03LI%s\x0D"%passcode)
- import os.path
- tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
- if __name__ == '__main__':
- # CherryPy always starts with app.root when trying to map request URIs
- # to objects, so we need to mount a request handler root. A request
- # to '/' will be mapped to HelloWorld().index().
- cherrypy.quickstart(HelloWorld(), config=tutconf)
- else:
- # This branch is for the test suite; you can ignore it.
- cherrypy.tree.mount(HelloWorld(), config=tutconf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement