Passed
Push — master ( 288f92...809e6b )
by
unknown
13:11 queued 13s
created

myems-admin/app/controllers/settings/energystoragecontainer/energystoragecontainergrid.controller.js   A

Complexity

Total Complexity 38
Complexity/F 1.27

Size

Lines of Code 238
Function Count 30

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 38
eloc 168
dl 0
loc 238
rs 9.36
c 0
b 0
f 0
mnd 8
bc 8
fnc 30
bpm 0.2666
cpm 1.2666
noi 0
1
'use strict';
2
3
app.controller('EnergyStorageContainerGridController', function(
4
	$scope,
5
	$rootScope,
6
	$window,
7
	$translate,
8
	$uibModal,
9
	EnergyStorageContainerService,
10
	EnergyStorageContainerGridService,
11
	PointService,
12
	MeterService,
13
	toaster,
14
	SweetAlert) {
15
      $scope.energystoragecontainers = [];
16
      $scope.energystoragecontainergrids = [];
17
	  $scope.points = [];
18
	  $scope.meters = [];
19
      $scope.currentEnergyStorageContainer = null;
20
	  $scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
21
      $scope.getAllEnergyStorageContainers = function() {
22
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
23
  		EnergyStorageContainerService.getAllEnergyStorageContainers(headers, function (response) {
24
  			if (angular.isDefined(response.status) && response.status === 200) {
25
  				$scope.energystoragecontainers = response.data;
26
  			} else {
27
  				$scope.energystoragecontainers = [];
28
  			}
29
  		});
30
  	};
31
32
	$scope.getAllPoints = function() {
33
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
34
		PointService.getAllPoints(headers, function (response) {
35
			if (angular.isDefined(response.status) && response.status === 200) {
36
				$scope.points = response.data;
37
			} else {
38
				$scope.points = [];
39
			}
40
		});
41
	};
42
43
	$scope.getAllMeters = function() {
44
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
45
		MeterService.getAllMeters(headers, function (response) {
46
			if (angular.isDefined(response.status) && response.status === 200) {
47
				$scope.meters = response.data;
48
			} else {
49
				$scope.meters = [];
50
			}
51
		});
52
	};
53
  	$scope.getEnergyStorageContainerGridsByEnergyStorageContainerID = function(id) {
54
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
55
  		EnergyStorageContainerGridService.getEnergyStorageContainerGridsByEnergyStorageContainerID(id, headers, function (response) {
56
			if (angular.isDefined(response.status) && response.status === 200) {
57
				$scope.energystoragecontainergrids = response.data;
58
			} else {
59
          	$scope.energystoragecontainergrids=[];
60
        }
61
			});
62
  	};
63
64
  	$scope.changeEnergyStorageContainer=function(item,model){
65
    	$scope.currentEnergyStorageContainer=item;
66
    	$scope.currentEnergyStorageContainer.selected=model;
67
        $scope.is_show_add_energystoragecontainer_grid = true;
68
    	$scope.getEnergyStorageContainerGridsByEnergyStorageContainerID($scope.currentEnergyStorageContainer.id);
69
  	};
70
71
  	$scope.addEnergyStorageContainerGrid = function() {
72
73
  		var modalInstance = $uibModal.open({
74
  			templateUrl: 'views/settings/energystoragecontainer/energystoragecontainergrid.model.html',
75
  			controller: 'ModalAddEnergyStorageContainerGridCtrl',
76
  			windowClass: "animated fadeIn",
77
  			resolve: {
78
  				params: function() {
79
  					return {
80
						meters: angular.copy($scope.meters),
81
						points: angular.copy($scope.points),
82
  					};
83
  				}
84
  			}
85
  		});
86
  		modalInstance.result.then(function(energystoragecontainergrid) {
87
			energystoragecontainergrid.power_point_id = energystoragecontainergrid.power_point.id;
88
			energystoragecontainergrid.buy_meter_id = energystoragecontainergrid.buy_meter.id;
89
			energystoragecontainergrid.sell_meter_id = energystoragecontainergrid.sell_meter.id;
90
91
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
92
  			EnergyStorageContainerGridService.addEnergyStorageContainerGrid($scope.currentEnergyStorageContainer.id, energystoragecontainergrid, headers, function (response) {
93
  				if (angular.isDefined(response.status) && response.status === 201) {
94
  					toaster.pop({
95
  						type: "success",
96
  						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
97
  						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_GRID")}),
98
  						showCloseButton: true,
99
  					});
100
  					$scope.getEnergyStorageContainerGridsByEnergyStorageContainerID($scope.currentEnergyStorageContainer.id);
101
            		$scope.$emit('handleEmitEnergyStorageContainerGridChanged');
102
  				} else {
103
  					toaster.pop({
104
  						type: "error",
105
  						title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_GRID")}),
106
  						body: $translate.instant(response.data.description),
107
  						showCloseButton: true,
108
  					});
109
  				}
110
  			});
111
  		}, function() {
112
113
  		});
114
		$rootScope.modalInstance = modalInstance;
115
  	};
116
117
  	$scope.editEnergyStorageContainerGrid = function(energystoragecontainergrid) {
118
  		var modalInstance = $uibModal.open({
119
  			templateUrl: 'views/settings/energystoragecontainer/energystoragecontainergrid.model.html',
120
  			controller: 'ModalEditEnergyStorageContainerGridCtrl',
121
    		windowClass: "animated fadeIn",
122
  			resolve: {
123
  				params: function() {
124
  					return {
125
  						energystoragecontainergrid: angular.copy(energystoragecontainergrid),
126
						meters: angular.copy($scope.meters),
127
						points: angular.copy($scope.points),
128
  					};
129
  				}
130
  			}
131
  		});
132
133
  		modalInstance.result.then(function(modifiedEnergyStorageContainerGrid) {
134
			modifiedEnergyStorageContainerGrid.power_point_id = modifiedEnergyStorageContainerGrid.power_point.id;
135
			modifiedEnergyStorageContainerGrid.buy_meter_id = modifiedEnergyStorageContainerGrid.buy_meter.id;
136
			modifiedEnergyStorageContainerGrid.sell_meter_id = modifiedEnergyStorageContainerGrid.sell_meter.id;
137
138
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
139
  			EnergyStorageContainerGridService.editEnergyStorageContainerGrid($scope.currentEnergyStorageContainer.id, modifiedEnergyStorageContainerGrid, headers, function (response) {
140
  				if (angular.isDefined(response.status) && response.status === 200) {
141
  					toaster.pop({
142
  						type: "success",
143
  						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
144
  						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_GRID")}),
145
  						showCloseButton: true,
146
  					});
147
  					$scope.getEnergyStorageContainerGridsByEnergyStorageContainerID($scope.currentEnergyStorageContainer.id);
148
            		$scope.$emit('handleEmitEnergyStorageContainerGridChanged');
149
  				} else {
150
  					toaster.pop({
151
  						type: "error",
152
  						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_GRID")}),
153
  						body: $translate.instant(response.data.description),
154
  						showCloseButton: true,
155
  					});
156
  				}
157
  			});
158
  		}, function() {
159
  			//do nothing;
160
  		});
161
		$rootScope.modalInstance = modalInstance;
162
  	};
163
164
  	$scope.deleteEnergyStorageContainerGrid = function(energystoragecontainergrid) {
165
  		SweetAlert.swal({
166
  				title: $translate.instant("SWEET.TITLE"),
167
  				text: $translate.instant("SWEET.TEXT"),
168
  				type: "warning",
169
  				showCancelButton: true,
170
  				confirmButtonColor: "#DD6B55",
171
  				confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
172
  				cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
173
  				closeOnConfirm: true,
174
  				closeOnCancel: true
175
  			},
176
  			function(isConfirm) {
177
  				if (isConfirm) {
178
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
179
  					EnergyStorageContainerGridService.deleteEnergyStorageContainerGrid($scope.currentEnergyStorageContainer.id, energystoragecontainergrid.id, headers, function (response) {
180
  						if (angular.isDefined(response.status) && response.status === 204) {
181
							toaster.pop({
182
								type: "success",
183
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
184
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_GRID")}),
185
								showCloseButton: true,
186
							});
187
							$scope.getEnergyStorageContainerGridsByEnergyStorageContainerID($scope.currentEnergyStorageContainer.id);
188
							$scope.$emit('handleEmitEnergyStorageContainerGridChanged');
189
  						} else {
190
							toaster.pop({
191
								type: "error",
192
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("ENERGY_STORAGE_CONTAINER.ENERGY_STORAGE_CONTAINER_GRID")}),
193
								body: $translate.instant(response.data.description),
194
								showCloseButton: true,
195
							});
196
  				   		}
197
  					});
198
  				}
199
  			});
200
  	};
201
202
  	$scope.getAllEnergyStorageContainers();
203
	$scope.getAllPoints();
204
	$scope.getAllMeters();
205
    $scope.$on('handleBroadcastEnergyStorageContainerChanged', function(event) {
206
      $scope.getAllEnergyStorageContainers();
207
  	});
208
209
  });
210
211
212
  app.controller('ModalAddEnergyStorageContainerGridCtrl', function($scope, $uibModalInstance, params) {
213
214
  	$scope.operation = "ENERGY_STORAGE_CONTAINER.ADD_ENERGY_STORAGE_CONTAINER_GRID";
215
	$scope.points=params.points;
216
	$scope.meters=params.meters;
217
  	$scope.ok = function() {
218
  		$uibModalInstance.close($scope.energystoragecontainergrid);
219
  	};
220
221
  	$scope.cancel = function() {
222
  		$uibModalInstance.dismiss('cancel');
223
  	};
224
  });
225
226
  app.controller('ModalEditEnergyStorageContainerGridCtrl', function($scope, $uibModalInstance, params) {
227
  	$scope.operation = "ENERGY_STORAGE_CONTAINER.EDIT_ENERGY_STORAGE_CONTAINER_GRID";
228
  	$scope.energystoragecontainergrid = params.energystoragecontainergrid;
229
	$scope.points=params.points;
230
	$scope.meters=params.meters;
231
  	$scope.ok = function() {
232
  		$uibModalInstance.close($scope.energystoragecontainergrid);
233
  	};
234
235
  	$scope.cancel = function() {
236
  		$uibModalInstance.dismiss('cancel');
237
  	};
238
  });
239