summaryrefslogtreecommitdiffstats
path: root/sdnr/wireless-transport/code-Carbon-SR1/ux/mwtnInventory/mwtnInventory-module/src/main/resources/mwtnInventory/mwtnInventory.controller.ts
blob: f4cb3e86a4a2462b34b91e7f066d6ad9af3494e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
declare var angular: angular.IAngularStatic; 

import { InventoryService } from "./mwtnInventory.service"; 
import "./mwtnInventory.service";
import "./components/equipment";
import "./components/equipmentGroup";

const mwtnInventory = angular.module('app.mwtnInventory');

interface IMwtnInventoryScope extends ng.IScope {
  message: string,
  equipments: {}[];
  loading: boolean;
  activeMountPoints: string[];
  selectedMountPoint: string;
}

class MwtnInventoryCtrl {
  constructor(private $rootScope, private $scope: IMwtnInventoryScope, private $state, private $timeout, private mwtnInventoryService: InventoryService) {
    // todo: change this
    $rootScope.section_logo = 'src/app/mwtnInventory/images/mwtnInventory.png';

    $scope.loading = false;
    $scope.message = "Empty";
    $scope.equipments = [];
    $scope.selectedMountPoint = null;

    $scope.activeMountPoints = [];

    const getAllChildEquipments = async (equipmentsRootId: string, equimentIds: string[]) => {
      if (!equimentIds || !equimentIds.length) {
        return [];
      }
      const equipmentObjects = (await Promise.all(equimentIds.map(id => {
        return mwtnInventoryService.getEquipmentDetails(equipmentsRootId, id);
      }))).map(eq => (eq["equipment"][0]));

      const equipmentConditionals = (await Promise.all(equimentIds.map(id => {
        return mwtnInventoryService.getEquipmentConditionals(equipmentsRootId, id);
      }))).map(eq => (eq["equipment-pac"][0]));
      
      let results = await Promise.all(equipmentObjects.map(eq => {
        let fruNames: string[] = (eq["contained-holder"] || []).map(ch => ch["occupying-fru"]).filter(fru => !!fru);
        return getAllChildEquipments(equipmentsRootId, fruNames);
      }));

      return equipmentObjects.reduce((acc, cur, ind, arr) => {
        let conditional = equipmentConditionals[ind] || null;
        // ensure ENVERY property can be null or undefined
        let manufacturedThing = cur['manufactured-thing'];
        let equipmentType = manufacturedThing && manufacturedThing['equipment-type'];
        let manufacturerProperties = manufacturedThing && manufacturedThing['manufacturer-properties'];
        let equipmentInstance = manufacturedThing && manufacturedThing['equipment-instance'];

        let card = {
          name: cur.name,
          label: cur.label,
          uuid: cur.uuid,
          manufacturer: {
            version: equipmentType && equipmentType["version"],
            description: equipmentType && equipmentType["description"],
            partTypeIdentifier: equipmentType && equipmentType["part-type-identifier"],
            modelIdentifier: equipmentType && equipmentType["model-identifier"],
            partTypeIdetypeNamentifier: equipmentType && equipmentType["type-name"],
            id: manufacturerProperties && manufacturerProperties['manufacturer-identifier'],
            date: equipmentInstance && equipmentInstance['manufacture-date'] && Date.parse(equipmentInstance && equipmentInstance['manufacture-date']),
            serial: equipmentInstance && equipmentInstance['serial-number']
          },
          conditional: conditional
        };
        (results[ind].length ? card['children'] = results[ind] : null);
        acc.push(card);
        return acc;
      }, []);
    }

    const pleaseSelect = "Please select a mount point";

    const refresh = async (equipmentsRootId: string) => {
      let rootIdentifiers = await mwtnInventoryService.getRootIdentifiers(equipmentsRootId);
      let equipments = rootIdentifiers && await getAllChildEquipments(equipmentsRootId, rootIdentifiers);
      $timeout(() => {
        $scope.equipments = equipments;
      });
    };

    mwtnInventoryService.getConnectedMountpoints().then(res => {
      $scope.activeMountPoints = [pleaseSelect, ...res] ;
      $scope.selectedMountPoint = $scope.selectedMountPoint || pleaseSelect;
    });

    $scope.$watch(() => ($state.params.nodeId), (newVal: string, oldVal: string) => {
      $scope.selectedMountPoint = newVal;
    });

    $scope.$watch("selectedMountPoint", async (newVal: string, oldVal: string) => {
      if (!newVal || newVal === pleaseSelect) {
        $scope.equipments = [];
        return;
      }
      if ($scope.activeMountPoints[0] === pleaseSelect) {
        [, ...$scope.activeMountPoints] = $scope.activeMountPoints;
      } 
      $scope.loading = true;
      if ($state.params.nodeId !== newVal) {
        $state.go('main.mwtnInventory', { nodeId: newVal }, { notify: false });
      }
      await refresh(newVal).catch(err => {
        $timeout(() => { $scope.equipments = null; });
      });
      $timeout(() => { $scope.loading = false; });
    });
  }
}

mwtnInventory.controller('mwtnInventoryCtrl', ['$rootScope', '$scope', '$state', '$timeout', 'mwtnInventoryService', MwtnInventoryCtrl]);