Author Topic: HELP Convert Groovy to Lua Heating  (Read 166 times)

Offline Newzwaver

  • Full Member
  • ***
  • Posts: 197
  • Karma: +4/-3
HELP Convert Groovy to Lua Heating
« on: March 03, 2017, 07:54:09 am »
Hi

I need your help, trying to make a plug in for a thermostat and it uses Groovy so i am hoping to convert the following;
def login() {
   data.server="https://neviweb.com/"
   data.requested=false
    def params = [
        uri: "${data.server}",
        path: 'api/login',
        requestContentType: "application/x-www-form-urlencoded; charset=UTF-8",
        body: ["email": settings.email, "password": settings.password, "stayConnected": "0"]
    ]
    requestApi("login", params);
    if (data.auth.error){
       log.warn(data.auth.error.code);
       data.error = error(data.auth.error.code)
       sendEvent(name: 'error', value: "${data.error}")
       log.error("${data.error}")
       data.error=true
       logout()
   }else{
      log.info("login and password :: OK")
       data.error=false
       sendEvent(name: 'error', value: "")
       gatewayId()
   }

I have worked this to the following code taken from another plugin
local HOST = "www.neviweb.com"
local URL_PORTAL = "https://" .. HOST .. "/api/login/,"
 local response, status, header = https.request{
      url = URL_PORTAL,
      method = "POST",
      headers = {
    ["Origin"] = URL_PORTAL,
    ["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8",
    ["Content-Length"] = #theBody,
      },
      source = ltn12.source.string(theBody),
      sink = ltn12.sink.table(auth_response)
   }

Should this not work?  It should send the same message.

Thanks
 

Online akbooer

  • Master Member
  • *******
  • Posts: 4947
  • Karma: +211/-67
  • "Less is more"
Re: HELP Convert Groovy to Lua Heating
« Reply #1 on: March 03, 2017, 08:09:55 am »
Looks about right.  Easiest thing would be to try it out in the Lua Test window.  Does it work there?

You do, of course, have to include the appropriate library module for https.request to work.


3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.