from .evstore import initdb, fetch
from .gps303proto import (
GPS_POSITIONING,
+ STATUS,
WIFI_POSITIONING,
parse_message,
)
msg = parse_message(packet, is_incoming=is_incoming)
result.append(
{
+ "type": "location",
"imei": imei,
"timestamp": str(
datetime.fromtimestamp(timestamp).astimezone(
),
"longitude": msg.longitude,
"latitude": msg.latitude,
+ "accuracy": "gps"
+ if isinstance(msg, GPS_POSITIONING)
+ else "approximate",
}
)
return result
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()
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"
log.debug("%s on fd %d", event, self.sock.fileno())
self.ws_data += self.ws.send(event.response())
elif isinstance(event, TextMessage):
- # TODO: save imei "subscription"
log.debug("%s on fd %d", event, self.sock.fileno())
msg = loads(event.data)
msgs.append(msg)
self.imeis,
self.sock.fileno(),
)
- return True # TODO: check subscriptions
+ return imei in self.imeis
def send(self, message):
if self.ready and message["imei"] in self.imeis:
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,
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 = []
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: