|  | @@ -1,30 +1,74 @@
 | 
		
	
		
			
			| 1 | 1 |  angular.module('camotion')
 | 
		
	
		
			
			| 2 |  | -    .controller('CamerasController', ['$scope', 'CamerasService',
 | 
		
	
		
			
			| 3 |  | -        function($scope, CamerasService) {
 | 
		
	
		
			
			|  | 2 | +    .controller('CamerasController', ['$scope', 'CamerasService', '$timeout',
 | 
		
	
		
			
			|  | 3 | +        function($scope, CamerasService, $timeout) {
 | 
		
	
		
			
			| 4 | 4 |  
 | 
		
	
		
			
			| 5 | 5 |              $scope.cameras = [];
 | 
		
	
		
			
			| 6 |  | -            $scope.rowCameras = [];
 | 
		
	
		
			
			| 7 |  | -
 | 
		
	
		
			
			| 8 |  | -            $scope.playingCameras = [];
 | 
		
	
		
			
			|  | 6 | +            $scope.images = {};
 | 
		
	
		
			
			|  | 7 | +            $scope.interval = 1000;
 | 
		
	
		
			
			| 9 | 8 |  
 | 
		
	
		
			
			| 10 | 9 |              $scope.playPause = function(camera)
 | 
		
	
		
			
			| 11 | 10 |              {
 | 
		
	
		
			
			| 12 |  | -                var idx = $scope.playingCameras.indexOf(camera.Id);
 | 
		
	
		
			
			| 13 |  | -                if (idx > -1) {
 | 
		
	
		
			
			| 14 |  | -                    $scope.playingCameras.splice(idx, 1);
 | 
		
	
		
			
			|  | 11 | +                if ($scope.isPlaying(camera)) {
 | 
		
	
		
			
			|  | 12 | +                    $scope.pause(camera);
 | 
		
	
		
			
			| 15 | 13 |                  }
 | 
		
	
		
			
			| 16 | 14 |                  else {
 | 
		
	
		
			
			| 17 |  | -                    $scope.playingCameras.push(camera.Id);
 | 
		
	
		
			
			|  | 15 | +                    $scope.play(camera);
 | 
		
	
		
			
			|  | 16 | +                }
 | 
		
	
		
			
			|  | 17 | +            };
 | 
		
	
		
			
			|  | 18 | +
 | 
		
	
		
			
			|  | 19 | +            $scope.play = function(camera)
 | 
		
	
		
			
			|  | 20 | +            {
 | 
		
	
		
			
			|  | 21 | +                if (!$scope.isPlaying(camera)) {
 | 
		
	
		
			
			|  | 22 | +                    $scope.images[camera.Id] = {
 | 
		
	
		
			
			|  | 23 | +                        image: null,
 | 
		
	
		
			
			|  | 24 | +                        timerId: 0
 | 
		
	
		
			
			|  | 25 | +                    };
 | 
		
	
		
			
			|  | 26 | +                    $scope.updateCamera(camera);
 | 
		
	
		
			
			|  | 27 | +                }
 | 
		
	
		
			
			|  | 28 | +            };
 | 
		
	
		
			
			|  | 29 | +
 | 
		
	
		
			
			|  | 30 | +            $scope.pause = function(camera)
 | 
		
	
		
			
			|  | 31 | +            {
 | 
		
	
		
			
			|  | 32 | +                if ($scope.isPlaying(camera)) {
 | 
		
	
		
			
			|  | 33 | +                    $scope.images[camera.Id] = null;
 | 
		
	
		
			
			| 18 | 34 |                  }
 | 
		
	
		
			
			| 19 | 35 |              };
 | 
		
	
		
			
			| 20 | 36 |  
 | 
		
	
		
			
			| 21 | 37 |              $scope.isPlaying = function(camera)
 | 
		
	
		
			
			| 22 | 38 |              {
 | 
		
	
		
			
			| 23 |  | -                return $scope.playingCameras.indexOf(camera.Id) > -1;
 | 
		
	
		
			
			|  | 39 | +                return $scope.images[camera.Id] != null;
 | 
		
	
		
			
			|  | 40 | +            };
 | 
		
	
		
			
			|  | 41 | +
 | 
		
	
		
			
			|  | 42 | +            $scope.updateCamera = function(camera)
 | 
		
	
		
			
			|  | 43 | +            {
 | 
		
	
		
			
			|  | 44 | +                if ($scope.isPlaying(camera)) {
 | 
		
	
		
			
			|  | 45 | +                    CamerasService.getImage({camera_id: camera.Id})
 | 
		
	
		
			
			|  | 46 | +                        .then(function (image) {
 | 
		
	
		
			
			|  | 47 | +                            if ($scope.isPlaying(camera)) {
 | 
		
	
		
			
			|  | 48 | +                                $scope.images[camera.Id] = {
 | 
		
	
		
			
			|  | 49 | +                                    image: image.Image,
 | 
		
	
		
			
			|  | 50 | +                                    timerId: $timeout(function () {
 | 
		
	
		
			
			|  | 51 | +                                        $scope.updateCamera(camera);
 | 
		
	
		
			
			|  | 52 | +                                    }, $scope.interval)
 | 
		
	
		
			
			|  | 53 | +                                }
 | 
		
	
		
			
			|  | 54 | +                            }
 | 
		
	
		
			
			|  | 55 | +                        }, function (error) {
 | 
		
	
		
			
			|  | 56 | +                            $scope.pause(camera);
 | 
		
	
		
			
			|  | 57 | +                        });
 | 
		
	
		
			
			|  | 58 | +                }
 | 
		
	
		
			
			| 24 | 59 |              };
 | 
		
	
		
			
			| 25 | 60 |  
 | 
		
	
		
			
			| 26 | 61 |              CamerasService.getAll({}).then(function(cameras)
 | 
		
	
		
			
			| 27 | 62 |              {
 | 
		
	
		
			
			|  | 63 | +                $scope.images = {};
 | 
		
	
		
			
			| 28 | 64 |                  $scope.cameras = cameras.Data;
 | 
		
	
		
			
			|  | 65 | +                for (var i = 0; i < $scope.cameras.length; ++i) {
 | 
		
	
		
			
			|  | 66 | +                    var camera = angular.copy($scope.cameras[i]);
 | 
		
	
		
			
			|  | 67 | +                    $scope.play(camera);
 | 
		
	
		
			
			|  | 68 | +                }
 | 
		
	
		
			
			|  | 69 | +            });
 | 
		
	
		
			
			|  | 70 | +
 | 
		
	
		
			
			|  | 71 | +            $scope.$on('$destroy', function(){
 | 
		
	
		
			
			|  | 72 | +                $scope.images = {};
 | 
		
	
		
			
			| 29 | 73 |              });
 | 
		
	
		
			
			| 30 | 74 |      }]);
 |