Passed
Push — master ( 0609f7...f69d02 )
by
unknown
10:47
created

myems-admin/app/controllers/settings/microgrid/microgridload.controller.js   F

Complexity

Total Complexity 62
Complexity/F 1.35

Size

Lines of Code 424
Function Count 46

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 62
eloc 306
dl 0
loc 424
rs 3.44
c 0
b 0
f 0
mnd 16
bc 16
fnc 46
bpm 0.3478
cpm 1.3478
noi 0

How to fix   Complexity   

Complexity

Complex classes like myems-admin/app/controllers/settings/microgrid/microgridload.controller.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict';
2
3
app.controller('MicrogridLoadController', function(
4
	$scope,
5
	$rootScope,
6
	$window,
7
	$translate,
8
	$uibModal,
9
	MicrogridService,
10
	MicrogridLoadService,
11
	MicrogridDataSourceService,
12
	PointService,
13
	MeterService,
14
	toaster,
15
	SweetAlert) {
16
      $scope.microgrids = [];
17
      $scope.microgridloads = [];
18
	  $scope.points = [];
19
	  $scope.meters = [];
20
      $scope.currentMicrogrid = null;
21
	  $scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
22
      $scope.getAllMicrogrids = function() {
23
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
24
  		MicrogridService.getAllMicrogrids(headers, function (response) {
25
  			if (angular.isDefined(response.status) && response.status === 200) {
26
  				$scope.microgrids = response.data;
27
  			} else {
28
  				$scope.microgrids = [];
29
  			}
30
  		});
31
  	};
32
33
    $scope.getDataSourcesByMicrogridID = function(id) {
34
      let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
35
      MicrogridDataSourceService.getDataSourcesByMicrogridID(id, headers, function(response) {
36
        if (angular.isDefined(response.status) && response.status === 200) {
37
          $scope.datasources = response.data;
38
        } else {
39
          $scope.datasources = [];
40
        }
41
      });
42
    };
43
44
    $scope.getDataSourcePointsByMicrogridID = function(id) {
45
      let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
46
      MicrogridDataSourceService.getDataSourcePointsByMicrogridID(id, headers, function(response) {
47
        if (angular.isDefined(response.status) && response.status === 200) {
48
          $scope.points = response.data;
49
        } else {
50
          $scope.points = [];
51
        }
52
      });
53
    };
54
55
	$scope.getAllMeters = function() {
56
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
57
		MeterService.getAllMeters(headers, function (response) {
58
			if (angular.isDefined(response.status) && response.status === 200) {
59
				$scope.meters = response.data;
60
			} else {
61
				$scope.meters = [];
62
			}
63
		});
64
	};
65
  	$scope.getMicrogridLoadsByMicrogridID = function(id) {
66
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
67
  		MicrogridLoadService.getMicrogridLoadsByMicrogridID(id, headers, function (response) {
68
			if (angular.isDefined(response.status) && response.status === 200) {
69
				$scope.microgridloads = response.data;
70
			} else {
71
          	$scope.microgridloads=[];
72
        }
73
			});
74
  	};
75
76
  	$scope.changeMicrogrid=function(item,model){
77
    	$scope.currentMicrogrid=item;
78
    	$scope.currentMicrogrid.selected=model;
79
        $scope.is_show_add_microgrid_load = true;
80
    	$scope.getMicrogridLoadsByMicrogridID($scope.currentMicrogrid.id);
81
    	$scope.getDataSourcesByMicrogridID($scope.currentMicrogrid.id);
82
        $scope.getDataSourcePointsByMicrogridID($scope.currentMicrogrid.id);
83
  	};
84
85
  	$scope.addMicrogridLoad = function() {
86
  		var modalInstance = $uibModal.open({
87
  			templateUrl: 'views/settings/microgrid/microgridload.model.html',
88
  			controller: 'ModalAddMicrogridLoadCtrl',
89
  			windowClass: "animated fadeIn",
90
  			resolve: {
91
  				params: function() {
92
  					return {
93
						meters: angular.copy($scope.meters),
94
						points: angular.copy($scope.points),
95
  					};
96
  				}
97
  			}
98
  		});
99
  		modalInstance.result.then(function(microgridload) {
100
			microgridload.power_point_id = microgridload.power_point.id;
101
			microgridload.meter_id = microgridload.meter.id;
102
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
103
  			MicrogridLoadService.addMicrogridLoad($scope.currentMicrogrid.id, microgridload, headers, function (response) {
104
  				if (angular.isDefined(response.status) && response.status === 201) {
105
  					toaster.pop({
106
  						type: "success",
107
  						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
108
  						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("MICROGRID.MICROGRID_LOAD")}),
109
  						showCloseButton: true,
110
  					});
111
  					$scope.getMicrogridLoadsByMicrogridID($scope.currentMicrogrid.id);
112
  					$scope.getDataSourcesByMicrogridID($scope.currentMicrogrid.id);
113
                    $scope.getDataSourcePointsByMicrogridID($scope.currentMicrogrid.id);
114
            		$scope.$emit('handleEmitMicrogridLoadChanged');
115
  				} else {
116
  					toaster.pop({
117
  						type: "error",
118
  						title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("MICROGRID.MICROGRID_LOAD")}),
119
  						body: $translate.instant(response.data.description),
120
  						showCloseButton: true,
121
  					});
122
  				}
123
  			});
124
  		}, function() {
125
126
  		});
127
		$rootScope.modalInstance = modalInstance;
128
  	};
129
130
  	$scope.editMicrogridLoad = function(microgridload) {
131
  		var modalInstance = $uibModal.open({
132
  			templateUrl: 'views/settings/microgrid/microgridload.model.html',
133
  			controller: 'ModalEditMicrogridLoadCtrl',
134
    		windowClass: "animated fadeIn",
135
  			resolve: {
136
  				params: function() {
137
  					return {
138
  						microgridload: angular.copy(microgridload),
139
						meters: angular.copy($scope.meters),
140
						points: angular.copy($scope.points),
141
  					};
142
  				}
143
  			}
144
  		});
145
146
  		modalInstance.result.then(function(modifiedMicrogridLoad) {
147
			modifiedMicrogridLoad.power_point_id = modifiedMicrogridLoad.power_point.id;
148
			modifiedMicrogridLoad.meter_id = modifiedMicrogridLoad.meter.id;
149
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
150
  			MicrogridLoadService.editMicrogridLoad($scope.currentMicrogrid.id, modifiedMicrogridLoad, headers, function (response) {
151
  				if (angular.isDefined(response.status) && response.status === 200) {
152
  					toaster.pop({
153
  						type: "success",
154
  						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
155
  						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_LOAD")}),
156
  						showCloseButton: true,
157
  					});
158
  					$scope.getMicrogridLoadsByMicrogridID($scope.currentMicrogrid.id);
159
  					$scope.getDataSourcesByMicrogridID($scope.currentMicrogrid.id);
160
                    $scope.getDataSourcePointsByMicrogridID($scope.currentMicrogrid.id);
161
            		$scope.$emit('handleEmitMicrogridLoadChanged');
162
  				} else {
163
  					toaster.pop({
164
  						type: "error",
165
  						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_LOAD")}),
166
  						body: $translate.instant(response.data.description),
167
  						showCloseButton: true,
168
  					});
169
  				}
170
  			});
171
  		}, function() {
172
  			//do nothing;
173
  		});
174
		$rootScope.modalInstance = modalInstance;
175
  	};
176
177
  	$scope.deleteMicrogridLoad = function(microgridload) {
178
  		SweetAlert.swal({
179
  				title: $translate.instant("SWEET.TITLE"),
180
  				text: $translate.instant("SWEET.TEXT"),
181
  				type: "warning",
182
  				showCancelButton: true,
183
  				confirmButtonColor: "#DD6B55",
184
  				confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
185
  				cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
186
  				closeOnConfirm: true,
187
  				closeOnCancel: true
188
  			},
189
  			function(isConfirm) {
190
  				if (isConfirm) {
191
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
192
  					MicrogridLoadService.deleteMicrogridLoad($scope.currentMicrogrid.id, microgridload.id, headers, function (response) {
193
  						if (angular.isDefined(response.status) && response.status === 204) {
194
							toaster.pop({
195
								type: "success",
196
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
197
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_LOAD")}),
198
								showCloseButton: true,
199
							});
200
							$scope.getMicrogridLoadsByMicrogridID($scope.currentMicrogrid.id);
201
							$scope.$emit('handleEmitMicrogridLoadChanged');
202
  						} else {
203
							toaster.pop({
204
								type: "error",
205
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_LOAD")}),
206
								body: $translate.instant(response.data.description),
207
								showCloseButton: true,
208
							});
209
  				   		}
210
  					});
211
  				}
212
  			});
213
  	};
214
215
    $scope.bindMicrogridLoadPoint = function (load) {
216
      var modalInstance = $uibModal.open({
217
        templateUrl:
218
          "views/settings/microgrid/microgridloadpoint.model.html",
219
        controller: "ModalBindMicrogridLoadCtrl",
220
        windowClass: "animated fadeIn",
221
        resolve: {
222
          params: function () {
223
            return {
224
              user_uuid: $scope.cur_user.uuid,
225
              token: $scope.cur_user.token,
226
              microgridid: $scope.currentMicrogrid.id,
227
              microgridload: angular.copy(load),
228
              meters: angular.copy($scope.meters),
229
              datasources: angular.copy($scope.datasources),
230
              points: angular.copy($scope.points),
231
            };
232
          },
233
        },
234
      });
235
      $rootScope.modalInstance = modalInstance;
236
    };
237
238
  	$scope.getAllMicrogrids();
239
	$scope.getAllMeters();
240
    $scope.$on('handleBroadcastMicrogridChanged', function(event) {
241
      $scope.getAllMicrogrids();
242
  	});
243
244
  });
245
246
247
  app.controller('ModalAddMicrogridLoadCtrl', function($scope, $uibModalInstance, params) {
248
249
  	$scope.operation = "MICROGRID.ADD_MICROGRID_LOAD";
250
	$scope.points=params.points;
251
	$scope.meters=params.meters;
252
  	$scope.ok = function() {
253
  		$uibModalInstance.close($scope.microgridload);
254
  	};
255
256
  	$scope.cancel = function() {
257
  		$uibModalInstance.dismiss('cancel');
258
  	};
259
  });
260
261
  app.controller('ModalEditMicrogridLoadCtrl', function($scope, $uibModalInstance, params) {
262
  	$scope.operation = "MICROGRID.EDIT_MICROGRID_LOAD";
263
  	$scope.microgridload = params.microgridload;
264
	$scope.points=params.points;
265
	$scope.meters=params.meters;
266
  	$scope.ok = function() {
267
  		$uibModalInstance.close($scope.microgridload);
268
  	};
269
270
  	$scope.cancel = function() {
271
  		$uibModalInstance.dismiss('cancel');
272
  	};
273
  });
274
275
app.controller(
276
  "ModalBindMicrogridLoadCtrl",
277
  function (
278
    $scope,
279
    $uibModalInstance,
280
    toaster,
281
    $translate,
282
    MicrogridLoadService,
283
    PointService,
284
    params
285
  ) {
286
    $scope.operation = "MICROGRID.MICROGRID_LOAD";
287
    $scope.microgridid = params.microgridid;
288
    $scope.microgridload = params.microgridload;
289
    $scope.datasources = params.datasources;
290
    $scope.boundpoints = [];
291
292
    let headers = { "User-UUID": params.user_uuid, Token: params.token };
293
    MicrogridLoadService.getPointsByLoadID(
294
      $scope.microgridid,
295
      $scope.microgridload.id,
296
      headers,
297
      function (response) {
298
        if (angular.isDefined(response.status) && response.status === 200) {
299
          $scope.boundpoints = response.data;
300
        } else {
301
          $scope.boundpoints = [];
302
        }
303
      }
304
    );
305
306
    $scope.cancel = function () {
307
      $uibModalInstance.dismiss("cancel");
308
    };
309
310
    $scope.changeDataSource = function (item, model) {
311
      $scope.currentDataSource = model;
312
      $scope.getPointsByDataSourceID($scope.currentDataSource);
313
    };
314
315
    $scope.getPointsByDataSourceID = function (id) {
316
      let headers = { "User-UUID": params.user_uuid, Token: params.token };
317
      PointService.getPointsByDataSourceID(id, headers, function (response) {
318
        if (angular.isDefined(response.status) && response.status === 200) {
319
          $scope.points = response.data;
320
        } else {
321
          $scope.points = [];
322
        }
323
      });
324
    };
325
326
    $scope.pairPoint = function (dragEl, dropEl) {
327
      var pointid = angular.element("#" + dragEl).scope().point.id;
328
      let headers = { "User-UUID": params.user_uuid, Token: params.token };
329
      MicrogridLoadService.addLoadPair(
330
        params.microgridid,
331
        params.microgridload.id,
332
        pointid,
333
        headers,
334
        function (response) {
335
          if (angular.isDefined(response.status) && response.status === 201) {
336
            toaster.pop({
337
              type: "success",
338
              title: $translate.instant("TOASTER.SUCCESS_TITLE"),
339
              body: $translate.instant("TOASTER.BIND_POINT_SUCCESS"),
340
              showCloseButton: true,
341
            });
342
            let headers = {
343
              "User-UUID": params.user_uuid,
344
              Token: params.token,
345
            };
346
            MicrogridLoadService.getPointsByLoadID(
347
              params.microgridid,
348
              params.microgridload.id,
349
              headers,
350
              function (response) {
351
                if (
352
                  angular.isDefined(response.status) &&
353
                  response.status === 200
354
                ) {
355
                  $scope.boundpoints = response.data;
356
                } else {
357
                  $scope.boundpoints = [];
358
                }
359
              }
360
            );
361
          } else {
362
            toaster.pop({
363
              type: "error",
364
              title: $translate.instant(response.data.title),
365
              body: $translate.instant(response.data.description),
366
              showCloseButton: true,
367
            });
368
          }
369
        }
370
      );
371
    };
372
373
    $scope.deletePointPair = function (dragEl, dropEl) {
374
      if (angular.element("#" + dragEl).hasClass("source")) {
375
        return;
376
      }
377
378
      var pointid = angular.element("#" + dragEl).scope().boundpoint.id;
379
      let headers = { "User-UUID": params.user_uuid, Token: params.token };
380
      MicrogridLoadService.deleteLoadPair(
381
        params.microgridid,
382
        params.microgridload.id,
383
        pointid,
384
        headers,
385
        function (response) {
386
          if (angular.isDefined(response.status) && response.status === 204) {
387
            toaster.pop({
388
              type: "success",
389
              title: $translate.instant("TOASTER.SUCCESS_TITLE"),
390
              body: $translate.instant("TOASTER.UNBIND_POINT_SUCCESS"),
391
              showCloseButton: true,
392
            });
393
            let headers = {
394
              "User-UUID": params.user_uuid,
395
              Token: params.token,
396
            };
397
            MicrogridLoadService.getPointsByLoadID(
398
              params.microgridid,
399
              params.microgridload.id,
400
              headers,
401
              function (response) {
402
                if (
403
                  angular.isDefined(response.status) &&
404
                  response.status === 200
405
                ) {
406
                  $scope.boundpoints = response.data;
407
                } else {
408
                  $scope.boundpoints = [];
409
                }
410
              }
411
            );
412
          } else {
413
            toaster.pop({
414
              type: "error",
415
              title: $translate.instant(response.data.title),
416
              body: $translate.instant(response.data.description),
417
              showCloseButton: true,
418
            });
419
          }
420
        }
421
      );
422
    };
423
  }
424
);