IoT Python USB Relay Part 2

휴대폰에서 릴레이를 움직여보세요



relay 피 c. 라고 r. 코 m/아 5K게임 DfMXH — 히라타 유 (@hiratayutaka) 2018년 9월 27일




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>



부트 스트랩 4 용 리아웃 템플릿




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 문이 없지만"의 대답



파이썬의 입장에서 볼 때 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 문장이 없습니까? ?



파이썬에는 switch가 없지만


좋은 웹페이지 즐겨찾기