使用python代码同步烧瓶网页

时间:2015-12-12 10:30:41

标签: python

打开在我的pi上运行的警报系统我使用2个脚本,index.html提供带有开/关按钮的网页和webserver.py,它传递html文件并控制子进程中的主要警报脚本。 我没有那么好的编码器和.html和.py中使用的烧瓶代码我经过一些研究后发现,我为我的项目调整了它并且工作正常。但我想改变两件事。

  1. 第一个(也是最重要的)是开/关按钮 不保存警报状态;我在手机上打开它 当我妻子的电话登录后,按钮的状态就会出现 永远是默认的,关闭。我们可以关闭闹钟 那种情况通过按下然后关闭但我更喜欢按钮 将与所有连接的设备同步,但我不知道如何 去做。

  2. -2th(不是那么重要),我想将滑块更改为具有不同开/关图像的按钮,以便更好地集成     html布局(还有待做)。

  3. 希望任何人都可以提供帮助。以下是代码:

        <!doctype html><head>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script>
    </head>
    <style>
    h3, h4 {text-align: center;}
    span {font-weight: bold;}
    </style>
    <script type=text/javascript>
        $(
        // When the LED button is pressed (change)
        // do an ajax request to server to change LED state
        function()
        {
            $('#flip-1').change(function()
            {
            $.getJSON('/_led', {state: $('#flip-1').val()});
            });
        }
        );
    </script>
    <!-- Simple JQuery Mobile page that display the button state on the breadoard -->
    <!-- You can also change the LED state with the slider switch -->
    <!-- The Raspberry Pi uptime is displayed in the footer (Jinja2 expands the template tag) -->
    <div data-role="page" data-theme="b">
      <div data-role="header">
        <div><h3>Alarm System Web Control V1.0</h3></div>
      </div>
      <div data-role="content">
        <form>
        <p>Alarm is <span id="buttonState"></span></p>
        <br>
        <select name="flip-1" id="flip-1" data-role="slider" style="float: left;">
            <option value="off" selected="selected">UIT</option>
            <option value="on">AAN</option>
        </select>
        </form>
      </div>
     <div data-role="footer">
         <div><h4>Time System Is {{uptime}}</h4></div>
      </div>
    </div>
    

        #!/usr/bin/python
    
    from flask import Flask, render_template, request, jsonify
    import webguitest
    import time
    import subprocess
    
    app = Flask(__name__)
    
    P2 = 8 # redled
    
    # return index page when IP address of RPi is typed in the browser
    @app.route("/")
    def Index():
        return render_template("index.html", uptime=GetUptime())
    
    # ajax GET call this function to set led state
    # depeding on the GET parameter sent
    @app.route("/_led")
    def _led():
        state = request.args.get('state')
        if state=="on":
    #       time.sleep(60) #give owner time to clear before alarm starts
            webguitest.LEDon()
        else:
            webguitest.LEDoff()
        return ""
    
    def _reboot():
        state = request.args.get('state')
        if state=="on":
            p = subprocess.Popen(["sudo", "reboot"], stdout=subprocess.PIPE)
        else:
            webguitest.LEDoff()
        return ""
    
    
    # ajax GET call this function periodically to read button state
    # the state is sent back as json data
    @app.route("/_button")
    def _button():
        if webguitest.ReadButton():
            state = "pressed"
        else:
            state = "not pressed"
        return jsonify(buttonState=state)
    
    def GetUptime():
    # get uptime from the linux terminal command
        from subprocess import check_output
        output = check_output(["uptime"])
        # return only uptime info
        uptime = output[output.find("up"):output.find("user")-5]
        return uptime
    
    # run the webserver on standard port 80, requires sudo
    if __name__ == "__main__":
        webguitest.Init()
        app.run(host='0.0.0.0', port=80, debug=True)
    

0 个答案:

没有答案