From dd0fbc6d1ba578327d28eab7e5078e5179effd0f Mon Sep 17 00:00:00 2001
From: Sven Schneider <icedoggy@gmx.de>
Date: Fri, 16 Jul 2021 20:40:02 +0200
Subject: [PATCH] continued but not finished working on getting data from
 several buildings

---
 public/js/appCesium.js | 173 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 154 insertions(+), 19 deletions(-)

diff --git a/public/js/appCesium.js b/public/js/appCesium.js
index 1625356..8483dcb 100644
--- a/public/js/appCesium.js
+++ b/public/js/appCesium.js
@@ -1,5 +1,6 @@
 "use strict";
 
+
 // Functions
 import {
     aggregateResponse,
@@ -29,6 +30,7 @@ import {
 } from "./appChart.js";
 
 var ALLDATA = [];
+var bld = 0;
 
 Cesium.Ion.defaultAccessToken =
     "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIyODgxYzJlNi1kNDZiLTQ3ZmQtYmUxYy0yMWI0OGM3NDA5MzAiLCJpZCI6NDczOSwic2NvcGVzIjpbImFzciIsImdjIl0sImlhdCI6MTU0MTUyMzU0MX0.shj2hM3pvsvcmE_wMb2aBDuk_cKWmFmbolltInGImwU";
@@ -251,6 +253,8 @@ const activate3DTileFeaturePicking = function() {
             selected.feature.color = selected.originalColor;
             selected.feature = undefined;
         }
+        ALLDATA = [];
+        cnt = 0;
         // Pick a new feature
         const pickedFeature = viewer.scene.pick(movement.position);
         if (!Cesium.defined(pickedFeature)) {
@@ -324,7 +328,8 @@ const activate3DTileFeaturePicking = function() {
         const BUILDING_STREAM_ID = [75, 76, 77, 78, 79, 80];
         const BUILDING_ID = ["101", "102", "107", "112, 118", "125", "225"];
         // ALLDATA = [];
-        var bld = 0;
+        var cnt = 0;
+
 
         for (bld = 0; bld < BUILDING_STREAM_ID.length; bld++) {
 
@@ -353,9 +358,9 @@ const activate3DTileFeaturePicking = function() {
                         combinedObservations.push(...dataArrays);
                     });
                     // DEBUG: Check total number of observations
-                    console.log(combinedObservations.length);
+                    // console.log(combinedObservations.length);
                     // DEBUG: Print the array of observations
-                    console.log(combinedObservations);
+                    // console.log(combinedObservations);
 
                     return combinedObservations;
                 })
@@ -366,31 +371,34 @@ const activate3DTileFeaturePicking = function() {
                 .then((observationArr) => {
                     var agg = aggregateResponse(observationArr, 0, 'mean');
                     var tmpObj = {
-                        id: BUILDING_ID[bld],
-                        streamID: BUILDING_STREAM_ID[bld],
+                        BldID: BUILDING_ID[cnt],
+                        streamID: BUILDING_STREAM_ID[cnt],
                         timestamp: agg.aggDates,
                         data: agg.aggVals,
                     };
                     ALLDATA.push(tmpObj);
                     // console.log(agg);
+
                     drawHeatMapHC(formatSTAResponseForHeatMap(agg.originalFormat));
                     drawLineChartHC(formatSTAResponseForLineChart(agg.originalFormat));
-                    console.log(ALLDATA.length);
-                    var selectedDate = document.getElementById("DateSelected").innerHTML;
 
-                    var date = new Date(
-                        Date.parse(
-                            switchDayMonth_inDate(selectedDate)
-                        )
-                    );
+                    if (ALLDATA.length == 6) {
+                        var selectedDate = document.getElementById("DateSelected").innerHTML;
 
-                    var p = whereIsDateInArray(ALLDATA[0].timestamp, date);
-                    if (p == -1)
-                        console.log('date not found in date array');
-                    else
-                        console.log('date found at position' + p);
+                        var date = new Date(
+                            Date.parse(
+                                switchDayMonth_inDate(selectedDate)
+                            )
+                        );
 
-                    console.log(date);
+                        var p = whereIsDateInArray(ALLDATA[0].timestamp, date);
+                        const DATA = getDataForAllBuildingsPerDate(p);
+                        colorBlds(DATA);
+                        console.log(date);
+                    }
+
+                    console.log(ALLDATA.length);
+                    cnt++
 
                     // alert('waiting...');
                 });
@@ -399,8 +407,135 @@ const activate3DTileFeaturePicking = function() {
 
         }
 
+
     }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
 
 };
 
-activate3DTileFeaturePicking();
\ No newline at end of file
+activate3DTileFeaturePicking();
+
+function goon(ALLDATA) {
+
+    if (ALLDATA.length > 0) {
+        var selectedDate = document.getElementById("DateSelected").innerHTML;
+
+        var date = new Date(
+            Date.parse(
+                switchDayMonth_inDate(selectedDate)
+            )
+        );
+
+        var p = whereIsDateInArray(ALLDATA[0].timestamp, date);
+        const DATA = getDataForAllBuildingsPerDate(p);
+        colorBlds(DATA);
+        console.log(date);
+    }
+}
+
+
+
+// /**
+//  * 
+//  * @param {Number} datePos 
+//  */
+
+export const getDataForAllBuildingsPerDate = function(datePos) {
+
+    var dataObjPerBld = [];
+    var dataArray = [];
+    for (var i = 0; i < ALLDATA.length; i++) {
+
+        var tmp = {
+            BldId: ALLDATA[i].BldID,
+            dateVal: ALLDATA[i].timestamp[datePos],
+            dataVal: ALLDATA[i].data[datePos]
+        };
+        dataArray.push(tmp.dataVal);
+        dataObjPerBld.push(tmp);
+    }
+
+
+    return {
+        dataPerBld: dataObjPerBld,
+        dataArray: dataArray
+    };
+}
+
+export const colorBlds = function(bldInfo) {
+
+
+    // see: https://github.com/PimpTrizkit/PJs/wiki/12.-Shade,-Blend-and-Convert-a-Web-Color-(pSBC.js)
+    const RGB_Linear_Blend = (p, c0, c1) => {
+        var i = parseInt,
+            r = Math.round,
+            P = 1 - p,
+            [a, b, c, d] = c0.split(","),
+            [e, f, g, h] = c1.split(","),
+            x = d || h,
+            j = x ? "," + (!d ? h : !h ? d : r((parseFloat(d) * P + parseFloat(h) * p) * 1000) / 1000 + ")") : ")";
+        return "rgb" + (x ? "a(" : "(") + r(i(a[3] == "a" ? a.slice(5) : a.slice(4)) * P + i(e[3] == "a" ? e.slice(5) : e.slice(4)) * p) + "," + r(i(b) * P + i(f) * p) + "," + r(i(c) * P + i(g) * p) + d;
+    }
+
+    // get the gradient colors between the two defined colors;
+    const c1 = "rgb(255,64,0)";
+    const c2 = "rgb(63,131,163)";
+    var L = bldInfo.dataArray.length;
+    var offset = 1 / (L - 1);
+    var colorsForBld = [];
+    var p = 0;
+    for (var i = 0; i < L; i++) {
+        var cnew = RGB_Linear_Blend(p, c1, c2);
+        cnew = cnew.replace('undefined', ')');
+        colorsForBld.push(cnew);
+        p += offset;
+        console.log(p);
+    }
+
+    var numbers = bldInfo.dataArray,
+        ma = numbers.reduce(function(a, b) { return Math.max(a, b); }),
+        mi = numbers.reduce(function(a, b) { return Math.min(a, b); }),
+        dif = ma - mi,
+        l = numbers.length,
+        i;
+
+    for (i = 0; i < l; i++) {
+        numbers[i] = (numbers[i] - mi) / dif;
+    }
+
+    function sortWithIndeces(toSort) {
+        for (var i = 0; i < toSort.length; i++) {
+            toSort[i] = [toSort[i], i];
+        }
+        toSort.sort(function(left, right) {
+            return left[0] < right[0] ? -1 : 1;
+        });
+        toSort.sortIndices = [];
+        for (var j = 0; j < toSort.length; j++) {
+            toSort.sortIndices.push(toSort[j][1]);
+            toSort[j] = toSort[j][0];
+        }
+        return toSort;
+    }
+
+
+    sortWithIndeces(numbers);
+    // alert(numbers.sortIndices.join(","));
+    var sortedIndices = numbers.sortIndices;
+    // convert object to array
+    var i = 0,
+        arr = [];
+    for (var ob in inputObj)
+        arr[i++] = ob;
+
+
+    // numbers = (6)[0.37684400819550146, 0, 0.5862015830883935, 1, 0.444816858861194, 0.9179110359561272],
+
+    console.log(numbers);
+    // assign the color to the buildings based on their id
+    // var sortedValues = bldInfo.dataArray.sort
+
+    // if (!Cesium.defined(name)) {
+    //     name = pickedFeature.getProperty("id");
+    // }
+
+}
\ No newline at end of file
-- 
GitLab