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

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

Complexity

Total Complexity 66
Complexity/F 1.38

Size

Lines of Code 402
Function Count 48

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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