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.

sensoredit.controller.js 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Created by robin on 11/4/15.
  3. */
  4. angular.module('camotionAdmin')
  5. .controller('SensorEditController', ['$scope', 'SensorsService', 'data', 'dialogs', '$q', 'luticateAuthCache', 'HelperService',
  6. function($scope, SensorsService, data, dialogs, $q, luticateAuthCache, HelperService) {
  7. if (data != null) {
  8. $scope.sensor = data;
  9. $scope.sensor.Data = JSON.parse($scope.sensor.Data);
  10. $scope.sensor.sensor_id = $scope.sensor.Id;
  11. $scope.permission = "CAMOTION_SENSOR_EDIT";
  12. }
  13. else {
  14. $scope.sensor = {
  15. Name: "",
  16. Description: "",
  17. Data: null,
  18. HostId: null,
  19. SensorTypeId: null
  20. };
  21. $scope.permission = "CAMOTION_SENSOR_ADD";
  22. }
  23. $scope.hosts = {};
  24. $scope.sensorTypes = {};
  25. var promiseLoadForeign = {
  26. id: "promiseLoadForeign",
  27. groups: ["modal"]
  28. };
  29. HelperService.getForeignEntities(SensorsService, promiseLoadForeign).then(function(data)
  30. {
  31. $scope.hosts = data.hosts;
  32. $scope.sensorTypes = data.types;
  33. if ($scope.sensor.Id == null) {
  34. var keys = Object.keys($scope.hosts);
  35. if (keys.length != 0) {
  36. $scope.sensor.HostId = $scope.hosts[keys[0]].Id;
  37. }keys = Object.keys($scope.sensorTypes);
  38. if (keys.length != 0) {
  39. $scope.sensor.SensorTypeId = $scope.sensorTypes[keys[0]].Id;
  40. $scope.setSensorType($scope.sensorTypes[keys[0]]);
  41. }
  42. }
  43. else if ($scope.sensor.Data == null) {
  44. $scope.setSensorType($scope.sensorTypes[$scope.sensor.SensorTypeId]);
  45. }
  46. });
  47. $scope.submitForm = function()
  48. {
  49. if ($scope.sensor.Data == null) {
  50. $scope.sensor.Data = {};
  51. }
  52. if (luticateAuthCache.hasEffectivePermission($scope.permission)) {
  53. $scope.sensor.Data = JSON.stringify($scope.sensor.Data);
  54. if ($scope.sensor.Id != null) {
  55. var promiseEditSensor = {
  56. id: "promiseEditSensor",
  57. loaderGroups: ["modal"]
  58. };
  59. return SensorsService.edit($scope.sensor, promiseEditSensor);
  60. }
  61. else {
  62. var promiseAddSensor = {
  63. id: "promiseAddSensor",
  64. loaderGroups: ["modal"]
  65. };
  66. return SensorsService.add($scope.sensor, promiseAddSensor);
  67. }
  68. }
  69. else {
  70. var defer = $q.defer();
  71. defer.resolve();
  72. return defer.promise;
  73. }
  74. };
  75. $scope.setSensorType = function(sensorType)
  76. {
  77. $scope.sensor.SensorTypeId = sensorType.Id;
  78. $scope.sensor.Data = JSON.parse($scope.sensorTypes[$scope.sensor.SensorTypeId].DefaultData);
  79. };
  80. }]);