From a7f0291d3b3827890754c23d69d94bef1c6d466f Mon Sep 17 00:00:00 2001
From: Oleksandr Husak <oleksandr.husak@mailbox.tu-dresden.de>
Date: Tue, 19 Apr 2022 12:22:56 +0200
Subject: [PATCH] remove old points, fix consistency between map and table

---
 src/app/components/map/map.component.ts     | 19 +++++++++----------
 src/app/components/table/table.component.ts | 13 ++++++++++---
 src/app/model/base-model.ts                 | 20 +++++++++-----------
 3 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/src/app/components/map/map.component.ts b/src/app/components/map/map.component.ts
index 396395d..ff0f43a 100644
--- a/src/app/components/map/map.component.ts
+++ b/src/app/components/map/map.component.ts
@@ -73,19 +73,18 @@ export class MapComponent implements OnInit {
   registerPoint(key: string, desc: { [key: string]: any }) {
     // --- Leyers for markers
     if (key in this.markOverlays) {
-      console.log(this.markOverlays)
-      // this.markOverlays[key].clearLayers();
+      this.markOverlays[key].clearLayers();
 
 
-      var myIconReplc = L.Icon.extend({
-        options: {}
-      });
+      // var myIconReplc = L.Icon.extend({
+      //   options: {}
+      // });
 
-      this.markOverlays[key].eachLayer(layer => {
-        if (!(layer instanceof L.Circle)) {
-          this.map.removeLayer(layer)
-        }
-      });
+      // this.markOverlays[key].eachLayer(layer => {
+      //   if (!(layer instanceof L.Circle)) {
+      //     this.map.removeLayer(layer)
+      //   }
+      // });
 
 
 
diff --git a/src/app/components/table/table.component.ts b/src/app/components/table/table.component.ts
index 0fa9088..72a9dad 100644
--- a/src/app/components/table/table.component.ts
+++ b/src/app/components/table/table.component.ts
@@ -1,5 +1,5 @@
 import { Component, OnInit, ViewChild } from '@angular/core';
-import {Agent, PositionUpdate} from 'src/app/model/base-model';
+import { isAgent, PositionUpdate } from 'src/app/model/base-model';
 
 import { IMqttMessage, MqttService } from 'ngx-mqtt';
 import { Subscription } from 'rxjs';
@@ -38,15 +38,22 @@ export class TableComponent implements OnInit {
     this.subsPosition = this._mqttService.observe('ipos/client/position').subscribe((message: IMqttMessage) => {
       try {
         let upd: PositionUpdate = JSON.parse(message.payload.toString())
-        // upd.objects.forEach(obj => validateTypeObject(obj));
-        this.dataSource.push(...upd.objects);
 
+        // 1. Validate
+        let oldAgentNames = [...new Set(this.dataSource.map((agent: any) => agent.id))]
+        let validAgents = upd.objects.filter((agent: any) => {
+          return isAgent(agent) || oldAgentNames.includes(agent.id)
+        });
+
+        // 2. Extract
+        this.dataSource.push(...validAgents); 
         let agentNames = [...new Set(this.dataSource.map((agent: any) => agent.id))]
         this.dataSource.forEach((agent: any)=> {
           let idx = agentNames.indexOf(agent.id) + 1
           agent.color = getMarkerConfig(idx).color
         })
 
+        // 3. Update table
         this.table?.renderRows();
       } catch(e) {
           console.log(e)
diff --git a/src/app/model/base-model.ts b/src/app/model/base-model.ts
index 32c8dc7..30864c0 100644
--- a/src/app/model/base-model.ts
+++ b/src/app/model/base-model.ts
@@ -14,6 +14,7 @@ export interface Agent {
     lastPosUpdate: string
     zoneDescriptors: ZoneDesc[]
     extractedAttributes?: ExtAttribute
+    [x: string]: any
 }
 
 export interface Position {
@@ -87,27 +88,24 @@ export function validateTypeAgent(obj: any) {
 // --- Type Guards
 
 function isWGS84(obj: any): obj is WGS84 {
-    return Object.prototype.hasOwnProperty.call(obj, "latitude")
-        && Object.prototype.hasOwnProperty.call(obj, "longitude")
+    return obj
+        && "latitude" in obj
+        && "longitude" in obj
 } 
 
 function isRelativePos(obj: any): obj is RelativePos {
-    return Object.prototype.hasOwnProperty.call(obj, "x")
-        && Object.prototype.hasOwnProperty.call(obj, "y")
+    return obj
+        && "x" in obj 
+        && "y" in obj
 } 
 
 export function isPosition(obj: any): obj is Position {
     return isWGS84(obj.point) || isRelativePos(obj.point)
-    && Object.prototype.hasOwnProperty.call(obj, "refSystemId")
 } 
 
 export function isAgent(obj: any): obj is Agent {
-    return "id" in obj
-        && "type" in obj
-        && "sensorId" in obj
-        && "sensorType" in obj
+    return obj
+        && "id" in obj
         && "position" in obj
         && isPosition(obj.position)
-        && "lastPosUpdate" in obj
-        && "zoneDescriptors" in obj
 } 
\ No newline at end of file
-- 
GitLab