/**
 * Alarm and Periodic / Hourly chime Service
 *
 * Supported alarm types:
 * - Periodic (i.e. hourly chime)
 * - At specific time (e.g. "11:30am")
 *
 */

'use strict';

// var example_rules = [
//     {
//         "type": "time",
//         "enabled": true,
//         "name": "hourly chime",
//         "alarmType": "perodic",
//         "alarmHour": "1",
//         "alarmMinute": "",
//         "actionType": "play-pattern",
//         "patternId": "groovy"
//     },
//     {
//         "type": "time",
//         "enabled": true,
//         "name": "alarm at 4:45pm",
//         "alarmType": "exact",
//         "alarmHour": "17",
//         "alarmMinute": "04",
//         "actionType": "play-pattern",
//         "patternId": "red flash"
//     }
//
// ];

var conf = require('../configuration');
var log = require('../logger');

var PatternsService = require('./patternsService');

var intervalSecs = 1;

var TimeService = {
	config: {},
	rules: {},
    timer: null,
    reloadConfig: function() {
		this.config = conf.readSettings('eventServices:timeService');
		if( !this.config ) {
			log.msg("TimeService.reloadConfig: NO CONFIG");
			this.config = {
				type: 'time',
				service: 'timeService',
				enabled: true,
			};
			conf.saveSettings('eventServices:timeService', this.config);
		}
		// var allrules = conf.readSettings('eventRules') || [];
        var allrules = conf.readSettings('eventRules') || [];
		this.rules = allrules.filter( function(r){return r.type === 'time';} );
    },
    start: function() {
        this.reloadConfig();
        this.timer = setInterval(this.checkTime.bind(this), 1000 * intervalSecs);
    },
    stop: function() {
        clearInterval(this.timer);
    },
    // called every interval
    checkTime: function() {
        var now = new Date();
        this.rules.map( function(rule) {
            // log.msg("TimeService.checkTime: rule=",rule);
            if( rule.alarmType === 'periodic' ) {
                var rHour = parseInt(rule.alarmHours);
                var rMin = parseInt( rule.alarmMinutes ) || 0;
                var rSec = parseInt( rule.alarmSeconds ) || 0;
                // log.msg("TimeService.checkTime: periodic:", rHour,rMin,rSec,
                //     " - ", now.getHours(),now.getMinutes(),now.getSeconds());
                if( now.getHours() === rHour &&
                    now.getMinutes() === rMin &&
                    now.getSeconds() === rSec ) {
                    log.addEvent( {type:'trigger', source:'time', id:rule.name, text:'' });
                    PatternsService.playPatternFrom( rule.name, rule.patternId, rule.blink1Id );
                }
            }
            else if( rule.alarmType === 'exact' ) {
                // log.msg("TimeService.checkTime: exact: no logic yet for this case");
            }
        });
    }
};



module.exports = TimeService;

@@ -1004,16 +1004,61 @@
 ion() %7B%0A
+        log.msg(%22TimeService.reloadConfig%22);%0A
 %09%09this.c
@@ -1878,24 +1878,68 @@
 ion(rule) %7B%0A
+            if( !rule.enabled ) %7B return; %7D%0A
             
@@ -2003,50 +2003,47 @@
     
-if( rule.alarmType === 'periodic' ) %7B%0A    
+var rHour = parseInt(rule.alarmHours);%0A
     
@@ -2047,36 +2047,35 @@
            var r
-Hour
+Min
  = parseInt(rule
@@ -2062,32 +2062,33 @@
 rMin = parseInt(
+ 
 rule.alarmHours)
@@ -2077,36 +2077,40 @@
 ( rule.alarm
-Hours);%0A    
+Minutes ) %7C%7C 0;%0A
             
@@ -2106,35 +2106,35 @@
            var r
-Min
+Sec
  = parseInt( rul
@@ -2132,38 +2132,38 @@
 eInt( rule.alarm
-Minute
+Second
 s ) %7C%7C 0;%0A      
@@ -2172,58 +2172,415 @@
     
-    var rSec = parseInt( rule.alarmSeconds ) %7C%7C 0;
+if( rule.alarmType === 'hourly' ) %7B%0A                if( now.getMinutes() === rMin &&%0A                    now.getSeconds() === rSec ) %7B%0A                    log.addEvent( %7Btype:'trigger', source:'time', id:rule.name, text:'' %7D);%0A                    PatternsService.playPatternFrom( rule.name, rule.patternId, rule.blink1Id );%0A                %7D%0A            %7D%0A            else if( rule.alarmType === 'daily' ) %7B
 %0A   
@@ -3065,32 +3065,32 @@
 ule.blink1Id );%0A
+
                 
@@ -3109,162 +3109,8 @@
   %7D%0A
-            else if( rule.alarmType === 'exact' ) %7B%0A                // log.msg(%22TimeService.checkTime: exact: no logic yet for this case%22);%0A            %7D%0A
     

fix timeservice logic for hourly and daily alarmTypes