Running different segments within agent

Greetings!
I’m currently working on a project that requires the agent to run separate segments of code depending on what is received via an httprequest. The specified segment of code will run until a different httprequest is made.
Right now I have these segments broken up by “if” statements. After the request is received it’s response is tested by the ifs and the appropriate code is executed and is auto updated using a imp.wakeup with the appropriate delay.

First question, is there a more efficient way of doing this? A few if statements is fine for now but hopefully the project will expand to include numerous different code segments. Ideally the ability to separate the segments into different models and the httprequest would specify which model for the device to run. It’s my understanding that this isn’t currently a possibility.

Second, by utilizing imp.wakeup the first segment of code selected will continue to update because of the imp.wakeup isn’t able to be cancelled from my understanding. Any ideas on how to “refresh” a certain code segment until a new http:request is received?

Thanks for your help in advance.

After some refinement of my code I believe I am getting close. I’ve switched from ifs to a case/switch. I’m still having some issues with the updating.
My update function in agent looks like the following:

function weatherupdate() { if (response.program == "weather") { send(getWeather(response)); //send to device imp.wakeup(300,weatherupdate); //update 5 mins } }
response.program is coming from the httprequest. This function updates every 300 seconds like it should until a different response.program comes in. The problem occurs when response.program once again becomes “weather”. Once this happens two wakeups are scheduled and thus the updates start stepping on each other. Is there a way to either cancel a wakeup?

If the Agent is like the imp then no cancelling! I had a similar issue on the imp when I first started I was constantly adding more of the same wakeup I improved it by using flags to tell me a wakeup timer was running on another imp I log time() and use it to work out what woke me up long/short sleep may be a combination of these approaches might help.

We talked about it internally, but it would appear that now is the time for wakeups to return handles to allow modification/cancellation… will file a feature request.

Thanks for your help guys. I’ll look forward to the roll out of this feature in the future.

Thanks for adding feature don’t know if this is a different feature req but it would be useful for the Agent to know how long an imp is deep sleeping for. This is already appears in the log “sleeping until 1364379505000” They agent would then know that the imp has/nt checked in as expected.