IoT Control Center compatibility
IoT Control Center compatibility

file:a/mqtt.lua -> file:b/mqtt.lua
--- a/mqtt.lua
+++ b/mqtt.lua
@@ -25,73 +25,76 @@
 end)
 
 -- on receive message
-mqtt:on("message", function(conn, topic, data)
+mqtt:on("message", function(conn, topic, message)
 
-    print ("\tReceived topic : " .. topic .. " / data : " .. data)
+    print ("\tReceived topic : " .. topic .. " / message : " .. message)
 
-    if (topic == prefix .. device .. "/toggle1/control") then
-        if data == "1" then
+    local msg = cjson.decode(message)
+
+    -- catch json errors
+    local json, msg = pcall(function()
+        return cjson.decode(message)
+    end)
+
+    if json then
+
+    else
+        print("\tError parsing JSON : " .. message)
+        return
+    end
+
+    if (topic == prefix .. device .. "/toggle1/data") then
+        if msg.status == "1" then
             print "\t\tReceived message ON@Relay1"
             gpio.write(gpio12, gpio.HIGH)
             status1 = 1
-        elseif data == "0" then
+        elseif msg.status == "0" then
             print "\t\tReceived message OFF@Relay1"
             gpio.write(gpio12, gpio.LOW)
             status1 = 0
         end
-        -- publish status to be in sync with all devices connected
-        mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
+        -- publish status confirmation to be in sync with all devices connected
+        mqtt:publish(prefix .. device .. "/toggle1/confirm", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
         end)
-    elseif topic == prefix .. device .. "/toggle2/control" then
-        if data == "1" then
+    elseif topic == prefix .. device .. "/toggle2/data" then
+        if msg.status == "1" then
             print "\t\tReceived message ON@Relay2"
             gpio.write(gpio13, gpio.HIGH)
             status2 = 1
-        elseif data == "0" then
+        elseif msg.status == "0" then
             print "\t\tReceived message OFF@Relay2"
             gpio.write(gpio13, gpio.LOW)
             status2 = 0
         end
-        -- publish status to be in sync with all devices connected
-        mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
+        -- publish status confirmation to be in sync with all devices connected
+        mqtt:publish(prefix .. device .. "/toggle2/confirm", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
         end)
-    else
-        local list = {}
-        local k = 0
-        for v in string.gmatch(topic, "([^/]+)") do
-            list[k] = v
-            k = k + 1
-        end
-        local id = list[1]
-        local msg = cjson.decode(data)
-        print('\tNew request from IoT Manager: id="' .. id .. '", command="' .. msg.command .. '", param="' .. msg.param .. '"');
+    elseif topic == prefix .. "/device" then
+        print('\tNew request from IoT Control Center: clientId="' .. clientId .. '"');
 
-        if msg.command == "getPages" then
-            mqtt:publish(prefix .. device .. "/response", '{"pages" : [{"pageId" : 10, "pageName" : "Page 1"}, {"pageId" : 20, "pageName" : "Page 2"}]}', 1, 0, function(conn)
-            end)
-        elseif msg.command == "getPageById" then
-            if (msg.param == "10" or msg.param == "0") then
-                msg = '{"id":"1", "page": "Page 1", "pageId": 10, "widget":"toggle", "descr":"Relay 1", "topic":"' .. prefix .. device ..'/toggle1"}'
-                mqtt:publish(prefix .. device .. "/config", msg, 1, 0, function(conn)
-                end)
-                mqtt:publish(prefix .. device .. "/toggle1/status", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
-                end)
-            end
-            if (msg.param == "20" or msg.param == "0") then
-                msg = '{"id":"2", "page": "Page 2", "pageId": 20, "widget":"toggle", "descr":"Relay 2", "topic":"' .. prefix .. device ..'/toggle2"}'
-                mqtt:publish(prefix .. device .. "/config", msg, 1, 1, function(conn)
-                end)
-                mqtt:publish(prefix .. device .. "/toggle2/status", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
-                end)
-            end
-        end
+        -- check clientId's privileges here
+
+        mqtt:publish(prefix .. device .. "/device", '{"pages" : [{"pageId" : 10, "pageName" : "Page 1"}, {"pageId" : 20, "pageName" : "Page 2"}]}', 1, 0, function(conn)
+        end)
+
+        msg = '{"pageName": "Dogs heating", "pageId": 10, "widget":"toggle", "title":"Mara", "topic":"' .. prefix .. device ..'/toggle1", "checked":' .. status1 ..', "template": "template-1", "icon": "ion-ios-paw", "class4": "bg-green", "order" : 10}'
+        mqtt:publish(prefix .. device .. "/toggle1/config", msg, 1, 0, function(conn)
+        end)
+        mqtt:publish(prefix .. device .. "/toggle1/confirm", "{\"status\":" .. status1 .. "}", 1, 0, function(conn)
+        end)
+
+        msg = '{"pageName": "Dogs heating", "pageId": 10, "widget":"toggle", "title":"Linda", "topic":"' .. prefix .. device ..'/toggle2", "checked":' .. status2 ..', "template": "template-1", "icon": "ion-ios-paw", "class4": "bg-green", "order" : 20}'
+        mqtt:publish(prefix .. device .. "/toggle2/config", msg, 1, 1, function(conn)
+        end)
+        mqtt:publish(prefix .. device .. "/toggle2/confirm", "{\"status\":" .. status2 .. "}", 1, 0, function(conn)
+        end)
     end
 end)
 
 mqtt:connect(mqttConfig.host, mqttConfig.port, mqttConfig.secure, 1, function(conn)
     print ("\tConnected to " .. mqttConfig.host .. ":" .. mqttConfig.port .. " MQTT broker\n")
     -- subscribe topic with qos = 1
-    mqtt:subscribe({[prefix .. "/+/+/control"]=1, [prefix .. "/+/request"]=1}, function(conn)
+    mqtt:subscribe({[prefix .. "/+/+/data"]=1, [prefix .. "/device"]=1}, function(conn)
     end)
 end)
 

comments