require"misc"
require"mqtt"
require"common"
module(...,package.seeall)

local ssub,schar,smatch,sbyte,slen = string.sub,string.char,string.match,string.byte,string.len
--ʱԼķ
local PROT,ADDR,PORT = "TCP","lbsmqtt.airm2m.com",1884
local mqttclient


--[[
print
  ӡӿڣļедӡtestǰ׺
  
ֵ
]]
local function print(...)
	_G.print("test",...)
end

local qos0cnt,qos1cnt = 1,1

--[[
pubqos0testsndcb
  1qosΪ0ϢͽĻص
  
		usertagmqttclient:publishʱusertag
		resulttrueʾͳɹfalsenilʧ
ֵ
]]
local function pubqos0testsndcb(usertag,result)
	print("pubqos0testsndcb",usertag,result)
	sys.timer_start(pubqos0test,10000)
	qos0cnt = qos0cnt+1
end

--[[
pubqos0test
  1qosΪ0Ϣ
  
ֵ
]]
function pubqos0test()
	--ע⣺ڴ˴Լȥpayloadݱ룬mqttвpayloadκαת
	mqttclient:publish("/qos0topic","qos0data",0,pubqos0testsndcb,"publish0test_"..qos0cnt)
end

--[[
pubqos1testackcb
  1qosΪ1ϢյPUBACKĻص
  
		usertagmqttclient:publishʱusertag
		resulttrueʾɹfalsenilʾʧ
ֵ
]]
local function pubqos1testackcb(usertag,result)
	print("pubqos1testackcb",usertag,result)
	sys.timer_start(pubqos1test,20000)
	qos1cnt = qos1cnt+1
end

--[[
pubqos1test
  1qosΪ1Ϣ
  
ֵ
]]
function pubqos1test()
	--ע⣺ڴ˴Լȥpayloadݱ룬mqttвpayloadκαת
	mqttclient:publish("/qos1topic","qos1data",1,pubqos1testackcb,"publish1test_"..qos1cnt)
end

--[[
subackcb
  MQTT SUBSCRIBE֮յSUBACKĻص
  
		usertagmqttclient:subscribeʱusertag
		resulttrueʾĳɹfalsenilʾʧ
ֵ
]]
local function subackcb(usertag,result)
	print("subackcb",usertag,result)
end

--[[
rcvmessage
  յPUBLISHϢʱĻص
  
		topicϢ⣨gb2312룩
		payloadϢأԭʼ룬յpayloadʲôݣʲôݣûκαת
		qosϢȼ
ֵ
]]
local function rcvmessagecb(topic,payload,qos)
	print("rcvmessagecb",topic,payload,qos)
end

--[[
discb
  MQTTӶϿĻص
  		
ֵ
]]
local function discb()
	print("discb")
	--20½MQTT
	sys.timer_start(connect,20000)
end

--[[
disconnect
  ϿMQTT
  		
ֵ
]]
local function disconnect()
	mqttclient:disconnect(discb)
end

--[[
connectedcb
  MQTT CONNECTɹص
  		
ֵ
]]
local function connectedcb()
	print("connectedcb")
	--
	mqttclient:subscribe({{topic="/event0",qos=0}, {topic="/event1",qos=1}}, subackcb, "subscribetest")
	--ע¼ĻصMESSAGE¼ʾյPUBLISHϢ
	mqttclient:regevtcb({MESSAGE=rcvmessagecb})
	--һqosΪ0Ϣ
	pubqos0test()
	--һqosΪ1Ϣ
	pubqos1test()
	--20ϿMQTT
	--sys.timer_start(disconnect,20000)
end

--[[
connecterrcb
  MQTT CONNECTʧܻص
  
		rʧԭֵ
			1Connection Refused: unacceptable protocol version
			2Connection Refused: identifier rejected
			3Connection Refused: server unavailable
			4Connection Refused: bad user name or password
			5Connection Refused: not authorized
ֵ
]]
local function connecterrcb(r)
	print("connecterrcb",r)
end

--[[
sckerrcb
  SOCKET쳣صע⣺˴ǻָ쳣һַʽ<ģʽӺ˳ģʽ>޷Լ󣬿Լ쳣
  
		rstringͣʧԭֵ
			CONNECTmqttڲsocketһֱʧܣٳԶ
			SVRNODATAmqttڲ3KEEP ALIVEʱ+ӣն˺ͷûκͨţΪͨ쳣
ֵ
]]
local function sckerrcb(r)
	print("sckerrcb",r)
	misc.setflymode(true)
	sys.timer_start(misc.setflymode,30000,false)
end

function connect()
	--mqtt
	--mqtt libУsocket쳣ĬϻԶ
	--עsckerrcbע͵sckerrcbmqtt libsocket쳣ʱԶǵsckerrcb
	mqttclient:connect(misc.getimei(),240,"user","password",connectedcb,connecterrcb--[[,sckerrcb]])
end

local function statustest()
	print("statustest",mqttclient:getstatus())
end

--[[
imeirdy
  IMEIȡɹɹ󣬲ȥmqtt clientӷΪõIMEI
  		
ֵ
]]
local function imeirdy()
	--һmqtt clientĬʹõMQTTЭ汾3.1Ҫʹ3.1.1ע--[[,"3.1.1"]]
	mqttclient = mqtt.create(PROT,ADDR,PORT--[[,"3.1.1"]])
	--,Ҫһд룬ҸԼwill
	--mqttclient:configwill(1,0,0,"/willtopic","will payload")
	--clean session־Ҫһд룬ҸԼcleansessionãĬΪ1
	--mqttclient:setcleansession(0)
	--ѯclient״̬
	--sys.timer_loop_start(statustest,1000)
	connect()
end

local procer =
{
	IMEI_READY = imeirdy,
}
--עϢĴ
sys.regapp(procer)
