UI: Fix server time diff calculation.
This commit is contained in:
		
							parent
							
								
									d7e9da6321
								
							
						
					
					
						commit
						abc16e6bb6
					
				@ -17,7 +17,14 @@ export default angular.module('thingsboard.api.dashboard', [])
 | 
			
		||||
    .factory('dashboardService', DashboardService).name;
 | 
			
		||||
 | 
			
		||||
/*@ngInject*/
 | 
			
		||||
function DashboardService($http, $q, $location, customerService) {
 | 
			
		||||
function DashboardService($rootScope, $http, $q, $location, customerService) {
 | 
			
		||||
 | 
			
		||||
    var stDiffPromise;
 | 
			
		||||
 | 
			
		||||
    $rootScope.dadshboardServiceStateChangeStartHandle = $rootScope.$on('$stateChangeStart', function () {
 | 
			
		||||
        stDiffPromise = undefined;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    var service = {
 | 
			
		||||
        assignDashboardToCustomer: assignDashboardToCustomer,
 | 
			
		||||
@ -113,18 +120,23 @@ function DashboardService($http, $q, $location, customerService) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function getServerTimeDiff() {
 | 
			
		||||
        var deferred = $q.defer();
 | 
			
		||||
        var url = '/api/dashboard/serverTime';
 | 
			
		||||
        var ct1 = Date.now();
 | 
			
		||||
        $http.get(url, { ignoreLoading: true }).then(function success(response) {
 | 
			
		||||
            var ct2 = Date.now();
 | 
			
		||||
            var st = response.data;
 | 
			
		||||
            var stDiff = Math.ceil(st - (ct1+ct2)/2);
 | 
			
		||||
            deferred.resolve(stDiff);
 | 
			
		||||
        }, function fail() {
 | 
			
		||||
            deferred.reject();
 | 
			
		||||
        });
 | 
			
		||||
        return deferred.promise;
 | 
			
		||||
        if (stDiffPromise) {
 | 
			
		||||
            return stDiffPromise;
 | 
			
		||||
        } else {
 | 
			
		||||
            var deferred = $q.defer();
 | 
			
		||||
            stDiffPromise = deferred.promise;
 | 
			
		||||
            var url = '/api/dashboard/serverTime';
 | 
			
		||||
            var ct1 = Date.now();
 | 
			
		||||
            $http.get(url, {ignoreLoading: true}).then(function success(response) {
 | 
			
		||||
                var ct2 = Date.now();
 | 
			
		||||
                var st = response.data;
 | 
			
		||||
                var stDiff = Math.ceil(st - (ct1 + ct2) / 2);
 | 
			
		||||
                deferred.resolve(stDiff);
 | 
			
		||||
            }, function fail() {
 | 
			
		||||
                deferred.reject();
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        return stDiffPromise;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function getDashboard(dashboardId) {
 | 
			
		||||
 | 
			
		||||
@ -80,9 +80,7 @@ export default class Subscription {
 | 
			
		||||
            this.alarms = [];
 | 
			
		||||
 | 
			
		||||
            this.originalTimewindow = null;
 | 
			
		||||
            this.timeWindow = {
 | 
			
		||||
                stDiff: this.ctx.stDiff
 | 
			
		||||
            }
 | 
			
		||||
            this.timeWindow = {};
 | 
			
		||||
            this.useDashboardTimewindow = options.useDashboardTimewindow;
 | 
			
		||||
 | 
			
		||||
            if (this.useDashboardTimewindow) {
 | 
			
		||||
@ -124,9 +122,7 @@ export default class Subscription {
 | 
			
		||||
            this.data = [];
 | 
			
		||||
            this.hiddenData = [];
 | 
			
		||||
            this.originalTimewindow = null;
 | 
			
		||||
            this.timeWindow = {
 | 
			
		||||
                stDiff: this.ctx.stDiff
 | 
			
		||||
            }
 | 
			
		||||
            this.timeWindow = {};
 | 
			
		||||
            this.useDashboardTimewindow = options.useDashboardTimewindow;
 | 
			
		||||
            this.stateData = options.stateData;
 | 
			
		||||
            if (this.useDashboardTimewindow) {
 | 
			
		||||
@ -213,27 +209,51 @@ export default class Subscription {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    initAlarmSubscription() {
 | 
			
		||||
    loadStDiff() {
 | 
			
		||||
        var deferred = this.ctx.$q.defer();
 | 
			
		||||
        if (!this.ctx.aliasController) {
 | 
			
		||||
            this.configureAlarmsData();
 | 
			
		||||
            deferred.resolve();
 | 
			
		||||
        } else {
 | 
			
		||||
            var subscription = this;
 | 
			
		||||
            this.ctx.aliasController.resolveAlarmSource(this.alarmSource).then(
 | 
			
		||||
                function success(alarmSource) {
 | 
			
		||||
                    subscription.alarmSource = alarmSource;
 | 
			
		||||
                    subscription.configureAlarmsData();
 | 
			
		||||
        if (this.ctx.getStDiff && this.timeWindow) {
 | 
			
		||||
            this.ctx.getStDiff().then(
 | 
			
		||||
                (stDiff) => {
 | 
			
		||||
                    this.timeWindow.stDiff = stDiff;
 | 
			
		||||
                    deferred.resolve();
 | 
			
		||||
                },
 | 
			
		||||
                function fail() {
 | 
			
		||||
                    deferred.reject();
 | 
			
		||||
                () => {
 | 
			
		||||
                    this.timeWindow.stDiff = 0;
 | 
			
		||||
                    deferred.resolve();
 | 
			
		||||
                }
 | 
			
		||||
            );
 | 
			
		||||
        } else {
 | 
			
		||||
            if (this.timeWindow) {
 | 
			
		||||
                this.timeWindow.stDiff = 0;
 | 
			
		||||
            }
 | 
			
		||||
            deferred.resolve();
 | 
			
		||||
        }
 | 
			
		||||
        return deferred.promise;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    initAlarmSubscription() {
 | 
			
		||||
        var deferred = this.ctx.$q.defer();
 | 
			
		||||
        var subscription = this;
 | 
			
		||||
        this.loadStDiff().then(() => {
 | 
			
		||||
            if (!subscription.ctx.aliasController) {
 | 
			
		||||
                subscription.configureAlarmsData();
 | 
			
		||||
                deferred.resolve();
 | 
			
		||||
            } else {
 | 
			
		||||
                subscription.ctx.aliasController.resolveAlarmSource(subscription.alarmSource).then(
 | 
			
		||||
                    function success(alarmSource) {
 | 
			
		||||
                        subscription.alarmSource = alarmSource;
 | 
			
		||||
                        subscription.configureAlarmsData();
 | 
			
		||||
                        deferred.resolve();
 | 
			
		||||
                    },
 | 
			
		||||
                    function fail() {
 | 
			
		||||
                        deferred.reject();
 | 
			
		||||
                    }
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        return deferred.promise;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    configureAlarmsData() {
 | 
			
		||||
        var subscription = this;
 | 
			
		||||
        var registration;
 | 
			
		||||
@ -252,22 +272,24 @@ export default class Subscription {
 | 
			
		||||
 | 
			
		||||
    initDataSubscription() {
 | 
			
		||||
        var deferred = this.ctx.$q.defer();
 | 
			
		||||
        if (!this.ctx.aliasController) {
 | 
			
		||||
            this.configureData();
 | 
			
		||||
            deferred.resolve();
 | 
			
		||||
        } else {
 | 
			
		||||
            var subscription = this;
 | 
			
		||||
            this.ctx.aliasController.resolveDatasources(this.datasources).then(
 | 
			
		||||
                function success(datasources) {
 | 
			
		||||
                    subscription.datasources = datasources;
 | 
			
		||||
                    subscription.configureData();
 | 
			
		||||
                    deferred.resolve();
 | 
			
		||||
                },
 | 
			
		||||
                function fail() {
 | 
			
		||||
                    deferred.reject();
 | 
			
		||||
                }
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
        var subscription = this;
 | 
			
		||||
        this.loadStDiff().then(() => {
 | 
			
		||||
            if (!subscription.ctx.aliasController) {
 | 
			
		||||
                subscription.configureData();
 | 
			
		||||
                deferred.resolve();
 | 
			
		||||
            } else {
 | 
			
		||||
                subscription.ctx.aliasController.resolveDatasources(subscription.datasources).then(
 | 
			
		||||
                    function success(datasources) {
 | 
			
		||||
                        subscription.datasources = datasources;
 | 
			
		||||
                        subscription.configureData();
 | 
			
		||||
                        deferred.resolve();
 | 
			
		||||
                    },
 | 
			
		||||
                    function fail() {
 | 
			
		||||
                        deferred.reject();
 | 
			
		||||
                    }
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        return deferred.promise;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -75,7 +75,6 @@ function Dashboard() {
 | 
			
		||||
            prepareDashboardContextMenu: '&?',
 | 
			
		||||
            prepareWidgetContextMenu: '&?',
 | 
			
		||||
            loadWidgets: '&?',
 | 
			
		||||
            getStDiff: '&?',
 | 
			
		||||
            onInit: '&?',
 | 
			
		||||
            onInitFailed: '&?',
 | 
			
		||||
            dashboardStyle: '=?',
 | 
			
		||||
@ -102,8 +101,6 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
 | 
			
		||||
 | 
			
		||||
    vm.gridster = null;
 | 
			
		||||
 | 
			
		||||
    vm.stDiff = 0;
 | 
			
		||||
 | 
			
		||||
    vm.isMobileDisabled = angular.isDefined(vm.isMobileDisabled) ? vm.isMobileDisabled : false;
 | 
			
		||||
 | 
			
		||||
    vm.isMobileSize = false;
 | 
			
		||||
@ -500,7 +497,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
 | 
			
		||||
        sortWidgets();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    loadStDiff();
 | 
			
		||||
    loadDashboard();
 | 
			
		||||
 | 
			
		||||
    function sortWidgets() {
 | 
			
		||||
        vm.widgets.sort(function (widget1, widget2) {
 | 
			
		||||
@ -515,28 +512,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function reload() {
 | 
			
		||||
        loadStDiff();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function loadStDiff() {
 | 
			
		||||
        if (vm.getStDiff) {
 | 
			
		||||
            var promise = vm.getStDiff();
 | 
			
		||||
            if (promise) {
 | 
			
		||||
                promise.then(function (stDiff) {
 | 
			
		||||
                    vm.stDiff = stDiff;
 | 
			
		||||
                    loadDashboard();
 | 
			
		||||
                }, function () {
 | 
			
		||||
                    vm.stDiff = 0;
 | 
			
		||||
                    loadDashboard();
 | 
			
		||||
                });
 | 
			
		||||
            } else {
 | 
			
		||||
                vm.stDiff = 0;
 | 
			
		||||
                loadDashboard();
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            vm.stDiff = 0;
 | 
			
		||||
            loadDashboard();
 | 
			
		||||
        }
 | 
			
		||||
        loadDashboard();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function loadDashboard() {
 | 
			
		||||
 | 
			
		||||
@ -113,7 +113,6 @@
 | 
			
		||||
										 stateController: vm.stateController,
 | 
			
		||||
										 isEdit: vm.isEdit,
 | 
			
		||||
										 isMobile: vm.isMobileSize,
 | 
			
		||||
										 stDiff: vm.stDiff,
 | 
			
		||||
										 dashboardTimewindow: vm.dashboardTimewindow,
 | 
			
		||||
										 dashboardTimewindowApi: vm.dashboardTimewindowApi }">
 | 
			
		||||
									</div>
 | 
			
		||||
 | 
			
		||||
@ -21,7 +21,7 @@ import Subscription from '../../api/subscription';
 | 
			
		||||
 | 
			
		||||
/*@ngInject*/
 | 
			
		||||
export default function WidgetController($scope, $state, $timeout, $window, $element, $q, $log, $injector, $filter, $compile, tbRaf, types, utils, timeService,
 | 
			
		||||
                                         datasourceService, alarmService, entityService, deviceService, visibleRect, isEdit, isMobile, stDiff, dashboardTimewindow,
 | 
			
		||||
                                         datasourceService, alarmService, entityService, dashboardService, deviceService, visibleRect, isEdit, isMobile, dashboardTimewindow,
 | 
			
		||||
                                         dashboardTimewindowApi, widget, aliasController, stateController, widgetInfo, widgetType) {
 | 
			
		||||
 | 
			
		||||
    var vm = this;
 | 
			
		||||
@ -159,7 +159,7 @@ export default function WidgetController($scope, $state, $timeout, $window, $ele
 | 
			
		||||
        widgetUtils: widgetContext.utils,
 | 
			
		||||
        dashboardTimewindowApi: dashboardTimewindowApi,
 | 
			
		||||
        types: types,
 | 
			
		||||
        stDiff: stDiff,
 | 
			
		||||
        getStDiff: dashboardService.getServerTimeDiff,
 | 
			
		||||
        aliasController: aliasController
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -170,7 +170,6 @@ export default function DashboardController(types, utils, dashboardUtils, widget
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    vm.getServerTimeDiff = getServerTimeDiff;
 | 
			
		||||
    vm.addWidget = addWidget;
 | 
			
		||||
    vm.addWidgetFromType = addWidgetFromType;
 | 
			
		||||
    vm.exportDashboard = exportDashboard;
 | 
			
		||||
@ -334,10 +333,6 @@ export default function DashboardController(types, utils, dashboardUtils, widget
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function getServerTimeDiff() {
 | 
			
		||||
        return dashboardService.getServerTimeDiff();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function loadDashboard() {
 | 
			
		||||
        if (vm.widgetEditMode) {
 | 
			
		||||
            var widget = {
 | 
			
		||||
 | 
			
		||||
@ -137,8 +137,7 @@
 | 
			
		||||
                                     dashboard-ctx="vm.dashboardCtx"
 | 
			
		||||
                                     is-edit="vm.isEdit"
 | 
			
		||||
                                     is-mobile="vm.forceDashboardMobileMode"
 | 
			
		||||
                                     widget-edit-mode="vm.widgetEditMode"
 | 
			
		||||
                                     get-st-diff="vm.getServerTimeDiff()">
 | 
			
		||||
                                     widget-edit-mode="vm.widgetEditMode">
 | 
			
		||||
                </tb-dashboard-layout>
 | 
			
		||||
            </div>
 | 
			
		||||
            <md-sidenav ng-if="vm.layouts.right.show"
 | 
			
		||||
@ -157,8 +156,7 @@
 | 
			
		||||
                                     dashboard-ctx="vm.dashboardCtx"
 | 
			
		||||
                                     is-edit="vm.isEdit"
 | 
			
		||||
                                     is-mobile="vm.forceDashboardMobileMode"
 | 
			
		||||
                                     widget-edit-mode="vm.widgetEditMode"
 | 
			
		||||
                                     get-st-diff="vm.getServerTimeDiff()">
 | 
			
		||||
                                     widget-edit-mode="vm.widgetEditMode">
 | 
			
		||||
                </tb-dashboard-layout>
 | 
			
		||||
            </md-sidenav>
 | 
			
		||||
        </div>
 | 
			
		||||
 | 
			
		||||
@ -29,8 +29,7 @@ export default function DashboardLayout() {
 | 
			
		||||
            dashboardCtx: '=',
 | 
			
		||||
            isEdit: '=',
 | 
			
		||||
            isMobile: '=',
 | 
			
		||||
            widgetEditMode: '=',
 | 
			
		||||
            getStDiff: '&?'
 | 
			
		||||
            widgetEditMode: '='
 | 
			
		||||
        },
 | 
			
		||||
        controller: DashboardLayoutController,
 | 
			
		||||
        controllerAs: 'vm',
 | 
			
		||||
 | 
			
		||||
@ -64,7 +64,6 @@
 | 
			
		||||
            prepare-dashboard-context-menu="vm.prepareDashboardContextMenu()"
 | 
			
		||||
            prepare-widget-context-menu="vm.prepareWidgetContextMenu(widget)"
 | 
			
		||||
            on-remove-widget="vm.removeWidget(event, widget)"
 | 
			
		||||
            get-st-diff="vm.getStDiff()"
 | 
			
		||||
            on-init="vm.dashboardInited(dashboard)"
 | 
			
		||||
            on-init-failed="vm.dashboardInitFailed(e)"
 | 
			
		||||
            ignore-loading="vm.layoutCtx.ignoreLoading">
 | 
			
		||||
 | 
			
		||||
@ -31,7 +31,7 @@ import AliasController from '../../api/alias-controller';
 | 
			
		||||
/*@ngInject*/
 | 
			
		||||
export default function AttributeTableDirective($compile, $templateCache, $rootScope, $q, $mdEditDialog, $mdDialog,
 | 
			
		||||
                                                $mdUtil, $document, $translate, $filter, utils, types, dashboardUtils,
 | 
			
		||||
                                                dashboardService, entityService, attributeService, widgetService) {
 | 
			
		||||
                                                entityService, attributeService, widgetService) {
 | 
			
		||||
 | 
			
		||||
    var linker = function (scope, element, attrs) {
 | 
			
		||||
 | 
			
		||||
@ -407,10 +407,6 @@ export default function AttributeTableDirective($compile, $templateCache, $rootS
 | 
			
		||||
            scope.getEntityAttributes(true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        scope.getServerTimeDiff = function() {
 | 
			
		||||
            return dashboardService.getServerTimeDiff();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        scope.addWidgetToDashboard = function($event) {
 | 
			
		||||
            if (scope.mode === 'widget' && scope.widgetsListCache.length > 0) {
 | 
			
		||||
                var widget = scope.widgetsListCache[scope.widgetsCarousel.index][0];
 | 
			
		||||
 | 
			
		||||
@ -158,7 +158,6 @@
 | 
			
		||||
                <tb-dashboard
 | 
			
		||||
                        alias-controller="aliasController"
 | 
			
		||||
                        widgets="widgets"
 | 
			
		||||
                        get-st-diff="getServerTimeDiff()"
 | 
			
		||||
                        columns="20"
 | 
			
		||||
                        is-edit="false"
 | 
			
		||||
                        is-mobile-disabled="true"
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user