controller('crSummariesController', function($scope, $http, $rootScope, $sce, $timeout, cr, $q, _) { $rootScope.sidebar = false; $rootScope.rightSidebar = false; $scope.returnedResults = []; //console.log(_); var self = this; Date.prototype.outputUTC = function() { return Date.UTC(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds()); }; self.Initialise = function() { $scope.allresults = []; $scope.summaries = { timeframe: 'Today', workType: 'count', }; $scope.Math = Math; grabAppData('machine-profile', $rootScope.factoryKey).then(function(machineData) { grabAppData('shift-profile', $rootScope.factoryKey).then(function(shiftData) { //console.log(machineData); //console.log($scope.machineObj); $scope.shiftsObj = shiftData; angular.forEach($scope.machineObj, function(value, key) { value.machineQueries = {}; value.machineQueries.shiftInfo = []; value.machineQueries.workTotal = { count: 0, timer: 0, }; angular.forEach(machineData, function(profile, machineKey) { if (value.Address === profile.remoteAddr) { value.machineQueries.machineInfo = profile; } }); value.machineQueries.shifts = {}; tempVar = 0; angular.forEach(shiftData, function(shiftProfile, theKeys) { value.machineQueries.shifts[tempVar] = { name: shiftProfile.name, sum: { count: '0', timer: '0', } }; tempVar++; angular.forEach(shiftProfile.machines, function(machines, macKeys) { if (value.Address === macKeys) { value.machineQueries.shiftInfo.push(shiftProfile); } }); }); //console.log($scope.machineObj); }); //console.log($scope.machineObj); }, function(error) { console.log('Failure...', error); }); }, function(error) { console.log('Failure...', error); }); }; $scope.enableWarMachine = function() { $scope.shiftC = 0; alert('You have been warned!'); //console.log($scope.machineObj); var daysOfWeek = { "sunday": 0, "monday": 1, "tuesday": 2, "wednesday": 3, "thursday": 4, "friday": 5, "saturday": 6 }; var inverseDaysOfWeek = { 0: "sunday", 1: "monday", 2: "tuesday", 3: "wednesday", 4: "thursday", 5: "friday", 6: "saturday", }; // $scope.reporting.machineSelect.SoftwareKey, $scope.reporting.machineSelect.Address, // processObj, Math.floor(new Date(d) / 1000), Math.floor(new Date(da) / 1000), // $scope.reporting.hardware // console.log('http://54.213.13.56/api/' + value.SoftwareKey + '/' + value.Address + // '?process[]=' + 'sum' + '&date[]=' + startDate / 1000 + '&date[]=' + endDate / 1000 + '&index=' + // value.machineQueries.machineInfo.inputSelected); // endDate = new Date(); // startDate = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate(), 0, 0, 0, 0); // endDate = new Date(startDate.getFullYear(), startDate.getMonth(), endDate.getDate(), 23, 59, 59, 0); endDate = new Date(1436846399000); startDate = new Date(1436760000000); angular.forEach($scope.machineObj, function(value, key) { webCall(value.SoftwareKey, value.Address, 'sum', startDate / 1000, endDate / 1000, value.machineQueries.machineInfo.inputSelected).then(function(data) { value.machineQueries.workTotal.count = data.sum.count; value.machineQueries.workTotal.timer = Math.round((data.sum.timer / 60) * 10) / 10; //console.log(value); }); }); //var defer; machineCounter = 0; function callShifts(machineCount) { if (machineCount < $scope.machineObj.length - 1) { shiftsCounter = 0; var tempCount = 0; $scope.counter = 0; recurseShifts(shiftsCounter, machineCount, tempCount); //console.log($scope.machineObj[machineCount].Name); machineCount++; callShifts(machineCount); } else { console.log($scope.returnedResults); } } function recurseShifts(shiftCounter, machineCount, tmpCnt) { //console.log(tmpCnt) if (shiftCounter <= $scope.machineObj[machineCount].machineQueries.shiftInfo.length - 1) { //console.log($scope.machineObj[machineCount].machineQueries.shiftInfo[shiftCounter].name); shiftDaysCounter = 0; weeklyCounter = 0; recurseDays(shiftDaysCounter, weeklyCounter, shiftCounter, machineCount, tmpCnt); $scope.counter = 0; shiftCounter++; recurseShifts(shiftCounter, machineCount, tmpCnt); } } function recurseDays(shiftC, weekC, shiftyCounter, machineyCount, tmp) { //console.log(_.size($scope.machineObj[machineyCount].machineQueries.shiftInfo[shiftyCounter].days)); //if (shiftC <= _.size($scope.machineObj[machineyCount].machineQueries.shiftInfo[shiftyCounter].days) - 1) { if (weekC < _.size(inverseDaysOfWeek) - 1) { if ($scope.machineObj[machineyCount].machineQueries.shiftInfo[shiftyCounter].days[inverseDaysOfWeek[weekC]] === true) { if (daysOfWeek[inverseDaysOfWeek[weekC]] === startDate.getDay()) { // console.log($scope.machineObj[machineyCount].Name); // console.log($scope.machineObj[machineyCount].machineQueries.shiftInfo[shiftyCounter].name); // console.log(inverseDaysOfWeek[weekC]); $scope.shiftC++; //console.log($scope.shiftC); tempStartTime = new Date($scope.machineObj[machineyCount].machineQueries.shiftInfo[shiftyCounter].startTime); tempStartTime = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), tempStartTime.getHours(), tempStartTime.getMinutes(), 0, 0); tempEndTime = new Date($scope.machineObj[machineyCount].machineQueries.shiftInfo[shiftyCounter].endTime); tempEndTime = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), tempEndTime.getHours(), tempEndTime.getMinutes(), 0, 0); webCall($scope.machineObj[machineyCount].SoftwareKey, $scope.machineObj[machineyCount].Address, 'sum', tempStartTime / 1000, tempEndTime / 1000, $scope.machineObj[machineyCount].machineQueries.machineInfo.inputSelected).then(function(data) { data.shiftName = $scope.machineObj[machineyCount].machineQueries.shiftInfo[shiftyCounter].name; data.machine = $scope.machineObj[machineyCount].Address; $scope.returnedResults.push(data); console.log($scope.counter); // for (i = 0; i < $scope.machineObj[machineyCount].machineQueries.shiftInfo.length - 1; i++) { // if ($scope.machineObj[machineyCount].machineQueries.shiftInfo[i].name === data.name && $scope.machineObj[machineyCount].machineQueries.machineInfo.remoteAddr === data.machine) { $scope.machineObj[machineyCount].machineQueries.shifts[$scope.counter] = data; // } // } //console.log(data); $scope.counter++; //console.log($scope.allresults); }); } } // if(inverseDaysOfWeek[weekC] === $scope.machineObj[machineyCount].machineQueries.shiftInfo[shiftyCounter].days[inverseDaysOfWeek[weekC]]) { // console.log(inverseDaysOfWeek[weekC]); // } // console.log(inverseDaysOfWeek[weekC]); //console.log($scope.machineObj[machineyCount].machineQueries.shiftInfo[shiftyCounter].days[inverseDaysOfWeek[weekC]]); weekC++; recurseDays(shiftC, weekC, shiftyCounter, machineyCount); } else { $scope.counter = 0; } //console.log(shiftC); //shiftC++; //recurseDays(shiftC, weekC, shiftyCounter, machineyCount); //} } callShifts(machineCounter); // angular.forEach($scope.machineObj, function(machine, machineKey) { // var tempCount = 0; // angular.forEach(machine.machineQueries.shiftInfo, function(shifts, shiftsKey) { // //angular.forEach(shifts.days, function(value, key) { // angular.forEach($scope.shiftsObj, function(shiftVal, shiftKay) { // //if (daysOfWeek[key] === startDate.getDay()) { // tempStartTime = new Date(shifts.startTime); // tempEndTime = new Date(shifts.endTime); // tempStartTime = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), tempStartTime.getHours(), tempStartTime.getMinutes(), 0, 0); // tempEndTime = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), tempEndTime.getHours(), tempEndTime.getMinutes(), 0, 0); // if (shifts.name === shiftVal.name) { // //defer = $q.defer(); // webCall(machine.SoftwareKey, machine.Address, 'sum', tempStartTime / 1000, tempEndTime / 1000, machine.machineQueries.machineInfo.inputSelected).then(function(data) { // data.name = shifts.name; // data.machine = machine.Address; // for (i = 0; i < machine.machineQueries.shiftInfo.length - 1; i++) { // if (machine.machineQueries.shiftInfo[i].name === data.name && machine.machineQueries.machineInfo.remoteAddr === data.machine) { // // machine.machineQueries.shifts[tempCount] = data; // } // } // // tempCount++; // $scope.allresults.push(data); // //machine.machineQueries.shifts.push(data.sum.timer); // //console.log(data); // }); // } // //} // console.log($scope.allresults); // // }); // //}); // // // }); // }); }; self.log = function(message) { console.log(message); }; self.Initialise(); function grabAppData(appKey, factoryKey) { var defer = $q.defer(); console.log('http://54.213.13.56/api/app/' + appKey + '/' + factoryKey); $http.get('http://54.213.13.56/api/app/' + appKey + '/' + factoryKey). success(function(data) { // console.log(data); if (appKey === 'shift-profile') { angular.forEach(data, function(items, key) { angular.forEach(items.days, function(daySelected, day) { items.days[day] = daySelected == "true"; }); //console.log(data); angular.forEach(items.machines, function(machineSelected, machine) { items.machines[machine] = machineSelected == "true"; }); }); } defer.resolve(data); }).error(function(data) { console.log('Error in grabAppData: ' + data); }); return defer.promise; } function webCall(softwareKey, address, processObj, startDate, endDate, hardware) { var defer = $q.defer(); console.log('http://54.213.13.56/api/' + softwareKey + '/' + address + '?process[]=' + processObj + '&date[]=' + startDate + '&date[]=' + endDate + '&index=' + hardware); $http.get('http://54.213.13.56/api/' + softwareKey + '/' + address + '?process[]=' + processObj + '&date[]=' + startDate + '&date[]=' + endDate + '&index=' + hardware). success(function(data) { //console.log('Data: ' + data); defer.resolve(data); }). error(function(data) { alert(data); }); return defer.promise; } });