const ngapp = angular.module("popup", []); const id = window.location.search.split('=')[1].split('&')[0]; const ip = window.location.search.split('=')[2]; const SocketRequest = payload => { return new Promise((resolve, reject) => { ws = new WebSocket(ip); ws.onmessage = msg => resolve(JSON.parse(msg.data)); ws.onopen = () => ws.send(JSON.stringify(payload)); ws.onerror = () => reject(); }); } const ctrl = ngapp.controller("popup-content", async ($scope) => { $scope.done = false; const data = (await SocketRequest({ type: "getEssentials", uuid: new Date().toISOString() })).data; $scope.mp = data.mountingPoints.find(m => m.id == id); $scope.assets = (await SocketRequest({ type: "getAssets", uuid: new Date().toISOString(), data: { assetTypeId: $scope.mp.supportedAssetTypeIds } })).data; $scope.selectedAsset = $scope.mp.assetId; $scope.moreInfo = false; $scope.mounted = (value, _index, _array) => { if (data.mountingPoints.filter(m => m.id != id).map(m => m.assetId).includes(value.id)) return false; else return true; }; $scope.isBuildingSensorModule = $scope.mp.supportedAssetTypeIds.includes(data.assetTypes.find(a => a.designation == "BuildingSensorModule").id); $scope.isMounted = $scope.mp.assetId != -1; $scope.flash = () => { //#region Animation let btn = document.getElementById("btn-flash"); btn.disabled = true; btn.style.animation = "flash ease-in-out 0.25s"; setTimeout(() => { btn.style.animation = "none"; btn.disabled = false; }, 250); //#endregion //TODO: flash bsm console.log("Flash!"); }; async function getIp() { try { $scope.ip = (await SocketRequest({ type: "getAssetState", uuid: new Date().toISOString(), data: { assetId: $scope.mp.assetId }})).data.ipAddr; } catch { $scope.ip = ""; } } $scope.mount = () => { $scope.done = false; if ($scope.mp.assetId == -1) { // not mounted -> mount SocketRequest({ type: "mount", uuid: new Date().toISOString(), data: { mountingPointId: +$scope.mp.id, assetId: +$scope.selectedAsset }}).then( res => { if (res.type !== "ok") { console.error(res); return; } window.parent.postMessage({ type: "mount", mpId: $scope.mp.id, aId: $scope.selectedAsset }, "*"); $scope.mp.assetId = $scope.selectedAsset; $scope.isBuildingSensorModule = $scope.mp.supportedAssetTypeIds.includes(data.assetTypes.find(a => a.designation == "BuildingSensorModule").id); $scope.isMounted = $scope.mp.assetId != -1; getIp().then(() => { $scope.done = true; $scope.$apply(); }); } ); } else { // mounted -> unmount SocketRequest({ type: "unmountMountingPoint", uuid: new Date().toISOString(), data: { mountingPointId: +$scope.mp.id }}).then( res => { if (res.type !== "ok") { console.error(res); return; } window.parent.postMessage({ type: "unmount", mpId: $scope.mp.id }, "*"); $scope.mp.assetId = -1; $scope.isBuildingSensorModule = $scope.mp.supportedAssetTypeIds.includes(data.assetTypes.find(a => a.designation == "BuildingSensorModule").id); $scope.isMounted = $scope.mp.assetId != -1; getIp().then(() => { $scope.done = true; $scope.$apply(); }); } ); } }; getIp().then(() => { $scope.done = true; $scope.$apply(); }); });