multiple counters running in an Applet

6 replies [Last post]
BodgeIT
Offline
London, United Kingdom
Joined: 10 Jun 2010

Hi,

is this possible?

what would be the notation to do that?

I've tried using a variable to initialise it but the moment I add a 2nd, the first stops running.

I've tried adding counters to a table and using table.key to initialise but same as above.

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Hi,I haven't used multiple

Hi,

I haven't used multiple timers myself but the weather applet that Brett wrote in the samples directory does. Here's the code snippet:

 

function init()
   local expireImmediately = true
   -- lookup new weather forecast every hour.
   xap.Timer(lookupWeather, 60*60):start(expireImmediately)
   -- report every 2 minutes.
   xap.Timer(sendWeather, 2*60):start(expireImmediately)
end
BodgeIT
Offline
London, United Kingdom
Joined: 10 Jun 2010
Hi Garry,Thanks, I think the

Hi Garry,

Thanks, I think the difference here is they both call different functions where mine all call the same function?

Code looks like this:

 

    dev         = string.match(loc, "dbzoo%.hal%.Locate:*%.(%a+)")
    timer.dev = xap.Timer(lessen, myInt)
   
    if pPblty == 100 and r == 0 then
        timer.dev:start()
    end

brett
Offline
Providence, United States
Joined: 9 Jan 2010
I can't see anything wrong

I can't see anything wrong with the above code, nor why two or more timers in the same applet isn't working for you.  Can you expand and give an example where its failing?

If you are trying to wildcard match on an event you would be better off using a Filter and '*' or '>'.
Lets see what you are trying to do, it might be your approach could be better optimized.

Brett

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Correction I can see

Correction I can see something wrong with what you are attempting to do.

dev         = string.match(loc, "dbzoo%.hal%.Locate:*%.(%a+)")
timer.dev = xap.Timer(lessen, myInt)

** timer.dev will create a variable called 'dev' in the timer table.

What you mean is :

timer[dev] = xap.Timer(lessen, myInt)

This will create a variable using the contents of DEV in the timer table.

----

If you are trying to have a TIMEOUT for each ENDPOINT you would be better of changing your design to have one REAPER function based on a timer and a TTL field that tracks for each endpoint how long its lived.  This is the same design that I use for the JEENODE enpoints.  See jeenode.lua#grimreaper

The bluenode.lua code also has a reaper in it you can examine.

Brett

BodgeIT
Offline
London, United Kingdom
Joined: 10 Jun 2010
Thanks Brett, Ill check that

Thanks Brett, Ill check that out. Tables, sets and arrays seem to be filled with subtle differences that provide enormous flexibility but a bit complicated to grasp.

I was in the process of creating  my 'reaper' somewhat dramatically called "Destroyer" of location probablity.

I started by creating an Applet to detect from various sources and assigning a probability of 100 to that location as a level, then added the destroyer applet to start a countdown knocking off 10 each time over a few seconds.

It started to get a bit more complicated when I added a timer reset if the location was detected and the timer was running.  Nevertheless this seemed to work ok until I tested being in two places at the same time. The activation of the second timer stopped the first...no matter what I tried.

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Perhaps by way of example

Perhaps by way of example this will be more clear:

[brett@wombat ~]$ lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> a={}
> a.dev="hello"
> =a.dev
hello
> =a['dev']
hello
> b='dev'
> =a[b]
hello
>

Hardware Info