Passed
Push — master ( 46ddd0...0c924f )
by
unknown
12:05 queued 14s
created

myems-admin/app/controllers/settings/photovoltaicpowerstation/photovoltaicpowerstationdatasource.controller.js   A

Complexity

Total Complexity 20
Complexity/F 1.43

Size

Lines of Code 171
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 20
eloc 121
dl 0
loc 171
rs 10
c 0
b 0
f 0
mnd 6
bc 6
fnc 14
bpm 0.4285
cpm 1.4285
noi 0
1
"use strict";
2
3
app.controller(
4
  "PhotovoltaicPowerStationDataSourceController",
5
  function (
6
    $scope,
7
    $window,
8
    $timeout,
9
    $translate,
10
    PhotovoltaicPowerStationService,
11
    DataSourceService,
12
    PhotovoltaicPowerStationDataSourceService,
13
    PointService,
14
    toaster
15
  ) {
16
    $scope.cur_user = JSON.parse(
17
      $window.localStorage.getItem("myems_admin_ui_current_user")
18
    );
19
    $scope.currentPhotovoltaicPowerStation = { selected: undefined };
20
21
    $scope.getAllDataSources = function () {
22
      let headers = {
23
        "User-UUID": $scope.cur_user.uuid,
24
        Token: $scope.cur_user.token,
25
        "API-KEY": $scope.cur_user.api_key
26
      };
27
      DataSourceService.getAllDataSources(headers, function (response) {
28
        if (angular.isDefined(response.status) && response.status === 200) {
29
          $scope.datasources = response.data;
30
        } else {
31
          $scope.datasources = [];
32
        }
33
      });
34
    };
35
36
    $scope.getDataSourcesByPhotovoltaicPowerStationID = function (id) {
37
      let headers = {
38
        "User-UUID": $scope.cur_user.uuid,
39
        Token: $scope.cur_user.token,
40
        "API-KEY": $scope.cur_user.api_key
41
      };
42
      PhotovoltaicPowerStationDataSourceService.getDataSourcesByPhotovoltaicPowerStationID(
43
        id,
44
        headers,
45
        function (response) {
46
          if (angular.isDefined(response.status) && response.status === 200) {
47
            $scope.photovoltaicpowerstationdatasources = response.data;
48
          } else {
49
            $scope.photovoltaicpowerstationdatasources = [];
50
          }
51
        }
52
      );
53
    };
54
55
    $scope.changePhotovoltaicPowerStation = function (item, model) {
56
      $scope.currentPhotovoltaicPowerStation = item;
57
      $scope.currentPhotovoltaicPowerStation.selected = model;
58
      $scope.getDataSourcesByPhotovoltaicPowerStationID(
59
        $scope.currentPhotovoltaicPowerStation.id
60
      );
61
    };
62
63
    $scope.getAllPhotovoltaicPowerStations = function () {
64
      let headers = {
65
        "User-UUID": $scope.cur_user.uuid,
66
        Token: $scope.cur_user.token,
67
        "API-KEY": $scope.cur_user.api_key
68
      };
69
      PhotovoltaicPowerStationService.getAllPhotovoltaicPowerStations(
70
        headers,
71
        function (response) {
72
          if (angular.isDefined(response.status) && response.status === 200) {
73
            $scope.photovoltaicpowerstations = response.data;
74
            $timeout(function () {
75
              $scope.getDataSourcesByPhotovoltaicPowerStationID(
76
                $scope.currentPhotovoltaicPowerStation.id
77
              );
78
            }, 1000);
79
          } else {
80
            $scope.photovoltaicpowerstations = [];
81
          }
82
        }
83
      );
84
    };
85
86
    $scope.pairDataSource = function (dragEl, dropEl) {
87
      var datasourceid = angular.element("#" + dragEl).scope().datasource.id;
88
      var photovoltaicpowerstationid = $scope.currentPhotovoltaicPowerStation.id;
89
      let headers = {
90
        "User-UUID": $scope.cur_user.uuid,
91
        Token: $scope.cur_user.token,
92
        "API-KEY": $scope.cur_user.api_key
93
      };
94
      PhotovoltaicPowerStationDataSourceService.addPair(
95
        photovoltaicpowerstationid,
96
        datasourceid,
97
        headers,
98
        function (response) {
99
          if (angular.isDefined(response.status) && response.status === 201) {
100
            toaster.pop({
101
              type: "success",
102
              title: $translate.instant("TOASTER.SUCCESS_TITLE"),
103
              body: $translate.instant("TOASTER.BIND_DATASOURCE_SUCCESS"),
104
              showCloseButton: true,
105
            });
106
            $scope.getDataSourcesByPhotovoltaicPowerStationID(
107
              $scope.currentPhotovoltaicPowerStation.id
108
            );
109
          } else {
110
            toaster.pop({
111
              type: "error",
112
              title: $translate.instant(response.data.title),
113
              body: $translate.instant(response.data.description),
114
              showCloseButton: true,
115
            });
116
          }
117
        }
118
      );
119
    };
120
121
    $scope.deleteDataSourcePair = function (dragEl, dropEl) {
122
      if (angular.element("#" + dragEl).hasClass("source")) {
123
        return;
124
      }
125
      var datasourceid = angular
126
        .element("#" + dragEl)
127
        .scope().photovoltaicpowerstationdatasource.id;
128
      var photovoltaicpowerstationid = $scope.currentPhotovoltaicPowerStation.id;
129
      let headers = {
130
        "User-UUID": $scope.cur_user.uuid,
131
        Token: $scope.cur_user.token,
132
        "API-KEY": $scope.cur_user.api_key
133
      };
134
      PhotovoltaicPowerStationDataSourceService.deletePair(
135
        photovoltaicpowerstationid,
136
        datasourceid,
137
        headers,
138
        function (response) {
139
          if (angular.isDefined(response.status) && response.status === 204) {
140
            toaster.pop({
141
              type: "success",
142
              title: $translate.instant("TOASTER.SUCCESS_TITLE"),
143
              body: $translate.instant("TOASTER.UNBIND_DATASOURCE_SUCCESS"),
144
              showCloseButton: true,
145
            });
146
            $scope.getDataSourcesByPhotovoltaicPowerStationID(
147
              $scope.currentPhotovoltaicPowerStation.id
148
            );
149
          } else {
150
            toaster.pop({
151
              type: "error",
152
              title: $translate.instant(response.data.title),
153
              body: $translate.instant(response.data.description),
154
              showCloseButton: true,
155
            });
156
          }
157
        }
158
      );
159
    };
160
161
    $scope.getAllDataSources();
162
    $scope.getAllPhotovoltaicPowerStations();
163
164
    $scope.$on(
165
      "handleBroadcastPhotovoltaicPowerStationChanged",
166
      function (event) {
167
        $scope.getAllPhotovoltaicPowerStations();
168
      }
169
    );
170
  }
171
);
172