Skip to content

Commit e3837ac

Browse files
committed
improved my-view refresh event + reload new timers from server
1 parent 762eec7 commit e3837ac

File tree

2 files changed

+66
-31
lines changed

2 files changed

+66
-31
lines changed

www/scripts/sepiaFW.ui.events.js

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function sepiaFW_build_events(){
106106

107107
Events.loadContextualEvents = function(forceNew) {
108108
if (!SepiaFW.assistant.id && !SepiaFW.client.isDemoMode()){
109-
SepiaFW.debug.err("Events: tried to get events before channel-join completed!");
109+
SepiaFW.debug.err("Events: tried to get contextual events before channel-join completed!");
110110
return;
111111
}
112112
//prevent multiple consecutive calls in short intervall...
@@ -225,31 +225,46 @@ function sepiaFW_build_events(){
225225
var scheduleDelay = 5000; //to prevent multiple syncs in a short time-period the current sync is delayed and gets shifted with each new sync request
226226
var SyncSchedulers = {};
227227

228+
var lastTimeEventsCheck = 0;
229+
228230
//----------------- Broadcasting -------------------
229231

230232
//see at top of class ...
231233

232234
//--------------------------------------------------
233235

234236
//SETUP TimeEvents
235-
Events.setupTimeEvents = function(){
236-
//reset ids tracking timeEvents
237-
resetTimeEventNotificationIds();
238-
//clear all background notifications
239-
Events.clearAllTimeEventBackgroundNotifications(function(){
240-
//reload Timers
241-
var options = {};
242-
options.loadOnlyData = true;
243-
options.updateMyViewTimers = true; //update my-view (but only timers) afterwards
244-
var dataset = {}; dataset.info = "direct_cmd";
245-
dataset.cmd = "timer;;action=<show>;;alarm_type=<timer>;;"; //TODO: make a function for that
246-
dataset.newReceiver = SepiaFW.assistant.id;
247-
SepiaFW.client.sendCommand(dataset, options);
248-
var dataset2 = {}; dataset2.info = "direct_cmd";
249-
dataset2.cmd = "timer;;action=<show>;;alarm_type=<alarmClock>;;"; //TODO: make a function for that
250-
dataset2.newReceiver = SepiaFW.assistant.id;
251-
SepiaFW.client.sendCommand(dataset2, options);
252-
});
237+
Events.setupTimeEvents = function(forceNew){
238+
if (!SepiaFW.assistant.id && !SepiaFW.client.isDemoMode()){
239+
SepiaFW.debug.err("Events: tried to get time-events before channel-join completed!");
240+
return;
241+
}
242+
//prevent multiple consecutive calls in short intervall...
243+
var now = new Date().getTime();
244+
if (forceNew || (now - lastTimeEventsCheck) > 10*1000){ //interval: 10s
245+
lastTimeEventsCheck = now;
246+
247+
//reset ids tracking timeEvents - TODO: what does this do again?
248+
resetTimeEventNotificationIds();
249+
250+
//TODO: the next actions will not remove old timer cards only add new ones and disable old background notifications
251+
252+
//clear all background notifications
253+
Events.clearAllTimeEventBackgroundNotifications(function(){
254+
//reload Timers
255+
var options = {};
256+
options.loadOnlyData = true;
257+
options.updateMyViewTimers = true; //update my-view (but only timers) afterwards
258+
var dataset = {}; dataset.info = "direct_cmd";
259+
dataset.cmd = "timer;;action=<show>;;alarm_type=<timer>;;"; //TODO: make a function for that
260+
dataset.newReceiver = SepiaFW.assistant.id;
261+
SepiaFW.client.sendCommand(dataset, options);
262+
var dataset2 = {}; dataset2.info = "direct_cmd";
263+
dataset2.cmd = "timer;;action=<show>;;alarm_type=<alarmClock>;;"; //TODO: make a function for that
264+
dataset2.newReceiver = SepiaFW.assistant.id;
265+
SepiaFW.client.sendCommand(dataset2, options);
266+
});
267+
}
253268
}
254269

255270
//add timeEvent to UI and activate it

www/scripts/sepiaFW.ui.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -594,24 +594,32 @@ function sepiaFW_build_ui(){
594594
}
595595

596596
//Update myView
597-
var myViewUpdateInterval = 20*60*1000; //<- automatic updates will not be done more than once within this interval
597+
var myViewUpdateInterval = 15*60*1000; //<- automatic updates will not be done more than once within this interval
598598
var lastMyViewUpdate = 0;
599+
var myViewPostponedUpdateTries = 0;
599600
var myViewUpdateTimer;
600601
var contextEventsLoadDelayTimer = undefined;
602+
var timeEventsLoadDelayTimer = undefined;
601603
UI.updateMyView = function(forceUpdate, checkGeolocationFirst, updateSource){
602604
//console.log('My-view update source: ' + updateSource); //DEBUG
603605

604606
//is client active or demo-mode?
605607
if ((!SepiaFW.client.isActive() || !SepiaFW.assistant.id) && !SepiaFW.client.isDemoMode()){
606608
clearTimeout(myViewUpdateTimer);
609+
myViewPostponedUpdateTries++;
607610
myViewUpdateTimer = setTimeout(function(){
608611
//try again
609-
UI.updateMyView(true, checkGeolocationFirst, 'notActiveRetry');
610-
}, 10000);
611-
SepiaFW.debug.err("Events: tried to update my-view before client is active! Will try again in 10s.");
612+
if (myViewPostponedUpdateTries <= 3){
613+
UI.updateMyView(true, checkGeolocationFirst, 'notActiveRetry');
614+
}else{
615+
myViewPostponedUpdateTries = 0;
616+
}
617+
}, 8000);
618+
SepiaFW.debug.err("Events: tried to update my-view before client is active! Will try again in 8s.");
612619
return;
613620
}
614621
//console.log('passed');
622+
myViewPostponedUpdateTries = 0;
615623

616624
//with GPS first
617625
if (checkGeolocationFirst){
@@ -621,10 +629,10 @@ function sepiaFW_build_ui(){
621629
//console.log('---------------GET BEST LOCATION--------------'); //DEBUG
622630
SepiaFW.geocoder.getBestLocation();
623631
}else{
624-
UI.updateMyView(false, false, 'geoCoderBlockedUpdate'); //TODO: should we use 'forceUpdate' variable instead of false?
632+
UI.updateMyView(forceUpdate, false, 'geoCoderBlockedUpdate'); //TODO: should we use 'forceUpdate' variable instead of false?
625633
}
626634
}else{
627-
UI.updateMyView(false, false, 'geoCoderSkippedUpdate'); //TODO: should we use 'forceUpdate' variable instead of false?
635+
UI.updateMyView(forceUpdate, false, 'geoCoderSkippedUpdate'); //TODO: should we use 'forceUpdate' variable instead of false?
628636
}
629637

630638
//without GPS
@@ -641,8 +649,8 @@ function sepiaFW_build_ui(){
641649
//contextual events update
642650
UI.updateMyContextualEvents(forceUpdate);
643651

644-
//check for near timeEvents (within 18h (before) and 120h (past))
645-
UI.updateMyTimers(now + 18*60*60*1000);
652+
//reload timers and check for near timeEvents
653+
UI.updateMyTimeEvents(forceUpdate);
646654

647655
//trigger my custom buttons refresh (checks internally if buttons have changed)
648656
if (SepiaFW.ui.customButtons){
@@ -651,12 +659,13 @@ function sepiaFW_build_ui(){
651659
}
652660
}
653661
}
662+
//Update the timers shown on my-view (no database reload)
654663
UI.updateMyTimers = function(maximumPreviewTargetTime){
655-
var maxTargetTime = maximumPreviewTargetTime || (new Date().getTime() + 18*60*60*1000);
664+
var maxTargetTime = maximumPreviewTargetTime || (new Date().getTime() + 18*60*60*1000); //within 18h (before) and 120h (past)
656665
var includePastMs = 120*60*60*1000;
657666
var nextTimers = SepiaFW.events.getNextTimeEvents(maxTargetTime, '', includePastMs);
658667
var myView = document.getElementById('sepiaFW-my-view'); //TODO: don't we have a method for this or a permanent variable?
659-
//TODO: clean-up my-view of old timers first
668+
//TODO: smart clean-up of old timers - This has to be done before when we get the new list
660669
$.each(nextTimers, function(index, Timer){
661670
//check if alarm is present in myView
662671
var timerPresentInMyView = $(myView).find('[data-id="' + Timer.data.eventId + '"]'); //TODO: we don't need this if we clean first
@@ -669,6 +678,15 @@ function sepiaFW_build_ui(){
669678
}
670679
});
671680
}
681+
682+
//Reload from database and show events
683+
UI.updateMyTimeEvents = function(forceUpdate){
684+
if (timeEventsLoadDelayTimer) clearTimeout(timeEventsLoadDelayTimer);
685+
timeEventsLoadDelayTimer = setTimeout(function(){
686+
//console.log('---------------GET TIME EVENTS--------------'); //DEBUG
687+
SepiaFW.events.setupTimeEvents(forceUpdate); //just in case we want to use GPS some day (see below)
688+
}, 1000);
689+
}
672690
UI.updateMyContextualEvents = function(forceUpdate){
673691
if (contextEventsLoadDelayTimer) clearTimeout(contextEventsLoadDelayTimer);
674692
contextEventsLoadDelayTimer = setTimeout(function(){
@@ -742,13 +760,13 @@ function sepiaFW_build_ui(){
742760
SepiaFW.debug.info('UI.listenToVisibilityChange: ' + document[visibility]);
743761

744762
//became visible
745-
if (SepiaFW.client.isActive()){
763+
//if (SepiaFW.client.isActive()){
746764
if (!isFirstVisibilityChange && (UI.isVisible() || forceTriggerVisible)){
747765
//update myView (is automatically skipped if called too early)
748766
UI.updateMyView(false, true, 'visibilityChange');
749767
}
750768
isFirstVisibilityChange = false;
751-
}
769+
//}
752770
}
753771
//broadcaster for myView show
754772
var isFirstMyViewShow = true;
@@ -772,6 +790,8 @@ function sepiaFW_build_ui(){
772790
if (!SepiaFW.client.allowBackgroundConnection){
773791
//reconnect
774792
SepiaFW.client.resumeClient();
793+
//GPS
794+
775795
}
776796
}, 100);
777797
}

0 commit comments

Comments
 (0)