Cordova IOT Lesson002

时间:2022-01-25 09:51:52

hello

index.html

 <!DOCTYPE html>
 <html>
     <head>
         <meta charset="utf-8">
         <title>我的App</title>
         <meta name="format-detection" content="telephone=no">
         <meta name="msapplication-tap-highlight" content="no">
         <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
         <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" />
         <script src="js/jquery-2.1.3.min.js"></script>
         <script>
         $(document).on('mobileinit', function() {
             $.mobile.pushStateEnabled = false;
         });
         </script>
         <script src="js/jquery.mobile-1.4.5.min.js"></script>
     </head>
     <body>
         <div data-role="page" id="p1">
             <div data-role="header">
                 <h1>第 1 页</h1>
             </div>
             <div data-role="content">
                 <ul data-role="listview">
                     <li><a href="#p2">第 2 页</a></li>
                     <li><a href="#p3">第 3 页</a></li>
                 </ul>
             </div>
             <div data-role="footer" data-position="fixed">
                 <h4>页脚</h4>
             </div>
         </div>

         <div data-role="page" id="p2">
             <div data-role="header">
                 <h1>第 2 页</h1>
             </div>
             <div data-role="content">
                 内容
             </div>
             <div data-role="footer" data-position="fixed">
                 <h4>页脚</h4>
             </div>
         </div>

         <div data-role="page" id="p3">
             <div data-role="header">
                 <h1>第 3 页</h1>
             </div>
             <div data-role="content">
                 内容
             </div>
             <div data-role="footer" data-position="fixed">
                 <h4>页脚</h4>
             </div>
         </div>

     </body>
 </html>

hello_back

index.html

 <!DOCTYPE html>
 <html>
     <head>
         <meta charset="utf-8">
         <title>我的App</title>
         <meta name="format-detection" content="telephone=no">
         <meta name="msapplication-tap-highlight" content="no">
         <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
         <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" />
         <script src="js/jquery-2.1.3.min.js"></script>
         <script>
         $(document).on('mobileinit', function() {
             $.mobile.pushStateEnabled = false;
         });
         </script>
         <script src="js/jquery.mobile-1.4.5.min.js"></script>
     </head>
     <body>
         <div data-role="page" id="p1">
             <div data-role="header">
                 <h1>第 1 页</h1>
             </div>
             <div data-role="content">
                 <ul data-role="listview">
                     <li><a href="#p2">第 2 页</a></li>
                     <li><a href="#p3">第 3 页</a></li>
                 </ul>
                 <br>
                 <a href="#popAbout" data-rel="popup" data-role="button">App简介</a>
                 <div data-role="popup" id="popAbout" class="ui-content">
                     <h2>App简介</h2>
                     <p>这是用jQuery Mobile打造的App接口</p>
                 </div>
             </div>
             <div data-role="footer" data-position="fixed">
                 <h4>页脚</h4>
             </div>
         </div>

         <div data-role="page" id="p2">
             <div data-role="header">
                 <h1>第 2 页</h1>
             </div>
             <div data-role="content">
                 <a href="#" data-role="button" data-inline="true" data-rel="back">上一页</a>
                 <br>
                 <a href="faq.html" data-role="button" rel="external">开启FAQ</a>
             </div>
             <div data-role="footer" data-position="fixed">
                 <h4>页脚</h4>
             </div>
         </div>

         <div data-role="page" id="p3">
             <div data-role="header">
                 <h1>第 3 页</h1>
             </div>
             <div data-role="content">
                 <a href="#" data-role="button" data-inline="true" data-rel="back">上一页</a>
             </div>
             <div data-role="footer" data-position="fixed">
                 <h4>页脚</h4>
             </div>
         </div>

     </body>
 </html>

wifiBot

index.html

 <!DOCTYPE html>
 <html>
     <head>
         <meta charset="utf-8">
         <title>灯光控制器</title>
         <meta name="format-detection" content="telephone=no">
         <meta name="msapplication-tap-highlight" content="no">
         <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
         <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" />
         <script src="cordova.js"></script>
         <script src="js/jquery-2.1.3.min.js"></script>
         <script>
         $(document).on('mobileinit', function() {
             $.mobile.pushStateEnabled = false;
         });
         </script>
         <script src="js/jquery.mobile-1.4.5.min.js"></script>
         <script src="js/index.js"></script>
         <style type="text/css">
         #splashPage {
             background-color:#fac800;
             text-align: center;
         }
         #splashPage div h1 {
             border-radius: 10px;
             padding:10px 30px;
             display:inline-block;
             background-color:#fff;
             margin-bottom:30px;
         }
         </style>
     </head>
     <body>
     <!-- 起始画面 -->
     <div data-role="page" id="splashPage">
         <div role="main" class="ui-content">
             <h1>网络灯光控制器</h1>
             <img src="data:images/app_logo.svg" width="300" height="300">
         </div>
     </div>

     <!-- 主控画面 --->
     <div data-role="page" id="ctrlPage" data-theme="a">
         <div data-role="header">
             <h1>灯光控制器</h1>
             <a href="#netPage" class="ui-btn-right ui-btn ui-btn-inline ui-mini ui-corner-all ui-btn-icon-right ui-icon-gear">网络设置</a>
         </div>

         <div role="main" class="ui-content">
             <div class="ui-field-contain">
                 <label for="ledSw">壁灯</label>
                 <select name="ledSw" id="ledSw" data-role="slider" data-mini="true">
                     <option value="0">关</option>
                     <option value="1">开</option>
                 </select>
             </div>
             <div class="ui-field-contain">
                 <label for="pwmSlider">台灯</label>
                 <input type="range" name="pwmSlider" id="pwmSlider" value="25" min="0" max="100"  data-highlight="true">
             </div>
             <div id="msg"></div>
         </div>

         <div data-role="footer" data-position="fixed">
           &copy; 2016 swf.com.tw
         </div>
     </div>

     <!-- "WiFi设置画面" -->
     <div data-role="page" id="netPage" data-theme="a">
         <div data-role="header">
             <h1>网络链接设置</h1>
         </div>

         <div role="main" class="ui-content">
             <form>
                 <label for="deviceIP">设备IP地址或网域名</label>
                 <input type="text" name="deviceIP" id="deviceIP" value="" placeholder="jarvis.local" data-clear-btn="true">
                 <label for="devicePort">设备端口号</label>
                 <input type="number" name="devicePort" id="devicePort" value="80" data-clear-btn="true">
             </form>
             <p><a href="#" id="saveBtn" class="ui-btn ui-corner-all ui-btn-b ui-btn-icon-left ui-icon-refresh ui-shadow-icon">保存设置</a></p>
             <p><a href="#ctrlPage" class="ui-btn ui-shadow ui-corner-all ui-btn-b">回主控画面</a></p>

         </div>
         <div data-role="footer" data-position="fixed">
           &copy; 2016 swf.com.tw
         </div>
     </div>
 </body>
 </html>

index.js

 var app = {
     nextPage:"",
     host:"",
     port:80,
     splashTime:3000,
     init: function() {
         $(document).on('deviceready', app.onDeviceReady);
     },
     onDeviceReady: function() {
         $("#ledSw").on("change", function(e){
             var val = $(this).val();
             var swData = {"pin":13, "sw":val};
             // 向第13脚送出开关信号
             $.ajax({
                 url: "http://" + app.host + ":" + app.port + "/sw",
                 data: swData,
                 success: function( d ) {
                     app.showMsg("收到服务器回应:" + d );
                 }
             });
         });

         $('#saveBtn').on('tap', function(){
             app.host = $("#deviceIP").val();
             app.port = $("#devicePort").val();
             localStorage.setItem('deviceIP', app.host);
             localStorage.setItem('devicePort', app.port);
             location.hash = 'ctrlPage';
         });
     },
     timeoutId: 0,
     showMsg: function(msg) {
         if (app.timeoutId) {
             clearTimeout(app.timeoutId);
         }
         $('#msg').text(msg);
         app.timeoutId = setTimeout(function() { $('#msg').text(""); }, 4000);
     },
     splashTimer : function(){
        setTimeout(function() {
            // 进入下一页
           location.hash = app.nextPage;
        }, app.splashTime);
     }
 };

 $(document).on("pageshow", "#ctrlPage", pageEvt);
 function pageEvt (e) {
   $( "#pwmSlider" ).on( "slidestop", function( e ) {
     var pwm = Math.ceil($(this).val() * 2.55);
     var pwmData = {"pin":9, "pwm":pwm};
     // 向第9脚送出PWM信号
     $.ajax({
         url: "http://" + app.host + ":" + app.port + "/pwm",
         data: pwmData,
         success: function( d ) {
             app.showMsg("收到服务器回应:" + d );
         }
     });
   });

   $(document).off("pageshow", "#ctrlPage", pageEvt);
 }

 $(document).on("pageshow", "#splashPage", function( e ) {
     var host = localStorage.getItem('deviceIP');
     if (host === null) {
       app.nextPage = "netPage";
     } else {
       app.host = host;
       app.port = localStorage.getItem('devicePort');
       $("#deviceIP").val(app.host);
       $("#devicePort").val(app.port);
       app.nextPage = "ctrlPage";
     }
     // 启动计时器
     app.splashTimer();
 });

 app.init();