poconsole.controller('dashboardCtrl', function($scope, $route, $http, $sails, Page, $log, tags) {
	Page.setTitle('Dashboard');
    Page.setPage('dashboard');
    $scope.loadDashboard = function(){
        $scope.loading = true;
        var getCurrentValues = tags.getCurrentValues();
        getCurrentValues.then(function(data) {
        	$scope.loading = false;
        	$scope.vals = data.vals;
            $log.info($scope.vals);
        });
    };
    $scope.loadDashboard();

    $sails.get("/tag_val").success(function(data, status, headers, jwr){
        $log.info(data);
    });

    var valHandler = $sails.on('tag_val', function(message){
        if (message.verb === "created"){
            $log.info(message.data);
            for(var i = 0; i < $scope.vals.length; i++){
                if(message.data.tagID == $scope.vals[i].t_id){
                    $scope.vals[i].val = message.data.val;
                    $scope.vals[i].dtime = message.data.createdAt;
                }
            }
        }
    });

    $scope.$on('$destroy', function() {
      $sails.off('tag_val', valHandler);
    });


});
