--- /dev/null
+""" Watch for locevt and print them """
+
+from datetime import datetime, timezone
+from logging import getLogger
+from os import umask
+from sys import argv, stdin
+import zmq
+
+from . import common
+from .zmsg import LocEvt
+
+log = getLogger("gps303/watch")
+
+
+def main(conf):
+ zctx = zmq.Context()
+ zpub = zctx.socket(zmq.PUB)
+ oldmask = umask(0o117)
+ zpub.bind(conf.get("lookaside", "publishurl"))
+ umask(oldmask)
+
+ while True:
+ line = stdin.readline()
+ line = line.rstrip("\r\n")
+ if not line:
+ break
+ print(line.encode())
+ args = line.split(" ")
+ imei = args[0]
+ kwargs = dict([arg.split("=") for arg in args[1:]])
+ msg = LocEvt(imei=imei, **kwargs)
+ print("Publishing:", msg)
+ zpub.send(msg.packed)
+
+
+if __name__.endswith("__main__"):
+ main(common.init(log))
self.write()
return msgs
- def send(self, imei, message):
+ def wants(self, imei):
+ return True # TODO: check subscriptions
+
+ def send(self, message):
# TODO: filter only wanted imei got from the client
- self.ws_data += self.ws.send(Message(data=message))
+ self.ws_data += self.ws.send(Message(data=message.json))
def write(self):
try:
log.debug("Received: %s", msg)
return result
- def send(self, msgs):
+ def send(self, msg):
for clnt in self.by_fd.values():
- clnt.send(msgs)
- clnt.write()
+ if clnt.wants(msg.imei):
+ clnt.send(msg)
+ clnt.write()
def runserver(conf):
tosend = []
topoll = []
tostop = []
- events = poller.poll(1000)
+ events = poller.poll(5000)
+ log.debug("got events: %s", events)
for sk, fl in events:
if sk is zsub:
while True:
try:
- msg = zsub.recv(zmq.NOBLOCK)
- tosend.append(LocEvt(msg))
+ zmsg = LocEvt(zsub.recv(zmq.NOBLOCK))
+ tosend.append(zmsg)
except zmq.Again:
break
elif sk == tcpfd:
).encode()
)
+ # And this is full json that can be sent over websocket etc.
+ @property
+ def json(self):
+ return dumps(
+ {
+ "imei": self.imei,
+ "devtime": str(self.devtime),
+ "latitude": self.lat,
+ "longitude": self.lon,
+ "is-gps": self.is_gps,
+ }
+ )
+
def decode(self, buffer):
self.imei = buffer[:16].decode()
json_data = loads(buffer[16:])
}
function ws_onmessage(event) {
console.log("message " + event);
- log.innerHTML += "message " + event + "<br>";
+ log.innerHTML += "message " + event.data + "<br>";
}
function ws_onerror(event) {
console.log("error " + event);