Passed
Push — master ( 8e7a04...f78861 )
by
unknown
11:43 queued 16s
created

myems-admin/app/controllers/settings/hybridpowerstation/hybridpowerstationmcu.controller.js   F

Complexity

Total Complexity 66
Complexity/F 1.38

Size

Lines of Code 399
Function Count 48

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 66
eloc 283
dl 0
loc 399
rs 3.12
c 0
b 0
f 0
mnd 18
bc 18
fnc 48
bpm 0.375
cpm 1.375
noi 2

How to fix   Complexity   

Complexity

Complex classes like myems-admin/app/controllers/settings/hybridpowerstation/hybridpowerstationmcu.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('HybridPowerStationMCUController', function(
4
	$scope,
5
	$rootScope,
6
	$window,
7
	$translate,
8
	$uibModal,
9
	HybridPowerStationService,
10
	HybridPowerStationMCUService,
11
	DataSourceService,
12
	PointService,
13
	MeterService,
14
	toaster,
15
	SweetAlert) {
16
      $scope.hybridpowerstations = [];
17
      $scope.hybridpowerstationmcus = [];
18
	  $scope.points = [];
19
	  $scope.meters = [];
20
      $scope.currentHybridPowerStation = null;
21
	  $scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
22
      $scope.getAllHybridPowerStations = function() {
23
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
24
  		HybridPowerStationService.getAllHybridPowerStations(headers, function (response) {
25
  			if (angular.isDefined(response.status) && response.status === 200) {
26
  				$scope.hybridpowerstations = response.data;
27
  			} else {
28
  				$scope.hybridpowerstations = [];
29
  			}
30
  		});
31
  	};
32
33
	$scope.getAllDataSources = function() {
34
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
35
		DataSourceService.getAllDataSources(headers, function (response) {
36
			if (angular.isDefined(response.status) && response.status === 200) {
37
				$scope.datasources = response.data;
38
				if ($scope.datasources.length > 0) {
39
					$scope.currentDataSource = $scope.datasources[0].id;
40
					$scope.getPointsByDataSourceID($scope.currentDataSource);
41
				}
42
			} else {
43
				$scope.datasources = [];
44
			}
45
		});
46
	};
47
48
	$scope.getAllPoints = function() {
49
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
50
		PointService.getAllPoints(headers, function (response) {
51
			if (angular.isDefined(response.status) && response.status === 200) {
52
				$scope.points = response.data;
53
			} else {
54
				$scope.points = [];
55
			}
56
		});
57
	};
58
59
	$scope.getPointsByDataSourceID = function(id) {
60
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
61
		PointService.getPointsByDataSourceID(id, headers, function (response) {
62
			if (angular.isDefined(response.status) && response.status === 200) {
63
				$scope.points = response.data;
64
			} else {
65
				$scope.points = [];
66
			}
67
		});
68
	};
69
70
	$scope.getAllMeters = function() {
71
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
72
		MeterService.getAllMeters(headers, function (response) {
73
			if (angular.isDefined(response.status) && response.status === 200) {
74
				$scope.meters = response.data;
75
			} else {
76
				$scope.meters = [];
77
			}
78
		});
79
	};
80
  	$scope.getHybridPowerStationMCUsByHybridPowerStationID = function(id) {
81
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
82
  		HybridPowerStationMCUService.getHybridPowerStationMCUsByHybridPowerStationID(id, headers, function (response) {
83
			if (angular.isDefined(response.status) && response.status === 200) {
84
				$scope.hybridpowerstationmcus = response.data;
85
			} else {
86
          	$scope.hybridpowerstationmcus=[];
87
        }
88
			});
89
  	};
90
91
  	$scope.changeHybridPowerStation=function(item,model){
92
    	$scope.currentHybridPowerStation=item;
93
    	$scope.currentHybridPowerStation.selected=model;
94
        $scope.is_show_add_hybridpowerstation_mcu = true;
95
    	$scope.getHybridPowerStationMCUsByHybridPowerStationID($scope.currentHybridPowerStation.id);
96
  	};
97
98
  	$scope.addHybridPowerStationMCU = function() {
99
100
  		var modalInstance = $uibModal.open({
101
  			templateUrl: 'views/settings/hybridpowerstation/hybridpowerstationmcu.model.html',
102
  			controller: 'ModalAddHybridPowerStationMCUCtrl',
103
  			windowClass: "animated fadeIn",
104
  			resolve: {
105
  				params: function() {
106
  					return {
107
						meters: angular.copy($scope.meters),
108
						points: angular.copy($scope.points),
109
  					};
110
  				}
111
  			}
112
  		});
113
  		modalInstance.result.then(function(hybridpowerstationmcu) {
114
			hybridpowerstationmcu.power_point_id = hybridpowerstationmcu.power_point.id;
115
			hybridpowerstationmcu.buy_meter_id = hybridpowerstationmcu.buy_meter.id;
116
			hybridpowerstationmcu.sell_meter_id = hybridpowerstationmcu.sell_meter.id;
117
118
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
119
  			HybridPowerStationMCUService.addHybridPowerStationMCU($scope.currentHybridPowerStation.id, hybridpowerstationmcu, headers, function (response) {
120
  				if (angular.isDefined(response.status) && response.status === 201) {
121
  					toaster.pop({
122
  						type: "success",
123
  						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
124
  						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("HYBRID_POWER_STATION.HYBRID_POWER_STATION_MCU")}),
125
  						showCloseButton: true,
126
  					});
127
  					$scope.getHybridPowerStationMCUsByHybridPowerStationID($scope.currentHybridPowerStation.id);
128
            		$scope.$emit('handleEmitHybridPowerStationMCUChanged');
129
  				} else {
130
  					toaster.pop({
131
  						type: "error",
132
  						title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("HYBRID_POWER_STATION.HYBRID_POWER_STATION_MCU")}),
133
  						body: $translate.instant(response.data.description),
134
  						showCloseButton: true,
135
  					});
136
  				}
137
  			});
138
  		}, function() {
139
140
  		});
141
		$rootScope.modalInstance = modalInstance;
142
  	};
143
144
  	$scope.editHybridPowerStationMCU = function(hybridpowerstationmcu) {
145
  		var modalInstance = $uibModal.open({
146
  			templateUrl: 'views/settings/hybridpowerstation/hybridpowerstationmcu.model.html',
147
  			controller: 'ModalEditHybridPowerStationMCUCtrl',
148
    		windowClass: "animated fadeIn",
149
  			resolve: {
150
  				params: function() {
151
  					return {
152
  						hybridpowerstationmcu: angular.copy(hybridpowerstationmcu),
153
						meters: angular.copy($scope.meters),
154
						points: angular.copy($scope.points),
155
  					};
156
  				}
157
  			}
158
  		});
159
160
  		modalInstance.result.then(function(modifiedHybridPowerStationMCU) {
161
			modifiedHybridPowerStationMCU.power_point_id = modifiedHybridPowerStationMCU.power_point.id;
162
			modifiedHybridPowerStationMCU.buy_meter_id = modifiedHybridPowerStationMCU.buy_meter.id;
163
			modifiedHybridPowerStationMCU.sell_meter_id = modifiedHybridPowerStationMCU.sell_meter.id;
164
165
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
166
  			HybridPowerStationMCUService.editHybridPowerStationMCU($scope.currentHybridPowerStation.id, modifiedHybridPowerStationMCU, headers, function (response) {
167
  				if (angular.isDefined(response.status) && response.status === 200) {
168
  					toaster.pop({
169
  						type: "success",
170
  						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
171
  						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("HYBRID_POWER_STATION.HYBRID_POWER_STATION_MCU")}),
172
  						showCloseButton: true,
173
  					});
174
  					$scope.getHybridPowerStationMCUsByHybridPowerStationID($scope.currentHybridPowerStation.id);
175
            		$scope.$emit('handleEmitHybridPowerStationMCUChanged');
176
  				} else {
177
  					toaster.pop({
178
  						type: "error",
179
  						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("HYBRID_POWER_STATION.HYBRID_POWER_STATION_MCU")}),
180
  						body: $translate.instant(response.data.description),
181
  						showCloseButton: true,
182
  					});
183
  				}
184
  			});
185
  		}, function() {
186
  			//do nothing;
187
  		});
188
		$rootScope.modalInstance = modalInstance;
189
  	};
190
191
	$scope.bindHybridPowerStationMCUPoint = function(hybridpowerstationmcu) {
192
	var modalInstance = $uibModal.open({
193
		templateUrl: 'views/settings/hybridpowerstation/hybridpowerstationmcupoint.model.html',
194
		controller: 'ModalBindHybridPowerStationMCUCtrl',
195
		windowClass: "animated fadeIn",
196
			resolve: {
197
				params: function() {
198
					return {
199
						user_uuid: $scope.cur_user.uuid,
200
						token: $scope.cur_user.token,
201
						hybridpowerstationid: $scope.currentHybridPowerStation.id,
202
						hybridpowerstationmcu: angular.copy(hybridpowerstationmcu),
203
						meters: angular.copy($scope.meters),
204
						datasources: angular.copy($scope.datasources),
205
						points: angular.copy($scope.points),
206
					};
207
				}
208
			}
209
		});
210
		$rootScope.modalInstance = modalInstance;
211
	};
212
  	$scope.deleteHybridPowerStationMCU = function(hybridpowerstationmcu) {
213
  		SweetAlert.swal({
214
  				title: $translate.instant("SWEET.TITLE"),
215
  				text: $translate.instant("SWEET.TEXT"),
216
  				type: "warning",
217
  				showCancelButton: true,
218
  				confirmButtonColor: "#DD6B55",
219
  				confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
220
  				cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
221
  				closeOnConfirm: true,
222
  				closeOnCancel: true
223
  			},
224
  			function(isConfirm) {
225
  				if (isConfirm) {
226
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
227
  					HybridPowerStationMCUService.deleteHybridPowerStationMCU($scope.currentHybridPowerStation.id, hybridpowerstationmcu.id, headers, function (response) {
228
  						if (angular.isDefined(response.status) && response.status === 204) {
229
							toaster.pop({
230
								type: "success",
231
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
232
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("HYBRID_POWER_STATION.HYBRID_POWER_STATION_MCU")}),
233
								showCloseButton: true,
234
							});
235
							$scope.getHybridPowerStationMCUsByHybridPowerStationID($scope.currentHybridPowerStation.id);
236
							$scope.$emit('handleEmitHybridPowerStationMCUChanged');
237
  						} else {
238
							toaster.pop({
239
								type: "error",
240
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("HYBRID_POWER_STATION.HYBRID_POWER_STATION_MCU")}),
241
								body: $translate.instant(response.data.description),
242
								showCloseButton: true,
243
							});
244
  				   		}
245
  					});
246
  				}
247
  			});
248
  	};
249
250
  	$scope.getAllHybridPowerStations();
251
	$scope.getAllDataSources();
252
	$scope.getAllPoints();
253
	$scope.getAllMeters();
254
    $scope.$on('handleBroadcastHybridPowerStationChanged', function(event) {
255
      $scope.getAllHybridPowerStations();
256
  	});
257
258
  });
259
260
261
  app.controller('ModalAddHybridPowerStationMCUCtrl', function($scope, $uibModalInstance, params) {
262
263
  	$scope.operation = "HYBRID_POWER_STATION.ADD_HYBRID_POWER_STATION_MCU";
264
	$scope.points=params.points;
265
	$scope.meters=params.meters;
266
  	$scope.ok = function() {
267
  		$uibModalInstance.close($scope.hybridpowerstationmcu);
268
  	};
269
270
  	$scope.cancel = function() {
271
  		$uibModalInstance.dismiss('cancel');
272
  	};
273
  });
274
275
  app.controller('ModalEditHybridPowerStationMCUCtrl', function($scope, $uibModalInstance, params) {
276
  	$scope.operation = "HYBRID_POWER_STATION.EDIT_HYBRID_POWER_STATION_MCU";
277
  	$scope.hybridpowerstationmcu = params.hybridpowerstationmcu;
278
	$scope.points=params.points;
279
	$scope.meters=params.meters;
280
  	$scope.ok = function() {
281
  		$uibModalInstance.close($scope.hybridpowerstationmcu);
282
  	};
283
284
  	$scope.cancel = function() {
285
  		$uibModalInstance.dismiss('cancel');
286
  	};
287
  });
288
289
  app.controller('ModalBindHybridPowerStationMCUCtrl', function(
290
	$scope,
291
	$uibModalInstance,
292
	toaster,
293
	$translate,
294
	HybridPowerStationMCUService,
295
	PointService,
296
	params) {
297
	$scope.operation = "HYBRID_POWER_STATION.EDIT_HYBRID_POWER_STATION_MCU";
298
	$scope.hybridpowerstationid = params.hybridpowerstationid;
299
	$scope.hybridpowerstationmcu = params.hybridpowerstationmcu;
300
	$scope.datasources=params.datasources;
301
	$scope.boundpoints=params.boundpoints;
302
303
	let headers = { "User-UUID": params.user_uuid, "Token": params.token };
304
	HybridPowerStationMCUService.getPointsByMCUID($scope.hybridpowerstationid, $scope.hybridpowerstationmcu.id, headers, function (response) {
305
		if (angular.isDefined(response.status) && response.status === 200) {
306
			$scope.boundpoints = response.data;
307
		} else {
308
			$scope.boundpoints = [];
309
		}
310
	});
311
312
	$scope.cancel = function() {
313
		$uibModalInstance.dismiss('cancel');
314
	};
315
316
    $scope.changeDataSource = function (item, model) {
317
		console.log('changeDataSource');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
318
        $scope.currentDataSource = model;
319
		console.log($scope.currentDataSource);
320
        $scope.getPointsByDataSourceID($scope.currentDataSource);
321
    };
322
323
    $scope.getPointsByDataSourceID = function(id) {
324
		console.log('getPointsByDataSourceID');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
325
		let headers = { "User-UUID": params.user_uuid, "Token": params.token };
326
        PointService.getPointsByDataSourceID(id, headers, function (response) {
327
            if (angular.isDefined(response.status) && response.status === 200) {
328
                $scope.points = response.data;
329
            } else {
330
                $scope.points = [];
331
            }
332
        });
333
    };
334
335
    $scope.pairPoint = function (dragEl, dropEl) {
336
        var pointid = angular.element('#' + dragEl).scope().point.id;
337
		let headers = { "User-UUID": params.user_uuid, "Token": params.token };
338
        HybridPowerStationMCUService.addPair(params.hybridpowerstationid, params.hybridpowerstationmcu.id, pointid, headers, function (response) {
339
            if (angular.isDefined(response.status) && response.status === 201) {
340
                toaster.pop({
341
                    type: "success",
342
                    title: $translate.instant("TOASTER.SUCCESS_TITLE"),
343
                    body: $translate.instant("TOASTER.BIND_POINT_SUCCESS"),
344
                    showCloseButton: true,
345
                });
346
                let headers = { "User-UUID": params.user_uuid, "Token": params.token };
347
				HybridPowerStationMCUService.getPointsByMCUID(params.hybridpowerstationid, params.hybridpowerstationmcu.id, headers, function (response) {
348
					if (angular.isDefined(response.status) && response.status === 200) {
349
						$scope.boundpoints = response.data;
350
					} else {
351
						$scope.boundpoints = [];
352
					}
353
				});
354
            } else {
355
                toaster.pop({
356
                    type: "error",
357
                    title: $translate.instant(response.data.title),
358
                    body: $translate.instant(response.data.description),
359
                    showCloseButton: true,
360
                });
361
            }
362
        });
363
    };
364
365
    $scope.deletePointPair = function (dragEl, dropEl) {
366
        if (angular.element('#' + dragEl).hasClass('source')) {
367
            return;
368
        }
369
370
		var pointid  = angular.element('#' + dragEl).scope().boundpoint.id;
371
		let headers = { "User-UUID": params.user_uuid, "Token": params.token };
372
        HybridPowerStationMCUService.deletePair(params.hybridpowerstationid, params.hybridpowerstationmcu.id, pointid, headers, function (response) {
373
            if (angular.isDefined(response.status) && response.status === 204) {
374
                toaster.pop({
375
                    type: "success",
376
                    title: $translate.instant("TOASTER.SUCCESS_TITLE"),
377
                    body: $translate.instant("TOASTER.UNBIND_POINT_SUCCESS"),
378
                    showCloseButton: true,
379
                });
380
                let headers = { "User-UUID": params.user_uuid, "Token": params.token };
381
				HybridPowerStationMCUService.getPointsByMCUID(params.hybridpowerstationid, params.hybridpowerstationmcu.id, headers, function (response) {
382
					if (angular.isDefined(response.status) && response.status === 200) {
383
						$scope.boundpoints = response.data;
384
					} else {
385
						$scope.boundpoints = [];
386
					}
387
				});
388
            } else {
389
                toaster.pop({
390
                    type: "error",
391
                    title: $translate.instant(response.data.title),
392
                    body: $translate.instant(response.data.description),
393
                    showCloseButton: true,
394
                });
395
            }
396
        });
397
    };
398
399
  });
400