-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebFrontend.cpp
68 lines (50 loc) · 1.32 KB
/
WebFrontend.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright (c) 2024 Quantag IT Solutions GmbH
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "WebFrontend.h"
#include "Log.h"
#include "ws/WSSession.h"
#include "Utils.h"
WebFrontend::WebFrontend() {
this->wsSession = nullptr;
}
WebFrontend::~WebFrontend() {
}
int WebFrontend::loadCode(const std::string& sourceCode) {
LOGI("");
if (!wsSession) return 1;
nlohmann::json json;
json["command"] = "load";
std::string encodedSourceCode = Utils::encode64(sourceCode);
json["code"] = encodedSourceCode;
return send(json);
}
void to_json(nlohmann::json& j, const complexNumber& c) {
j = nlohmann::json{ {"a", c.a}, {"b", c.b} };
}
void to_json(nlohmann::json& j, const matrix2d& matrix) {
for (const auto& row : matrix) {
j.push_back(row);
}
}
int WebFrontend::updateState(const FrontState& state) {
LOGI("line = %d", state.currentLine);
if (!wsSession) return 1;
nlohmann::json json;
json["command"] = "update";
json["line"] = state.currentLine;
json["states"] = state.states;
json["code"] = state.code;
return send(json);
}
int WebFrontend::send(const nlohmann::json& _data) {
if (!wsSession)
return 1;
std::string data = _data.dump();
LOGI("FRONTEND>> '%s'", data.c_str());
wsSession->send(data);
return 0;
}