1 /*
  2  * This is system cordova_plugin (TV specific API).
  3  * Apache License (2004). See http://www.apache.org/licenses/LICENSE-2.0
  4  *
  5  * Copyright (c) 2014, LG Electronics, Inc.
  6  */
  7 
  8 
  9 /**
 10  * This represents the power API itself, and provides a global namespace for operating power service.
 11  * @class
 12  */
 13 cordova.define('cordova/plugin/power', function (require, exports, module) { // jshint ignore:line
 14     
 15     function log(msg) {
 16         //console.log//will be removed // jshint ignore:line
 17     }
 18     
 19     var service;
 20     if(window.PalmSystem) { // jshint ignore:line
 21         log("Window.PalmSystem Available");
 22         service = require('cordova/plugin/webos/service');
 23     } else {
 24         service = {
 25             Request : function(uri, params) {
 26                log(uri + " invoked. But I am a dummy because PalmSystem is not available");
 27                         
 28                if (typeof params.onFailure === 'function') {
 29                   params.onFailure({ returnValue:false,
 30                      errorText:"PalmSystem Not Available. Cordova is not installed?"});
 31                }
 32         }};
 33     }
 34 
 35     /**
 36      * power interface
 37      */
 38     var Power = function () {
 39     };
 40     
 41     function checkErrorCodeNText(result, errorCode, errorText) {
 42         
 43         if (result.errorCode === undefined || result.errorCode === null ) {
 44             result.errorCode = errorCode;
 45         }
 46         if (result.errorText ===undefined || result.errorText === null) {
 47             result.errorText = errorText;
 48         }
 49     }
 50     
 51     /*
 52     function padZero(num, size) {
 53         var s = num+"";
 54         while (s.length < size) s = "0" + s;
 55         return s;
 56     }
 57     */
 58     
 59     /**
 60      * @namespace Power.PowerMode
 61      */
 62     Power.PowerCommand = {
 63     /**
 64      * shutdown
 65      * @since 1.0
 66      * @constant
 67      */
 68     SHUTDOWN : "powerOff",
 69     /**
 70      * reboot
 71      * @since 1.0
 72      * @constant
 73      */
 74     REBOOT : "reboot"
 75     };
 76     
 77     /**
 78      * @namespace Power.DisplayMode
 79      */
 80     Power.DisplayMode = {
 81     /**
 82      * display off
 83      * @since 1.0
 84      * @constant
 85      */
 86     DISPLAY_OFF : "Screen Off",
 87     /**
 88      * display on
 89      * @since 1.0
 90      * @constant
 91      */
 92     DISPLAY_ON : "Active"
 93     };
 94     
 95     /**
 96      * @namespace Power.TimerWeek
 97      */
 98     Power.TimerWeek = {
 99     /**
100      * Monday
101      * @since 1.0
102      * @constant
103      */
104     MONDAY : 1,
105     /**
106      * Tuesday
107      * @since 1.0
108      * @constant
109      */
110     TUESDAY : 2,
111     /**
112      * Wednesday
113      * @since 1.0
114      * @constant
115      */
116     WEDNESDAY : 4,
117     /**
118      * Thursday
119      * @since 1.0
120      * @constant
121      */
122     THURSDAY : 8,
123     /**
124      * Friday
125      * @since 1.0
126      * @constant
127      */
128     FRIDAY : 16,
129     /**
130      * Saturday
131      * @since 1.0
132      * @constant
133      */
134     SATURDAY : 32,
135     /**
136      * Sunday
137      * @since 1.0
138      * @constant
139      */
140     SUNDAY : 64,
141     /**
142      * Everyday
143      * @since 1.0
144      * @constant
145      */
146     EVERYDAY : 127
147     };
148     
149     /**
150      * Gets power status 
151      * @class Power
152      * @param {Function} successCallback success callback function.
153      * @param {Function} errorCallback failure callback function.
154      * @return <p>{Object} </p>
155      * <div align=left>
156      * <table class="hcap_spec" width=400>
157      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th></tr></thead>
158      *   <tbody>
159      *       <tr><th>wakeOnLan</th><th>Boolean</th><th>true : enabled / false : disabled </th></tr>
160      *       <tr class="odd"><th>displayMode</th><th>String</th><th><a href="Power.DisplayMode.html#.DISPLAY_ON">Power.DisplayMode.DISPLAY_ON</a> | <a href="Power.DisplayMode.html#.DISPLAY_OFF">Power.DisplayMode.DISPLAY_OFF</a> </th></tr>
161      *       <tr><th>allOnTimer</th><th>Boolean</th><th>true : enabled / false : disabled </th></tr>
162      *       <tr class="odd"><th>allOffTimer</th><th>Boolean</th><th>true : enabled / false : disabled </th></tr>
163      *   </tbody>
164      * </table>
165      * </div>
166      *
167      * @example
168      * // Javascript code
169      * function getPowerStatus () {
170      *   function successCb(cbObject) {
171      *      console.log("cbObject : " + JSON.stringify(cbObject));
172      *      console.log("wakeOnLan : " + cbObject.wakeOnLan);
173      *      console.log("displayMode : " + cbObject.displayMode);
174      *      console.log("allOnTimer : " + cbObject.allOnTimer);
175      *      console.log("allOffTimer : " + cbObject.allOffTimer);         
176      *
177      *      // Do something
178      *         ...
179      *   }
180      *
181      *   function failureCb(cbObject) {
182      *      var errorCode = cbObject.errorCode;
183      *      var errorText = cbObject.errorText;
184      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
185      *   }
186      *
187      *   var power = new Power();
188      *   power.getPowerStatus(successCb, failureCb);
189      * }
190      * @since 1.0
191      * @see
192      * <a href="Power%23setWakeOnLan.html">Power.setWakeOnLan()</a>, 
193      * <a href="Power%23setDisplayMode.html">Power.setDisplayMode()</a><br>
194      */
195     Power.prototype.getPowerStatus = function (successCallback, errorCallback) {
196          
197     log("getPowerStatus: ");
198     
199     /*
200     service.Request("luna://com.webos.service.tvpower/power/", {
201     */
202     service.Request("luna://com.webos.service.tv.signage/", {
203         method : "getPowerState",
204             onSuccess : function(result) {
205                 log("getPowerStatus: On Success");
206                 var cbObj = {};
207                 
208                 if(result.returnValue === true) {
209                     cbObj.displayMode = result.state;
210                 }
211                 
212                 service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
213                     method : "get",
214                     parameters : {
215                         category : "commercial",
216                         keys : ["wolEnable"]
217                     },
218                     onSuccess : function(result) {
219                         log("getPowerStatus: On Success 2");
220                         
221                         if(result.returnValue === true) {
222                             cbObj.wakeOnLan = (result.settings.wolEnable === "1" ? true : false );
223                         }
224                         
225                         service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
226                             method : "get",
227                             parameters : {
228                                 category : "time",
229                                 keys : ["onTimerEnable", "offTimerEnable"]
230                             },
231                             onSuccess : function(result) {
232                                 log("getPowerStatus: On Success 3");
233                                 
234                                 if(result.returnValue === true) {
235                                     cbObj.allOnTimer = (result.settings.onTimerEnable === "on" ? true : false );
236                                     cbObj.allOffTimer = (result.settings.offTimerEnable === "on" ? true : false );
237                                 
238                                     if(successCallback && typeof successCallback === 'function') {
239                                         successCallback(cbObj);
240                                     }
241                                 }
242                             },
243                             onFailure : function(result) {
244                                 log("getPowerStatus: On Failure 3");
245                                 delete result.returnValue;
246                                 if(errorCallback && typeof errorCallback === 'function') {
247                                     checkErrorCodeNText(result, "PGPS", "Power.getPowerStatus returns failure.");
248                                     errorCallback(result);
249                                 }
250                             }
251                         });
252                     },
253                     onFailure : function(result) {
254                         log("getPowerStatus: On Failure 2");
255                         delete result.returnValue;
256                         if(errorCallback && typeof errorCallback === 'function') {
257                             checkErrorCodeNText(result, "PGPS", "Power.getPowerStatus returns failure.");
258                             errorCallback(result);
259                         }
260                     }
261                 });
262             },
263             onFailure : function(result) {
264                 log("getPowerStatus: On Failure");
265                 delete result.returnValue;
266                 //TODO; Is this an error case?
267                 if(errorCallback && typeof errorCallback === 'function') {
268                     checkErrorCodeNText(result, "PGPS", "Power.getPowerStatus returns failure.");
269                     errorCallback(result);
270                 }
271             }
272         });
273     
274     log("Power.getPowerStatus Done");
275         
276     };
277     
278     /**
279      * Controls all "on timer". This API enables/disables all "on timer" at once. Values of each "on timer" which was set by Power.addOnTimer() are not changed when option.clearOnTimer is false.
280      * 
281      * @class Power
282      * @param {Function} successCallback success callback function.
283      * @param {Function} errorCallback failure callback function.     
284      * @param {Object} options
285      * <div align=left>
286      * <table class="hcap_spec" width=400>
287      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
288      *   <tbody>
289      *       <tr><th>allOnTimer</th><th>Boolean</th><th>true : enabled / false : disabled </th><th>required</th></tr>
290      *       <tr class="odd"><th>clearOnTimer</th><th>Boolean</th><th>true : removes all "on Timer" / false : do nothing. (default) </th><th>optional</th></tr>
291      *   </tbody>
292      * </table>
293      * </div>
294      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
295      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
296      * @example
297      * // Javascript code
298      * function enableAllOnTimer () {
299      *   var options = {
300      *      allOnTimer : true
301      *   };
302      *     
303      *   function successCb() {
304      *      // Do something
305      *   }
306      *
307      *   function failureCb(cbObject) {
308      *      var errorCode = cbObject.errorCode;
309      *      var errorText = cbObject.errorText;
310      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
311      *   }
312      *
313      *   var power = new Power();
314      *   power.enableAllOnTimer(successCb, failureCb, options);
315      * }
316      * @since 1.0
317      * @since 1.3 optons.clearOnTimer
318      * @see
319      * <a href="Power%23getPowerStatus.html">Power.getPowerStatus()</a><br>
320      */
321     Power.prototype.enableAllOnTimer = function (successCallback, errorCallback, options) {
322     
323     log("enableAllOnTimer: " + JSON.stringify(options));
324     
325     var op = null;
326     
327     switch(options.allOnTimer) {
328     case true :
329         op = "on";
330         break;
331     case false :
332         op = "off";
333         break;
334     }
335         
336     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
337             method : "set",
338             parameters : {
339                 category : "time",
340                 settings : {"onTimerEnable":op}
341             },
342             onSuccess : function() {
343             	
344             	if (options.clearOnTimer === true) {
345             		
346             		 var count = 0;
347                      var hour = ["0","0","0","0","0","0","0"], min = ["0","0","0","0","0","0","0"], week = ["0","0","0","0","0","0","0"], source = ["0","0","0","0","0","0","0"], schedule = [];
348                      
349                      service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
350                          method : "set",
351                          parameters : {
352                              category : "commercial",
353                              settings : {
354                              "multiOnTimerHour":hour,
355                              "multiOnTimerMinute":min,
356                              "multiOnTimerWeekday":week,
357                              "multiOnTimerSource":source,
358                              "onTimerCount":count,
359                              "onTimerSchedule":schedule
360                              }
361                          },
362                          onSuccess : function() {
363                              log("enableAllOnTimer: On Success 2");
364                              
365                              if(typeof successCallback === 'function'){
366                                  successCallback();
367                              }
368                          },
369                          onFailure : function(result) {
370                              log("enableAllOnTimer: On Failure 2");
371                              delete result.returnValue;
372                              if (typeof errorCallback === 'function') {
373                                  checkErrorCodeNText(result, "PEAOT", "Power.enableAllOnTimer returns failure. / clearOnTimer");
374                                  errorCallback(result);
375                              }
376                                  
377                          }
378                      });
379                      
380             	} else {
381             		
382             		if(typeof successCallback === 'function') {
383                         log("enableAllOnTimer: On Success");
384                         successCallback();
385                     }
386             	}
387             },
388             onFailure : function(result) {
389                 delete result.returnValue;
390                 if(typeof errorCallback === 'function') {
391                     checkErrorCodeNText(result, "PEAOT", "Power.enableAllOnTimer returns failure.");
392                     log("enableAllOnTimer: On Failure");
393                     errorCallback(result);
394                 }
395             }
396         });
397     
398     log("Power.enableAllOnTimer Done");
399     
400     };
401         
402     /**
403      * Controls all "off timer". This API enables/disables all "off timer" at once. Values of each "off timer" which was set by Power.addOffTimer() are not changed when options.clearOffTimer is false.
404      *   
405      * @class Power
406      * @param {Function} successCallback success callback function.
407      * @param {Function} errorCallback failure callback function.     
408      * @param {Object} options
409      * <div align=left>
410      * <table class="hcap_spec" width=400>
411      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
412      *   <tbody>
413      *       <tr><th>allOffTimer</th><th>Boolean</th><th>true : enabled / false : disabled </th><th>required</th></tr>
414      *       <tr class="odd"><th>clearOffTimer</th><th>Boolean</th><th>true : removes all "off Timer" / false : do nothing. (default) </th><th>optional</th></tr>
415      *   </tbody>
416      * </table>
417      * </div>
418      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
419      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
420      * @example
421      * // Javascript code
422      * function enableAllOffTimer () {
423      *   var options = {
424      *      allOffTimer : true
425      *   };
426      *     
427      *   function successCb() {
428      *      // Do something
429      *   }
430      *
431      *   function failureCb(cbObject) {
432      *      var errorCode = cbObject.errorCode;
433      *      var errorText = cbObject.errorText;
434      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
435      *   }
436      *
437      *   var power = new Power();
438      *   power.enableAllOffTimer(successCb, failureCb, options);
439      * }
440      * @since 1.0
441      * @since 1.3 options.clearOffTimer
442      * @see
443      * <a href="Power%23getPowerStatus.html">Power.getPowerStatus()</a><br>
444      */
445     Power.prototype.enableAllOffTimer = function (successCallback, errorCallback, options) {
446     
447     log("enableAllOffTimer: " + JSON.stringify(options));
448     
449     var op = null;
450     
451     switch(options.allOffTimer) {
452     case true :
453         op = "on";
454         break;
455     case false :
456         op = "off";
457         break;
458     }
459         
460     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
461             method : "set",
462             parameters : {
463                 category : "time",
464                 settings : {"offTimerEnable":op}
465             },
466             onSuccess : function() {
467             	
468             	
469             	if (options.clearOffTimer === true) {
470             		
471             		var count = 0;
472                     var hour = ["0","0","0","0","0","0","0"], min = ["0","0","0","0","0","0","0"], week = ["0","0","0","0","0","0","0"], schedule = [];
473                     
474                 	service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
475                         method : "set",
476                         parameters : {
477                             category : "commercial",
478                             settings : {
479                             "multiOffTimerHour":hour,
480                             "multiOffTimerMinute":min,
481                             "multiOffTimerWeekday":week,
482                             "offTimerCount":count,
483                             "offTimerSchedule":schedule
484                             }
485                         },
486                         onSuccess : function() {
487                             log("enableAllOffTimer: On Success 2");
488                             
489                             if(typeof successCallback === 'function'){
490                                 successCallback();
491                             }
492                         },
493                         onFailure : function(result) {
494                             log("enableAllOffTimer: On Failure 2");
495                             delete result.returnValue;
496                             if (typeof errorCallback === 'function') {
497                                 checkErrorCodeNText(result, "PEAOT", "Power.enableAllOffTimer returns failure. / clearOffTimer");
498                                 errorCallback(result);
499                             }
500                                 
501                         }
502                     });
503             		
504             	} else {
505             		
506             		if(typeof successCallback === 'function') {
507                         log("enableAllOffTimer: On Success");
508                         successCallback();
509                     }
510             		
511             	}
512             	
513             },
514             onFailure : function(result) {
515                 delete result.returnValue;
516                 if(typeof errorCallback === 'function') {
517                     checkErrorCodeNText(result, "PEAOT", "Power.enableAllOffTimer returns failure.");
518                     log("enableAllOffTimer: On Failure");
519                     errorCallback(result);
520                 }
521             }
522         });
523     
524     log("Power.enableAllOffTimer Done");
525     
526     };
527     
528     /**
529      * Enables or disables 'wake on LAN'. 
530      * @class Power
531      * @param {Function} successCallback success callback function.
532      * @param {Function} errorCallback failure callback function.
533      * @param {Object} options
534      * <div align=left>
535      * <table class="hcap_spec" width=400>
536      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
537      *   <tbody>
538      *       <tr><th>wakeOnLan</th><th>Boolean</th><th>true : enabled / false : disabled </th><th>required</th></tr>
539      *   </tbody>
540      * </table>
541      * </div>
542      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
543      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
544      * @example
545      * // Javascript code
546      * function enableWakeOnLan () {
547      *   var options = {
548      *      wakeOnLan : true
549      *   };
550      *     
551      *   function successCb() {
552      *      // Do something
553      *   }
554      *
555      *   function failureCb(cbObject) {
556      *      var errorCode = cbObject.errorCode;
557      *      var errorText = cbObject.errorText;
558      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
559      *   }
560      *
561      *   var power = new Power();
562      *   power.enableWakeOnLan(successCb, failureCb, options);
563      * }
564      * @since 1.0
565      * @see
566      * <a href="Power%23getPowerStatus.html">Power.getPowerStatus()</a><br>
567      */
568     Power.prototype.enableWakeOnLan = function (successCallback, errorCallback, options) {
569     
570     log("enableWakeOnLan: " + JSON.stringify(options));
571     
572     var op = null;
573     
574     switch(options.wakeOnLan) {
575     case true :
576         op = "1";
577         break;
578     case false :
579         op = "0";
580         break;
581     }
582         
583     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
584             method : "set",
585             parameters : {
586                 category : "commercial",
587                 settings : {"wolEnable":op}
588             },
589             onSuccess : function() {
590                 if(typeof successCallback === 'function') {
591                     log("enableWakeOnLan: On Success");
592                     successCallback();
593                 }
594             },
595             onFailure : function(result) {
596                 delete result.returnValue;
597                 if(typeof errorCallback === 'function') {
598                     checkErrorCodeNText(result, "PSWOL", "Power.enableWakeOnLan returns failure.");
599                     log("enableWakeOnLan: On Failure");
600                     errorCallback(result);
601                 }
602             }
603         });
604     
605     log("Power.enableWakeOnLan Done");
606     
607     };
608     
609     var version = null;
610     function checkPlatformVersion(cb) {
611         
612         if (version === null) {
613             
614             service.Request('luna://com.webos.service.tv.systemproperty', {
615                 method: 'getSystemInfo',
616                 parameters: {
617                     keys: ["sdkVersion"]
618                 },
619                 onSuccess: function(result) {
620                     log("getPlatformInfo: onSuccess");
621                     log("version : " + result.sdkVersion);
622                     
623                     var temp = result.sdkVersion.split('.');
624                     if (temp.length >= 1 && temp[0] === '1') {
625                         version = 1;
626                     } else if (temp.length >= 1 && temp[0] === '2') {
627                         version = 2;
628                     } else {
629                         version = 0;
630                     }
631                     
632                     delete result.returnValue;
633                     
634                     cb(version);
635                     
636                 },
637                 onFailure: function(error) {
638                     log("getPlatformInfo: onFailure");
639                     delete error.returnValue;
640                     version = 0;
641                     
642                     cb(version);
643                 }
644             });
645             
646         } else {
647             cb(version);
648         }
649     }
650     
651     function convertExternal(extStr) {
652         switch (extStr) {
653         case 'ext://hdmi:1' :
654             return ( version === 1 ? "HDMI1" : "HDMI");
655         case 'ext://hdmi:2' :
656             return "HDMI2";
657         case 'ext://dvi:1' :
658             return "DVI";
659         case 'ext://dp:1':
660             return "DISPLAYPORT";
661         case 'ext://rgb:1':
662             return "RGB";
663         case 'HDMI1':
664             return "ext://hdmi:1";
665         case 'HDMI':
666             return "ext://hdmi:1";
667         case 'HDMI2':
668             return "ext://hdmi:2";
669         case 'DVI':
670             return "ext://dvi:1";
671         case 'DISPLAYPORT':
672             return "ext://dp:1";
673         case 'RGB' :
674             return "ext://rgb:1";
675         }
676         return null; 
677     }
678     
679     /*
680     function convertWeekToStr(week) {
681         
682         var str = "";
683         week *= 1;
684         
685         if ( (week & Power.TimerWeek.EVERYDAY) === Power.TimerWeek.EVERYDAY ) { // jshint ignore:line
686             return "Everyday";
687         }
688         if ( (week & Power.TimerWeek.MONDAY) === Power.TimerWeek.MONDAY ) { // jshint ignore:line
689             str += "Mon ";
690             week -= Power.TimerWeek.MONDAY;
691         }
692         if ( (week & Power.TimerWeek.TUESDAY) === Power.TimerWeek.TUESDAY ) { // jshint ignore:line
693             str += "Tue ";
694             week -= Power.TimerWeek.TUESDAY;
695         }
696         if ( (week & Power.TimerWeek.WEDNESDAY) === Power.TimerWeek.WEDNESDAY ) { // jshint ignore:line
697             str += "Wed ";
698             week -= Power.TimerWeek.WEDNESDAY;
699         }
700         if ( (week & Power.TimerWeek.THURSDAY) === Power.TimerWeek.THURSDAY ) { // jshint ignore:line
701             str += "Thu ";
702             week -= Power.TimerWeek.THURSDAY;
703         }
704         if ( (week & Power.TimerWeek.FRIDAY) === Power.TimerWeek.FRIDAY ) { // jshint ignore:line
705             str += "Fri ";
706             week -= Power.TimerWeek.FRIDAY;
707         }
708         if ( (week & Power.TimerWeek.SATURDAY) === Power.TimerWeek.SATURDAY ) { // jshint ignore:line
709             str += "Sat ";
710             week -= Power.TimerWeek.SATURDAY;
711         }
712         if ( (week & Power.TimerWeek.SUNDAY) === Power.TimerWeek.SUNDAY ) { // jshint ignore:line
713             str += "Sun ";
714             week -= Power.TimerWeek.SUNDAY;
715         }
716         
717         if (str === "")  {
718             str = "None";
719         }
720 
721 
722         return str;
723     }
724     */
725     
726     /**
727      * Adds 'on timer'. 
728      * @class Power
729      * @param {Function} successCallback success callback function.
730      * @param {Function} errorCallback failure callback function.     
731      * @param {Object} options
732      * <div align=left>
733      * <table class="hcap_spec" width=400>
734      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
735      *   <tbody>
736      *       <tr><th>hour</th><th>Number</th><th>hour (0~23)</th><th>required</th></tr>
737      *       <tr class="odd"><th>minute</th><th>Number</th><th>minute (0~59)</th><th>required</th></tr>
738      *       <tr><th>week</th><th>Number</th><th>week <a href="Power.TimerWeek.html#constructor">Power.TimerWeek</a> <br>To use it on multiple days of week, set the sum of week. <br>For example, 1 + 64 (Power.TimerWeek.Monday + Power.TimerWeek.Sunday) means that this timer works on every Monday and Sunday.</th><th>required</th></tr>
739      *       <tr class="odd"><th>inputSource</th><th>String</th><th>Define the input source. ext://[externalInput]:[portNum]. (eg: "ext://hdmi:1")</th><th>required</th></tr>
740      *   </tbody>
741      * </table>
742      * </div>
743      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
744      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
745      * @example
746      * // Javascript code
747      * function addOnTimer () {
748      *   var options = {
749      *      hour : 9,
750      *      minute : 0,
751      *      week : Power.TimerWeek.MONDAY + Power.TimerWeek.FRIDAY,
752      *      inputSource : "ext://hdmi:1"
753      *   };
754      *     
755      *   function successCb() {
756      *      // Do something
757      *   }
758      *
759      *   function failureCb(cbObject) {
760      *      var errorCode = cbObject.errorCode;
761      *      var errorText = cbObject.errorText;
762      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
763      *   }
764      *
765      *   var power = new Power();
766      *   power.addOnTimer(successCb, failureCb, options);
767      * }
768      * @since 1.0
769      * @see
770      * <a href="Power%23getOnTimerList.html">Power.getOnTimerList()</a>, 
771      * <a href="InputSource.%23getInputSourceStatus.html">InputSource.getInputSourceStatus()</a><br>
772      */
773     Power.prototype.addOnTimer = function (successCallback, errorCallback, options) {
774     
775     log("addOnTimer: " + JSON.stringify(options));
776     
777     // bound check
778     if (options.hour === undefined || isNaN(options.hour) || typeof options.hour !== 'number' || options.hour < 0 || options.hour > 23 ||
779             options.minute === undefined || isNaN(options.minute) || typeof options.minute !== 'number' || options.minute < 0 || options.minute > 59 ||
780             options.week === undefined || isNaN(options.week) || typeof options.week !== 'number' || options.week <= 0 || options.week > 127 ||
781             options.inputSource === undefined || typeof options.inputSource !== 'string' || options.inputSource.indexOf("ext://") !== 0) {
782         
783         if (typeof errorCallback === 'function') {
784             var result = {};
785             checkErrorCodeNText(result, "PAOT", "Power.addOnTimer returns failure. invalid parameters or out of range.");
786             errorCallback(result);
787         }
788         
789         return;
790     }
791     
792     //1. timer enable / disable
793     //2. set time information
794     //3. set the number of timer.
795     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
796         method : "get",
797         parameters : {
798             category : "commercial",
799             keys : ["multiOnTimerHour", "multiOnTimerMinute", "multiOnTimerWeekday", "multiOnTimerSource", "onTimerSchedule", "onTimerCount" ]
800         },
801         onSuccess : function(result) {
802             //log("addOnTimer: get timer data " + JSON.stringify(result));
803             
804             if (result.returnValue === true) {
805                 
806                 checkPlatformVersion( function(ver) {
807                     log("version : " + ver);
808                     
809                     // in exceptional case of luna result error
810                     if ( typeof result.settings.multiOnTimerHour === 'string') {
811                         result.settings.multiOnTimerHour = JSON.parse(result.settings.multiOnTimerHour);
812                     }
813                     
814                     if ( typeof result.settings.multiOnTimerMinute === 'string') {
815                         result.settings.multiOnTimerMinute = JSON.parse(result.settings.multiOnTimerMinute);
816                     }
817                     
818                     if ( typeof result.settings.multiOnTimerWeekday === 'string') {
819                         result.settings.multiOnTimerWeekday = JSON.parse(result.settings.multiOnTimerWeekday);
820                     }
821                     
822                     if ( typeof result.settings.multiOnTimerSource === 'string') {
823                         result.settings.multiOnTimerSource = JSON.parse(result.settings.multiOnTimerSource);
824                     }
825                     
826                     if ( typeof result.settings.onTimerSchedule === 'string') {
827                         result.settings.onTimerSchedule = JSON.parse(result.settings.onTimerSchedule);
828                     }
829                     // end of exceptional case
830                     
831                     var index = (result.settings.onTimerSchedule === null || result.settings.onTimerSchedule === undefined ? 0 : result.settings.onTimerSchedule.length);
832                     
833                     if (result.settings.multiOnTimerHour.length <= index) {
834                         // out of range
835                         if(typeof errorCallback === 'function') {
836                             checkErrorCodeNText(result, "PSOT", "Power.addOnTimer returns failure. No space to add timer.");
837                             errorCallback(result);
838                         }
839                         return;
840                     }
841                     
842                     result.settings.multiOnTimerHour[index] = options.hour;
843                     result.settings.multiOnTimerMinute[index] = options.minute;
844                     result.settings.multiOnTimerWeekday[index] = options.week;
845                     result.settings.multiOnTimerSource[index] = convertExternal(options.inputSource);
846                     //result.settings.onTimerSchedule[index] = "" + padZero(options.hour, 2) +":"+ padZero(options.minute, 2) +", "+ convertWeekToStr(options.week);
847                     
848                     var seed = 360;
849                     result.settings.onTimerSchedule[index] =
850                     {
851                         "_id" : "" + seed++,
852                         "hour": options.hour,
853                         "input": convertExternal(options.inputSource),
854                         "minute": options.minute,
855                         "weekday": options.week
856                     };
857                     
858                                     
859                     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
860                         method : "set",
861                         parameters : {
862                             category : "commercial",
863                             settings : {
864                             "multiOnTimerHour":result.settings.multiOnTimerHour,
865                             "multiOnTimerMinute":result.settings.multiOnTimerMinute,
866                             "multiOnTimerWeekday":result.settings.multiOnTimerWeekday,
867                             "multiOnTimerSource":result.settings.multiOnTimerSource,
868                             "onTimerCount":index + 1,
869                             "onTimerSchedule":result.settings.onTimerSchedule
870                             }
871                         },
872                         onSuccess : function() {
873                             log("addOnTimer: On Success 2");
874                             
875                             if(typeof successCallback === 'function'){
876                                 successCallback();
877                             }
878                         },
879                         onFailure : function(result) {
880                             log("addOnTimer: On Failure 2");
881                             delete result.returnValue;
882                             if (typeof errorCallback === 'function') {
883                                 checkErrorCodeNText(result, "PAOT", "Power.addOnTimer returns failure.");
884                                 errorCallback(result);
885                             }
886                                 
887                         }
888                     });
889                 });
890             }
891         },
892         onFailure : function(result) {
893             log("addOnTimer: On Failure");
894             delete result.returnValue;
895             if(errorCallback && typeof errorCallback === 'function') {
896                 checkErrorCodeNText(result, "PAOT", "Power.addOnTimer returns failure.");
897                 errorCallback(result);
898             }
899         }
900     });
901     
902     log("Power.addOnTimer Done");
903     
904     };
905     
906     /**
907      * Deletes 'on timer'. The timer in the list that matches the parameter will be removed.
908      * @class Power
909      * @param {Function} successCallback success callback function.
910      * @param {Function} errorCallback failure callback function.     
911      * @param {Object} options
912      * <div align=left>
913      * <table class="hcap_spec" width=400>
914      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
915      *   <tbody>
916      *       <tr><th>hour</th><th>Number</th><th>hour (0~23)</th><th>required</th></tr>
917      *       <tr class="odd"><th>minute</th><th>Number</th><th>minute (0~59)</th><th>required</th></tr>
918      *       <tr><th>week</th><th>Number</th><th>week <a href="Power.TimerWeek.html#constructor">Power.TimerWeek</a> <br>To use it on multiple day of week, set the sum of week. <br>For example, 1 + 64 (Power.TimerWeek.Monday + Power.TimerWeek.Sunday) means that this timer works on every Monday and Sunday.</th><th>required</th></tr>
919      *       <tr class="odd"><th>inputSource</th><th>String</th><th>Define the input source. ext://[externalInput]:[portNum]. (eg: "ext://hdmi:1")</th><th>required</th></tr>
920      *   </tbody>
921      * </table>
922      * </div>
923      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
924      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
925      * @example
926      * // Javascript code
927      * function deleteOnTimer () {
928      *   var options = {
929      *      hour : 9,
930      *      minute : 0,
931      *      week : Power.TimerWeek.MONDAY + Power.TimerWeek.FRIDAY,
932      *      inputSource : "ext://hdmi:1"
933      *   };
934      *     
935      *   function successCb() {
936      *      // Do something
937      *   }
938      *
939      *   function failureCb(cbObject) {
940      *      var errorCode = cbObject.errorCode;
941      *      var errorText = cbObject.errorText;
942      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
943      *   }
944      *
945      *   var power = new Power();
946      *   power.deleteOnTimer(successCb, failureCb, options);
947      * }
948      * @since 1.0
949      * @see
950      * <a href="Power%23getOnTimerList.html">Power.getOnTimerList()</a>, 
951      * <a href="InputSource.%23getInputSourceStatus.html">InputSource.getInputSourceStatus()</a><br>
952      */
953     Power.prototype.deleteOnTimer = function (successCallback, errorCallback, options) {
954     
955     log("deleteOnTimer: " + JSON.stringify(options));
956     
957     // bound check
958     if (options.hour === undefined || isNaN(options.hour) || typeof options.hour !== 'number' || options.hour < 0 || options.hour > 23 ||
959             options.minute === undefined || isNaN(options.minute) || typeof options.minute !== 'number' || options.minute < 0 || options.minute > 59 ||
960             options.week === undefined || isNaN(options.week) || typeof options.week !== 'number' || options.week <= 0 || options.week > 127 ||
961             options.inputSource === undefined || typeof options.inputSource !== 'string' || options.inputSource.indexOf("ext://") !== 0) {
962         
963         if (typeof errorCallback === 'function') {
964             var result = {};
965             checkErrorCodeNText(result, "PDOT", "Power.deleteOnTimer returns failure. invalid parameters or out of range.");
966             errorCallback(result);
967         }
968         
969         return;
970     }
971     
972     //1. timer enable / disable
973     //2. set time information
974     //3. set the number of timer.
975     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
976         method : "get",
977         parameters : {
978             category : "commercial",
979             keys : ["multiOnTimerHour", "multiOnTimerMinute", "multiOnTimerWeekday", "multiOnTimerSource", "onTimerSchedule", "onTimerCount" ]
980         },
981         onSuccess : function(result) {
982             //log("deleteOnTimer: get timer data " + JSON.stringify(result));
983             
984             if (result.returnValue === true) {
985                 checkPlatformVersion(function(ver){
986                     
987                     log("version : " + ver);
988                     
989                     // in exceptional case of luna result error
990                     if ( typeof result.settings.multiOnTimerHour === 'string') {
991                         result.settings.multiOnTimerHour = JSON.parse(result.settings.multiOnTimerHour);
992                     }
993                     
994                     if ( typeof result.settings.multiOnTimerMinute === 'string') {
995                         result.settings.multiOnTimerMinute = JSON.parse(result.settings.multiOnTimerMinute);
996                     }
997                     
998                     if ( typeof result.settings.multiOnTimerWeekday === 'string') {
999                         result.settings.multiOnTimerWeekday = JSON.parse(result.settings.multiOnTimerWeekday);
1000                     }
1001                     
1002                     if ( typeof result.settings.multiOnTimerSource === 'string') {
1003                         result.settings.multiOnTimerSource = JSON.parse(result.settings.multiOnTimerSource);
1004                     }
1005                     
1006                     if ( typeof result.settings.onTimerSchedule === 'string') {
1007                         result.settings.onTimerSchedule = JSON.parse(result.settings.onTimerSchedule);
1008                     }
1009                     // end of exceptional case
1010                     
1011                     var addIndex = 0, count = (result.settings.onTimerSchedule === null || result.settings.onTimerSchedule === undefined ? 0 : result.settings.onTimerSchedule.length );
1012                     var hour = ["0","0","0","0","0","0","0"], min = ["0","0","0","0","0","0","0"], week = ["0","0","0","0","0","0","0"], source = ["0","0","0","0","0","0","0"], schedule = [];
1013                     var inputSource = convertExternal(options.inputSource);
1014                     var deleted = false;
1015                     for (var i=0; i<count; i++) {
1016                         
1017                         log("deleteOnTimer: " + inputSource);
1018                         
1019                         if (result.settings.onTimerSchedule[i] === null) {
1020                             continue;
1021                         }
1022                         
1023                         if (deleted === false &&
1024                                 options.hour === result.settings.onTimerSchedule[i].hour &&
1025                                 options.minute === result.settings.onTimerSchedule[i].minute &&
1026                                 options.week === result.settings.onTimerSchedule[i].weekday &&
1027                                 inputSource === result.settings.onTimerSchedule[i].input) {
1028                             // do nothing
1029                             log("deleteOnTimer: index " + i);
1030                             deleted = true;
1031                             
1032                         } else {
1033                             hour[addIndex] = result.settings.multiOnTimerHour[i];
1034                             min[addIndex] = result.settings.multiOnTimerMinute[i];
1035                             week[addIndex] = result.settings.multiOnTimerWeekday[i];
1036                             source[addIndex] = result.settings.multiOnTimerSource[i];
1037                             schedule[addIndex] =  result.settings.onTimerSchedule[i];
1038                             addIndex++;
1039                         }
1040                     }
1041                     
1042                     if (deleted === true) {count --;}
1043                     
1044                     if (count === 0) { schedule = [];}
1045                     
1046                     if (result.settings.onTimerSchedule.length === count) {
1047                         if (typeof errorCallback === 'function') {
1048                             checkErrorCodeNText(result, "PDOT", "Power.deleteOnTimer returns failure. There is no 'on timer' matched in the list.");
1049                             errorCallback(result);
1050                         }
1051                         return;
1052                     }
1053                                     
1054                     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
1055                         method : "set",
1056                         parameters : {
1057                             category : "commercial",
1058                             settings : {
1059                             "multiOnTimerHour":hour,
1060                             "multiOnTimerMinute":min,
1061                             "multiOnTimerWeekday":week,
1062                             "multiOnTimerSource":source,
1063                             "onTimerCount":count,
1064                             "onTimerSchedule":schedule
1065                             }
1066                         },
1067                         onSuccess : function() {
1068                             log("deleteOnTimer: On Success 2");
1069                             
1070                             if(typeof successCallback === 'function'){
1071                                 successCallback();
1072                             }
1073                         },
1074                         onFailure : function(result) {
1075                             log("deleteOnTimer: On Failure 2");
1076                             delete result.returnValue;
1077                             if (typeof errorCallback === 'function') {
1078                                 checkErrorCodeNText(result, "PDOT", "Power.deleteOnTimer returns failure.");
1079                                 errorCallback(result);
1080                             }
1081                                 
1082                         }
1083                     });
1084                 
1085                 });
1086                 
1087             }
1088         },
1089         onFailure : function(result) {
1090             log("deleteOnTimer: On Failure");
1091             delete result.returnValue;
1092             if(errorCallback && typeof errorCallback === 'function') {
1093                 checkErrorCodeNText(result, "PDOT", "Power.deleteOnTimer returns failure.");
1094                 errorCallback(result);
1095             }
1096         }
1097     });
1098     
1099     log("Power.deleteOnTimer Done");
1100     
1101     };
1102     
1103     /**
1104      * Adds 'off timer'. 
1105      * @class Power
1106      * @param {Function} successCallback success callback function.
1107      * @param {Function} errorCallback failure callback function.     
1108      * @param {Object} options
1109      * <div align=left>
1110      * <table class="hcap_spec" width=400>
1111      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
1112      *   <tbody>
1113      *       <tr><th>hour</th><th>Number</th><th>hour (0~23)</th><th>required</th></tr>
1114      *       <tr class="odd"><th>minute</th><th>Number</th><th>minute (0~59)</th><th>required</th></tr>
1115      *       <tr><th>week</th><th>Number</th><th>week <a href="Power.TimerWeek.html#constructor">Power.TimerWeek</a> <br>To use it on multiple days of week, set the sum of week. <br>For example, 1 + 64 (Power.TimerWeek.Monday + Power.TimerWeek.Sunday) means that this timer works on every Monday and Sunday.</th><th>required</th></tr>
1116      *   </tbody>
1117      * </table>
1118      * </div>
1119      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
1120      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
1121      * @example
1122      * // Javascript code
1123      * function addOffTimer () {
1124      *   var options = {
1125      *      hour : 9,
1126      *      minute : 0,
1127      *      week : Power.TimerWeek.MONDAY + Power.TimerWeek.FRIDAY
1128      *   };
1129      *     
1130      *   function successCb() {
1131      *      // Do something
1132      *   }
1133      *
1134      *   function failureCb(cbObject) {
1135      *      var errorCode = cbObject.errorCode;
1136      *      var errorText = cbObject.errorText;
1137      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
1138      *   }
1139      *
1140      *   var power = new Power();
1141      *   power.addOffTimer(successCb, failureCb, options);
1142      * }
1143      * @since 1.0
1144      * @see
1145      * <a href="Power%23getOffTimerList.html">Power.getOffTimerList()</a><br>
1146      */
1147     Power.prototype.addOffTimer = function (successCallback, errorCallback, options) {
1148         
1149         log("addOffTimer: " + JSON.stringify(options));
1150         
1151         // bound check
1152         if (options.hour === undefined || isNaN(options.hour) || typeof options.hour !== 'number' || options.hour < 0 || options.hour > 23 ||
1153                 options.minute === undefined || isNaN(options.minute) || typeof options.minute !== 'number' || options.minute < 0 || options.minute > 59 ||
1154                 options.week === undefined || isNaN(options.week) || typeof options.week !== 'number' || options.week <= 0 || options.week > 127 ) {
1155             
1156             if (typeof errorCallback === 'function') {
1157                 var result = {};
1158                 checkErrorCodeNText(result, "PAOT", "Power.addOffTimer returns failure. Invalid parameter.");
1159                 errorCallback(result);
1160             }
1161             
1162             return;
1163         }
1164         
1165         //1. timer enable / disable
1166         //2. set time information
1167         //3. set the number of timer.
1168         
1169         service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
1170             method : "get",
1171             parameters : {
1172                 category : "commercial",
1173                 keys : ["multiOffTimerHour", "multiOffTimerMinute", "multiOffTimerWeekday", "offTimerSchedule", "offTimerCount" ]
1174             },
1175             onSuccess : function(result) {
1176                 //log("addOffTimer: get timer data " + JSON.stringify(result));
1177                 
1178                 if (result.returnValue === true) {
1179                     
1180                     // in exceptional case of luna result error
1181                     if (typeof result.settings.multiOffTimerHour === 'string') {
1182                         result.settings.multiOffTimerHour = JSON.parse(result.settings.multiOffTimerHour);
1183                     }
1184                     
1185                     if (typeof result.settings.multiOffTimerMinute === 'string') {
1186                         result.settings.multiOffTimerMinute = JSON.parse(result.settings.multiOffTimerMinute);
1187                     }
1188                     
1189                     if (typeof result.settings.multiOffTimerWeekday === 'string') {
1190                         result.settings.multiOffTimerWeekday = JSON.parse(result.settings.multiOffTimerWeekday);
1191                     }
1192                     
1193                     if (typeof result.settings.offTimerSchedule === 'string') {
1194                         result.settings.offTimerSchedule = JSON.parse(result.settings.offTimerSchedule);
1195                     }
1196                     // end of exceptional case
1197                     
1198                     var index = (result.settings.offTimerSchedule === null || result.settings.offTimerSchedule === undefined ? 0 : result.settings.offTimerSchedule.length);
1199                     
1200                     if (result.settings.multiOffTimerHour.length <= index ) {
1201                         // out of range
1202                         if(typeof errorCallback === 'function') {
1203                             checkErrorCodeNText(result, "PAOT", "Power.addOffTimer returns failure. No space to add timer.");
1204                             errorCallback(result);
1205                         }
1206                         return;
1207                     }
1208                     
1209                     
1210                     
1211                     
1212                     result.settings.multiOffTimerHour[index] = options.hour;
1213                     result.settings.multiOffTimerMinute[index] = options.minute;
1214                     result.settings.multiOffTimerWeekday[index] = options.week;
1215                     //result.settings.offTimerSchedule[index] = "" + padZero(options.hour, 2) +":"+ padZero(options.minute, 2) +", "+ convertWeekToStr(options.week);
1216                     
1217                     var seed = 360;
1218                     result.settings.offTimerSchedule[index] = 
1219                     {
1220                         "_id":""+seed++,
1221                         "hour": options.hour,
1222                         "minute": options.minute,
1223                         "weekday": options.week
1224                     };
1225                 
1226                     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
1227                         method : "set",
1228                         parameters : {
1229                             category : "commercial",
1230                             settings : {
1231                             "multiOffTimerHour":result.settings.multiOffTimerHour,
1232                             "multiOffTimerMinute":result.settings.multiOffTimerMinute,
1233                             "multiOffTimerWeekday":result.settings.multiOffTimerWeekday,
1234                             "offTimerCount": index + 1,
1235                             "offTimerSchedule":result.settings.offTimerSchedule}
1236                         },
1237                         onSuccess : function() {
1238                             log("addOffTimer: On Success 2");
1239                             if(typeof successCallback === 'function'){
1240                                 successCallback();
1241                             }
1242                         },
1243                         onFailure : function(result) {
1244                             log("addOffTimer: On Failure 2");
1245                             delete result.returnValue;
1246                             if(typeof errorCallback === 'function') {
1247                                 checkErrorCodeNText(result, "PAOT", "Power.addOffTimer returns failure.");
1248                                 errorCallback(result);
1249                             }
1250                         }
1251                     });
1252                 }
1253             },
1254             onFailure : function(result) {
1255                 log("addOffTimer: On Failure");
1256                 delete result.returnValue;
1257                 if(errorCallback && typeof errorCallback === 'function') {
1258                     checkErrorCodeNText(result, "PAOT", "Power.addOffTimer returns failure.");
1259                     errorCallback(result);
1260                 }
1261             }
1262         });
1263         
1264         log("Power.addOffTimer Done");
1265     };
1266     
1267     /**
1268      * Deletes 'off timer'. The timer in the list that matches the parameter will be removed.
1269      * @class Power
1270      * @param {Function} successCallback success callback function.
1271      * @param {Function} errorCallback failure callback function.     
1272      * @param {Object} options
1273      * <div align=left>
1274      * <table class="hcap_spec" width=400>
1275      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
1276      *   <tbody>
1277      *       <tr><th>hour</th><th>Number</th><th>hour (0~23)</th><th>required</th></tr>
1278      *       <tr class="odd"><th>minute</th><th>Number</th><th>minute (0~59)</th><th>required</th></tr>
1279      *       <tr><th>week</th><th>Number</th><th>week <a href="Power.TimerWeek.html#constructor">Power.TimerWeek</a> <br>To use it on multiple day of week, set the sum of week. <br>For example, 1 + 64 (Power.TimerWeek.Monday + Power.TimerWeek.Sunday) means that this timer works on every Monday and Sunday.</th><th>required</th></tr>
1280      *   </tbody>
1281      * </table>
1282      * </div>
1283      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
1284      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
1285      * @example
1286      * // Javascript code
1287      * function deleteOffTimer () {
1288      *   var options = {
1289      *      hour : 9,
1290      *      minute : 0,
1291      *      week : Power.TimerWeek.MONDAY + Power.TimerWeek.FRIDAY
1292      *   };
1293      *     
1294      *   function successCb() {
1295      *      // Do something
1296      *   }
1297      *
1298      *   function failureCb(cbObject) {
1299      *      var errorCode = cbObject.errorCode;
1300      *      var errorText = cbObject.errorText;
1301      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
1302      *   }
1303      *
1304      *   var power = new Power();
1305      *   power.deleteOffTimer(successCb, failureCb, options);
1306      * }
1307      * @since 1.0
1308      * @see
1309      * <a href="Power%23getOffTimerList.html">Power.getOffTimerList()</a>, 
1310      * <a href="InputSource.%23getInputSourceStatus.html">InputSource.getInputSourceStatus()</a><br>
1311      */
1312     Power.prototype.deleteOffTimer = function (successCallback, errorCallback, options) {
1313     
1314     log("deleteOffTimer: " + JSON.stringify(options));
1315     
1316     // bound check
1317     if (options.hour === undefined || isNaN(options.hour) || typeof options.hour !== 'number' || options.hour < 0 || options.hour > 23 ||
1318             options.minute === undefined || isNaN(options.minute) || typeof options.minute !== 'number' || options.minute < 0 || options.minute > 59 ||
1319             options.week === undefined || isNaN(options.week) || typeof options.week !== 'number' || options.week <= 0 || options.week > 127 ) {
1320         
1321         if (typeof errorCallback === 'function') {
1322             var result = {};
1323             checkErrorCodeNText(result, "PDOT", "Power.deleteOffTimer returns failure. invalid parameters or out of range.");
1324             errorCallback(result);
1325         }
1326         
1327         return;
1328     }
1329     
1330     //1. timer enable / disable
1331     //2. set time information
1332     //3. set the number of timer.
1333     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
1334         method : "get",
1335         parameters : {
1336             category : "commercial",
1337             keys : ["multiOffTimerHour", "multiOffTimerMinute", "multiOffTimerWeekday", "offTimerSchedule", "offTimerCount" ]
1338         },
1339         onSuccess : function(result) {
1340             //log("deleteOffTimer: get timer data " + JSON.stringify(result));
1341             
1342             if (result.returnValue === true) {
1343                 
1344                 // in exceptional case of luna result error
1345                 if ( typeof result.settings.multiOffTimerHour === 'string') {
1346                     result.settings.multiOffTimerHour = JSON.parse(result.settings.multiOffTimerHour);
1347                 }
1348                 
1349                 if ( typeof result.settings.multiOffTimerMinute === 'string') {
1350                     result.settings.multiOffTimerMinute = JSON.parse(result.settings.multiOffTimerMinute);
1351                 }
1352                 
1353                 if ( typeof result.settings.multiOffTimerWeekday === 'string') {
1354                     result.settings.multiOffTimerWeekday = JSON.parse(result.settings.multiOffTimerWeekday);
1355                 }
1356                 
1357                 if ( typeof result.settings.offTimerSchedule === 'string') {
1358                     result.settings.offTimerSchedule = JSON.parse(result.settings.offTimerSchedule);
1359                 }
1360                 // end of exceptional case
1361                 
1362                 var addIndex = 0, count = (result.settings.offTimerSchedule === null || result.settings.offTimerSchedule === undefined ? 0 : result.settings.offTimerSchedule.length);
1363                 var hour = ["0","0","0","0","0","0","0"], min = ["0","0","0","0","0","0","0"], week = ["0","0","0","0","0","0","0"], schedule = [];
1364                 
1365                 //console.log("result.settings.offTimerSchedule.length  in " + result.settings.offTimerSchedule.length);
1366                 var deleted = false;
1367                 
1368                 for (var i=0; i<count; i++) {
1369                 
1370                     //console.log("result.settings.offTimerSchedule.length  in " + JSON.stringify(result.settings.offTimerSchedule[i]));
1371                     if (result.settings.offTimerSchedule[i] === null) {
1372                         continue;
1373                     }
1374                     
1375                     if (deleted === false && options.hour === result.settings.offTimerSchedule[i].hour && options.minute === result.settings.offTimerSchedule[i].minute && options.week === result.settings.offTimerSchedule[i].weekday ) {
1376                         // do nothing
1377                         //log("deleteOffTimer: index " + i);
1378                         deleted = true;
1379                         
1380                     } else {
1381                         hour[addIndex] = result.settings.multiOffTimerHour[i];
1382                         min[addIndex] = result.settings.multiOffTimerMinute[i];
1383                         week[addIndex] = result.settings.multiOffTimerWeekday[i];
1384                         schedule[addIndex] =  result.settings.offTimerSchedule[i];
1385                         addIndex++;
1386                     }
1387                 }
1388                 
1389                 if (deleted === true) {count --;}
1390                 
1391                 //console.log("result.settings.offTimerSchedule.length out  " + result.settings.offTimerSchedule.length);
1392                 
1393                 if (count === 0) { schedule = [];}
1394                 
1395                 if (result.settings.offTimerSchedule.length === count) {
1396                     if (typeof errorCallback === 'function') {
1397                         checkErrorCodeNText(result, "PDOT", "Power.deleteOffTimer returns failure. There is no 'off timer' matched in the list.");
1398                         errorCallback(result);
1399                     }
1400                     return;
1401                 }
1402                                 
1403                 service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
1404                     method : "set",
1405                     parameters : {
1406                         category : "commercial",
1407                         settings : {
1408                         "multiOffTimerHour":hour,
1409                         "multiOffTimerMinute":min,
1410                         "multiOffTimerWeekday":week,
1411                         "offTimerCount":count,
1412                         "offTimerSchedule":schedule
1413                         }
1414                     },
1415                     onSuccess : function() {
1416                         log("deleteOffTimer: On Success 2");
1417                         
1418                         if(typeof successCallback === 'function'){
1419                             successCallback();
1420                         }
1421                     },
1422                     onFailure : function(result) {
1423                         log("deleteOffTimer: On Failure 2");
1424                         delete result.returnValue;
1425                         if (typeof errorCallback === 'function') {
1426                             checkErrorCodeNText(result, "PDOT", "Power.deleteOffTimer returns failure.");
1427                             errorCallback(result);
1428                         }
1429                             
1430                     }
1431                 });
1432             }
1433         },
1434         onFailure : function(result) {
1435             log("deleteOffTimer: On Failure");
1436             delete result.returnValue;
1437             if(errorCallback && typeof errorCallback === 'function') {
1438                 checkErrorCodeNText(result, "PDOT", "Power.deleteOffTimer returns failure.");
1439                 errorCallback(result);
1440             }
1441         }
1442     });
1443     
1444     log("Power.deleteOffTimer Done");
1445     
1446     };
1447     
1448     /**
1449      * Gets 'on timer' list. 
1450      * @class Power
1451      * @param {Function} successCallback success callback function.
1452      * @param {Function} errorCallback failure callback function.
1453      * @return <p>{Object} list of timer object.</p>
1454      * <div align=left>
1455      * <table class="hcap_spec" width=400>
1456      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th></tr></thead>
1457      *   <tbody>
1458      *       <tr><th>timerList[]</th><th>Array</th><th>An array with the timer information object as its elements. </th></tr>
1459      *       <tr class="odd"><th>timerList[].hour</th><th>Number</th><th>hour (0~23) </th></tr>
1460      *       <tr><th>timerList[].minute</th><th>Number</th><th>minute (0~59) </th></tr>
1461      *       <tr class="odd"><th>timerList[].week</th><th>Number</th><th>week <a href="Power.TimerWeek.html#constructor">Power.TimerWeek</a> </th></tr>
1462      *       <tr><th>timerList[].inputSource</th><th>String</th><th>Input source</th></tr>
1463      *   </tbody>
1464      * </table>
1465      * </div>
1466      *
1467      * @example
1468      * // Javascript code
1469      * function getOnTimerList () {
1470      *   function successCb(cbObject) {
1471      *      var timerList = cbObject.timerList;
1472      *      for ( var i = 0; i < timerList.length; i++) {
1473      *         console.log("timerList[" + i + "] : " + JSON.stringify(timerList[i]));
1474      *         console.log("timerList[" + i + "].hour : " + timerList[i].hour);
1475      *         console.log("timerList[" + i + "].minute : " + timerList[i].minute);
1476      *         console.log("timerList[" + i + "].week : " + timerList[i].week);
1477      *         console.log("timerList[" + i + "].inputSource : " + timerList[i].inputSource);
1478      *      }
1479      *
1480      *      // Do something
1481      *         ...
1482      *   }
1483      *
1484      *   function failureCb(cbObject) {
1485      *      var errorCode = cbObject.errorCode;
1486      *      var errorText = cbObject.errorText;
1487      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
1488      *   }
1489      *
1490      *   var power = new Power();
1491      *   power.getOnTimerList(successCb, failureCb);
1492      * }
1493      * @since 1.0
1494      * @see
1495      * <a href="Power%23setOnTimer.html">Power.setOnTimer()</a><br>
1496      */
1497     Power.prototype.getOnTimerList = function (successCallback, errorCallback) {
1498     
1499     log("getOnTimerList: ");
1500     
1501     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
1502             method : "get",
1503             parameters : {
1504                 category : "commercial",
1505                 keys : ["onTimerSchedule"]
1506             },
1507             onSuccess : function(result) {
1508                 
1509                 if(result.returnValue === true) {
1510                     
1511                     checkPlatformVersion(function(ver){
1512 
1513                         log("version : " + ver);
1514                         
1515                         var cbObj = {};
1516                         
1517                         if ( typeof result.settings.onTimerSchedule === 'string') {
1518                             result.settings.onTimerSchedule = JSON.parse(result.settings.onTimerSchedule);
1519                         }
1520                         
1521                         var timerList = new Array(result.settings.onTimerSchedule === null || result.settings.onTimerSchedule === undefined ? 0 : result.settings.onTimerSchedule.length);
1522                         //console.log("timerList " + timerList);
1523                         //console.log("timerList.length " + timerList.length);
1524                         for(var index=0, i=0; i<timerList.length; i++) {
1525                             if (result.settings.onTimerSchedule[i] === null || result.settings.onTimerSchedule[i] === undefined ) {
1526                                 continue;
1527                             }
1528                             timerList[index] = {hour:0, minute:0, week:0, inputSource:0};
1529                             timerList[index].hour = result.settings.onTimerSchedule[i].hour;
1530                             timerList[index].minute = result.settings.onTimerSchedule[i].minute;
1531                             timerList[index].week = result.settings.onTimerSchedule[i].weekday;
1532                             timerList[index++].inputSource = convertExternal(result.settings.onTimerSchedule[i].input);
1533                         }
1534                         
1535                         cbObj.timerList = timerList;
1536                     
1537                         if(successCallback && typeof successCallback === 'function') {
1538                             successCallback(cbObj);
1539                         }
1540                     
1541                     });
1542                 }
1543             },
1544             onFailure : function(result) {
1545                 log("getOnTimerList: On Failure");
1546                 delete result.returnValue;
1547                 if(errorCallback && typeof errorCallback === 'function') {
1548                     checkErrorCodeNText(result, "PGOTL", "Power.getOnTimerList returns failure.");
1549                     errorCallback(result);
1550                 }
1551             }
1552         });
1553     
1554     log("Power.getOnTimerList Done");
1555     
1556     };
1557     
1558     /**
1559      * Gets 'off timer' list. 
1560      * @class Power
1561      * @param {Function} successCallback success callback function.
1562      * @param {Function} errorCallback failure callback function.
1563      * @return <p>{Object} list of timer object.</p>
1564      * <div align=left>
1565      * <table class="hcap_spec" width=400>
1566      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th></tr></thead>
1567      *   <tbody>
1568      *       <tr><th>timerList[]</th><th>Array</th><th>An array with the timer information object as its elements. </th></tr>  
1569      *       <tr class="odd"><th>timerList[].hour</th><th>Number</th><th>hour (0~23) </th></tr>
1570      *       <tr><th>timerList[].minute</th><th>Number</th><th>minute (0~59) </th></tr>
1571      *       <tr class="odd"><th>timerList[].week</th><th>Number</th><th>week <a href="Power.TimerWeek.html#constructor">Power.TimerWeek</a> </th></tr>
1572      *   </tbody>
1573      * </table>
1574      * </div>
1575      *
1576      * @example
1577      * // Javascript code
1578      * function getOffTimerList () {
1579      *   function successCb(cbObject) {
1580      *      var timerList = cbObject.timerList;
1581      *      for ( var i = 0; i < timerList.length; i++) {
1582      *         console.log("timerList[" + i + "] : " + JSON.stringify(timerList[i]));
1583      *         console.log("timerList[" + i + "].hour : " + timerList[i].hour);
1584      *         console.log("timerList[" + i + "].minute : " + timerList[i].minute);
1585      *         console.log("timerList[" + i + "].week : " + timerList[i].week);
1586      *      }
1587      *
1588      *      // Do something
1589      *         ...
1590      *   }
1591      *
1592      *   function failureCb(cbObject) {
1593      *      var errorCode = cbObject.errorCode;
1594      *      var errorText = cbObject.errorText;
1595      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
1596      *   }
1597      *
1598      *   var power = new Power();
1599      *   power.getOffTimerList(successCb, failureCb);
1600      * }
1601      * @since 1.0
1602      * @see
1603      * <a href="Power%23setOffTimer.html">Power.setOffTimer()</a><br>
1604      */
1605     Power.prototype.getOffTimerList = function (successCallback, errorCallback) {
1606     
1607     log("getOffTimerList: ");
1608     
1609         service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
1610             method : "get",
1611             parameters : {
1612                 category : "commercial",
1613                 keys : ["offTimerSchedule"]
1614             },
1615             onSuccess : function(result) {
1616                 log("getOffTimerList: On Success");
1617                 
1618                 if (result.returnValue === true) {
1619                     var cbObj = {};
1620                     
1621                     if (typeof result.settings.offTimerSchedule === 'string') {
1622                         result.settings.offTimerSchedule = JSON.parse(result.settings.offTimerSchedule);
1623                     }
1624                     
1625                     var timerList = new Array(result.settings.offTimerSchedule === null || result.settings.offTimerSchedule === undefined ? 0 : result.settings.offTimerSchedule.length);
1626                     
1627                     //console.log("timerList " + timerList);
1628                     //console.log("timerList.length " + timerList.length);
1629                     
1630                     for (var index=0, i=0; i<timerList.length; i++) {
1631                         if (result.settings.offTimerSchedule[i] === null || result.settings.offTimerSchedule[i] === undefined ) {
1632                             continue;
1633                         }
1634                         timerList[index] = {hour:0, minute:0, week:0};
1635                         timerList[index].hour = result.settings.offTimerSchedule[i].hour;
1636                         timerList[index].minute = result.settings.offTimerSchedule[i].minute;
1637                         timerList[index++].week = result.settings.offTimerSchedule[i].weekday;
1638                     }
1639                     
1640                     cbObj.timerList = timerList;
1641                 
1642                     if (successCallback && typeof successCallback === 'function') {
1643                         successCallback(cbObj);
1644                     }
1645                 }
1646             },
1647             onFailure : function(result) {
1648                 log("getOffTimerList: On Failure");
1649                 delete result.returnValue;
1650                 if (errorCallback && typeof errorCallback === 'function') {
1651                     checkErrorCodeNText(result, "PGOTL", "Power.getOffTimerList returns failure.");
1652                     errorCallback(result);
1653                 }
1654             }
1655         });
1656     
1657     log("Power.getOffTimerList Done");
1658     };
1659     
1660     /**
1661      * Turns on/off the display panel. This will only affect the panel, not the main power. 
1662      * @class Power
1663      * @param {Function} successCallback success callback function.
1664      * @param {Function} errorCallback failure callback function.
1665      * @param {Object} options
1666      * <div align=left>
1667      * <table class="hcap_spec" width=400>
1668      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
1669      *   <tbody>
1670      *       <tr><th>displayMode</th><th>String</th><th><a href="Power.DisplayMode.html#constructor">Power.DisplayMode</a> </th><th>required</th></tr>
1671      *   </tbody>
1672      * </table>
1673      * </div>
1674      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
1675      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
1676      * @example
1677      * // Javascript code
1678      * function setDisplayMode () {
1679      *   var options = {
1680      *      displayMode : Power.DisplayMode.DISPLAY_OFF
1681      *   };
1682      *     
1683      *   function successCb() {
1684      *      // Do something
1685      *   }
1686      *
1687      *   function failureCb(cbObject) {
1688      *      var errorCode = cbObject.errorCode;
1689      *      var errorText = cbObject.errorText;
1690      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
1691      *   }
1692      *
1693      *   var power = new Power();
1694      *   power.setDisplayMode(successCb, failureCb, options);
1695      * }
1696      * @since 1.0
1697      * @see
1698      * <a href="Power%23getPowerStatus.html">Power.getPowerStatus()</a><br>
1699      */
1700     Power.prototype.setDisplayMode = function (successCallback, errorCallback, options) {
1701     
1702     log("setDisplayMode: " + JSON.stringify(options));
1703     
1704     var command = null;
1705     
1706     switch (options.displayMode) {
1707     case Power.DisplayMode.DISPLAY_OFF :
1708         command = "turnOffScreen";
1709         break;
1710     case Power.DisplayMode.DISPLAY_ON :
1711         command = "turnOnScreen";
1712         break;
1713     }
1714     
1715     log("setDisplayMode: " + command);
1716     
1717     if (command === null && errorCallback && typeof errorCallback === 'function') {
1718         var result = {};
1719         checkErrorCodeNText(result, "PSDM", "Power.setDisplayMode returns failure. command was not defined.");
1720         errorCallback(result);
1721         log("Power.setDisplayMode invalid ");
1722         return;
1723     }
1724     
1725     /*
1726     service.Request("luna://com.webos.service.tvpower/power/", {
1727     */
1728     service.Request("luna://com.webos.service.tv.signage/", {
1729         method : "getPowerState",
1730         onSuccess : function(result) {
1731             log("setDisplayMode: On Success");
1732             
1733             if (result.returnValue === true && result.state === options.displayMode) {
1734                 // no need to do any action.
1735                 if (successCallback && typeof successCallback === 'function') {
1736                     log("setDisplayMode: no need to do any action.");
1737                     successCallback();
1738                 }
1739                 return;
1740             }
1741             /*
1742             service.Request("luna://com.webos.service.tvpower/power/", {
1743             */
1744             service.Request("luna://com.webos.service.tv.signage/", {
1745                 method : command,
1746                 onSuccess : function(result) {
1747                     log("setDisplayMode: On Success");
1748                     
1749                     if (result.returnValue === true) {
1750                         if (successCallback && typeof successCallback === 'function') {
1751                             successCallback();
1752                         }
1753                     }
1754                 },
1755                 onFailure : function(result) {
1756                     log("setDisplayMode: On Failure");
1757                     delete result.returnValue;
1758                     if (errorCallback && typeof errorCallback === 'function') {
1759                         checkErrorCodeNText(result, "PSDM", "Power.setDisplayMode returns failure.");
1760                         errorCallback(result);
1761                     }
1762                 }
1763             });
1764         },
1765     
1766         onFailure : function(result) {
1767             log("setDisplayMode: On Failure 2");
1768             delete result.returnValue;
1769             if(errorCallback && typeof errorCallback === 'function') {
1770                 checkErrorCodeNText(result, "PSDM", "Power.setDisplayMode returns failure.");
1771                 errorCallback(result);
1772             }
1773         }
1774     });
1775     
1776     log("Power.setDisplayMode Done");
1777     };
1778     
1779     /**
1780      * Executes power related command. 
1781      * @class Power
1782      * @param {Function} successCallback success callback function.
1783      * @param {Function} errorCallback failure callback function.     
1784      * @param {Object} options
1785      * <div align=left>
1786      * <table class="hcap_spec" width=400>
1787      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
1788      *   <tbody>
1789      *       <tr><th>powerCommand</th><th>String</th><th><a href="Power.PowerCommand.html#constructor">Power.PowerCommand</a> </th><th>required</th></tr>
1790      *   </tbody>
1791      * </table>
1792      * </div>
1793      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
1794      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
1795      * @example
1796      * // Javascript code
1797      * function executePowerCommand () {
1798      *   var options = {
1799      *      powerCommand : Power.PowerCommand.REBOOT
1800      *   };   
1801      *     
1802      *   function successCb() {
1803      *      // Do something
1804      *   }
1805      *
1806      *   function failureCb(cbObject) {
1807      *      var errorCode = cbObject.errorCode;
1808      *      var errorText = cbObject.errorText;
1809      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
1810      *   }
1811      *
1812      *   var power = new Power();
1813      *   power.executePowerCommand(successCb, failureCb, options);
1814      * }
1815      * @since 1.0
1816      * @see
1817      * <a href="Power%23getPowerStatus.html">Power.getPowerStatus()</a><br>
1818      */
1819     Power.prototype.executePowerCommand = function (successCallback, errorCallback, options) {
1820     
1821     log("executePowerCommand: " + JSON.stringify(options));
1822     
1823     if ( options.powerCommand === undefined || typeof options.powerCommand !== 'string' || options.powerCommand === null || options.powerCommand.length <= 0) {
1824         
1825         if (typeof errorCallback === 'function') {
1826             var result = {};
1827             checkErrorCodeNText(result, "PEPM", "Power.executePowerCommand returns failure. invalid argument or out of range. ");
1828             errorCallback(result);
1829         }
1830         return;
1831     }
1832     
1833     /*
1834     service.Request("luna://com.webos.service.tvpower/power/", {
1835     */
1836     service.Request("luna://com.webos.service.tv.signage/", {
1837             method : options.powerCommand,
1838             parameters : {
1839                 reason : "unknown"
1840             },
1841             onSuccess : function(result) {
1842                 log("executePowerCommand: On Success");
1843                 
1844                 if (result.returnValue === true) {
1845                     if (successCallback && typeof successCallback === 'function') {
1846                         successCallback();
1847                     }
1848                 }
1849             },
1850             onFailure : function(result) {
1851                 log("executePowerCommand: On Failure");
1852                 delete result.returnValue;
1853                 if (errorCallback && typeof errorCallback === 'function') {
1854                     checkErrorCodeNText(result, "PEPM", "Power.executePowerCommand returns failure.");
1855                     errorCallback(result);
1856                 }
1857             }
1858         });
1859     
1860     log("Power.executePowerCommand Done");
1861     
1862     };
1863     
1864     module.exports = Power;
1865 });
1866 
1867 Power = cordova.require('cordova/plugin/power'); // jshint ignore:line
1868 
1869