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

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

Complexity

Total Complexity 69
Complexity/F 1.38

Size

Lines of Code 418
Function Count 50

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 69
eloc 297
dl 0
loc 418
rs 2.88
c 0
b 0
f 0
mnd 19
bc 19
fnc 50
bpm 0.38
cpm 1.3798
noi 2

How to fix   Complexity   

Complexity

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