* Improved JSON encoding.
authorUrban Wallasch <urban.wallasch@freenet.de>
Mon, 22 Jul 2019 03:05:50 +0000 (05:05 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Mon, 22 Jul 2019 03:05:50 +0000 (05:05 +0200)
* Small improvements.

index.html
telehttpd.c
telelogger.c
telemetry.cpp
telemetry.h

index 800fd2636800185e1f1d206dacaaab84e0c9d95a..5c8c78bad7757d04639b693925f23ea8bfda22fc 100644 (file)
@@ -19,15 +19,16 @@ html, body {
 </head>
 <body>
 <div id="speed" class="speed">77</div>
-<div id="cc" class="speed">12</div>
+<div id="cctrl" class="speed">12</div>
 <script>
 function loadDoc() {
   var xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
     if (this.readyState == 4 && this.status == 200) {
       myObj = JSON.parse(this.responseText);
-      document.getElementById("speed").innerHTML = myObj.speed;
-      document.getElementById("cc").innerHTML = myObj.cc;
+      console.log(this.responseText);
+      document.getElementById("speed").innerHTML = Math.round(myObj.speed * 3.6);
+      document.getElementById("cctrl").innerHTML = Math.round(myObj.cctrl * 3.6);
     }
   };
   xhttp.open("GET", "/json", true);
index 90240c9abb3ebc3497cb243465ddd68846ad1826..e31e62f591440a24b57ab231a61199d200de2609 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <stdbool.h>
+#include <inttypes.h>
 #include <string.h>
 #include <errno.h>
 #include <signal.h>
@@ -33,6 +34,26 @@ enum respond_code {
     r_none
 };
 
+static void write_telejson( int fd ) {
+    char buf[512];
+    write(fd, buf, sprintf( buf, "{\n" ) );
+    write(fd, buf, sprintf( buf, "  \"timestamp\": %"PRIu64",\n", telemetry->timestamp ) );
+    write(fd, buf, sprintf( buf, "  \"x\": %f,\n", telemetry->x ) );
+    write(fd, buf, sprintf( buf, "  \"y\": %f,\n", telemetry->y ) );
+    write(fd, buf, sprintf( buf, "  \"z\": %f,\n", telemetry->z ) );
+    write(fd, buf, sprintf( buf, "  \"heading\": %f,\n", telemetry->heading ) );
+    write(fd, buf, sprintf( buf, "  \"pitch\": %f,\n", telemetry->pitch ) );
+    write(fd, buf, sprintf( buf, "  \"roll\": %f,\n", telemetry->roll ) );
+    write(fd, buf, sprintf( buf, "  \"speed\": %f,\n", telemetry->speed ) );
+    write(fd, buf, sprintf( buf, "  \"rpm\": %f,\n", telemetry->rpm ) );
+    write(fd, buf, sprintf( buf, "  \"gear\": %d,\n", telemetry->gear ) );
+    write(fd, buf, sprintf( buf, "  \"cctrl\": %f,\n", telemetry->cctrl ) );
+    write(fd, buf, sprintf( buf, "  \"fc_avg\": %f,\n", telemetry->fc_avg ) );
+    // add more here
+    write(fd, buf, sprintf( buf, "  \"dummy\": 0\n" ) );
+    write(fd, buf, sprintf( buf, "}\n" ) );
+}
+
 static void respond(int fd, const char *req, int code) {
     char buf[4096];
     char origin[256] = "http://localhost";
@@ -60,8 +81,7 @@ static void respond(int fd, const char *req, int code) {
             write(fd, buf, sprintf (buf, "Access-Control-Allow-Origin: %s\r\n", origin));
             write(fd, buf, sprintf (buf, "Content-type: text/json\r\n"));
             write(fd, buf, sprintf (buf, "Connection: close\r\n\r\n"));
-            write(fd, buf, sprintf (buf, "{ \"fc_avg\": %3.2f, \"speed\": %3.2f, \"cc\": %3.2f }",
-                        100.0 * telemetry->fc_avg , telemetry -> speed * 3.6, telemetry -> cc * 3.6));
+            write_telejson(fd);
         } else {
             struct file_info_t fi;
             if (0 == fserv_open_server("index.html", &fi)){
index 4c9750cfbb4c726643b42fbcebdadeb8c4d24732..9e856ffd3bc22c3853b804f8ed6e4a708fbb907f 100644 (file)
@@ -26,7 +26,7 @@ static int log_console(void) {
             printf( "orientation: n.a.\n" );
         }
         printf( "speed: %.1fkm/h (%.1fm/s)\n", telemetry->speed * 3.6, telemetry->speed );
-        printf( "cctrl: %.1fkm/h (%.1fm/s)\n", telemetry->cc * 3.6, telemetry->cc );
+        printf( "cctrl: %.1fkm/h (%.1fm/s)\n", telemetry->cctrl * 3.6, telemetry->cctrl );
         printf( "rpm: %.0f\n", telemetry->rpm );
         printf( "gear: %d\n", telemetry->gear );
         printf( "fc_avg: %.1f\n", telemetry->fc_avg );
index 6030b3a4d481e5110242288e07d5d2a950c6c6d6..b24de2005a56a7d427c0fbddd0cef644dc33f97d 100644 (file)
@@ -156,7 +156,7 @@ SCSAPI_VOID telemetry_frame_end(const scs_event_t UNUSED(event), const void *con
     log_line(
         ";%3.2f;%3.2f;%f;%d",
         telemetry->speed * 3.6,
-        telemetry->cc * 3.6,
+        telemetry->cctrl * 3.6,
         telemetry->rpm,
         telemetry->gear
     );
@@ -421,7 +421,7 @@ SCSAPI_RESULT scs_telemetry_init(const scs_u32_t version, const scs_telemetry_in
     version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_speed, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_float, &telemetry->speed);
     version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_engine_rpm, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_float, &telemetry->rpm);
     version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_engine_gear, SCS_U32_NIL, SCS_VALUE_TYPE_s32, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_s32, &telemetry->gear);
-    version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_cruise_control, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_float, &telemetry->cc);
+    version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_cruise_control, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_float, &telemetry->cctrl);
     version_params->register_for_channel(SCS_TELEMETRY_TRUCK_CHANNEL_fuel_average_consumption, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_float, &telemetry->fc_avg);
 
     // Set the structure with defaults.
index 2c09682bab8eb877161e72566db764c9632794cd..4495922999519238aded95874165b8ac4c95d3af 100644 (file)
@@ -20,7 +20,7 @@ struct telemetry_state_t {
     float   speed;
     float   rpm;
     int     gear;
-    float   cc;         // cruise_control
+    float   cctrl;      // cruise_control
     float   fc_avg;     // fuel consumption average
 };