We have moved at community.getvera.com

Author Topic: Luup code question about tonumber(code)  (Read 1082 times)

Offline mjbok

  • Full Member
  • ***
  • Posts: 148
  • Karma: +5/-1
Luup code question about tonumber(code)
« on: December 21, 2018, 10:29:16 am »
I'm trying to use someone else's code: 
Code: [Select]
local http=require("socket.http")
local auroraToken = "xxxxxxxxxxxxxxxxxxxxxxx";
local siteurl = "http://192.168.x.xx:16021/api/v1/"..auroraToken.."/state/on/"
local siteurl1 = "http://192.168.x.xx:16021/api/v1/"..auroraToken.."/state/"
local respback = { }
  local res, code, response_headers, status = http.request
  {
    url = siteurl,
    method = "GET",
    headers =
    {
      ["Content-Type"] = "application/json"
    },
    sink = ltn12.sink.table(respback)
  }

if tonumber(code) == 200
then
for key,value in pairs(respback) do
auroraOn = string.match(value, "%a+", 8)
end
end

if auroraOn == 'false'
then auroraOn = 'true'
else
auroraOn = 'false'
end

local payload = '{"on" : {"value":'..auroraOn..'}}'
print(payload)

local respback = { }
  local res, code, response_headers, status = http.request
  {
    url = siteurl1,
    method = "PUT",
    headers =
    {
      ["Content-Type"] = "application/json",
      ["Content-Length"] = payload:len()
    },
source = ltn12.source.string(payload),
    sink = ltn12.sink.table(respback)
  }

It runs fine in the developer mode (in that it says it runs), but it doesn't do anything.  I believe the line "if tonumber(code) == 200" means that if the response code is 200 do what is next.  Running  the code in another language I get a 202 return code.  Should I change the 200 to 202, or is there something I can put in runs the if statement no matter the code returned?

Offline rafale77

  • Community Beta
  • Hero Member
  • ******
  • Posts: 1749
  • Karma: +101/-27
  • HA ≠ IoT as a blue sky is cloudless.
Re: Luup code question about tonumber(code)
« Reply #1 on: January 22, 2019, 12:45:35 pm »
You probably want to check the documentation of the API you are trying to talk to. Each code means something. 200 typically means success. I am not sure what 202 means.
openLuup (79 devices, 141 scenes, 19 apps) master to VeraPlus (142 zwave nodes, 8 Zigbee nodes, 221 devices,  20 scenes , 2 apps) +  Hubitat (15 Zigbee nodes) + Home-Assistant (API Integrations). Bridged to Siri and Alexa. Homewave. VeraPlus ExtRooted and mios server independent.

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Luup code question about tonumber(code)
« Reply #2 on: January 22, 2019, 12:49:07 pm »
Quote
202 Accepted. The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline Catman

  • Sr. Member
  • ****
  • Posts: 338
  • Karma: +10/-0
Re: Luup code question about tonumber(code)
« Reply #3 on: January 22, 2019, 01:17:50 pm »
It's probably worth a punt setting it to 202, but the point of a 202 is that it may, or may no be the result you expect, and you can't check.

I'd not be at all surprised if it gives you an inconsistent result.
As suggested, what's the API documentation suggest for a 202 return (although it really shouldn't be different, there may be some additional context) also, what's the other language? (and code?)
Finally, what's the API you're trying to use?

C