# signature { license=0, seed=277031123, key="b80e1e29316d055111213f3ff6630c75" }
-- ----------------------------------------------------------------------------



local api = import( 'api' )
local xmlpro = import( 'xmlpro' )

local json = [[
{
  "locationId": "%s",
  "systemKeyCodes": { "systemPrimaryKeyCode": "%s" },
  "version": {
    "software": "%s",
    "hardware": "%s"
  }
}]]


xmlpro.login( cfg.XmlProUserID, cfg:password( 'XmlPro' ) )
local response = xmlpro.post( '<get format="2"><mb mb="9016"/></get>' )
if not response then return end

cfg.SwVersion = response:match( '<k k="0">([^<]*)</k>' )
local HwVersion = response:match( '<k k="3">([^<]*)</k>' )
HwVersion = ({ 'SL2100', 'Aspire WX', 'Unicorn CP20'})[ tonumber( HwVersion ) ] or 'unknown'

response = api.decode( api.post( '/api/v1/Devices', json, cfg.LocationId, cfg.HardwareKeyCode, cfg.SwVersion, HwVersion ) )
if response.success then
	local uuid = response.data
	if uuid then
		cfg.DeviceId = uuid
		log( i18n("Received device id"), uuid )
	else
		log( i18n("Could not find a valid device ID in LMS response") )
		stop()
	end
else
	log( i18n("Problem contacting LMS:"), response.message or i18n("no connection") )
end

cfg:write()
