We have moved at community.getvera.com

Author Topic: [Solved]Extracting data from gc_jsonEvents  (Read 4238 times)

Offline JesperJ

  • Newbie
  • *
  • Posts: 17
  • Karma: +0/-0
[Solved]Extracting data from gc_jsonEvents
« on: September 24, 2014, 07:11:19 am »
I'm having trouble decoding the json.
I have tried to use the example from the User guide but i get an error when running the luup code.

Anyone else got this working?

I think it stops at the line : local json = require("json")

Regards Jesper
« Last Edit: October 12, 2018, 10:53:53 am by Stuart »

Offline Stuart

  • Moderator
  • Hero Member
  • *****
  • Posts: 728
  • Karma: +71/-2
Re: Extracting data from gc_jsonEvents
« Reply #1 on: September 24, 2014, 01:11:07 pm »
I'm having trouble decoding the json.
I have tried to use the example from the User guide but i get an error when running the luup code.

Anyone else got this working?

I think it stops at the line : local json = require("json")

Regards Jesper

Try this as a test and check the log file:

Apps-->Develop Apps --> Test Luup Code (Lua)

Code: [Select]
local jsonevents = "[{'eventEnd':1411581600,'eventStart':1411578000,'eventParameter':'','eventName':'TEST 1'},{'eventEnd':1411585200,'eventStart':1411581600,'eventParameter':'','eventName':'TEST 2'}]"
local json = require("json") -- loads the package
local events = json.decode(jsonevents)
package.loaded.json = nil -- unloads the package
for i = 1, #events do
  luup.log(events[i].eventName) 
end


The log file entry should look like this:

08      09/24/14 11:05:09.988   JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: RunLua <0x2d73b680>
08      09/24/14 11:05:09.989   JobHandler_LuaUPnP::HandleActionRequest argument Code=local jsonevents = "[{'eventEnd':1411581600,'eventStart':1411578000,'eventParameter':'','eventName':'TEST 1'},{'eventEnd':1411585200,'eventStart':1411581600,'eventParameter':'','eventName':'TEST 2'}]"
local json = require("json")
local events = json.decode(jsonevents)
package.loaded.json = nil -- unloads the package
for i = 1, #events do
  luup.log(events.eventName) 
end <0x2d73b680>
50      09/24/14 11:05:10.007   luup_log:0: TEST 1 <0x2d73b680>
50      09/24/14 11:05:10.007   luup_log:0: TEST 2

Offline JesperJ

  • Newbie
  • *
  • Posts: 17
  • Karma: +0/-0
Re: Extracting data from gc_jsonEvents
« Reply #2 on: September 24, 2014, 02:26:38 pm »
That worked great!

But when i run your test code i get this in my logfile:

01      09/24/14 20:22:45.621   LuaInterface::LoadCode: [string "local GCAL_SID = "urn:srs-com:serviceId:GCal3"..."]:28: '<eof>' expected near 'end' <0x2fefa680>
01      09/24/14 20:22:45.623   JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = "urn:srs-com:serviceId:GCal3"
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",myGooglePlugin)
if (jsonEvents == "[]") then
return
end

-- load the jason package
package.loaded.json = nil
local json = require("json")
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
--startEvent = os.date("%Y-%m-%dT%H:%M:%S", startevent)
startDate = os.date("%Y-%m-%d", startevent)
startTime = os.date("%H:%M:%S", startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date("%H:%M:%S", endevent)
eventname = eventList[ i ].eventName
event = "On " .. startDate .. " event " .. eventname .. " will start at " .. startTime .. " and end at " ..
endTime
luup.log(event)
end
-- unload json module
package.loaded.json = nil
end <0x2fefa680>

 

Offline Stuart

  • Moderator
  • Hero Member
  • *****
  • Posts: 728
  • Karma: +71/-2
Re: Extracting data from gc_jsonEvents
« Reply #3 on: September 24, 2014, 04:24:28 pm »
That worked great!

But when i run your test code i get this in my logfile:

01      09/24/14 20:22:45.621   LuaInterface::LoadCode: [string "local GCAL_SID = "urn:srs-com:serviceId:GCal3"..."]:28: '<eof>' expected near 'end' <0x2fefa680>
01      09/24/14 20:22:45.623   JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = "urn:srs-com:serviceId:GCal3"
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",myGooglePlugin)
if (jsonEvents == "[]") then
return
end

-- load the jason package
package.loaded.json = nil
local json = require("json")
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
--startEvent = os.date("%Y-%m-%dT%H:%M:%S", startevent)
startDate = os.date("%Y-%m-%d", startevent)
startTime = os.date("%H:%M:%S", startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date("%H:%M:%S", endevent)
eventname = eventList[ i ].eventName
event = "On " .. startDate .. " event " .. eventname .. " will start at " .. startTime .. " and end at " ..
endTime
luup.log(event)
end
-- unload json module
package.loaded.json = nil
end <0x2fefa680>

Yep !  The test code was presented as a function.  Your code is 'inline' and therefore there are one too many 'end' statements (i.e. you omitted the 'function testjson() ' statement from the start of the example.  Remove the last 'end' statement and it should work as expected.

Offline JesperJ

  • Newbie
  • *
  • Posts: 17
  • Karma: +0/-0
Re: Extracting data from gc_jsonEvents
« Reply #4 on: September 24, 2014, 04:57:06 pm »
I'm really appreciating your help.
Tried it but got a new error :

01      09/24/14 22:51:17.471   LuaInterface::LoadCode: [string "local GCAL_SID = "urn:srs-com:serviceId:GCal3"..."]:26: '=' expected near 'json' <0x2faa2680>
01      09/24/14 22:51:17.473   JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = "urn:srs-com:serviceId:GCal3"
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",myGooglePlugin)
if (jsonEvents == "[]") then
return
end

-- load the jason package
package.loaded.json = nil
local json = require("json")
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
--startEvent = os.date("%Y-%m-%dT%H:%M:%S", startevent)
startDate = os.date("%Y-%m-%d", startevent)
startTime = os.date("%H:%M:%S", startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date("%H:%M:%S", endevent)
eventname = eventList[ i ].eventName
event = "On " .. startDate .. " event " .. eventname .. " will start at " .. startTime .. " and end at " ..
endTime
luup.log(event)
end
unload json module
package.loaded.json = nil

Offline Stuart

  • Moderator
  • Hero Member
  • *****
  • Posts: 728
  • Karma: +71/-2
Re: Extracting data from gc_jsonEvents
« Reply #5 on: September 24, 2014, 05:14:24 pm »
I'm really appreciating your help.
Tried it but got a new error :

01      09/24/14 22:51:17.471   LuaInterface::LoadCode: [string "local GCAL_SID = "urn:srs-com:serviceId:GCal3"..."]:26: '=' expected near 'json' <0x2faa2680>
01      09/24/14 22:51:17.473   JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = "urn:srs-com:serviceId:GCal3"
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",myGooglePlugin)
if (jsonEvents == "[]") then
return
end

-- load the jason package
package.loaded.json = nil
local json = require("json")
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
--startEvent = os.date("%Y-%m-%dT%H:%M:%S", startevent)
startDate = os.date("%Y-%m-%d", startevent)
startTime = os.date("%H:%M:%S", startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date("%H:%M:%S", endevent)
eventname = eventList[ i ].eventName
event = "On " .. startDate .. " event " .. eventname .. " will start at " .. startTime .. " and end at " ..
endTime
luup.log(event)
end
unload json module
package.loaded.json = nil

This bit
....... = "urn:srs-com:serviceId:GCal3"..."]:26: .........
tells you approx where the error occurred i.e. line 26

there is no command 'unload json module'   you have changed it from a comment to something Lua cannot interpret
it needs to be commented back out ' -- unload json module'
« Last Edit: September 24, 2014, 06:53:01 pm by Stuart »

Offline JesperJ

  • Newbie
  • *
  • Posts: 17
  • Karma: +0/-0
Re: Extracting data from gc_jsonEvents
« Reply #6 on: September 25, 2014, 06:29:47 am »
Thanks!

Did that and got a new error.  :(

01      09/25/14 12:24:37.306   GetLuaInterface can't find device type: 0/0x10bd690 str: (null) <0x30418680>
01      09/25/14 12:24:37.306   luup_variable_get interface 0x10c1d88 args 3 <0x30418680>
01      09/25/14 12:24:37.335   LuaInterface::StartEngine failed run: 0 /usr/lib/lua/json.lua:362: attempt to get length of upvalue 'js_string' (a nil value) <0x30418680>
01      09/25/14 12:24:37.336   JobHandler_LuaUPnP::RunLua failed: local GCAL_SID = "urn:srs-com:serviceId:GCal:3"
local myGooglePlugin = 306
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",lul_device)
if (jsonEvents == "[]") then
return
end

-- load the jason package
package.loaded.json = nil
local json = require("json")
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
--startEvent = os.date("%Y-%m-%dT%H:%M:%S", startevent)
startDate = os.date("%Y-%m-%d", startevent)
startTime = os.date("%H:%M:%S", startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date("%H:%M:%S", endevent)
eventname = eventList[ i ].eventName
event = "On " .. startDate .. " event " .. eventname .. " will start at " .. startTime .. " and end at " ..
endTime
luup.log(event)
end
--unload json module
package.loaded.json = nil

Offline Stuart

  • Moderator
  • Hero Member
  • *****
  • Posts: 728
  • Karma: +71/-2
Updated Test Code
« Reply #7 on: September 25, 2014, 01:29:55 pm »
The test code in the documentation had the wrong GCAL_SID (from prior versions) and a variable name error
Use this code for testing - remember to replace myGooglePlugin with your device number

Code: [Select]
function testJson()
local GCAL_SID = "urn:srs-com:serviceId:GCalIII"
 -- change myGooglePlugin to be your GCal3 plugin device #
local myGooglePlugin = 8
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",myGooglePlugin)
if ((jsonEvents == "[]") or (jsonEvents == nil)) then
return
end

-- load the jason package
package.loaded.json = nil
local json = require("json")
local eventList =json.decode(jsonEvents)
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, endTime, eventname, event
for i = 1,numberEvents do
startevent = eventList[ i ].eventStart
--startEvent = os.date("%Y-%m-%dT%H:%M:%S", startevent)
startDate = os.date("%Y-%m-%d", startevent)
startTime = os.date("%H:%M:%S", startevent)
endevent = eventList[ i ].eventEnd
endTime = os.date("%H:%M:%S", endevent)
eventname = eventList[ i ].eventName
event = "On " .. startDate .. " event " .. eventname .. " will start at " .. startTime .. " and end at " ..
endTime
luup.log(event)
end
-- unload json module
package.loaded.json = nil
end

local test = testJson()
« Last Edit: September 25, 2014, 01:37:56 pm by Stuart »

Offline Stuart

  • Moderator
  • Hero Member
  • *****
  • Posts: 728
  • Karma: +71/-2
Re: Extracting data from gc_jsonEvents
« Reply #8 on: September 25, 2014, 01:37:21 pm »
Thanks!

Did that and got a new error.  :(


My bad - see post above.   The result of the call that should have been
localjsonEvents=luup.variable_get(GCAL_SID,"gc_jsonEvents",myGooglePlugin) was a nil for two reasons
(1) GCAL_SID was wrong and the third argument was 'lul_device' instead of 'myGooglePugin' (I'd copied it from my code not changed it)

That's why the error message was complaining about the length of the json string ......
« Last Edit: September 25, 2014, 01:39:04 pm by Stuart »

Offline JesperJ

  • Newbie
  • *
  • Posts: 17
  • Karma: +0/-0
Re: Extracting data from gc_jsonEvents
« Reply #9 on: September 26, 2014, 03:20:14 pm »
Thanks Stuart for all your help!
I got it working now, great plugin!

Best regards
Jesper

Offline JesperJ

  • Newbie
  • *
  • Posts: 17
  • Karma: +0/-0
Re: Extracting data from gc_jsonEvents
« Reply #10 on: November 06, 2014, 06:44:43 am »
Let's say i got 2 different events on the same day and want to use the start time from the second event.
Can this be done?
I understand that i should look for the eventname of the second event but how do i read out the time from the table?

/Jesper


Offline Stuart

  • Moderator
  • Hero Member
  • *****
  • Posts: 728
  • Karma: +71/-2
Re: Extracting data from gc_jsonEvents
« Reply #11 on: November 06, 2014, 03:32:14 pm »
Let's say i got 2 different events on the same day and want to use the start time from the second event.
Can this be done?
I understand that i should look for the eventname of the second event but how do i read out the time from the table?

/Jesper

Something like this - just regular Lua :

Code: [Select]
function getEventStartEnd(eventToMatch)
local GCAL_SID = "urn:srs-com:serviceId:GCalIII"
 -- change myGooglePlugin to be your GCal3 plugin device #
local myGooglePlugin = 8
local jsonEvents = luup.variable_get(GCAL_SID, "gc_jsonEvents",myGooglePlugin)
if ((jsonEvents == "[]") or (jsonEvents == nil)) then
return
end

-- load the jason package
package.loaded.json = nil
local json = require("json")

local eventList =json.decode(jsonEvents)

local startevent, endevent
for i,v in pairs(eventList) do
  if v.eventName == eventToMatch then
    startevent = eventList[ i ].eventStart
    endevent = eventList[ i ].eventEnd
    break
  end
end

-- unload json module
package.loaded.json = nil

-- do any time conversions e.g. to string that you want here

return startevent, endevent
end



local eventName = "NAME OF EVENT"
local eventStartTime, eventEndTime = getEventStartEnd(eventName)
print ((eventStartTime or "no match") .. " - " .. (eventEndTime or ""))



Offline JesperJ

  • Newbie
  • *
  • Posts: 17
  • Karma: +0/-0
Re: Extracting data from gc_jsonEvents
« Reply #12 on: November 11, 2014, 07:13:06 am »
Ok thanks, will try it soon.

/Jesper

Offline Stuart

  • Moderator
  • Hero Member
  • *****
  • Posts: 728
  • Karma: +71/-2
Re: Extracting data from gc_jsonEvents
« Reply #13 on: April 09, 2015, 01:33:51 pm »
Does this

http://forum.micasaverde.com/index.php/topic,26692.msg229451.html#msg229451

look interesting / usable for you guys ?

Offline mda

  • Sr. Member
  • ****
  • Posts: 464
  • Karma: +9/-0
Re: Extracting data from gc_jsonEvents
« Reply #14 on: April 09, 2015, 03:13:41 pm »
Does this

http://forum.micasaverde.com/index.php/topic,26692.msg229451.html#msg229451

look interesting / usable for you guys ?

I don't think I have the RAM for another Plug-in :)

Here is what I do now in case it helps anyone else:

I have one google calendar where we put overlapping events for things like vacation, guests, running the spa, opening the blinds, etc and i have a scene that runs every 3 minutes to check for overlapping events (here is the start of the code in that scene):

Code: [Select]
local active_events = luup.variable_get("urn:srs-com:serviceId:GCalIII","gc_ActiveEvents", d_gcalSensor) or ""

local    i, j = string.find(active_events, "VACATION")
if i == nil then
    luup.call_action("urn:dcineco-com:serviceId:MSwitch1","SetStatus6",{newStatus6=0},d_mode) -- turn off vacation multiswitch button if no active Vacation event
else -- found event
    luup.call_action("urn:dcineco-com:serviceId:MSwitch1","SetStatus6",{newStatus6=1},d_mode) -- turn on vacation multiswitch button if active Vacation event
end -- found event

local    i, j = string.find(active_events, "GUEST")
if i == nil then
     luup.call_action("urn:dcineco-com:serviceId:MSwitch1","SetStatus5",{newStatus5=0},d_mode) -- turn off guest multiswitch button if no active Guest event
else -- found event
     luup.call_action("urn:dcineco-com:serviceId:MSwitch1","SetStatus5",{newStatus5=1},d_mode) -- turn on guest multiswitch button if active Guest event
end -- found event

This works ok but it is not very efficient :)