Advertisement
Guest User

Untitled

a guest
Mar 13th, 2014
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.17 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import cherrypy
  4.  
  5. class Control:
  6.    
  7.     @cherrypy.expose
  8.     def index(self):
  9.         return '''<!DOCTYPE html>
  10.         <html>
  11.             <head>
  12.                 <title>RaspiCare</title>
  13.  
  14.                 <meta name="viewport" content="width=device-width, initial-scale=1">
  15.  
  16.                 <link rel="stylesheet" href="static/jquery.mobile-1.4.0.min.css">
  17.                 <link rel="stylesheet" href="static/style.css">
  18.                 <script src="static/jquery-1.10.2.min.js"></script>
  19.                 <script src="static/jquery.mobile-1.4.0.min.js"></script>
  20.                
  21.                 <script>
  22.                     $(document).ready(function(){
  23.                         $('#dbmgmt').click(function(){
  24.                             window.location.href='static/dbmgmt.php';
  25.                         });
  26.                        
  27.                         $('#left_button').click(function(){
  28.                             $.post('/turnCamera', {direction:"left"}).done(function (reply) {
  29.                                 $('#camerapos').empty().append(reply);
  30.                                 alert("left button clicked");});
  31.                            
  32.                         });
  33.                         $('#right_button').click(function(){
  34.                             $.post('/turnCamera', {direction:"right"}).done(function (reply) {
  35.                                 $('#camerapos').empty().append(reply);
  36.                                 alert("right button clicked");});
  37.                         });
  38.                         $('#dispense').click(function(){
  39.                             $.post('/turnCamera', {direction:"dispense"}).done(function (reply) {
  40.                                 $('#camerapos').empty().append(reply);
  41.                                 alert("dispense button clicked");});
  42.                         });
  43.                         $("#light_1").change(function () {$.post('/switch',{key_pressed:"power_"+$(this).val()})});
  44.                     });
  45.                 </script>
  46.             </head>
  47.             <body style="overflow: scroll;overflow-x:hidden;">
  48.                            <div data-role="page" data-theme="a" id="page1">
  49.  
  50.                 <div data-role="header">
  51.                     <img src="static/raspicare_logo.png">
  52.                    
  53.                 </div><!-- /header -->
  54.  
  55.                 <div role="main" class="ui-content">
  56.                     <p>Welcome to the Homepage of RaspiCare. RaspiCare is a smart system that facilitates the daily routine of hospitals.</p>
  57.  
  58.                   <div id="control_button">
  59.                     <img src="static/button_left.png" id="left_button" height="50" width="50">
  60.                     <img src="static/button_right.png" id="right_button" height="50" width="50">
  61.                     <p id="camerapos"> test </p>
  62.                   </div>
  63.  
  64.                     <div class="ui-grid-b">
  65.                         <div class="ui-block-a" ><div class="ui-bar ui-bar-a" style="height:30px">Light Control</div>
  66.                             <form>
  67.                                 <label for="light_1">Light 1</label>
  68.                                 <select name="light_1" id="light_1" data-role="slider" data-track-theme="b" data-theme="b">
  69.                                     <option value="off">Off</option>
  70.                                     <option value="on">On</option>
  71.                                 </select>
  72.                                 <label for="light_2">Light 2</label>
  73.                                 <select name="light_2" id="light_2" data-role="slider" data-track-theme="b" data-theme="b">
  74.                                     <option value="off">Off</option>
  75.                                     <option value="on">On</option>
  76.                                 </select>
  77.                                 <label for="light_3">Light 3</label>
  78.                                 <select name="light_3" id="light_3" data-role="slider" data-track-theme="b" data-theme="b">
  79.                                     <option value="off">Off</option>
  80.                                     <option value="on">On</option>
  81.                                 </select>
  82.                             </form>
  83.                         </div>
  84.                         <div class="ui-block-b"><div class="ui-bar ui-bar-a" style="height:30px">Sensor</div>
  85.                         test : value
  86.                         </div>
  87.                         <div class="ui-block-c"><div class="ui-bar ui-bar-a" style="height:30px">Pill Dispenser</div>
  88.                             <div class="ui-input-btn ui-btn ui-btn-b">
  89.                                 Database Management
  90.                                 <input data-enhanced="true" value="Enhanced - Theme swatch B" type="button" id="dbmgmt">
  91.                             </div>
  92.                             <div class="ui-input-btn ui-btn ui-btn-b">
  93.                                 Dispense Pill
  94.                                 <input data-enhanced="true" value="Enhanced - Theme swatch B" type="button" id="dispense">
  95.                             </div>
  96.                         </div>
  97.                     </div><!-- /grid-b -->
  98.                    
  99.                 </div><!-- /content -->
  100.                
  101.                 <div data-role="footer">
  102.                     <p align="center"><small>NTU EEE 2013-2014</small></p>
  103.                 </div><!-- /footer -->
  104.             </div><!-- /page -->
  105.  
  106.             </body>
  107.         </html>
  108. '''
  109.     @cherrypy.expose
  110.     def turnCamera (self, **data):
  111.  
  112.         import pigpio
  113.         import time
  114.  
  115.         # import serial
  116.         # def servo_control(command):
  117.         # serialport= serial.Serial ("/dev/ttyACM0", 9600, timeout=0.5)
  118.         # serialport.write(command)
  119.         # return serialport.readlines(1)
  120.  
  121.         servos=[4,7] #GPIO number
  122.         pigpio.start()
  123.         key = data['direction']
  124. ##        if key=="left":
  125. ##                servostatus = "left"
  126. ##                print servostatus
  127. ##                m=1500
  128. ##                
  129. ##        elif key=="right":
  130. ##                m=1000
  131.         if key=="dispense":
  132.             m=900
  133.             pigpio.set_servo_pulsewidth(servos[0], m)
  134.             servostat= "Position of servo: %s"%m
  135.             print servostat
  136.             time.sleep(1)
  137.             m=1500
  138.             pigpio.set_servo_pulsewidth(servos[0], m)
  139.             servostat= "Position of servo: %s"%m
  140.             print servostat
  141.             time.sleep(1)
  142.  
  143.  
  144.         pigpio.stop()
  145.  
  146.         return servostat
  147.  
  148.     @cherrypy.expose
  149.     def switch(self, **data):
  150.         import RPi.GPIO as GPIO #Import GPIO library
  151.         import time #Import 'time' library so that we can use 'sleep()'
  152.  
  153.         # Use physical board pin numbers
  154.         GPIO.setmode(GPIO.BOARD)
  155.         # Set up header pin 11 (GPIO17) as an output
  156.         print "Setup Pin 11"
  157.         GPIO.setup(11, GPIO.OUT)
  158.  
  159.         key = data['key_pressed']
  160.         if key=="power_on":
  161.             print "switch on light"
  162.             GPIO.output(11, True) #switch on
  163.             time.sleep(1)
  164.         elif key=="power_off":
  165.             print "switch off light"
  166.             GPIO.output(11, False) #switch off
  167.             time.sleep(1)
  168.        
  169.     #shutdown server
  170.     @cherrypy.expose
  171.     def shutdown(self):  
  172.         cherrypy.engine.exit()
  173.        
  174. import os.path
  175. tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
  176.  
  177. if __name__ == '__main__':
  178.     # CherryPy always starts with app.root when trying to map request URIs
  179.     # to objects, so we need to mount a request handler root. A request
  180.     # to '/' will be mapped to Comfort().index().
  181.     cherrypy.quickstart(Control(), config=tutconf)
  182. else:
  183.     # This branch is for the test suite; you can ignore it.
  184.     cherrypy.tree.mount(Control(), config=tutconf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement