mod_pubsub_mqtt/mod_pubsub_mqtt.lua
changeset 5116 9499b88f3453
parent 5115 6ba2188e2686
child 5117 85a7304cfea1
equal deleted inserted replaced
5115:6ba2188e2686 5116:9499b88f3453
     6 local pubsub_services = {};
     6 local pubsub_services = {};
     7 local pubsub_subscribers = {};
     7 local pubsub_subscribers = {};
     8 local packet_handlers = {};
     8 local packet_handlers = {};
     9 
     9 
    10 function handle_packet(session, packet)
    10 function handle_packet(session, packet)
    11 	module:log("warn", "MQTT packet received! Length: %d", packet.length);
    11 	module:log("debug", "MQTT packet received! Length: %d", packet.length);
    12 	for k,v in pairs(packet) do
    12 	for k,v in pairs(packet) do
    13 		module:log("debug", "MQTT %s: %s", tostring(k), tostring(v));
    13 		module:log("debug", "MQTT %s: %s", tostring(k), tostring(v));
    14 	end
    14 	end
    15 	local handler = packet_handlers[packet.type];
    15 	local handler = packet_handlers[packet.type];
    16 	if not handler then
    16 	if not handler then
    30 function packet_handlers.disconnect(session, packet)
    30 function packet_handlers.disconnect(session, packet)
    31 	session.conn:close();
    31 	session.conn:close();
    32 end
    32 end
    33 
    33 
    34 function packet_handlers.publish(session, packet)
    34 function packet_handlers.publish(session, packet)
    35 	module:log("warn", "PUBLISH to %s", packet.topic);
    35 	module:log("info", "PUBLISH to %s", packet.topic);
    36 	local host, node = packet.topic:match("^([^/]+)/(.+)$");
    36 	local host, node = packet.topic:match("^([^/]+)/(.+)$");
    37 	local pubsub = pubsub_services[host];
    37 	local pubsub = pubsub_services[host];
    38 	if not pubsub then
    38 	if not pubsub then
    39 		module:log("warn", "Unable to locate host/node: %s", packet.topic);
    39 		module:log("warn", "Unable to locate host/node: %s", packet.topic);
    40 		return;
    40 		return;
    49 	end
    49 	end
    50 end
    50 end
    51 
    51 
    52 function packet_handlers.subscribe(session, packet)
    52 function packet_handlers.subscribe(session, packet)
    53 	for _, topic in ipairs(packet.topics) do
    53 	for _, topic in ipairs(packet.topics) do
    54 		module:log("warn", "SUBSCRIBE to %s", topic);
    54 		module:log("info", "SUBSCRIBE to %s", topic);
    55 		local host, node = topic:match("^([^/]+)/(.+)$");
    55 		local host, node = topic:match("^([^/]+)/(.+)$");
    56 		local pubsub = pubsub_subscribers[host];
    56 		local pubsub = pubsub_subscribers[host];
    57 		if not pubsub then
    57 		if not pubsub then
    58 			module:log("warn", "Unable to locate host/node: %s", topic);
    58 			module:log("warn", "Unable to locate host/node: %s", topic);
    59 			return;
    59 			return;