Add external libraries

This commit is contained in:
sylvieee-iot 2024-05-02 20:28:41 +03:00 committed by GitHub
parent 70f443b06e
commit e8dc11cc31
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 2010 additions and 0 deletions

View file

@ -0,0 +1,110 @@
#include <string>
#include <functional>
#include <iostream>
#include <atomic>
#include <sstream>
#include <queue>
#include <IXWebSocket.h>
#ifdef _WIN32
#include <IXNetSystem.h>
#endif
#include "messageHandler.h"
#include "log.h"
typedef msg::SensorReading SensorClass;
// Helper class to store devices and access them outside of the library.
class DeviceClass {
public:
std::string deviceName;
std::string displayName;
std::vector<std::string> commandTypes;
std::vector<std::string> sensorTypes;
unsigned int deviceID;
};
// Main client class
class Client {
public:
// Constructor which initialized websockets for Windows. Add an IFDEF depending on compilation OS for portability.
Client(std::string url, unsigned int port) {
#ifdef _WIN32
ix::initNetSystem();
#endif
lUrl = url;
lPort = port;
}
Client(std::string url, unsigned int port, std::string logfile) {
#ifdef _WIN32
ix::initNetSystem();
#endif
lUrl = url;
lPort = port;
logging = 1;
if (!logfile.empty()) logInfo.init(logfile);
else logInfo.init("log.txt");
}
~Client() {
#ifdef _WIN32
ix::uninitNetSystem();
#endif
}
int connect(void (*callFunc)(const mhl::Messages));
// Atomic variables to store connection status. Can be accessed outside library too since atomic.
std::atomic<int> wsConnected{0};
std::atomic<int> isConnecting{0};
std::atomic<int> clientConnected{0};
// Condition variables for the atomics, we want C++11 support
std::condition_variable condClient;
std::condition_variable condWs;
// Public functions that send requests to server.
void startScan();
void stopScan();
void requestDeviceList();
void stopDevice(DeviceClass dev);
void stopAllDevices();
void sendScalar(DeviceClass dev, double str);
void sensorRead(DeviceClass dev, int senIndex);
void sensorSubscribe(DeviceClass dev, int senIndex);
void sensorUnsubscribe(DeviceClass dev, int senIndex);
// Mutex blocked function which grabs the currently connected devices and sensor reads.
std::vector<DeviceClass> getDevices();
SensorClass getSensors();
private:
// URL variables for the websocket.
std::string FullUrl;
std::string lUrl;
unsigned int lPort;
int logging = 0;
Logger logInfo;
ix::WebSocket webSocket;
// Message handler class, which takes messages, parses them and makes them to classes.
mhl::Messages messageHandler;
// Queue variable for passing received messages from server.
std::queue<std::string> q;
// Condition variabel to wait for received messages in the queue.
std::condition_variable cond;
// Mutex to ensure no race conditions.
std::mutex msgMx;
// Callback function for when a message is received and handled.
std::function<void(const mhl::Messages&)> messageCallback;
// Device and sensor class vector which is grabbed outside of the library.
std::vector<DeviceClass> devices;
SensorClass sensorData;
void connectServer();
void callbackFunction(const ix::WebSocketMessagePtr& msg);
void messageHandling();
void sendMessage(json msg, mhl::MessageTypes mType);
void updateDevices();
int findDevice(DeviceClass dev);
};

View file

@ -0,0 +1,42 @@
#include <string>
#include <vector>
#ifdef DEBUG
#define DEBUG_MSG(str) do { std::cout << str << std::endl; } while( false )
#else
#define DEBUG_MSG(str) do { } while ( false )
#endif
class DeviceCmdAttr {
public:
std::string FeatureDescriptor = "";
unsigned int StepCount = 0;
std::string ActuatorType = "";
std::string SensorType = "";
std::vector<int> SensorRange; // every two steps
//std::vector<std::string> Endpoints;
};
class DeviceCmd {
public:
std::string CmdType = "";
std::string StopDeviceCmd = "";
std::vector<DeviceCmdAttr> DeviceCmdAttributes;
};
class Device {
public:
std::string DeviceName;
unsigned int DeviceIndex;
unsigned int DeviceMessageTimingGap = 0;
std::string DeviceDisplayName = "";
std::vector<DeviceCmd> DeviceMessages;
};
class Scalar {
public:
unsigned int Index;
double ScalarVal;
std::string ActuatorType;
};

31
third_party/open-bp-cpp/include/log.h vendored Normal file
View file

@ -0,0 +1,31 @@
#include <fstream>
#include <vector>
#include <chrono>
class RequestQueue {
public:
unsigned int id = 1;
std::string requestType;
};
class Logger {
public:
Logger() {
start = std::chrono::system_clock::now();
}
~Logger() {
logFile.close();
}
void init(std::string filename);
void logSentMessage(std::string rqType, unsigned int id);
void logReceivedMessage(std::string repType, unsigned int id);
private:
std::fstream logFile;
std::vector<RequestQueue> rQueue;
// Using time point and system_clock
std::chrono::time_point<std::chrono::system_clock> start, end;
};

View file

@ -0,0 +1,95 @@
#include<string>
#include<map>
#include "messages.h"
namespace mhl {
// Enum class for message types for convenience.
enum class MessageTypes {
Ok,
Error,
Ping,
RequestServerInfo,
ServerInfo,
StartScanning,
StopScanning,
ScanningFinished,
RequestDeviceList,
DeviceList,
DeviceAdded,
DeviceRemoved,
StopDeviceCmd,
StopAllDevices,
ScalarCmd,
LinearCmd,
RotateCmd,
SensorReadCmd,
SensorReading,
SensorSubscribeCmd,
SensorUnsubscribeCmd
};
typedef std::map<MessageTypes, std::string> MessageMap_t;
// Class for request messages.
class Requests {
public:
msg::RequestServerInfo requestServerInfo;
msg::StartScanning startScanning;
msg::StopScanning stopScanning;
msg::ScanningFinished scanningFinished;
msg::RequestDeviceList requestDeviceList;
msg::DeviceRemoved deviceRemoved;
msg::StopDeviceCmd stopDeviceCmd;
msg::StopAllDevices stopAllDevices;
msg::ScalarCmd scalarCmd;
msg::SensorReadCmd sensorReadCmd;
msg::SensorSubscribeCmd sensorSubscribeCmd;
msg::SensorUnsubscribeCmd sensorUnsubscribeCmd;
};
// Class for messages received and for handling all types of messages.
class Messages {
public:
MessageTypes messageType = MessageTypes::Ok;
unsigned int Id;
// A map for message strings corresponding to enum. This is in this class since it is more convenient.
MessageMap_t messageMap = {
{MessageTypes::Ok, "Ok"},
{MessageTypes::Error, "Error"},
{MessageTypes::Ping, "Ping"},
{MessageTypes::RequestServerInfo, "RequestServerInfo"},
{MessageTypes::ServerInfo, "ServerInfo"},
{MessageTypes::StartScanning, "StartScanning"},
{MessageTypes::StopScanning, "StopScanning"},
{MessageTypes::ScanningFinished, "ScanningFinished"},
{MessageTypes::RequestDeviceList, "RequestDeviceList"},
{MessageTypes::DeviceList, "DeviceList"},
{MessageTypes::DeviceAdded, "DeviceAdded"},
{MessageTypes::DeviceRemoved, "DeviceRemoved"},
{MessageTypes::StopDeviceCmd, "StopDeviceCmd"},
{MessageTypes::StopAllDevices, "StopAllDevices"},
{MessageTypes::ScalarCmd, "ScalarCmd"},
{MessageTypes::LinearCmd, "LinearCmd"},
{MessageTypes::RotateCmd, "RotateCmd"},
{MessageTypes::SensorReadCmd, "SensorReadCmd"},
{MessageTypes::SensorReading, "SensorReading"},
{MessageTypes::SensorSubscribeCmd, "SensorSubscribeCmd"},
{MessageTypes::SensorUnsubscribeCmd, "SensorUnsubscribeCmd"}
};
msg::Ok ok;
msg::Error error;
msg::ServerInfo serverInfo;
msg::DeviceList deviceList;
msg::DeviceAdded deviceAdded;
msg::DeviceRemoved deviceRemoved;
msg::SensorReading sensorReading;
// Both server message and requests are handled in this class.
void handleServerMessage(json& msg);
json handleClientRequest(Requests req);
private:
};
}

View file

@ -0,0 +1,150 @@
#include <nlohmann/json.hpp>
#include "helperClasses.h"
#include <string>
#include <vector>
#include <iostream>
using json = nlohmann::json;
// Classes for the various message types and functions to convert to/from json.
namespace msg {
class Ok {
public:
unsigned int Id = 1;
//NLOHMANN_DEFINE_TYPE_INTRUSIVE(Ok, Id);
};
class Error {
public:
unsigned int Id = 1;
std::string ErrorMessage = "";
int ErrorCode = 0;
//NLOHMANN_DEFINE_TYPE_INTRUSIVE(Error, Id, ErrorMessage, ErrorCode);
};
class RequestServerInfo {
public:
unsigned int Id = 1;
std::string ClientName = "Default Client";
unsigned int MessageVersion = 3;
};
class ServerInfo {
public:
unsigned int Id;
std::string ServerName;
unsigned int MessageVersion;
unsigned int MaxPingTime;
//NLOHMANN_DEFINE_TYPE_INTRUSIVE(ServerInfo, Id, ServerName, MessageVersion, MaxPingTime);
};
class StartScanning {
public:
unsigned int Id = 1;
};
class StopScanning {
public:
unsigned int Id = 1;
};
class ScanningFinished {
public:
unsigned int Id = 1;
//NLOHMANN_DEFINE_TYPE_INTRUSIVE(ScanningFinished, Id);
};
class RequestDeviceList {
public:
unsigned int Id = 1;
};
class DeviceList {
public:
unsigned int Id = 1;
std::vector<Device> Devices;
};
class DeviceAdded {
public:
unsigned int Id = 1;
Device device;
};
class DeviceRemoved {
public:
unsigned int Id = 1;
unsigned int DeviceIndex;
};
class StopDeviceCmd {
public:
unsigned int Id = 1;
unsigned int DeviceIndex;
};
class StopAllDevices {
public:
unsigned int Id = 1;
};
class ScalarCmd {
public:
unsigned int Id = 1;
unsigned int DeviceIndex;
std::vector<Scalar> Scalars;
};
class SensorReadCmd {
public:
unsigned int Id = 1;
unsigned int DeviceIndex;
unsigned int SensorIndex;
std::string SensorType;
};
class SensorReading {
public:
unsigned int Id = 1;
unsigned int DeviceIndex;
unsigned int SensorIndex;
std::string SensorType;
std::vector<int> Data;
};
class SensorSubscribeCmd {
public:
unsigned int Id = 1;
unsigned int DeviceIndex;
unsigned int SensorIndex;
std::string SensorType;
};
class SensorUnsubscribeCmd {
public:
unsigned int Id = 1;
unsigned int DeviceIndex;
unsigned int SensorIndex;
std::string SensorType;
};
extern void to_json(json& j, const RequestServerInfo& k);
extern void to_json(json& j, const StartScanning& k);
extern void to_json(json& j, const StopScanning& k);
extern void to_json(json& j, const RequestDeviceList& k);
extern void to_json(json& j, const StopDeviceCmd& k);
extern void to_json(json& j, const StopAllDevices& k);
extern void to_json(json& j, const ScalarCmd& k);
extern void to_json(json& j, const SensorReadCmd& k);
extern void to_json(json& j, const SensorSubscribeCmd& k);
extern void to_json(json& j, const SensorUnsubscribeCmd& k);
extern void from_json(const json& j, Ok& k);
extern void from_json(const json& j, Error& k);
extern void from_json(const json& j, ServerInfo& k);
extern void from_json(const json& j, DeviceList& k);
extern void from_json(const json& j, DeviceAdded& k);
extern void from_json(const json& j, DeviceRemoved& k);
extern void from_json(const json& j, SensorReading& k);
}