国内综合精彩aⅤ无码不卡_日本少妇做爰全过程_欧美性爱在线播放免费_午夜?V日韩一区二区_免费a级毛片无码免费播放_成在人线av无码喷水_亚洲精品网站色视频_国产婷婷精品成人_老师撩起裙子让我桶的视频_秋霞影院国产

關(guān)于OPENWRT路由數(shù)據(jù)接口respone.lua內(nèi)命令的寫法

作者:hzl88688 | 更新時間:2016-03-11 | 瀏覽量:5086

-----------------------------------------------------------------------------
-- Openwrt與貝殼物聯(lián)平臺通訊示例
-- http://www.placeboworld.cn/help/5.html
-- Author: 貝殼物聯(lián)
-- Modify: hzl88688

-- Time: 2016/1/10
-----------------------------------------------------------------------------
local socket = require("socket")--引入Luasocket
local json = require("json")--引入Json4lua
local util = require "luci.util"--引入luci,調(diào)用cup負(fù)載
------------此處需修改-------------
DEVICEID = "xx" --設(shè)備ID
APIKEY = "xxxxxxxx" --設(shè)備APIKEY
INPUTID = "xx" --數(shù)據(jù)接口ID
-----------------------------------
host =  "www.placeboworld.cn"
port =  8181
lastTime = 0
lastUpdateTime = 0
if arg then
    host = arg[1] or host
    port = arg[2] or port
end
print("Attempting connection to host '" ..host.. "' and port " ..port.. "...")
c = assert(socket.connect(host, port))
c:settimeout(0)
print("Connected! Please type stuff (empty line to stop):")
while true do
    if ((os.time() - lastTime) > 40) then
        --print( os.time() )
        s = json.encode({M='checkin',ID=DEVICEID,K=APIKEY})
        assert(c:send( s.."\n" ))
        lastTime=os.time()
    end
    if ((os.time() - lastUpdateTime) > 10) then
        local sysinfo = luci.util.ubus("system", "info") or { }
        local load = sysinfo.load or { 0, 0, 0 } --獲取Openwrt系統(tǒng)負(fù)載
        local v = {[INPUTID]=load[1]} --多個接口數(shù)據(jù)可用v = {[INPUTID1]=load[1],[INPUTID2]=load[2]}
        local update = json.encode({['M']='update', ['ID']=DEVICEID, ['V']=v})
        assert(c:send( update.."\n" ))
        lastUpdateTime = os.time()
    end
    recvt, sendt, status = socket.select({c}, nil, 1)
    --#獲取table長度,即元素數(shù)
    while #recvt > 0 do
        local response, receive_status = c:receive()
        if receive_status ~= "closed" then
            if response then
                --print(response)
                r = json.decode(response)
                --table.foreach(r, print)
                if r.C then
                    if r.M == "say" then
-------------------------switch-case結(jié)構(gòu)開始(要添加命令只需要復(fù)制一段函數(shù)改相應(yīng)變量即可)-------------------
                             switch={}
                                  switch["name"]=function()   

                                                         luci.util.exec("xxxxxx")  ----------此個XXXXXX填入路由要執(zhí)行的命令

                                                         s = json.encode({M='say',ID=r.ID,C=' 我的名字叫玲玲!'})
                                                        assert(c:send( s.."\n" ))
                                                         end
                                           switch["age"]=function()
                                                       luci.util.exec("xxxxxx")  ----------此個XXXXXX填入路由要執(zhí)行的命令

                                                        s = json.encode({M='say',ID=r.ID,C=' 我剛出生喲!'})
                                                        assert(c:send( s.."\n" ))
                                                      end
                                           switch["play"]=function()
                                                 luci.util.exec("xxxxxx")  ----------此個XXXXXX填入路由要執(zhí)行的命令
                                                               s = json.encode({M='say',ID=r.ID,C=' 你開啟了音樂播放,音樂現(xiàn)在播放中.......'})
                                                              assert(c:send( s.."\n" ))
                                                        end
                                           switch["help"]=function()
                                    s = json.encode({M='say',ID=r.ID,C=' 我叫玲玲,我是物聯(lián)網(wǎng)Openwrt示例,你可以嘗試輸入name、age或help,也可以查看實時數(shù)據(jù),我隨時報告我的負(fù)荷情況,還可以遠(yuǎn)程啟動音樂播放。'})
                                 assert(c:send( s.."\n" ))
                                                      end
                                      
--------------switch-case結(jié)構(gòu)結(jié)束(要添加命令只需要復(fù)制一段函數(shù)改相應(yīng)變量即可--------------       
                                            if ( switch[r.C]) then
                                                  switch[r.C]()
                                            else                
                  s = json.encode({M='say',ID=r.ID,C='朋友,你輸入的命令我讀不懂!你可以嘗試輸入name、age、play或help 。'})
                         assert(c:send( s.."\n" ))             
                                            end                                                                

                    end
                end
                recvt, sendt, status = socket.select({c}, nil, 1)
            end
        else
            break
        end
    end
end


評論:共3條

貝殼物聯(lián) 評論于:2016-03-11 16:07:18
多謝分享,大家都像你一樣樂于分享就好了-_-!
另外你把代碼部分在編輯器中標(biāo)記為代碼,就可以高亮顯示,看著更舒服了。
hzl88688 評論于:2016-03-11 16:54:23
多謝提醒!
a386554965 評論于:2018-06-17 09:29:41
學(xué)習(xí)一下
返回頂部