Results 1 to 7 of 7
  1. ldj
    ldj is offline
    ldj's Avatar
    Posts
    256 Posts
    Global Posts
    264 Global Posts
       #1  
    I'm playing about calling alarms, trying to launch my app after a timeout. I can get the code to work when setting the 'in' parameter, but not when I set it using 'at'.

    E.g. In the following block of code, my log file shows that alarmtime is five minutes from when the code was hit (e.g. 17/12/2009 9:33:14 when I ran it in the emulator a few moments ago). I get the "....alarm set successfully" message, but nothing ever fires in my app!

    Code:
    var alarm = new Date();
    	var now = new Date();
    	
    	alarm.setMinutes(now.getMinutes()+5);
    	var day = alarm.getDate();
    	var month = alarm.getMonth()+1;
    	var year = alarm.getFullYear();
    	var hours = alarm.getHours();
    	var minutes = alarm.getMinutes();
    	var seconds = alarm.getSeconds();
    	var alarmString = day+"/"+month+"/"+year+" "+hours+":"+minutes+":"+seconds;
    	Mojo.Log.info("...Attempting to set an alarm for: ", alarmString);
    
    	this.wakeupRequest = new Mojo.Service.Request("palm://com.palm.power/timeout",
    			{
    				method: "set",
    				parameters: {
    					"key": "test|wake",
    					//"in": "00:05:00",
    					"at": alarmString,
    					"wakeup":true,
    					"uri": "palm://com.palm.applicationManager/open",
    					"params": {
    						"id": "co.uk.test.wake",
    						"params": {"action": "alarm"}
    					}
    				},
    				onSuccess: function(response){
    					Mojo.Log.info(".....Alarm set successfully");
    					//this.window.close();
    				},
    				onFailure: function(response){
    					Mojo.Log.info(".....Alarm set failure", response.returnValue, response.errorText);
    				}
    			});
    However, if I comment out "at": alarmString, and uncomment "in": "00:05:00",, after five minutes the app-assistant fires and everything works.
    Naturally I want to be able to set the wakeup by a specific date/time, but cannot figure out what it causing this to fail.

    Any ideas?

    Thanks
    Last edited by ldj; 12/17/2009 at 11:59 AM.
  2. #2  
    You have to use the UTC date functions (getUTCMonth vs. getMonth, etc.) Alarms are based on UTC time.
  3. ldj
    ldj is offline
    ldj's Avatar
    Posts
    256 Posts
    Global Posts
    264 Global Posts
       #3  
    Ah right. Thanks. Ill give that a shot now...
  4. #4  
    Another thing is the "key" should be in the format "your_app_id.your_timer_identifier" (where "your_app_id" is your app's id in the format com.companyname.appname). If you just have a timer name without incorporating your app id, you could run the risk of re-using a name that another app or service is using.

    And the "id" in the params object should be your app id.
  5. ldj
    ldj is offline
    ldj's Avatar
    Posts
    256 Posts
    Global Posts
    264 Global Posts
       #5  
    Thanks. Made the recommended changes, including:

    Code:
    var alarm = new Date();
    	var now = new Date();
    	
    	alarm.setMinutes(now.getMinutes()+2);
    	var day = alarm.getUTCDate();
    	var month = alarm.getUTCMonth()+1;
    	var year = alarm.getUTCFullYear();
    	var hours = alarm.getUTCHours();
    But still no joy. My output says:

    Code:
    ...Attempting to set an alarm for:  17/12/2009 11:49:29  it is now:  Thu Dec 17 2009 11:47:29
    And then....
    Code:
    .....Alarm set successfully
    but again nothing! I've double checked and the ID is eactly the same as the ID in my appinfo.json file. Also changed the key in case that was the cause...
  6. #6  
    The "key" suggestion wasn't causing your issue. I just thought I'd mention it.

    You still have a few problems which are:

    1) You're still using getMinutes() instead of getUTCMinutes().
    2) You're submitting the date in DD/MM/YYYY format instead of MM/DD/YYYY
    3) You're using setMinutes() instead of setTime() to add the number of minutes to the current time.
    4) You're not padding your month, day, hours, minutes, and seconds with leading zeroes (ex. if the current date/time is April 7th, 2009 7:05:09 am, the alarm string should be "04/07/2009 07:05:09" instead of "4/7/2009 7:5:9").

    So I'd do the following:

    Code:
            var alarm = new Date();
            var alarmTimeInMilliseconds = alarm.getTime() + (2 * 60 * 1000);
            alarm.setTime(alarmTimeInMilliseconds);
    
    	var day = alarm.getUTCDate();
    	var month = alarm.getUTCMonth()+1;
    	var year = alarm.getUTCFullYear();
    	var hours = alarm.getUTCHours();
    	var minutes = alarm.getUTCMinutes();
    	var seconds = alarm.getUTCSeconds();
    	var alarmString = padZeroes(month, 2) +"/"+padZeroes(day, 2)+"/"+year+" "+padZeroes(hours, 2)+":"+padZeroes(minutes, 2)+":"+padZeroes(seconds, 2);
    	Mojo.Log.info("...Attempting to set an alarm for: ", alarmString);
    
            //NOTE:  You'll have to write a routine called padZeroes that will return the given number as a string with the given length (in this case it would be 2).
  7. #7  
    Thank you! that solved a problem i was having with it!

Posting Permissions