example 'meshtastic': substitute Qt tile provider with Lisp one

This commit is contained in:
pls.153 2023-08-15 21:34:40 +02:00
parent d342d2c5a7
commit 2e63e44e46
11 changed files with 38 additions and 77 deletions

View file

@ -2,6 +2,7 @@
:serial t
:depends-on (#-depends-loaded :my-cl-protobufs
#-depends-loaded :trivial-package-local-nicknames
#-depends-loaded :usocket-server
#+mobile :s-http-server
#+mobile :zip) ; see 'hacks/zip/'
:components ((:file "lisp/meshtastic-proto")
@ -18,5 +19,6 @@
(:file "lisp/radios")
(:file "lisp/lora")
(:file "lisp/location")
(:file "lisp/tile-provider")
(:file "lisp/main")))

View file

@ -129,7 +129,6 @@ HEADERS += \
../../src/cpp/main.h \
cpp/ble.h \
cpp/ble_meshtastic.h \
cpp/tile_provider.h \
cpp/qt.h
SOURCES += \

View file

@ -1,6 +1,5 @@
#include "qt.h"
#include "ble_meshtastic.h"
#include "tile_provider.h"
#include <ecl_fun.h>
#include <QSqlQuery>
#include <QSqlError>
@ -182,14 +181,4 @@ QVariant QT::localIp() {
return QVariant();
}
QVariant QT::startTileProvider() {
static bool start = true;
if (start) {
start = false;
int port = 1702;
new TileProvider(port);
}
return QVariant();
}
QT_END_NAMESPACE

View file

@ -36,7 +36,6 @@ public:
// etc
Q_INVOKABLE QVariant dataPath();
Q_INVOKABLE QVariant localIp();
Q_INVOKABLE QVariant startTileProvider();
QT();

View file

@ -12,7 +12,6 @@ MOC_DIR = ./tmp/
HEADERS += \
ble.h \
ble_meshtastic.h \
tile_provider.h \
qt.h
SOURCES += \

View file

@ -1,61 +0,0 @@
#pragma once
#include <QTcpServer>
#include <QTcpSocket>
#include <QtDebug>
// trivial local tile provider which doesn't need an API key
// (default Qt provider requires an API key from thunderforest.com)
class TileProvider : public QTcpServer {
Q_OBJECT
public:
TileProvider(int port = 0, QObject* parent = nullptr) : QTcpServer(parent) {
listen(QHostAddress::Any, port);
qDebug() << "tile provider started at IP" << serverAddress() << "port" << serverPort();
}
void incomingConnection(qintptr socket) override {
QTcpSocket* s = new QTcpSocket(this);
connect(s, &QTcpSocket::readyRead, this, &TileProvider::readClient);
connect(s, &QTcpSocket::disconnected, this, &TileProvider::discardClient);
s->setSocketDescriptor(socket);
}
public Q_SLOTS:
void readClient() {
QString json = QStringLiteral(
"{\"UrlTemplate\": \"https://tile.openstreetmap.org/%z/%x/%y.png\","
" \"ImageFormat\": \"png\","
" \"QImageFormat\": \"Indexed8\","
" \"ID\": \"wmf-intl-1x\","
" \"MaximumZoomLevel\": 19,"
" \"MapCopyRight\": \"<a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>\","
" \"DataCopyRight\": \"\"}");
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
if (socket->canReadLine()) {
QString line = socket->readLine();
if (line.startsWith("GET")) {
QTextStream s(socket);
s.setCodec("UTF-8");
s << QStringLiteral("HTTP/1.0 200 Ok\r\n"
"Content-Type: application/json; charset=\"utf-8\"\r\n\r\n")
<< json
<< QStringLiteral("\r\n");
socket->close();
if (socket->state() == QTcpSocket::UnconnectedState) {
delete socket;
}
}
}
}
void discardClient() {
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
socket->deleteLater();
}
};

View file

@ -85,7 +85,7 @@
(defun activate-map ()
(unless (q< |active| ui:*map-loader*)
(qt:start-tile-provider qt:*cpp*)
(start-tile-provider)
(q> |active| ui:*map-loader* t)
#+mobile
(destructuring-bind (lat lon time)

View file

@ -111,6 +111,7 @@
#:position*
#:position-count
#:set-position
#:start-tile-provider
#:tile-path
#:update-my-position))

View file

@ -9,7 +9,6 @@
#:last-position
#:local-ip
#:start-device-discovery
#:start-tile-provider
#:read*
#:short-names
#:sql-query

View file

@ -0,0 +1,33 @@
(in-package :loc)
(defvar *tile-provider-json*
"{
\"UrlTemplate\": \"https://tile.openstreetmap.org/%z/%x/%y.png\",
\"ImageFormat\": \"png\",
\"QImageFormat\": \"Indexed8\",
\"ID\": \"wmf-intl-1x\",
\"MaximumZoomLevel\": 19,
\"MapCopyRight\": \"<a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>\",
\"DataCopyRight\": \"\"
}")
(defvar *newline* #.(format nil "~C~C" #\Return #\Newline))
(defun tile-provider-handler (stream)
(when (x:starts-with "GET" (read-line stream))
(princ (format nil "HTTP/1.0 200 Ok~AContent-Type: application/json; charset=\"utf-8\"~A~A"
*newline* *newline* *newline*)
stream)
(princ *tile-provider-json* stream)
(princ *newline* stream)
(finish-output)))
(defvar *tile-provider* nil)
(defun start-tile-provider ()
(unless *tile-provider*
(setf *tile-provider*
(mp:process-run-function
:tile-provider
(lambda () (usocket:socket-server (app:my-ip) 1702 'tile-provider-handler))))))

View file

@ -9,6 +9,7 @@
(asdf:load-system :my-cl-protobufs)
(asdf:load-system :trivial-package-local-nicknames)
(asdf:load-system :usocket-server)
(push :depends-loaded *features*)