module(...,package.seeall)

require"aliyuniotssl"
require"misc"

--ƻ2վϴĲƷProductKeyûʵֵ޸
local PRODUCT_KEY = "b0FMK1Ga5cp"
--PRODUCT_KEY⣬Ҫ豸ƺ豸֤
--豸ʹúgetDeviceNameķֵĬΪ豸IMEI
--豸֤ʹúgetDeviceSecretķֵĬΪ豸SN
--ʱֱ޸getDeviceNamegetDeviceSecretķֵ
--ʱʹ豸IMEISNģ飬ΨһIMEIûԼĲдIMEI豸ƣӦSN豸֤飩
--ûԽһ豸ϱIMEIضӦ豸֤飬Ȼmisc.setsnӿд豸SN

--[[
getDeviceName
  ȡ豸
  
ֵ豸
]]
local function getDeviceName()
	--Ĭʹ豸IMEIΪ豸
	--return "862991419835241"
	return misc.getimei()
end

--[[
getDeviceSecret
  ȡ豸֤
  
ֵ豸֤
]]
local function getDeviceSecret()
	--Ĭʹ豸SNΪ豸֤
	--ûʱڴ˴ֱӷذƵiot̨ɵ豸֤飬return "y7MTCG6Gk33Ux26bbWSpANl4OaI0bg5Q"
	--return "y7MTCG6Gk33Ux26bbWSpANl4OaI0bg5Q"
	return misc.getsn()
end


local qos1cnt = 1

--[[
print
  ӡӿڣļедӡaliyuniotǰ׺
  
ֵ
]]
local function print(...)
	_G.print("test",...)
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ݱ룬aliyuniotвpayloadκαת
	aliyuniotssl.publish("/"..PRODUCT_KEY.."/"..getDeviceName().."/update","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)
	aliyuniotssl.publish("/"..PRODUCT_KEY.."/"..getDeviceName().."/update","device receive:"..payload,qos)
end

--[[
connectedcb
  MQTT CONNECTɹص
  		
ֵ
]]
local function connectedcb()
	print("connectedcb")
	--
	aliyuniotssl.subscribe({{topic="/"..PRODUCT_KEY.."/"..getDeviceName().."/get",qos=0}, {topic="/"..PRODUCT_KEY.."/"..getDeviceName().."/get",qos=1}}, subackcb, "subscribegetopic")
	--ע¼ĻصMESSAGE¼ʾյPUBLISHϢ
	aliyuniotssl.regevtcb({MESSAGE=rcvmessagecb})
	--һqosΪ1Ϣ
	pubqos1test()
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

--òƷkey豸ƺ豸֤飻ڶ봫nil˲Ϊ˼ݰƺվ㣩
--ע⣺ʹimeisnΪ豸ƺ豸֤ʱҪgetDeviceNamegetDeviceSecret滻Ϊmisc.getimei()misc.getsn()
--Ϊ͵misc.getimei()misc.getsn()ȡֵ
aliyuniotssl.config(PRODUCT_KEY,nil,getDeviceName,getDeviceSecret)
--setMqttӿڲǱģaLiYun.luaӿõĲĬֵĬֵ㲻󣬲οע͵Ĵ룬ȥò
--aliyuniotssl.setMqtt(0)
aliyuniotssl.regcb(connectedcb,connecterrcb)





--[[
Ҫѣ
һʹ˰OTAԶģűܣǿҽʹdbgܣע͵Ĵ
ΪͨԶ°汾Ľű°汾Ľűʱ﷨Զ˵һαдİ汾
˺һԶһԶ°汾->°汾г->Զ˵ɰ汾ѭ¹쳣˷
籾дİ汾1.0.01.0.1汾1.0.1汾ʱ﷨豸ѭԶ1.0.1->1.0.1г->Զ˵1.0.0
һdbgܺ󣬷﷨󣬻Ὣ﷨ϱdbgԱ鿴﷨־Լʱ﷨󣬳汾
dbg֧TCPUDPЭ飬յκϱҪظдOK
Լdbgʹúṩ"UDP","ota.airm2m.com",9072
iot.openluat.comе¼һƷ"ѯdebug"пԲѯ豸ϱĴϢ
]]
--require"dbg"
--sys.timer_start(dbg.setup,12000,"UDP","ota.airm2m.com",9072)

--ҪʹðOTAܣοļ138aliyuniotssl.config(PRODUCT_KEY,nil,getDeviceName,getDeviceSecret)ȥòƷkey豸ƺ豸֤
--ȻذOTAģ(Ĵע)
--require"aliyuniotota"
--ðOTAȥģ¹̼ĬϵĹ̼汾ŸʽΪ_G.PROJECT.."_".._G.VERSION.."_"..sys.getcorever()򵽴ΪֹҪٿ˵


--ðOTAȥģӵMCUʵĴעͣýӿڽúʹ
--MCUǰеĹ̼汾
--aliyuniotota.setVer("MCU_VERSION_1.0.0")
--¹̼غ󱣴ļ
--aliyuniotota.setName("MCU_FIRMWARE.bin")

--[[
otaCb
  ¹̼ļؽĻص
  
		resultؽtrueΪɹfalseΪʧ
		filePath¹̼ļ·ֻresultΪtrueʱ˲
ֵ
]]
--[[
local function otaCb(result,filePath)
	print("otaCb",result,filePath)
	if result then
		--ԼȥʹļfilePath
		local fileHandle = io.open(filePath,"rb")
		if not fileHandle then print("otaCb open file error") return end
		local current = fileHandle:seek()
		local size = fileHandle:seek("end")
		fileHandle:seek("set",current)
		--ļ
		print("otaCb size",size)
		
		--ļݣļ̫һԶļݿܻڴ治㣬ִζԱ
		if size<=4096 then
			print(fileHandle:read("*all"))
		else
			--ֶζȡļ
		end
		
		fileHandle:close()
		
		--˴ϱ¹̼汾ţʹã
		--ûԼĳʱ¹ִ̼
		--ɹ󣬵aliyuniotota.setVerϱ¹̼汾
		--ʧܣaliyuniotota.setVerϱɹ̼汾
		aliyuniotota.setVer("MCU_VERSION_1.0.1")
	end
	
	--ļʹ֮ԺҪɾ
	if filePath then os.remove(filePath) end
end
]]

--¹̼ؽĻص
--aliyuniotota.setCb(otaCb)
