f4cb3e86a4a2462b34b91e7f066d6ad9af3494e5
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / mwtnInventory / mwtnInventory-module / src / main / resources / mwtnInventory / mwtnInventory.controller.ts
1 declare var angular: angular.IAngularStatic; 
2
3 import { InventoryService } from "./mwtnInventory.service"; 
4 import "./mwtnInventory.service";
5 import "./components/equipment";
6 import "./components/equipmentGroup";
7
8 const mwtnInventory = angular.module('app.mwtnInventory');
9
10 interface IMwtnInventoryScope extends ng.IScope {
11   message: string,
12   equipments: {}[];
13   loading: boolean;
14   activeMountPoints: string[];
15   selectedMountPoint: string;
16 }
17
18 class MwtnInventoryCtrl {
19   constructor(private $rootScope, private $scope: IMwtnInventoryScope, private $state, private $timeout, private mwtnInventoryService: InventoryService) {
20     // todo: change this
21     $rootScope.section_logo = 'src/app/mwtnInventory/images/mwtnInventory.png';
22
23     $scope.loading = false;
24     $scope.message = "Empty";
25     $scope.equipments = [];
26     $scope.selectedMountPoint = null;
27
28     $scope.activeMountPoints = [];
29
30     const getAllChildEquipments = async (equipmentsRootId: string, equimentIds: string[]) => {
31       if (!equimentIds || !equimentIds.length) {
32         return [];
33       }
34       const equipmentObjects = (await Promise.all(equimentIds.map(id => {
35         return mwtnInventoryService.getEquipmentDetails(equipmentsRootId, id);
36       }))).map(eq => (eq["equipment"][0]));
37
38       const equipmentConditionals = (await Promise.all(equimentIds.map(id => {
39         return mwtnInventoryService.getEquipmentConditionals(equipmentsRootId, id);
40       }))).map(eq => (eq["equipment-pac"][0]));
41       
42       let results = await Promise.all(equipmentObjects.map(eq => {
43         let fruNames: string[] = (eq["contained-holder"] || []).map(ch => ch["occupying-fru"]).filter(fru => !!fru);
44         return getAllChildEquipments(equipmentsRootId, fruNames);
45       }));
46
47       return equipmentObjects.reduce((acc, cur, ind, arr) => {
48         let conditional = equipmentConditionals[ind] || null;
49         // ensure ENVERY property can be null or undefined
50         let manufacturedThing = cur['manufactured-thing'];
51         let equipmentType = manufacturedThing && manufacturedThing['equipment-type'];
52         let manufacturerProperties = manufacturedThing && manufacturedThing['manufacturer-properties'];
53         let equipmentInstance = manufacturedThing && manufacturedThing['equipment-instance'];
54
55         let card = {
56           name: cur.name,
57           label: cur.label,
58           uuid: cur.uuid,
59           manufacturer: {
60             version: equipmentType && equipmentType["version"],
61             description: equipmentType && equipmentType["description"],
62             partTypeIdentifier: equipmentType && equipmentType["part-type-identifier"],
63             modelIdentifier: equipmentType && equipmentType["model-identifier"],
64             partTypeIdetypeNamentifier: equipmentType && equipmentType["type-name"],
65             id: manufacturerProperties && manufacturerProperties['manufacturer-identifier'],
66             date: equipmentInstance && equipmentInstance['manufacture-date'] && Date.parse(equipmentInstance && equipmentInstance['manufacture-date']),
67             serial: equipmentInstance && equipmentInstance['serial-number']
68           },
69           conditional: conditional
70         };
71         (results[ind].length ? card['children'] = results[ind] : null);
72         acc.push(card);
73         return acc;
74       }, []);
75     }
76
77     const pleaseSelect = "Please select a mount point";
78
79     const refresh = async (equipmentsRootId: string) => {
80       let rootIdentifiers = await mwtnInventoryService.getRootIdentifiers(equipmentsRootId);
81       let equipments = rootIdentifiers && await getAllChildEquipments(equipmentsRootId, rootIdentifiers);
82       $timeout(() => {
83         $scope.equipments = equipments;
84       });
85     };
86
87     mwtnInventoryService.getConnectedMountpoints().then(res => {
88       $scope.activeMountPoints = [pleaseSelect, ...res] ;
89       $scope.selectedMountPoint = $scope.selectedMountPoint || pleaseSelect;
90     });
91
92     $scope.$watch(() => ($state.params.nodeId), (newVal: string, oldVal: string) => {
93       $scope.selectedMountPoint = newVal;
94     });
95
96     $scope.$watch("selectedMountPoint", async (newVal: string, oldVal: string) => {
97       if (!newVal || newVal === pleaseSelect) {
98         $scope.equipments = [];
99         return;
100       }
101       if ($scope.activeMountPoints[0] === pleaseSelect) {
102         [, ...$scope.activeMountPoints] = $scope.activeMountPoints;
103       } 
104       $scope.loading = true;
105       if ($state.params.nodeId !== newVal) {
106         $state.go('main.mwtnInventory', { nodeId: newVal }, { notify: false });
107       }
108       await refresh(newVal).catch(err => {
109         $timeout(() => { $scope.equipments = null; });
110       });
111       $timeout(() => { $scope.loading = false; });
112     });
113   }
114 }
115
116 mwtnInventory.controller('mwtnInventoryCtrl', ['$rootScope', '$scope', '$state', '$timeout', 'mwtnInventoryService', MwtnInventoryCtrl]);