You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cameras.controller.js 2.4KB

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