iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 23
1

好的,前兩篇完成了溫濕度及電流的按鈕回傳數值功能,
接下來就要完成其他的了,
接下來要完成的是控制及電錶度數查詢功能,
控制的部分我們的畫面設想應該是這樣,

狀態的地方要去抓取資料庫當前的狀態並呈現出來,
開啟關閉要能反轉其狀態,
畫面設計如下,

class controlwind():
    def returna(self):
        controlprintdata = []
        for data in controldata.find():
            controlprintdata = data
        flex_message = FlexSendMessage(
            alt_text='hello',
            contents={
            "type": "carousel",
            "contents": [
                {
                "type": "bubble",
                "size": "micro",
                "hero": {
                    "type": "image",
                    "url": "https://i.imgur.com/O8lp0mk.png",
                    "size": "full",
                    "aspectMode": "fit",
                    "aspectRatio": "320:213",
                    "backgroundColor": "#FFEE99"
                },
                "body": {
                    "type": "box",
                    "layout": "vertical",
                    "contents": [
                    {
                        "type": "text",
                        "text": "排風風扇",
                        "weight": "bold",
                        "size": "md"
                    },
                    {
                        "type": "box",
                        "layout": "baseline",
                        "contents": [
                        {
                            "type": "text",
                            "text": "狀態:" + str(controlprintdata["humidity"]) ,
                            "size": "xxs",
                            "color": "#8c8c8c",
                            "margin": "xs",
                            "flex": 0
                        }
                        ]
                    },
                    {
                        "type": "box",
                        "layout": "vertical",
                        "contents": [
                        {
                            "type": "button",
                            "action": {
                            "type": "postback",
                            "label": "開啟",
                            "data": "開啟",
                            "displayText": "開啟"
                            }
                        },
                        {
                            "type": "button",
                            "action": {
                            "type": "postback",
                            "label": "關閉",
                            "data": "關閉",
                            "displayText": "關閉"
                            }
                        }
                        ]
                    }
                    ],
                    "spacing": "sm",
                    "paddingAll": "13px"
                }
                },
                {
                "type": "bubble",
                "size": "micro",
                "hero": {
                    "type": "image",
                    "url": "https://i.imgur.com/icAeax3.png",
                    "size": "full",
                    "aspectMode": "fit",
                    "aspectRatio": "320:213",
                    "backgroundColor": "#FFEE99"
                },
                "body": {
                    "type": "box",
                    "layout": "vertical",
                    "contents": [
                    {
                        "type": "text",
                        "text": "進風風扇",
                        "weight": "bold",
                        "size": "lg"
                    },
                    {
                        "type": "box",
                        "layout": "vertical",
                        "contents": [
                        {
                            "type": "box",
                            "layout": "baseline",
                            "spacing": "sm",
                            "contents": [
                            {
                                "type": "text",
                                "text": "狀態:"+ str(controlprintdata["inputFan"]) ,
                                "color": "#8c8c8c",
                                "size": "xxs",
                                "flex": 5
                            }
                            ]
                        }
                        ]
                    },
                    {
                        "type": "box",
                        "layout": "vertical",
                        "contents": [
                        {
                            "type": "button",
                            "action": {
                            "type": "postback",
                            "label": "開啟",
                            "data": "開啟",
                            "displayText": "開啟"
                            }
                        },
                        {
                            "type": "button",
                            "action": {
                            "type": "postback",
                            "label": "關閉",
                            "data": "關閉",
                            "displayText": "關閉"
                            }
                        }
                        ]
                    }
                    ],
                    "spacing": "sm",
                    "paddingAll": "13px"
                }
                },
                {
                "type": "bubble",
                "size": "micro",
                "hero": {
                    "type": "image",
                    "url": "https://i.imgur.com/0GJsShU.jpg",
                    "size": "full",
                    "aspectMode": "cover",
                    "aspectRatio": "320:213"
                },
                "body": {
                    "type": "box",
                    "layout": "vertical",
                    "contents": [
                    {
                        "type": "text",
                        "text": "加濕器",
                        "weight": "bold",
                        "size": "lg"
                    },
                    {
                        "type": "box",
                        "layout": "vertical",
                        "contents": [
                        {
                            "type": "box",
                            "layout": "baseline",
                            "spacing": "sm",
                            "contents": [
                            {
                                "type": "text",
                                "text": "狀態:"+ str(controlprintdata["outputFan"]),
                                "color": "#8c8c8c",
                                "size": "xxs",
                                "flex": 5
                            }
                            ]
                        },
                        {
                            "type": "box",
                            "layout": "vertical",
                            "contents": [
                            {
                                "type": "button",
                                "action": {
                                "type": "postback",
                                "label": "開啟",
                                "data": "開啟",
                                "displayText": "開啟"
                                }
                            },
                            {
                                "type": "button",
                                "action": {
                                "type": "postback",
                                "label": "關閉",
                                "data": "關閉",
                                "displayText": "關閉"
                                }
                            }
                            ]
                        }
                        ]
                    }
                    ],
                    "spacing": "sm",
                    "paddingAll": "13px"
                }
                }
            ]
            }
        )
        return flex_message

這樣就能從資料庫獲取狀態資料了,
views.py的部分則是加上這些,

for event in events:
            if isinstance(event, MessageEvent):  # 如果有normal訊息事件

                if event.message.text == "功能列表":
                
                    line_bot_api.reply_message(  # 回復「功能列表」按鈕樣板訊息
                        event.reply_token,
                        Featuresmodel().content()
                    )
                if event.message.text == "控制":
                
                    line_bot_api.reply_message(  # 回復「控制」按鈕輪播訊息
                        event.reply_token,
                        controlwind().returna()
                    )

多了一個message判別就可以了喔~
實際demo畫面就是上面那個構想圖啦~
而電錶數值查詢則跟昨天的溫室度沒有差異,
在下方的postback新增這些

# 電錶
                elif event.postback.data[0] == "電" and event.postback.data[1] == '錶': 
                    
                    line_bot_api.reply_message(
                        event.reply_token,
                        TextSendMessage(text=returnvalue().roomva())
                    )

再從資料庫獲得資料在message.py裡面就可以完成這個功能了,
而控制的反轉部分我還沒有完全完成,
所以就先不放上來了QQ


今天是 范逸臣 的 你愛我嗎 網址如下~~~

https://www.youtube.com/watch?v=HQgOYoD2eXY&list=PLZ_d6NX2sE80xcb4RjHSCBt3E1nTclmC4&index=11&ab_channel=%E8%B1%90%E8%8F%AF%E5%94%B1%E7%89%87ForwardMusic



上一篇
Day22 機房好狗走起(2)
下一篇
Day24 機房好狗走起(4)
系列文
狗狗 Line Bot 以python及Django建構30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言