mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
实现MQTT功能, 修改HTTP接口
This commit is contained in:
200
src/main.cpp
200
src/main.cpp
@@ -20,108 +20,150 @@
|
||||
|
||||
#include "rlsocket.h"
|
||||
#include "common/Spdlogger.h"
|
||||
|
||||
|
||||
|
||||
#include "database/DaoEntity.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "DataStruct.h"
|
||||
|
||||
#define wsa rlwsa
|
||||
int main(int argc, char** argv)
|
||||
void rlSocketTest()
|
||||
{
|
||||
rlwsa();
|
||||
rlSocket socket("127.0.0.1", 19801, 1);
|
||||
int ret = socket.connect();
|
||||
std::string s1 = "helloworld";
|
||||
socket.write(s1.c_str(), s1.size());
|
||||
|
||||
std::vector<char> buf(1024, 0);
|
||||
while (true)
|
||||
{
|
||||
NJsonNode jsonroot;
|
||||
NJson::parse(R"({"name": "Alice", "age": 25, "data":[["1","1","1"],["1","1","1"],["1","1","1"]]})", jsonroot);
|
||||
int len = socket.read(&buf[0], 1, 0);
|
||||
if (len > 0)
|
||||
{
|
||||
std::cout << "===>>> " << std::string(buf.begin(), buf.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<std::string>> v1;
|
||||
NJson::read<std::vector<std::vector<std::string>>>(jsonroot, "data", v1);
|
||||
void memberJsonTest()
|
||||
{
|
||||
std::string fromJson = "fromJson(const std::string& str) {\nNJsonNode jsonroot;\nauto ret = NJson::parse(str, jsonroot);\nif (!ret) { return; }\n";
|
||||
std::string toJson = "toJson() {\nNJsonNode jsonroot;";
|
||||
|
||||
std::vector<std::vector<std::string>> vec = {
|
||||
{"1", "1", "1", "1", "1", "1", "1", "1"},
|
||||
{"1", "1", "1", "1", "1", "1", "1", "1"},
|
||||
{"1", "1", "1", "1", "1", "1", "1", "1"},
|
||||
{"1", "1", "1", "1", "1", "1", "1", "1"}
|
||||
};
|
||||
|
||||
NJsonNode jsonroot1;
|
||||
jsonroot1["price_super_peak"] = 0.53;
|
||||
jsonroot1["price_peak"] = 0.53;
|
||||
jsonroot1["price_shoulder"] = 0.53;
|
||||
jsonroot1["price_off_peak"] = 0.53;
|
||||
jsonroot1["periods"] = vec;
|
||||
|
||||
std::cout << jsonroot1.dump() << std::endl;
|
||||
std::ifstream ifs("", std::ios::in);
|
||||
std::string line;
|
||||
std::string cpp = "#include \"DataStruct.h\"\n\n";
|
||||
if (ifs.is_open())
|
||||
{
|
||||
std::string s1;
|
||||
std::string s2;
|
||||
while (std::getline(ifs, line))
|
||||
{
|
||||
int pos = line.find("struct");
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
std::string className = line.substr(pos+7);
|
||||
s1 = "void " + className + "::" + fromJson;
|
||||
s2 = "}\nstd::string " + className + "::" + toJson;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string key;
|
||||
pos = line.find("uint");
|
||||
if (pos != std::string::npos) { key = line.substr(pos+9); }
|
||||
if (!key.empty())
|
||||
{
|
||||
pos = key.find(";");
|
||||
if (pos != std::string::npos) { key = key.substr(0, pos); }
|
||||
}
|
||||
if (!key.empty())
|
||||
{
|
||||
s1 += ("NJson::read(jsonroot, \"" + key + "\", " + key + ");\n");
|
||||
s2 += ("jsonroot[\"" + key + "\"] = " + key + ";\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
ifs.close();
|
||||
}
|
||||
|
||||
|
||||
//std::string strTmp = R"()";
|
||||
//std::vector<std::string> vecTmp;
|
||||
//Utils::split(strTmp, "\n", vecTmp);
|
||||
|
||||
//std::string from = "void fromJson(const std::string& str) {\nNJsonNode jsonroot;\nauto ret = NJson::parse(str, jsonroot);\nif (!ret) { return; }\n";
|
||||
//std::string to = "std::string toJson() {\nNJsonNode jsonroot;";
|
||||
//for (auto& item: vecTmp)
|
||||
//{
|
||||
// std::string key;
|
||||
// int pos = item.find_first_of("_");
|
||||
// if (pos != std::string::npos) { key = item.substr(pos+3); }
|
||||
// pos = key.find_first_of(";");
|
||||
// if (pos != std::string::npos) { key = key.substr(0, pos); }
|
||||
|
||||
// if (!key.empty())
|
||||
// {
|
||||
// from += ("NJson::read(jsonroot, \"" + key + "\", " + key + ");\n");
|
||||
// to += ("jsonroot[\"" + key + "\"] = " + key + ";\n");
|
||||
// }
|
||||
//}
|
||||
//from += "}";
|
||||
//to += "return jsonroot.dump();\n}";
|
||||
//std::cout << from << std::endl;
|
||||
//std::cout << to << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// 设置控制台输出为 UTF-8 编码
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
// 设置控制台输入为 UTF-8 编码(如果需要输入中文)
|
||||
SetConsoleCP(CP_UTF8);
|
||||
|
||||
Spdlogger::init(spdlog::level::debug, "");
|
||||
spdlog::info("[main] start ... ====================================================================================================");
|
||||
spdlog::info("spd info");
|
||||
spdlog::debug("spd debug");
|
||||
spdlog::error("spd error");
|
||||
spdlog::info("[main] start ... ======================================================================");
|
||||
|
||||
|
||||
//rlwsa();
|
||||
//rlSocket socket("127.0.0.1", 19801, 1);
|
||||
//int ret = socket.connect();
|
||||
//std::string s1 = "helloworld";
|
||||
//socket.write(s1.c_str(), s1.size());
|
||||
|
||||
//std::vector<char> buf(1024, 0);
|
||||
//while (true)
|
||||
//{
|
||||
// int len = socket.read(&buf[0], 1, 0);
|
||||
// if (len > 0)
|
||||
// {
|
||||
// std::cout << "===>>> " << std::string(buf.begin(), buf.end());
|
||||
// }
|
||||
//}
|
||||
|
||||
////std::cout << Snowflake::instance().getId() << std::endl;
|
||||
//for (int i = 0; i<=10; ++i) {
|
||||
// //std::cout << Snowflake::instance().getId() << std::endl;
|
||||
//}
|
||||
|
||||
|
||||
//DaoEntity dao("");
|
||||
//
|
||||
//std::string filename = "assets/html/data中文.txt";
|
||||
//
|
||||
//std::filesystem::path filePath = std::filesystem::u8path("assets/html/data中文.txt");
|
||||
////std::locale::global(locale(""));//将全局区域设为操作系统默认区域
|
||||
//std::ifstream ifs(filePath, std::ios::binary);
|
||||
////std::locale::global(locale("C"));//还原全局区域设定
|
||||
|
||||
//if (ifs.is_open())
|
||||
//{
|
||||
// // 将文件指针移动到末尾获取文件大小
|
||||
// ifs.seekg(0, std::ios::end);
|
||||
// std::streamsize size = ifs.tellg();
|
||||
// ifs.seekg(0, std::ios::beg);
|
||||
|
||||
// std::string buf(size, '\0');
|
||||
// ifs.read(&buf[0], size);
|
||||
// std::cout << "文件内容: " << buf << std::endl;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// std::cout << "error" << std::endl;
|
||||
//}
|
||||
|
||||
//TcpEntity tcpEntity;
|
||||
//tcpEntity.setHost("127.0.0.1", 9901, true);
|
||||
//tcpEntity.setReconnect(5000);
|
||||
//tcpEntity.start();
|
||||
std::cout << Snowflake::instance().getId() << std::endl;
|
||||
for (int i = 0; i<=10; ++i) {
|
||||
std::cout << Snowflake::instance().getId() << std::endl;
|
||||
}
|
||||
|
||||
// 运行后台服务
|
||||
Application::instance().init();
|
||||
|
||||
{
|
||||
//REGInfo reg;
|
||||
//std::string s = "BMS(电池堆)通信状态 R uint16 \"0:正常1:故障\" bit位从低到高分别对应1~16 0x2001";
|
||||
//int pos;
|
||||
//if (std::string::npos != (pos = s.find("\t0x")))
|
||||
//{
|
||||
// reg.name = s.substr(pos+1);
|
||||
// s = s.substr(0, pos);
|
||||
// std::cout << reg.name << std::endl;
|
||||
//}
|
||||
//if (std::string::npos != (pos = s.find("\t")))
|
||||
//{
|
||||
// reg.remark = s.substr(0, pos);
|
||||
// s = s.substr(pos+1);
|
||||
// std::cout << reg.remark << std::endl;
|
||||
//}
|
||||
//if (std::string::npos != (pos = s.find("uint")))
|
||||
//{
|
||||
// std::string bytename = s.substr(pos, 6);
|
||||
// std::cout << bytename << std::endl;
|
||||
// s = s.substr(pos+6);
|
||||
//}
|
||||
//std::cout << s << std::endl;
|
||||
//std::cout << s << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 运行QT主窗口
|
||||
//QApplication qapp(argc, argv);
|
||||
//MainWindow mainWin;
|
||||
|
||||
Reference in New Issue
Block a user