]> www.average.org Git - loctrkd.git/commitdiff
Report status (with battery %) to the webpage
authorEugene Crosser <crosser@average.org>
Mon, 23 May 2022 20:15:08 +0000 (22:15 +0200)
committerEugene Crosser <crosser@average.org>
Mon, 23 May 2022 20:15:08 +0000 (22:15 +0200)
gps303/wsgateway.py
webdemo/index.html

index d3c0541158941dedd017aba669e40042fd11f928..1843ce9b01d426aa897e4625d6ef6721bbc7421c 100644 (file)
@@ -21,6 +21,7 @@ from . import common
 from .evstore import initdb, fetch
 from .gps303proto import (
     GPS_POSITIONING,
+    STATUS,
     WIFI_POSITIONING,
     parse_message,
 )
@@ -40,6 +41,7 @@ def backlog(imei, numback):
         msg = parse_message(packet, is_incoming=is_incoming)
         result.append(
             {
+                "type": "location",
                 "imei": imei,
                 "timestamp": str(
                     datetime.fromtimestamp(timestamp).astimezone(
@@ -48,6 +50,9 @@ def backlog(imei, numback):
                 ),
                 "longitude": msg.longitude,
                 "latitude": msg.latitude,
+                "accuracy": "gps"
+                if isinstance(msg, GPS_POSITIONING)
+                else "approximate",
             }
         )
     return result
@@ -68,19 +73,14 @@ def try_http(data, fd, e):
             fd,
             headers,
         )
-        try:
-            pos = resource.index("?")
-            resource = resource[:pos]
-        except ValueError:
-            pass
         if op == "GET":
             if htmlfile is None:
                 return (
                     f"{proto} 500 No data configured\r\n"
                     f"Content-Type: text/plain\r\n\r\n"
-                    f"HTML data not configure on the server\r\n".encode()
+                    f"HTML data not configured on the server\r\n".encode()
                 )
-            elif resource == "/":
+            else:
                 try:
                     with open(htmlfile, "rb") as fl:
                         htmldata = fl.read()
@@ -96,12 +96,6 @@ def try_http(data, fd, e):
                         f"Content-Type: text/plain\r\n\r\n"
                         f"HTML file could not be opened\r\n".encode()
                     )
-            else:
-                return (
-                    f"{proto} 404 File not found\r\n"
-                    f"Content-Type: text/plain\r\n\r\n"
-                    f'We can only serve "/"\r\n'.encode()
-                )
         else:
             return (
                 f"{proto} 400 Bad request\r\n"
@@ -286,6 +280,10 @@ def runserver(conf):
                     zmq.SUBSCRIBE,
                     topic(WIFI_POSITIONING.PROTO, False, imei),
                 )
+                zsub.setsockopt(
+                    zmq.SUBSCRIBE,
+                    topic(STATUS.PROTO, True, imei),
+                )
             for imei in activesubs - neededsubs:
                 zsub.setsockopt(
                     zmq.UNSUBSCRIBE,
@@ -295,6 +293,10 @@ def runserver(conf):
                     zmq.UNSUBSCRIBE,
                     topic(WIFI_POSITIONING.PROTO, False, imei),
                 )
+                zsub.setsockopt(
+                    zmq.UNSUBSCRIBE,
+                    topic(STATUS.PROTO, True, imei),
+                )
             activesubs = neededsubs
             log.debug("Subscribed to: %s", activesubs)
             tosend = []
@@ -309,18 +311,36 @@ def runserver(conf):
                             zmsg = Bcast(zsub.recv(zmq.NOBLOCK))
                             msg = parse_message(zmsg.packet, zmsg.is_incoming)
                             log.debug("Got %s with %s", zmsg, msg)
-                            tosend.append(
-                                {
-                                    "imei": zmsg.imei,
-                                    "timestamp": str(
-                                        datetime.fromtimestamp(
-                                            zmsg.when
-                                        ).astimezone(tz=timezone.utc)
-                                    ),
-                                    "longitude": msg.longitude,
-                                    "latitude": msg.latitude,
-                                }
-                            )
+                            if isinstance(msg, STATUS):
+                                tosend.append(
+                                    {
+                                        "type": "status",
+                                        "imei": zmsg.imei,
+                                        "timestamp": str(
+                                            datetime.fromtimestamp(
+                                                zmsg.when
+                                            ).astimezone(tz=timezone.utc)
+                                        ),
+                                        "battery": msg.batt,
+                                    }
+                                )
+                            else:
+                                tosend.append(
+                                    {
+                                        "type": "location",
+                                        "imei": zmsg.imei,
+                                        "timestamp": str(
+                                            datetime.fromtimestamp(
+                                                zmsg.when
+                                            ).astimezone(tz=timezone.utc)
+                                        ),
+                                        "longitude": msg.longitude,
+                                        "latitude": msg.latitude,
+                                        "accuracy": "gps"
+                                        if zmsg.is_incoming
+                                        else "approximate",
+                                    }
+                                )
                         except zmq.Again:
                             break
                 elif sk == tcpfd:
index b857360c160b6866aa7f23757f3ed0a5f6fec7a9..d0af1ac9f940d067148ea5c940520fdcb56dbd96 100644 (file)
@@ -43,6 +43,7 @@
                send = document.getElementById("send");
                clear = document.getElementById("clear");
                subslist = document.getElementById("subslist");
+               devstatus = document.getElementById("devstatus");
                tstamp = document.getElementById("tstamp");
                if (qimei) {
                        imei.value = qimei;
                markers.addMarker(new OpenLayers.Marker(lonLat, icon));
                map.setCenter(lonLat, 14);
        }
+       function display_status(msg) {
+               console.log("status " + JSON.stringify(msg));
+               devstatus.innerHTML = "BAT: " + msg.battery;
+       }
 
        function open_ws() {
                wsurl = new URL("ws://localhost/");
        }
        function ws_onmessage(event) {
                console.log("message " + event.data);
-               set_marker(JSON.parse(event.data));
+               msg = JSON.parse(event.data);
+               if (msg.type === "location") {
+                       set_marker(msg);
+               } else if (msg.type === "status") {
+                       display_status(msg);
+               }
+
        }
        function ws_onerror(event) {
                console.log("error " + event);
                <input type="button" id="clear" name="clear" value="Clear"
                 onclick="sendIMEI(true)" disabled>
                <span id="subslist"></span>
-               <span id="tstamp" style="float:right"></span>
+               <span style="float:right">
+                       | <span id="devstatus"></span>
+                       | <span id="tstamp"></span>
+               </span>
        </div>
        <div style="width:100%; height:97%" id="map"></div>
        <div style="width:100%; height:1%">