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

Summary

Maintainability
F
6 days
Test Coverage
'use strict';

app.controller('MicrogridBatteryController', function(
    $scope,
    $rootScope,
    $window,
    $translate,
    $uibModal,
    MicrogridService,
    MicrogridBatteryService,
    PointService,
    MeterService,
    toaster,
    SweetAlert) {
      $scope.microgrids = [];
      $scope.microgridbatteries = [];
      $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.getMicrogridBatteriesByMicrogridID = function(id) {
        let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
          MicrogridBatteryService.getMicrogridBatteriesByMicrogridID(id, headers, function (response) {
            if (angular.isDefined(response.status) && response.status === 200) {
                $scope.microgridbatteries = response.data;
            } else {
              $scope.microgridbatteries=[];
        }
            });
      };

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

      $scope.addMicrogridBattery = function() {

          var modalInstance = $uibModal.open({
              templateUrl: 'views/settings/microgrid/microgridbattery.model.html',
              controller: 'ModalAddMicrogridBatteryCtrl',
              windowClass: "animated fadeIn",
              resolve: {
                  params: function() {
                      return {
                        meters: angular.copy($scope.meters),
                        points: angular.copy($scope.points),
                      };
                  }
              }
          });
          modalInstance.result.then(function(microgridbattery) {
            microgridbattery.battery_state_point_id = microgridbattery.battery_state_point.id;
            microgridbattery.soc_point_id = microgridbattery.soc_point.id;
            microgridbattery.power_point_id = microgridbattery.power_point.id;
            microgridbattery.charge_meter_id = microgridbattery.charge_meter.id;
            microgridbattery.discharge_meter_id = microgridbattery.discharge_meter.id;
            let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
              MicrogridBatteryService.addMicrogridBattery($scope.currentMicrogrid.id, microgridbattery, 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_BATTERY")}),
                          showCloseButton: true,
                      });
                      $scope.getMicrogridBatteriesByMicrogridID($scope.currentMicrogrid.id);
                    $scope.$emit('handleEmitMicrogridBatteryChanged');
                  } else {
                      toaster.pop({
                          type: "error",
                          title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("MICROGRID.MICROGRID_BATTERY")}),
                          body: $translate.instant(response.data.description),
                          showCloseButton: true,
                      });
                  }
              });
          }, function() {

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

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

          modalInstance.result.then(function(modifiedMicrogridBattery) {
            modifiedMicrogridBattery.battery_state_point_id = modifiedMicrogridBattery.battery_state_point.id;
            modifiedMicrogridBattery.soc_point_id = modifiedMicrogridBattery.soc_point.id;
            modifiedMicrogridBattery.power_point_id = modifiedMicrogridBattery.power_point.id;
            modifiedMicrogridBattery.charge_meter_id = modifiedMicrogridBattery.charge_meter.id;
            modifiedMicrogridBattery.discharge_meter_id = modifiedMicrogridBattery.discharge_meter.id;
            let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
              MicrogridBatteryService.editMicrogridBattery($scope.currentMicrogrid.id, modifiedMicrogridBattery, 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_BATTERY")}),
                          showCloseButton: true,
                      });
                      $scope.getMicrogridBatteriesByMicrogridID($scope.currentMicrogrid.id);
                    $scope.$emit('handleEmitMicrogridBatteryChanged');
                  } else {
                      toaster.pop({
                          type: "error",
                          title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_BATTERY")}),
                          body: $translate.instant(response.data.description),
                          showCloseButton: true,
                      });
                  }
              });
          }, function() {
              //do nothing;
          });
        $rootScope.modalInstance = modalInstance;
      };

      $scope.deleteMicrogridBattery = function(microgridbattery) {
          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 };
                      MicrogridBatteryService.deleteMicrogridBattery($scope.currentMicrogrid.id, microgridbattery.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_BATTERY")}),
                                showCloseButton: true,
                            });
                            $scope.getMicrogridBatteriesByMicrogridID($scope.currentMicrogrid.id);
                            $scope.$emit('handleEmitMicrogridBatteryChanged');
                          } else {
                            toaster.pop({
                                type: "error",
                                title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_BATTERY")}),
                                body: $translate.instant(response.data.description),
                                showCloseButton: true,
                            });
                             }
                      });
                  }
              });
      };

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

  });


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

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

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

  app.controller('ModalEditMicrogridBatteryCtrl', function($scope, $uibModalInstance, params) {
      $scope.operation = "MICROGRID.EDIT_MICROGRID_BATTERY";
      $scope.microgridbattery = params.microgridbattery;
    $scope.points=params.points;
    $scope.meters=params.meters;
      $scope.ok = function() {
          $uibModalInstance.close($scope.microgridbattery);
      };

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