1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
module(...,package.seeall)
require "aLiYun" require "misc" require "nvm"
local PRODUCT_KEY = "你自己的产品key" local PRODUCE_SECRET="你自己的产品密钥" local Shadow_Changed = false
local function getDeviceName() return misc.getImei() end
local function setDeviceSecret(s) misc.setSn(s) end
local function getDeviceSecret() return misc.getSn() end
sConnected = false
local function publishTestCb(result,para) log.info("myALiYun.publishTestCb",result,para) end
function updateShadow(desired) if sConnected then desired["rssi"] = net.getRssi() desired["PowerSwitch"] = nvm.get("PowerSwitch") local jdata = json.encode(desired) _G.shadow_version = _G.shadow_version + 1 local resp = "{\"method\":\"update\",\"state\": {\"reported\":" .. jdata .."},\"version\":".._G.shadow_version.."}" aLiYun.publish("/shadow/update/"..PRODUCT_KEY.."/"..getDeviceName(), resp, 1,publishTestCb, resp) else log.info("sorry, sConnected=false") end end
local function rcvCbFnc(topic,qos,payload) log.info("myALiYun.rcvCbFnc",topic,qos,payload) if topic == "/shadow/get/"..PRODUCT_KEY.."/"..getDeviceName() then local shadow,result,errinfo = json.decode(payload) if result then log.info("Shadow Method=" .. shadow["method"]) if shadow["version"] ~= nil then _G.shadow_version = shadow["version"] log.info("Shadow Version = " .. _G.shadow_version) end local desired = nil if shadow["method"] == "reply" and shadow["payload"]["state"] ~= nil and shadow["payload"]["state"]["desired"] ~= nil then return end if shadow["method"] == "control" and shadow["payload"]["state"] ~= nil and shadow["payload"]["state"]["desired"] ~= nil then desired = shadow["payload"]["state"]["desired"] end if desired ~= nil then log.info("Shadow control/reply begin ...") local tmp = json.encode(desired) log.info("desired=" .. tmp) Shadow_Changed = false if Shadow_Changed then updateShadow(desired) end log.info("Shadow control ... end") end else log.info("Bad Shadow JSON!") end return end local tjsondata,result,errinfo = json.decode(payload) if result then else log.info("BAD JSON") end end
local function connectCbFnc(result) log.info("testALiYun.connectCbFnc",result) sConnected = result if result then aLiYun.subscribe({["/"..PRODUCT_KEY.."/"..getDeviceName().."/get"]=0, ["/"..PRODUCT_KEY.."/"..getDeviceName().."/get"]=1, ["/shadow/get/"..PRODUCT_KEY.."/"..getDeviceName()]=0}) aLiYun.on("receive",rcvCbFnc) aLiYun.publish("/shadow/update/"..PRODUCT_KEY.."/"..getDeviceName(), "{\"method\":\"get\"}", 1,publishTestCb, "{\"method\":\"get\"}") end end
aLiYun.setup(PRODUCT_KEY,PRODUCE_SECRET,getDeviceName,getDeviceSecret,setDeviceSecret)
aLiYun.on("connect",connectCbFnc)
require"aLiYunOta"
|