print("Starting")
width = 100
height = 200
print("Started")
--gser = nil

function testfunc(a,b,c,d)
	--[[C_MyFunc()
	l1()
	l2()
	local X = TestClass()
	X:SetNumber(1)
	local Y = TestClass()
	Y:SetNumber(2)
	print(X:GetNumber(), Y:GetNumber())
	result = a+b+c+5*d
	print(a,b,c,d,"=",result)
	return result]]
	--blub
	return a+b+c+d
end

function test2()
	local ser = SMC:NewSerialPort("config1")
	local tmr = SMC:NewTimer(123, 456)
	print("ser", ser)
	print("tmr", tmr)
	ser = nil
	print("ser was set to nil")
	tmr = nil
	print("garbage", collectgarbage("count"))
	collectgarbage("collect")
	print("garbage#2", collectgarbage("count"))
end

function test3()
	local ser = SMC:NewSerialPort("config1")
	ser:Transmit("12345")
	ser = nil
	collectgarbage("collect")
end

function test4()
	gser = SMC:NewSerialPort("config1")
end

function test5()
	gser = nil
	collectgarbage("collect")
	return 1
end

function test6()
	gtimer = SMC:NewTimer("mytimer", 600)
	gtimer2 = SMC:NewTimer("mytimer2", 700)
	gtimer3 = SMC:NewTimer("mytimer3", 500)
end

function test7()
	gtimer = nil
	gtimer2 = nil
	gtimer3 = nil
	collectgarbage("collect")
	return 27
end

function test8()
	local t
	t = SMC:GetLocation()
	print(tostring(t))
	if t ~= nil then
		print("latitude="..tostring(t.latitude))
		print("longitude="..tostring(t.longitude))
	end
end

function test9()
	error("test9 raised an error")
end

-- function SMCReportLocation(loc)
-- 	print("SMCReportLocation: latitude="..tostring(loc.latitude).." longitude="..tostring(loc.longitude))
-- end

function SMCReportSerialData(portName,data)
	print("portName="..portName,"data="..data)
	return 1
end

function SMCReportTimeout(timerName)
	print("timerName="..timerName)

	if timerName=="mytimer" then
		gtimer = nil
	elseif timerName == "mytimer2" then
		gtimer2 = nil
	elseif timerName == "mytimer3" then
		gtimer3 = nil
	end

	if (gtimer == nil) and (gtimer2 == nil) and (gtimer3 == nil) then
		collectgarbage("collect")
	end

	return 38
end

function SMCReportEvent00(eventRx)

	if (tostring(eventRx.header.name) == "GPSSample") then

		local latitude = eventRx.properties["GPS-Latitude"][1]
		local longitude = eventRx.properties["GPS-Longitude"][1]

		if latitude and longitude then
			print("gps latitude="..tostring(latitude).." longitude="..tostring(longitude))
		end
	end

end

function TestPublish()

	SMC:PublishEvent({header={name="LWOutput",typename="LWEventName",domainname="LWDomainName"},
			  properties={somefield="some field value",otherfield={4,"INT16"}}})

end

function SMCReportEvent(eventRx)
	print("SMCReportEvent name="..tostring(eventRx.header.name))
	print("header "..tostring(eventRx.header))
	print("header.name "..tostring(eventRx.header.name))
	print("header.typename "..tostring(eventRx.header.typename))
	print("header.domainname "..tostring(eventRx.header.domainname))
	print("properties "..tostring(eventRx.properties))
	for k,v in pairs(eventRx.properties)
	do
		if type(v[1]) == "table" then

			local s2 = ""

			for k2,v2 in ipairs(v[1]) do
				if #s2 ~= 0 then
					s2 = s2..","
				end
				s2 = s2..tostring(k2)..":"..tostring(v2)
			end

			print("properties["..k.."] -> "..type(v[1]).."/"..s2.." "..tostring(v[2]))
		else
			print("properties["..k.."] -> "..type(v[1]).."/"..tostring(v[1]).." "..tostring(v[2]))
		end
	end
	print("time = "..tostring(eventRx.properties["GPS-TimeSec"][1]))
	
end

function TestTelemetryReport()

	local evt = { }
	
	evt.header = { name = "LWTelem1", typename = "LWTelem1", domainname="LqrSMAC" }
	evt.properties = { }
	evt.properties["Telem-BoardID"] = { 32, "UINT8" }
	evt.properties["Telem-TaskID"] = { 64, "UINT8" }
	evt.properties["StringMessage"] = "Hello, world!"
	
	SMC:PublishEvent(evt)

end

function TestSynchronousSendCommand()

	-- sends synchronous command "runshell date" to payload interface, the command will not return
	-- until it completes
	local result = SMC:SendCommand("PayloadInterface", "runshell date", true)
	
	-- printing out return values: { asynchronous?, result or asynchronous return tag }
	-- result will be a table of { value, storage type string }
	print("PayloadInterface runshell date: result="..tostring(result).." [1]="..tostring(result[1]).." [2]="..tostring(result[2]).." [2][1]="..tostring(result[2][1]).." [2][2]="..tostring(result[2][2]))
	
	-- sometimes servers return a string as a byte array: payload interface would use the "file"
	-- command to determine what file extension it would have (.txt or .jpg or whatever), but
	-- if you know it's a string, you can use string.char(unpack(result value)) to recast it
	-- as a string
	if type(result[2])=="table" and tostring(result[2][2]) == "UINT8_VECTOR" then
		print("as characters: "..string.char(unpack(result[2][1])))
	end
	
	-- this is another command but it returns asynchronously
	result = SMC:SendCommand("PayloadInterface", "runshell echo yes", false)
	
	-- printing out results
	print("PayloadInterface runshell date: result="..tostring(result).." [1]="..tostring(result[1]).." [2]="..tostring(result[2]).." [2][1]="..tostring(result[2][1]).." [2][2]="..tostring(result[2][2]))
	
	-- again printing out results
	if type(result[2])=="table" and tostring(result[2][2]) == "UINT8_VECTOR" then
		print("as characters: "..string.char(unpack(result[2][1])))
	end
	
	-- note that in this second case, the SendCommand function returns before the result
	-- is obtained; in this case, result[2] contains the tag that you should look for in
	-- the SMCReportEvent callback to notice when the command has actually completed
	
end

