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

Summary

Maintainability
F
2 wks
Test Coverage
'use strict';

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

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

      $scope.addMicrogridEVCharger = function() {
          var modalInstance = $uibModal.open({
              templateUrl: 'views/settings/microgrid/microgridevcharger.model.html',
              controller: 'ModalAddMicrogridEVChargerCtrl',
              windowClass: "animated fadeIn",
              resolve: {
                  params: function() {
                      return {
                        meters: angular.copy($scope.meters),
                        points: angular.copy($scope.points),
                      };
                  }
              }
          });
          modalInstance.result.then(function(microgridevcharger) {
            microgridevcharger.power_point_id = microgridevcharger.power_point.id;
            microgridevcharger.meter_id = microgridevcharger.meter.id;
            let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
              MicrogridEVChargerService.addMicrogridEVCharger($scope.currentMicrogrid.id, microgridevcharger, 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_EVCHARGER")}),
                          showCloseButton: true,
                      });
                      $scope.getMicrogridEVChargersByMicrogridID($scope.currentMicrogrid.id);
                    $scope.$emit('handleEmitMicrogridEVChargerChanged');
                  } else {
                      toaster.pop({
                          type: "error",
                          title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("MICROGRID.MICROGRID_EVCHARGER")}),
                          body: $translate.instant(response.data.description),
                          showCloseButton: true,
                      });
                  }
              });
          }, function() {

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

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

          modalInstance.result.then(function(modifiedMicrogridEVCharger) {
            modifiedMicrogridEVCharger.power_point_id = modifiedMicrogridEVCharger.power_point.id;
            modifiedMicrogridEVCharger.meter_id = modifiedMicrogridEVCharger.meter.id;
            let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
              MicrogridEVChargerService.editMicrogridEVCharger($scope.currentMicrogrid.id, modifiedMicrogridEVCharger, 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_EVCHARGER")}),
                          showCloseButton: true,
                      });
                      $scope.getMicrogridEVChargersByMicrogridID($scope.currentMicrogrid.id);
                    $scope.$emit('handleEmitMicrogridEVChargerChanged');
                  } else {
                      toaster.pop({
                          type: "error",
                          title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_EVCHARGER")}),
                          body: $translate.instant(response.data.description),
                          showCloseButton: true,
                      });
                  }
              });
          }, function() {
              //do nothing;
          });
        $rootScope.modalInstance = modalInstance;
      };

      $scope.deleteMicrogridEVCharger = function(microgridevcharger) {
          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 };
                      MicrogridEVChargerService.deleteMicrogridEVCharger($scope.currentMicrogrid.id, microgridevcharger.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_EVCHARGER")}),
                                showCloseButton: true,
                            });
                            $scope.getMicrogridEVChargersByMicrogridID($scope.currentMicrogrid.id);
                            $scope.$emit('handleEmitMicrogridEVChargerChanged');
                          } else {
                            toaster.pop({
                                type: "error",
                                title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("MICROGRID.MICROGRID_EVCHARGER")}),
                                body: $translate.instant(response.data.description),
                                showCloseButton: true,
                            });
                             }
                      });
                  }
              });
      };

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

  });


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

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

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

  app.controller('ModalEditMicrogridEVChargerCtrl', function($scope, $uibModalInstance, params) {
      $scope.operation = "MICROGRID.EDIT_MICROGRID_EVCHARGER";
      $scope.microgridevcharger = params.microgridevcharger;
    $scope.points=params.points;
    $scope.meters=params.meters;
      $scope.ok = function() {
          $uibModalInstance.close($scope.microgridevcharger);
      };

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