IoT Python USB Relay その2


携帯電話からリレーを動かいしてみる

file layout for bottle4 with bottle wsgi

relay
│  buttons.py
├─static
│  │  favicon.ico
│  │  favicon.png
│  │
│  ├─css
│  │      .DS_Store
│  │      bootstrap-grid.css
│  │      bootstrap-grid.css.map
│  │      bootstrap-grid.min.css
│  │      bootstrap-grid.min.css.map
│  │      bootstrap-reboot.css
│  │      bootstrap-reboot.css.map
│  │      bootstrap-reboot.min.css
│  │      bootstrap-reboot.min.css.map
│  │      bootstrap.css
│  │      bootstrap.css.map
│  │      bootstrap.min.css
│  │      bootstrap.min.css.map
│  │
│  └─js
│          bootstrap.bundle.js
│          bootstrap.bundle.js.map
│          bootstrap.bundle.min.js
│          bootstrap.bundle.min.js.map
│          bootstrap.js
│          bootstrap.js.map
│          bootstrap.min.js
│          bootstrap.min.js.map
│          jquery-3.3.1.min.js
│          popper.min.js
└─views
    │  buttons.tpl
    │  layout.tpl

Buttonを表示するためのテンプレート

buttons.tpl
% rebase('layout.tpl', title='Buttons')
<style>
.btnx {
    padding: 1px !important;
    margin: 1px !important;
    height: 60px !important;
    width:60px !important;
}
</style>
<br/>
<div class="container">
    % for i,x in enumerate(btns.items()):
        <button class="btnx btn {{"btn-"+colors[i%8]}}" onclick="location.href='/btn/{{x[1]}}'">{{x[0]}}</button>
    % end
</div>

bootstrap4用レアウトテンプレート

layout.tpl
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <meta name="apple-mobile-web-app-title" content="{{title}}">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ title }}</title>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet" />
    <link rel="shortcut icon" type="image/x-icon" href="/static/favicon.ico" />
    <link rel="apple-touch-icon" href="/static/favicon.png" />
    <script src="/static/js/jquery-3.3.1.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script>
    <script src="/static/js/popper.min.js"></script>
</head>
<body>
<div class="container">
{{!base}}
</div>
</body>
</html>

WSGIプログラム

bottle.py
from bottle import *
import serial
colors=["primary","secondary","success","danger","warning","info","light","dark"]
btn={'ON':'on','OFF':'off'} #ボタンの定義
btnf={k:False for k in btn} #ボタンのon/off flag

def ON(): #1
    with serial.Serial('COM4', 9600,timeout=0) as s : s.write(bytearray(b"\xA0\x01\x01\xA2")) 

def OFF(): #2
    with serial.Serial('COM4', 9600,timeout=0) as s : s.write(bytearray(b"\xA0\x01\x00\xA1")) 

switch={'on':ON,'off':OFF} #3

@route('/')
@view('buttons.tpl')
def home():
    return dict(btns=btn,msg="HOME",btnf=btnf,colors=colors)
@route('/btn/<btname>')
def btns(btname):
    if btname in switch :switch[btname]() #python switch 文 #4
    redirect("/")
@route('/static/<file_path:path>')
def static(file_path):
    return static_file(file_path, root='./static')
run(host="192.168.1.10",port=911)

解説

上記のプログラム中の#1,#2,#3,#4 「switch文がないけど」の答え

pythonの立場からするとswitch文は、いらない

if btname in switch :switch[btname]() #python switch 文 #4

key,valueのvalueに関数オブジェ定義している。

def ON(): #1
    with serial.Serial('COM4', 9600,timeout=0) as s : s.write(bytearray(b"\xA0\x01\x01\xA2")) 
def OFF(): #2
    with serial.Serial('COM4', 9600,timeout=0) as s : s.write(bytearray(b"\xA0\x01\x00\xA1")) 
switch={'on':ON,'off':OFF} #3

Pythonにswitch文無いんですか!?

pythonにswitchはないけれど