import store from './store';
import {updateTriggers, scheduledSwitchRelay, manualSwitchRelay} from './actions';
import forEach from 'lodash/collection/forEach';
import values from 'lodash/object/values';
import firebase from './firebase';

const timesheetsRef = firebase.child('timesheets');
const relaysRef = firebase.child('relays');

timesheetsRef.on('value', snapshot => {
    const timesheets = snapshot.val();
    forEach(timesheets, (timesheetsObject, relayId) => {
        store.dispatch(updateTriggers(values(timesheetsObject), relayId));
    });
});

relaysRef.on('value', snapshot => {
    const relays = snapshot.val();
    forEach(relays, ({manual, switched}, relayId) => {
        if (manual) store.dispatch(manualSwitchRelay(relayId, switched));
    });
});

console.log('Server has started :)');

@@ -554,16 +554,39 @@
 );%0A%7D);%0A%0A
+setTimeout(() =%3E %7B%0A    
 relaysRe
@@ -613,24 +613,28 @@
 ot =%3E %7B%0A    
+ 
+   
 const relays
@@ -648,32 +648,36 @@
 shot.val();%0A    
+   
+ 
 forEach(relays, 
@@ -703,32 +703,36 @@
 , relayId) =%3E %7B%0A
+    
         if (manu
@@ -785,32 +785,44 @@
 switched));%0A
+        %7D);%0A
     %7D);%0A
 %7D);%0A%0Aconsole
@@ -805,25 +805,31 @@
 %7D);%0A    %7D);%0A
-
 %7D
+, 5000
 );%0A%0Aconsole.

Set timeout on relays