myems-admin/app/controllers/settings/microgrid/microgridgrid.controller.js

Summary

Maintainability
F
5 days
Test Coverage
'use strict';

app.controller('MicrogridGridController', function(
    $scope,
    $rootScope,
    $window,
    $translate,
    $uibModal,
    MicrogridService,
    MicrogridGridService,
    PointService,
    MeterService,
    toaster,
    SweetAlert) {
      $scope.microgrids = [];
      $scope.microgridgrids = [];
      $scope.points = [];
      $scope.meters = [];
      $scope.currentMicrogrid = null;
      $scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
      $scope.getAllMicrogrids = function() {
        let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
          MicrogridService.getAllMicrogrids(headers, function (response) {
              if (angular.isDefined(response.status) && response.status === 200) {
                  $scope.microgrids = response.data;
              } else {
                  $scope.microgrids = [];
              }
          });
      };

    $scope.getAllPoints = function() {
        let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
        PointService.getAllPoints(headers, function (response) {
            if (angular.isDefined(response.status) && response.status === 200) {
                $scope.points = response.data;
            } else {
                $scope.points = [];
            }
        });
    };

    $scope.getAllMeters = function() {
        let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
        MeterService.getAllMeters(headers, function (response) {
            if (angular.isDefined(response.status) && response.status === 200) {
                $scope.meters = response.data;
            } else {
                $scope.meters = [];
            }
        });
    };
      $scope.getMicrogridGridsByMicrogridID = function(id) {
        let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
          MicrogridGridService.getMicrogridGridsByMicrogridID(id, headers, function (response) {
            if (angular.isDefined(response.status) && response.status === 200) {
                $scope.microgridgrids = response.data;
            } else {
              $scope.microgridgrids=[];
        }
            });
      };

      $scope.changeMicrogrid=function(item,model){
        $scope.currentMicrogrid=item;
        $scope.currentMicrogrid.selected=model;
        $scope.is_show_add_microgrid_grid = true;
        $scope.getMicrogridGridsByMicrogridID($scope.currentMicrogrid.id);
      };

      $scope.addMicrogridGrid = function() {

          var modalInstance = $uibModal.open({
              templateUrl: 'views/settings/microgrid/microgridgrid.model.html',
              controller: 'ModalAddMicrogridGridCtrl',
              windowClass: "animated fadeIn",
              resolve: {
                  params: function() {
                      return {
                        meters: angular.copy($scope.meters),
                        points: angular.copy($scope.points),
                      };
                  }
              }
          });
          modalInstance.result.then(function(microgridgrid) {
            microgridgrid.power_point_id = microgridgrid.power_point.id;
            microgridgrid.buy_meter_id = microgridgrid.buy_meter.id;
            microgridgrid.sell_meter_id = microgridgrid.sell_meter.id;
            let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
              MicrogridGridService.addMicrogridGrid($scope.currentMicrogrid.id, microgridgrid, headers, function (response) {
                  if (angular.isDefined(response.status) && response.status === 201) {
                      toaster.pop({
                          type: "success",
                          title: $translate.instant("TOASTER.SUCCESS_TITLE"),
                          body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("MICROGRID.MICROGRID_GRID")}),
                          showCloseButton: true,
                      });
                      $scope.getMicrogridGridsByMicrogridID($scope.currentMicrogrid.id);
                    $scope.$emit('handleEmitMicrogridGridChanged');
                  } else {
                      toaster.pop({
                          type: "error",
                          title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("MICROGRID.MICROGRID_GRID")}),
                          body: $translate.instant(response.data.description),
                          showCloseButton: true,
                      });
                  }
              });
          }, function() {

          });
        $rootScope.modalInstance = modalInstance;
      };

      $scope.editMicrogridGrid = function(microgridgrid) {
          var modalInstance = $uibModal.open({
              templateUrl: 'views/settings/microgrid/microgridgrid.model.html',
              controller: 'ModalEditMicrogridGridCtrl',
            windowClass: "animated fadeIn",
              resolve: {
                  params: function() {
                      return {
                          microgridgrid: angular.copy(microgridgrid),
                        meters: angular.copy($scope.meters),
                        points: angular.copy($scope.points),
                      };
                  }
              }
          });

          modalInstance.result.then(function(modifiedMicrogridGrid) {
            modifiedMicrogridGrid.power_point_id = modifiedMicrogridGrid.power_point.id;
            modifiedMicrogridGrid.buy_meter_id = modifiedMicrogridGrid.buy_meter.id;
            modifiedMicrogridGrid.sell_meter_id = modifiedMicrogridGrid.sell_meter.id;
            let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
              MicrogridGridService.editMicrogridGrid($scope.currentMicrogrid.id, modifiedMicrogridGrid, headers, function (response) {
                  if (angular.isDefined(response.status) && response.status === 200) {
                      toaster.pop({
                          type: "success",
                          title: $translate.instant("TOASTER.SUCCESS_TITLE"),
                          body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_GRID")}),
                          showCloseButton: true,
                      });
                      $scope.getMicrogridGridsByMicrogridID($scope.currentMicrogrid.id);
                    $scope.$emit('handleEmitMicrogridGridChanged');
                  } else {
                      toaster.pop({
                          type: "error",
                          title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_GRID")}),
                          body: $translate.instant(response.data.description),
                          showCloseButton: true,
                      });
                  }
              });
          }, function() {
              //do nothing;
          });
        $rootScope.modalInstance = modalInstance;
      };

      $scope.deleteMicrogridGrid = function(microgridgrid) {
          SweetAlert.swal({
                  title: $translate.instant("SWEET.TITLE"),
                  text: $translate.instant("SWEET.TEXT"),
                  type: "warning",
                  showCancelButton: true,
                  confirmButtonColor: "#DD6B55",
                  confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
                  cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
                  closeOnConfirm: true,
                  closeOnCancel: true
              },
              function(isConfirm) {
                  if (isConfirm) {
                    let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
                      MicrogridGridService.deleteMicrogridGrid($scope.currentMicrogrid.id, microgridgrid.id, headers, function (response) {
                          if (angular.isDefined(response.status) && response.status === 204) {
                            toaster.pop({
                                type: "success",
                                title: $translate.instant("TOASTER.SUCCESS_TITLE"),
                                body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_GRID")}),
                                showCloseButton: true,
                            });
                            $scope.getMicrogridGridsByMicrogridID($scope.currentMicrogrid.id);
                            $scope.$emit('handleEmitMicrogridGridChanged');
                          } else {
                            toaster.pop({
                                type: "error",
                                title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_GRID")}),
                                body: $translate.instant(response.data.description),
                                showCloseButton: true,
                            });
                             }
                      });
                  }
              });
      };

      $scope.getAllMicrogrids();
    $scope.getAllPoints();
    $scope.getAllMeters();
    $scope.$on('handleBroadcastMicrogridChanged', function(event) {
      $scope.getAllMicrogrids();
      });

  });


  app.controller('ModalAddMicrogridGridCtrl', function($scope, $uibModalInstance, params) {

      $scope.operation = "MICROGRID.ADD_MICROGRID_GRID";
    $scope.points=params.points;
    $scope.meters=params.meters;
      $scope.ok = function() {
          $uibModalInstance.close($scope.microgridgrid);
      };

      $scope.cancel = function() {
          $uibModalInstance.dismiss('cancel');
      };
  });

  app.controller('ModalEditMicrogridGridCtrl', function($scope, $uibModalInstance, params) {
      $scope.operation = "MICROGRID.EDIT_MICROGRID_GRID";
      $scope.microgridgrid = params.microgridgrid;
    $scope.points=params.points;
    $scope.meters=params.meters;
      $scope.ok = function() {
          $uibModalInstance.close($scope.microgridgrid);
      };

      $scope.cancel = function() {
          $uibModalInstance.dismiss('cancel');
      };
  });