diff --git a/examples/meshtastic/app.asd b/examples/meshtastic/app.asd index 850ba87..d1808c5 100644 --- a/examples/meshtastic/app.asd +++ b/examples/meshtastic/app.asd @@ -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"))) diff --git a/examples/meshtastic/app.pro b/examples/meshtastic/app.pro index 269ff5a..f95eeee 100644 --- a/examples/meshtastic/app.pro +++ b/examples/meshtastic/app.pro @@ -129,7 +129,6 @@ HEADERS += \ ../../src/cpp/main.h \ cpp/ble.h \ cpp/ble_meshtastic.h \ - cpp/tile_provider.h \ cpp/qt.h SOURCES += \ diff --git a/examples/meshtastic/cpp/qt.cpp b/examples/meshtastic/cpp/qt.cpp index 8fa7070..9f07b4f 100644 --- a/examples/meshtastic/cpp/qt.cpp +++ b/examples/meshtastic/cpp/qt.cpp @@ -1,6 +1,5 @@ #include "qt.h" #include "ble_meshtastic.h" -#include "tile_provider.h" #include #include #include @@ -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 diff --git a/examples/meshtastic/cpp/qt.h b/examples/meshtastic/cpp/qt.h index d29be8b..48c101d 100644 --- a/examples/meshtastic/cpp/qt.h +++ b/examples/meshtastic/cpp/qt.h @@ -36,7 +36,6 @@ public: // etc Q_INVOKABLE QVariant dataPath(); Q_INVOKABLE QVariant localIp(); - Q_INVOKABLE QVariant startTileProvider(); QT(); diff --git a/examples/meshtastic/cpp/qt.pro b/examples/meshtastic/cpp/qt.pro index f8f4a1c..1351c34 100644 --- a/examples/meshtastic/cpp/qt.pro +++ b/examples/meshtastic/cpp/qt.pro @@ -12,7 +12,6 @@ MOC_DIR = ./tmp/ HEADERS += \ ble.h \ ble_meshtastic.h \ - tile_provider.h \ qt.h SOURCES += \ diff --git a/examples/meshtastic/cpp/tile_provider.h b/examples/meshtastic/cpp/tile_provider.h deleted file mode 100644 index 4c47c3b..0000000 --- a/examples/meshtastic/cpp/tile_provider.h +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once - -#include -#include -#include - -// 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\": \"OpenStreetMap\"," - " \"DataCopyRight\": \"\"}"); - - QTcpSocket* socket = static_cast(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(sender()); - socket->deleteLater(); - } -}; - diff --git a/examples/meshtastic/lisp/location.lisp b/examples/meshtastic/lisp/location.lisp index 5fc1fe9..5080001 100644 --- a/examples/meshtastic/lisp/location.lisp +++ b/examples/meshtastic/lisp/location.lisp @@ -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) diff --git a/examples/meshtastic/lisp/package.lisp b/examples/meshtastic/lisp/package.lisp index 9adbc37..252d708 100644 --- a/examples/meshtastic/lisp/package.lisp +++ b/examples/meshtastic/lisp/package.lisp @@ -111,6 +111,7 @@ #:position* #:position-count #:set-position + #:start-tile-provider #:tile-path #:update-my-position)) diff --git a/examples/meshtastic/lisp/qt.lisp b/examples/meshtastic/lisp/qt.lisp index 1301962..a35baad 100644 --- a/examples/meshtastic/lisp/qt.lisp +++ b/examples/meshtastic/lisp/qt.lisp @@ -9,7 +9,6 @@ #:last-position #:local-ip #:start-device-discovery - #:start-tile-provider #:read* #:short-names #:sql-query diff --git a/examples/meshtastic/lisp/tile-provider.lisp b/examples/meshtastic/lisp/tile-provider.lisp new file mode 100644 index 0000000..aa332fa --- /dev/null +++ b/examples/meshtastic/lisp/tile-provider.lisp @@ -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\": \"OpenStreetMap\", + \"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)))))) + diff --git a/examples/meshtastic/run.lisp b/examples/meshtastic/run.lisp index c58f9fd..f03c53f 100644 --- a/examples/meshtastic/run.lisp +++ b/examples/meshtastic/run.lisp @@ -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*)