First untested version with user storage

This commit is contained in:
Martin Bauer
2021-05-16 13:45:00 +02:00
parent 06fdda1d93
commit 5984a233af
14 changed files with 174 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include "Dtypes.h"
#include "UserDB.h"
#include <ArduinoWebsockets.h>
template <typename T>
@@ -13,8 +14,8 @@ template <typename SessionT>
class WebsocketServer
{
public:
WebsocketServer(SessionManager<SessionT> &sessionManager, int port)
: sessionManager_(sessionManager), nextFreeClient_(0), port_(port),
WebsocketServer(SessionManager<SessionT> &sessionManager, UserStorage &userStorage, int port)
: sessionManager_(sessionManager), userStorage_(userStorage), nextFreeClient_(0), port_(port),
running_(false)
{
}
@@ -34,7 +35,12 @@ private:
void sendSessionStopMessages();
void sendNewDataMessages();
void sendUserList(websockets::WebsocketsClient &client);
void sendSessionList(websockets::WebsocketsClient &client, const String &userId);
SessionManager<SessionT> &sessionManager_;
UserStorage &userStorage_;
int nextFreeClient_;
int port_;
@@ -58,11 +64,15 @@ enum MessageType
SESSION_STARTED = 2,
SESSION_STOPPED = 3,
SESSION_NEW_DATA = 4,
ANSWER_USER_LIST = 5,
ANSWER_SESSION_LIST = 6,
// from frontend to device
START_SESSION = 5,
STOP_SESSION = 6,
TARE = 7
START_SESSION = 7,
STOP_SESSION = 8,
TARE = 9,
QUERY_USER_LIST = 10,
QUERY_SESSION_LIST = 11,
};
#pragma pack(push, 1)
@@ -129,6 +139,7 @@ private:
// book-keeping
size_t numMeasurements_;
};
#pragma pack(pop)
// ------------------------------------- WebsocketServer members ---------------------------
@@ -137,6 +148,7 @@ template <typename SessionT>
void WebsocketServer<SessionT>::iteration()
{
using namespace websockets;
auto onMessage = [this](WebsocketsClient &client, WebsocketsMessage message) {
if (message.isPing())
client.pong();
@@ -162,6 +174,18 @@ void WebsocketServer<SessionT>::iteration()
case TARE:
this->sessionManager_.tare();
break;
case QUERY_USER_LIST:
this->sendUserList(client);
break;
case QUERY_SESSION_LIST:
{
StaticJsonDocument<USER_STRING_ID_MAX_LEN + 16> doc;
deserializeMsgPack(doc, data, length);
String userId = doc.as<String>();
if (userId.length() > 0)
this->sendSessionList(client, userId);
}
break;
default:
client.close(CloseReason_UnsupportedData);
return;
@@ -200,10 +224,53 @@ void WebsocketServer<SessionT>::reportSessionUpdate()
for (int i = 0; i < MAX_CONNECTIONS; ++i)
numSentMeasurements_[i] = 0;
}
sendNewDataMessages();
}
template <typename SessionT>
void WebsocketServer<SessionT>::sendUserList(websockets::WebsocketsClient &client)
{
const auto numUsers = userStorage_.numUsers();
constexpr size_t constantSlack = 64;
DynamicJsonDocument result(JSON_ARRAY_SIZE(numUsers) + numUsers * (USER_STRING_ID_MAX_LEN + 2) + constantSlack);
JsonArray arr = result.to<JsonArray>();
for (auto userIt = userStorage_.beginWithoutUnassigned(); userIt != userStorage_.end(); ++userIt)
arr.add(userIt->stringId());
char buffer[MAX_USERS * (USER_STRING_ID_MAX_LEN + 1) + constantSlack];
size_t bytesWritten = serializeMsgPack(result, buffer, sizeof(buffer));
client.sendBinary(buffer, bytesWritten);
}
template <typename SessionT>
void WebsocketServer<SessionT>::sendSessionList(websockets::WebsocketsClient &client, const String &userId)
{
User *user = userStorage_.getUserInfo(userId);
if (user != nullptr)
{
DynamicJsonDocument result(JSON_ARRAY_SIZE(user->numSessions()) + user->numSessions() * (sizeof(SessionIdType) + 8));
JsonArray arr = result.to<JsonArray>();
for (SessionIdType *sIt = user->sessionBegin(); sIt != user->sessionEnd(); ++sIt)
arr.add(*sIt);
size_t bytesToWrite = measureMsgPack(result);
char *buffer = (char *)malloc(bytesToWrite);
size_t bytesWritten = serializeMsgPack(result, buffer, bytesToWrite);
assert(bytesWritten <= bytesToWrite);
client.sendBinary(buffer, bytesWritten);
free(buffer);
}
else
{
DynamicJsonDocument result(JSON_ARRAY_SIZE(1) + 8);
result.to<JsonArray>();
char buffer[32];
size_t bytesWritten = serializeMsgPack(result, buffer, sizeof(buffer));
client.sendBinary(buffer, bytesWritten);
}
}
template <typename SessionT>
void WebsocketServer<SessionT>::sendSessionStartMessages()
{

View File

@@ -7,6 +7,7 @@
#include <HTTPClient.h>
#include "esp_https_ota.h"
#include "esp_ota_ops.h"
#include "ESPmDNS.h"
// Own libs
@@ -20,12 +21,15 @@
#include "EspHttp.h"
#include "WebDAV.h"
#include "WebsocketServer.h"
#include "UserDB.h"
using Session_T = SimpleMeasurementSession<MeasurementT, CONFIG_SESSION_MAX_SIZE>;
SessionManager<Session_T> sessionManager;
UserStorage userStorage;
EspHttp espHttpServer;
WebsocketServer<Session_T> webSocketServer(sessionManager, 81);
WebsocketServer<Session_T> webSocketServer(sessionManager, userStorage, 81);
WifiManager wifiManager;
@@ -253,14 +257,14 @@ void httpSetup(SessionManager<SessionT> *sessionManager, WifiManager *wifiManage
prefs.begin("st_prefs");
json["valueRightShift"] = prefs.getInt("valueRightShift", CONFIG_VALUE_RIGHT_SHIFT);
json["tareAvgCount"] = prefs.getUInt("tareAvgCount", CONFIG_TARE_AVG_COUNT);
json["tareAvgCount"] = prefs.getUInt("tareAvgCount", CONFIG_TARE_AVG_COUNT);
json["autoStartMinThreshold"] = prefs.getUInt("aStartMinTh", CONFIG_AUTO_START_MIN_THRESHOLD);
json["autoStartMaxThreshold"] = prefs.getUInt("aStartMaxTh", CONFIG_AUTO_START_MAX_THRESHOLD);
json["autoStartMaxMeasurementsBetweenPeaks"] = prefs.getUInt("aStartCount", CONFIG_AUTO_START_MAX_MEASUREMENTS_BETWEEN_PEAKS);
json["autoStopThreshold"] = prefs.getUInt("aStopTh", CONFIG_AUTO_STOP_THRESHOLD);
json["autoStopNumMeasurements"] = prefs.getUInt("aStopCount", CONFIG_AUTO_STOP_NUM_MEASUREMENTS);
json["autoStartEnabled"] = prefs.getBool("aStartEnabled", true);
json["autoStopEnabled"] = prefs.getBool("aStopEnabled", true);
json["autoStartEnabled"] = prefs.getBool("aStartEnabled", true);
json["autoStopEnabled"] = prefs.getBool("aStopEnabled", true);
char jsonText[1024];
auto bytesWritten = serializeJson(json, jsonText);
httpd_resp_send(req, jsonText, bytesWritten);
@@ -287,25 +291,25 @@ void httpSetup(SessionManager<SessionT> *sessionManager, WifiManager *wifiManage
Preferences prefs;
prefs.begin("st_prefs");
if(json.containsKey("valueRightShift"))
if (json.containsKey("valueRightShift"))
prefs.putInt("valueRightShift", json["valueRightShift"].as<int>());
if(json.containsKey("tareAvgCount"))
if (json.containsKey("tareAvgCount"))
prefs.putUInt("tareAvgCount", json["tareAvgCount"].as<unsigned int>());
if(json.containsKey("autoStartMinThreshold"))
if (json.containsKey("autoStartMinThreshold"))
prefs.putUInt("aStartMinTh", json["autoStartMinThreshold"].as<unsigned int>());
if(json.containsKey("autoStartMaxThreshold"))
if (json.containsKey("autoStartMaxThreshold"))
prefs.putUInt("aStartMaxTh", json["autoStartMaxThreshold"].as<unsigned int>());
if(json.containsKey("autoStartMaxMeasurementsBetweenPeaks"))
if (json.containsKey("autoStartMaxMeasurementsBetweenPeaks"))
prefs.putUInt("aStartCount", json["autoStartMaxMeasurementsBetweenPeaks"].as<unsigned int>());
if(json.containsKey("autoStopThreshold"))
if (json.containsKey("autoStopThreshold"))
prefs.putUInt("aStopTh", json["autoStopThreshold"].as<unsigned int>());
if(json.containsKey("autoStopNumMeasurements"))
if (json.containsKey("autoStopNumMeasurements"))
prefs.putUInt("aStopCount", json["autoStopNumMeasurements"].as<unsigned int>());
if(json.containsKey("autoStartEnabled"))
if (json.containsKey("autoStartEnabled"))
prefs.putBool("aStartEnabled", json["autoStartEnabled"].as<bool>());
if(json.containsKey("autoStopEnabled"))
if (json.containsKey("autoStopEnabled"))
prefs.putBool("aStopEnabled", json["autoStopEnabled"].as<bool>());
sessionManagerSetup();
httpd_resp_send(req, "OK", -1);
};
@@ -399,6 +403,8 @@ void setup()
ESP_ERROR_CHECK(esp_event_loop_create_default());
userStorage.init();
// WiFi
String fullHostname = String(CONFIG_HOSTNAME) + getIdSuffix();
wifiManager.begin(fullHostname);