Snippets

Zenduit Team ea9aGg: Untitled snippet

Created by Shane Graham

File hos-distance-calculation.js Added

  • Ignore whitespace
  • Hide word diff
+const user = "b48";
+
+const from = new Date();
+const to = new Date();
+const offset = 13;
+
+const HOS_STATUSES = {
+    "D": "D",
+    "ON": "ON",
+    "OFF": "OFF",
+    "SB": "SB",
+    "YM": "YM",
+    "PC": "PC",
+    "WT": "WT",
+    "Exemption16H": "Exemption16H",
+    "AdverseWeather": "AdverseWeather",
+    "ExemptionOffDutyDeferral": "ExemptionOffDutyDeferral",
+    "Authority": "Authority"    
+};
+
+const call = (method, call) => {
+    return new Promise((resolve, reject) => {
+        api.call(method, call, resolve, reject);
+    });
+};
+
+const multicall = (call) => {
+    return new Promise((resolve, reject) => {
+        api.call("ExecuteMultiCall", {calls: call}, resolve, reject);
+    });
+};
+
+const getHOS = (async (user, from, to) => {
+    const params = {
+      "typeName": "DutyStatusLog",
+      "search": {
+        "userSearch": {
+          "id": user
+        },
+        "statuses": [
+          "D",
+          "ON",
+          "OFF",
+          "SB"
+        ],
+        "fromDate": from.toISOString(),
+        "toDate": to.toISOString(),
+        "includeBoundaryLogs": true,
+        "deviceSearch": null,
+        "includeModifications": true
+      }
+    };
+    return call("Get", params);
+});
+
+to.setDate(to.getDate()-offset);
+from.setDate(from.getDate()-offset);
+
+from.setHours(0,0,0,0);
+to.setHours(23,59,59, 999);
+
+const main = (async () => {
+    
+  //const HOSLogData = (await getHOS(user, from, to));
+  const DutyStatusLogs = (await getHOS(user, from, to)).filter(log => log.state == "Active").sort((a, b) => new Date(a.dateTime) - new Date(b.dateTime));
+  
+  const OdometerPulls = [];
+  
+  DutyStatusLogs.forEach((log, i) => {
+    DutyStatusLogs[i].dateTime = new Date(log.dateTime) < from ? from.toISOString() : log.dateTime;
+    DutyStatusLogs[i].dateTime = new Date(log.dateTime) > to ? to.toISOString() : log.dateTime;
+    DutyStatusLogs[i].start = DutyStatusLogs[i].dateTime;
+  });
+
+  DutyStatusLogs.forEach((log, i) => {
+    if(DutyStatusLogs[i + 1])
+      DutyStatusLogs[i].end = DutyStatusLogs[i + 1].dateTime;
+  });
+  
+  DutyStatusLogs.pop();
+  
+  const DriveStatuses = DutyStatusLogs.filter(log => ((log.status === HOS_STATUSES.D || log.status === HOS_STATUSES.ON)));
+  
+  const OdometerCalls = DriveStatuses.map(dutyStatusLog => {
+    return {
+      "method": "Get",
+      "params": {
+        "typeName": "StatusData",
+        "search": {
+          "deviceSearch": {
+            "id": dutyStatusLog.device.id
+          },
+          "diagnosticSearch": {
+            "id": "DiagnosticOdometerAdjustmentId"
+          },
+          "fromDate": dutyStatusLog.start,
+          "toDate": dutyStatusLog.end
+        }
+      }
+    }
+  });
+  
+  try {
+      const OdemeterResults = (await multicall(OdometerCalls));
+      
+      const OR = OdemeterResults.map(or => {
+         return (or[or.length - 1].data - or[0].data) / 1000
+      });
+      
+      var distance = parseFloat(OR.reduce((a, b) => a + b).toFixed(2).slice(0,-1), 10);
+  } catch(err) {
+      var distance = 0;
+  }
+  console.log(from, to);
+  console.log("DISTANCE:", distance);
+  
+})();
+
+
+/*opt nomin*/
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.