// Use Form key for listening (Dispatch / View data tab) var firebaseChannel = null; var firebasePath = ''; var firebasePathCurrent = ''; var firebaseCallBackCount = 0; var firebaseDB = null; // User Customer key for listening (Watchdog / Dispatch+) var firebaseChannel2 = null; var firebaseCallBackCount2 = 0; function initFirebase(token, _path) { // This is our Firebase realtime DB path that we'll listen to for updates // We'll initialize this later in openChannel() /** * This method is called every time an event is fired from Firebase */ function onMessage(newState) { // Init current database for Webclient if(authNS.currentUser.MobileKeyNew && firebaseDB == undefined){ firebaseDB = newState.val(); } // Auto-refresh for current client if(firebaseCallBackCount > 0){ try{ var updateCredit = false; // ========== Webclient if(authNS.currentUser.MobileKeyNew){ // Check data changed for new database var keyChanged = ''; var newDB = newState.val(); for (var _elt in newDB) { if(newDB[_elt]['Updated'] != firebaseDB[_elt]['Updated']){ keyChanged = _elt; break; } } // Re-update current database with new one firebaseDB = newDB; // Refresh Webclient if changed if(keyChanged != ''){ switch(keyChanged){ case 'Watchdog': var msgWarn = lbl_user_logout_message.replace('user ID and password', 'Mobile Number and PIN'); jAlert(msgWarn, lbl_alert, function(){ parent.window.location.replace("/formview/logout?delSession=false"); }); break; case 'UpdateFVDBInfoForJobs_finished': //waiting for data and , isRunOnce updateFVDBInfoForJobsNoWait(true, true); break; case 'UpdateFVDBInfoForIncompleted': hasNewIncompletedRecords = true; if ($('.fvWorkspaceScrollArea .mainMenuBarScroll .mb_mmb_review_icon').hasClass('accountMMBItemSelected')) { updateFVDBInfoForIncomple(true); } else { updateReviewCount(); } break; case 'UpdateFVDBInfoForCompleted': hasNewCompletedRecords = true; if ($('.fvWorkspaceScrollArea .mainMenuBarScroll .mb_mmb_review_icon').hasClass('accountMMBItemSelected')) { updateReviewSubTabsCount(WEB_CLIENT_REVIEW_COMPLETED_TAB, CALCULATION_INCREASE); updateFVDBInfoForComplete(false); } else { updateReviewCount(); } break; case 'UpdateFVDBInfoForSent': hasNewSentRecords = true; if ($('.fvWorkspaceScrollArea .mainMenuBarScroll .mb_mmb_review_icon').hasClass('accountMMBItemSelected')) { updateReviewSubTabsCount(WEB_CLIENT_REVIEW_SENT_TAB, CALCULATION_INCREASE); updateReviewCount(); updateFVDBInfoForSent(false); } else { updateReviewCount(); } break; case 'UpdateFVDBInfo_finished': receiveUpdateNotice = true; updateFVDBInfo(true); break; case CONS_ACTION_RECALL: updateFVDBDispatchRecall(); break; default: if(fvDBInfoNS.settings.behaviorUseGPNNotity){ //waiting for data and , isRunOnce updateFVDBInfoForJobsNoWait(false, true); } } } } // ========== Dispatch+ else if('.dispatchPlusLink' === currentTabName){ updateCredit = true; refreshCurrentViewScheduler4DP(); } // ========== Toggle auto-refresh else if(!$('#toggleAutoRefreshChk').prop('checked')){ // do nothing } // ========== View list (View pro) else if(isViewDataPro()){ updateCredit = true; $('.main_content .btn_new_view_data').trigger('click', ['refresh']); } // ========== View list (View old) else { updateCredit = true; submitData(formId, CONST_VALUE_FALSE_STR, '', 'current', ''); } // Update current credit if(updateCredit){ decreateHeaderCredit2(); //$('#header span.webappCredit').text(viewProData.WebAppCreditsPurchase) } } catch(e){} } firebaseCallBackCount++; } /** * This method is called every time an event is fired from Firebase for Watchdog */ function onMessage2(newState) { if(firebaseCallBackCount2 > 0){ try{ var isProjectUpdate = false; var isMobileUpdate = false; var newStateVal = newState.val(); if(newStateVal.project) { var userUpdate = new Date(newStateVal.Updated); var userProjectUpdate = new Date(newStateVal.project.Updated); if(userProjectUpdate.getTime() > userUpdate.getTime()) { isProjectUpdate = true; } } if(newStateVal.mobileUnit) { var userUpdate = new Date(newStateVal.Updated); var userMobileUpdate = new Date(newStateVal.mobileUnit.Updated); if(userMobileUpdate.getTime() > userUpdate.getTime()) { isMobileUpdate = true; } } if(isProjectUpdate || isMobileUpdate) { //do no thing } else { // Watchdog logout URL as default var logoutURL = '/user/logout?delSession=false'; if((typeof authNS == 'undefined') && homepage.currentUser.Key){ logoutURL = '/rms/logout?delSession=false'; // Reseller logout } jAlert(lbl_user_logout_message, lbl_alert, function(){ window.location.href = logoutURL; }); } } catch(e){ } } firebaseCallBackCount2++; } /** * This function opens a realtime communication channel with Firebase * It logs in securely using the client token passed from the server * then it sets up a listener on the proper database path (also passed by server) * finally, it calls onOpened() to let the server know it is ready to receive messages */ function openChannel() { if(firebasePath === '' || firebasePath === firebasePathCurrent){ // path listened return; } // sign into Firebase with the token passed from the server firebase.auth().signInWithCustomToken(token).catch(function(error) { console.log('Login Failed!', error.code); console.log('Error message: ', error.message); //jAlert('' + error.message) }); // save current path firebasePathCurrent = firebasePath; // setup a database reference //firebaseChannel = firebase.database().ref('channels/' + userKey); firebaseChannel = firebase.database().ref(firebasePath); // add a listener to the path that fires any time the value of the data changes firebaseChannel.on('value', function(data) { onMessage(data); }); } function openWatchDogChannel(_path) { // sign into Firebase with the token passed from the server firebase.auth().signInWithCustomToken(token).catch(function(error) { console.log('Login Failed!', error.code); console.log('Error message: ', error.message); //jAlert('' + error.message) }); // setup a database reference //firebaseChannel = firebase.database().ref('channels/' + userKey); firebaseChannel2 = firebase.database().ref(_path); // add a listener to the path that fires any time the value of the data changes firebaseChannel2.on('value', function(data) { onMessage2(data); }); } /** * This function opens a communication channel with the server * then it adds listeners to all the squares on the board * next it pulls down the initial game state from template values * finally it updates the game state with those values by calling onMessage() */ function initialize(_path) { if(_path !== ''){ openWatchDogChannel(_path); } else{ openChannel(); } } setTimeout(initialize(_path), 100); } function offFirebase(){ try{ // CO-877: Detach listening if(firebasePath !== '' && firebaseChannel){ firebaseChannel.off(); // reset path firebasePath = ''; firebasePathCurrent = ''; firebaseCallBackCount = 0; } } catch(e){} }