关于OPENWRT路由数据接口respone.lua内命令的写法

作者:hzl88688 | 更新时间:2016-03-11

-----------------------------------------------------------------------------
-- Openwrt与贝壳物联平台通讯示例
-- http://www.bigiot.net/help/5.html
-- Author: 贝壳物联
-- Modify: hzl88688

-- Time: 2016/1/10
-----------------------------------------------------------------------------
local socket = require("socket")--引入Luasocket
local json = require("json")--引入Json4lua
local util = require "luci.util"--引入luci,调用cup负载
------------此处需修改-------------
DEVICEID = "xx" --设备ID
APIKEY = "xxxxxxxx" --设备APIKEY
INPUTID = "xx" --数据接口ID
-----------------------------------
host =  "www.bigiot.net"
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系统负载
        local v = {[INPUTID]=load[1]} --多个接口数据可用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长度,即元素数
    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结构开始(要添加命令只需要复制一段函数改相应变量即可)-------------------
                             switch={}
                                  switch["name"]=function()   

                                                         luci.util.exec("xxxxxx")  ----------此个XXXXXX填入路由要执行的命令

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

                                                        s = json.encode({M='say',ID=r.ID,C=' 我刚出生哟!'})
                                                        assert(c:send( s.."\n" ))
                                                      end
                                           switch["play"]=function()
                                                 luci.util.exec("xxxxxx")  ----------此个XXXXXX填入路由要执行的命令
                                                               s = json.encode({M='say',ID=r.ID,C=' 你开启了音乐播放,音乐现在播放中.......'})
                                                              assert(c:send( s.."\n" ))
                                                        end
                                           switch["help"]=function()
                                    s = json.encode({M='say',ID=r.ID,C=' 我叫玲玲,我是物联网Openwrt示例,你可以尝试输入name、age或help,也可以查看实时数据,我随时报告我的负荷情况,还可以远程启动音乐播放。'})
                                 assert(c:send( s.."\n" ))
                                                      end
                                      
--------------switch-case结构结束(要添加命令只需要复制一段函数改相应变量即可--------------       
                                            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条

贝壳物联 评论于:2016-03-11 16:07:18
多谢分享,大家都像你一样乐于分享就好了-_-!
另外你把代码部分在编辑器中标记为代码,就可以高亮显示,看着更舒服了。
hzl88688 评论于:2016-03-11 16:54:23
多谢提醒!
a386554965 评论于:2018-06-17 09:29:41
学习一下
评论支持使用[code][/code]标签添加代码
返回顶部