18 changed files with 443 additions and 302 deletions
-
1CMakeLists.txt
-
5libs/README.md
-
22src/components/channel/channel_factory.cpp
-
24src/components/channel/channel_factory.hpp
-
44src/components/channel/idata_channel.hpp
-
44src/components/channel/zcanchannel.cpp
-
46src/components/channel/zcanchannel.hpp
-
9src/components/zcanreceiver/socket_can/socket_can.cpp
-
39src/components/zservice_container/zservice_container.hpp
-
102src/extapi_service.cpp
-
65src/extapi_service.hpp
-
23src/main.cpp
-
44src/main_control_service.cpp
-
60src/main_control_service.hpp
-
0src/service/app_config_service.cpp
-
30src/service/app_config_service.hpp
-
135src/service/extapi_service.cpp
-
52src/service/extapi_service.hpp
@ -1 +1,4 @@ |
|||
相关说明参考: https://iflytop1.feishu.cn/wiki/wikcn6BuFh5CVsWhCiGgW5489Fe |
|||
相关说明参考: https://iflytop1.feishu.cn/wiki/wikcn6BuFh5CVsWhCiGgW5489Fe |
|||
|
|||
libixwebsocket: https://machinezone.github.io/IXWebSocket/usage/ |
|||
|
@ -0,0 +1,22 @@ |
|||
#include "channel_factory.hpp"
|
|||
|
|||
using namespace iflytop; |
|||
using namespace std; |
|||
|
|||
static IDataChannel::OnData_t s_ondata; |
|||
static map<string, IDataChannel *> s_channels; |
|||
|
|||
map<string, IDataChannel *> ChannelFactory::createChannels(string cfgfile) { |
|||
ZCanChannel *canChannel = new ZCanChannel("zcan", "can0", 500000); |
|||
canChannel->initialize(); |
|||
canChannel->registerOnDataCallback([](IDataChannel *ch, const string &data) { |
|||
if (s_ondata) s_ondata(ch, data); |
|||
}); |
|||
s_channels[canChannel->getChannelName()] = canChannel; |
|||
return s_channels; |
|||
|
|||
// canChannel->initialize(can_if_name, baudrate);
|
|||
// channels["zcan"] = canChannel;
|
|||
} |
|||
|
|||
void ChannelFactory::regOnChannelData(IDataChannel::OnData_t ondata) { s_ondata = ondata; } |
@ -0,0 +1,24 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
//
|
|||
#include "idata_channel.hpp"
|
|||
#include "zcanchannel.hpp"
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
|
|||
class ChannelFactory { |
|||
public: |
|||
static map<string, IDataChannel*> createChannels(string cfgfile); |
|||
static void regOnChannelData(IDataChannel::OnData_t ondata); |
|||
}; |
|||
|
|||
} // namespace iflytop
|
@ -0,0 +1,44 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
#include "thirdlib/nlohmann/json.hpp"
|
|||
namespace iflytop { |
|||
using namespace nlohmann; |
|||
using namespace std; |
|||
|
|||
class IDataChannel { |
|||
private: |
|||
/* data */ |
|||
public: |
|||
typedef std::function<void(IDataChannel* ch, const string& data)> OnData_t; |
|||
|
|||
public: |
|||
virtual string getChannelName() = 0; |
|||
virtual list<string> getCmdList() = 0; |
|||
|
|||
virtual void senddata(const string& data) = 0; |
|||
virtual void registerOnDataCallback(OnData_t ondata) = 0; |
|||
virtual void callcmd(string cmd, unordered_map<string, string> param, json& receipt) = 0; |
|||
|
|||
virtual ~IDataChannel() {} |
|||
|
|||
protected: |
|||
string paramToString(const unordered_map<string, string>& param) { |
|||
string ret; |
|||
for (auto& [key, value] : param) { |
|||
ret += key + "=" + value + "&"; |
|||
} |
|||
return ret; |
|||
} |
|||
}; |
|||
|
|||
} // namespace iflytop
|
@ -0,0 +1,44 @@ |
|||
#include "zcanchannel.hpp"
|
|||
|
|||
#include "utils/stringutils.hpp"
|
|||
using namespace nlohmann; |
|||
using namespace iflytop; |
|||
|
|||
ZCanChannel::ZCanChannel(string name, string can_if_name, int baudrate) { |
|||
m_chname = name; |
|||
m_can_if_name = can_if_name; |
|||
m_baudrate = baudrate; |
|||
logger->info("New ZCanChannel {}, canif:{}, baudrate:{}", m_chname, can_if_name, baudrate); |
|||
} |
|||
|
|||
void ZCanChannel::initialize() { |
|||
logger->info("{} initialize", m_chname); |
|||
m_zcanreceiverhost.reset(new ZCanReceiverHost()); |
|||
m_zcanreceiverhost->initialize(m_can_if_name, m_baudrate); |
|||
|
|||
m_zcanreceiverhost->registerListener([this](uint8_t fromboardid, uint8_t* packet, size_t len) { |
|||
string hexStr = StringUtils().bytesToString((uint8_t*)packet, len); |
|||
logger->info("RX:<- {}({})", m_chname, hexStr, len); |
|||
if (m_ondata) m_ondata(this, hexStr); |
|||
}); |
|||
} |
|||
|
|||
void ZCanChannel::senddata(const string& data) { |
|||
logger->info("{} TX:-> {}({})", m_chname, data, data.length()); |
|||
vector<uint8_t> rxbyte; |
|||
StringUtils().hexStringToBytes(data, "", rxbyte); |
|||
m_zcanreceiverhost->sendPacket(rxbyte.data(), rxbyte.size()); |
|||
} |
|||
void ZCanChannel::registerOnDataCallback(OnData_t ondata) { m_ondata = ondata; } |
|||
void ZCanChannel::callcmd(string cmd, unordered_map<string, string> param, json& receipt) { |
|||
logger->info("{} callcmd {} {}", m_chname, cmd, paramToString(param)); |
|||
if (cmd == "restart") { |
|||
m_zcanreceiverhost->restartCanInterface(); |
|||
} |
|||
} |
|||
|
|||
list<string> ZCanChannel::getCmdList() { |
|||
list<string> cmdlist; |
|||
cmdlist.push_back("restart"); |
|||
return cmdlist; |
|||
} |
@ -0,0 +1,46 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
#include "components/zcanreceiver/zcanreceiverhost.hpp"
|
|||
#include "components/zservice_container/zservice_container.hpp"
|
|||
#include "idata_channel.hpp"
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
using namespace nlohmann; |
|||
using namespace core; |
|||
|
|||
class ZCanChannel : public IDataChannel { |
|||
ENABLE_LOGGER(ZCanChannel); |
|||
|
|||
private: |
|||
/* data */ |
|||
|
|||
shared_ptr<ZCanReceiverHost> m_zcanreceiverhost; |
|||
OnData_t m_ondata; |
|||
string m_chname; |
|||
string m_can_if_name; |
|||
int m_baudrate; |
|||
|
|||
public: |
|||
ZCanChannel(string name, string can_if_name, int baudrate); |
|||
|
|||
virtual string getChannelName() { return m_chname; } |
|||
|
|||
void initialize(); |
|||
|
|||
virtual void senddata(const string& data); |
|||
virtual void registerOnDataCallback(OnData_t ondata); |
|||
virtual void callcmd(string cmd, unordered_map<string, string> param, json& receipt); |
|||
virtual list<string> getCmdList(); |
|||
}; |
|||
|
|||
} // namespace iflytop
|
@ -1,102 +0,0 @@ |
|||
#include "extapi_service.hpp"
|
|||
|
|||
#include "configs/project_setting.hpp"
|
|||
#include "configs/version.hpp"
|
|||
#include "ixwebsocket/IXWebSocketServer.h"
|
|||
#include "utils/stringutils.hpp"
|
|||
#include "utils/urlparser.hpp"
|
|||
|
|||
// #include "iflytop/components/zcanreceiver/zcanreceiverhost.hpp"
|
|||
// #include "iflytop/core/components/stringutils.hpp"
|
|||
// #include "iflytop/core/core.hpp"
|
|||
|
|||
using namespace iflytop; |
|||
using namespace core; |
|||
using namespace std; |
|||
using namespace nlohmann; |
|||
using namespace ix; |
|||
|
|||
#define BIND
|
|||
namespace iflytop {}; |
|||
void ExtAPIService::initialize(string can_if_name, int baudrate) { |
|||
GET_TO_SERVICE(m_zconfig); |
|||
|
|||
m_zcanreceiverhost.reset(new ZCanReceiverHost()); |
|||
m_zcanreceiverhost->initialize(can_if_name, baudrate); |
|||
|
|||
initCanPassthroughServer(); |
|||
}; |
|||
void ExtAPIService::processRXData(weak_ptr<WebSocket> webSocket, const ix::WebSocketMessagePtr &msg) {} |
|||
|
|||
void ExtAPIService::initCanPassthroughServer() { |
|||
m_httpServer.reset(new HttpServer(19004, "0.0.0.0")); |
|||
m_httpServer->listen(); |
|||
m_httpServer->start(); |
|||
m_httpServer->setOnConnectionCallback( //
|
|||
[this](HttpRequestPtr request, std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr { |
|||
HTTP_URL httpurl = URLParser::Parse(request->uri); |
|||
|
|||
if (httpurl.getPath() == "/restartCanif") { |
|||
logger->info("call {}", request->uri); |
|||
m_zcanreceiverhost->restartCanInterface(); |
|||
} else if (httpurl.getPath() == "/killself") { |
|||
logger->info("call {}", request->uri); |
|||
exit(1); |
|||
} else { |
|||
logger->warn("call unknown {}", httpurl.getPath()); |
|||
} |
|||
|
|||
return std::make_shared<HttpResponse>(200, "", HttpErrorCode::Ok, WebSocketHttpHeaders(), ""); |
|||
}); |
|||
|
|||
/**
|
|||
* |
|||
* protocol: websocket |
|||
* port : 19005 |
|||
* descript: 直接透传CAN数据,不做任何处理 |
|||
* |
|||
*/ |
|||
m_canPassthroughServer.reset(new WebSocketServer(19005, "0.0.0.0")); |
|||
m_canPassthroughServer->setOnConnectionCallback([this](weak_ptr<WebSocket> webSocket, shared_ptr<ConnectionState> connectionState) { |
|||
logger->info("url: {}", webSocket.lock()->getUrl()); |
|||
|
|||
logger->info("m_canPassthroughServer on connect remote ip: {}", connectionState->getRemoteIp()); |
|||
auto ws = webSocket.lock(); |
|||
if (!ws) return; |
|||
|
|||
ws->setOnMessageCallback([this, webSocket](const ix::WebSocketMessagePtr &msg) { //
|
|||
logger->info("m_canPassthroughServer on message {}", webSocket.lock()->getUrl()); |
|||
try { |
|||
if (msg->type == ix::WebSocketMessageType::Message) { |
|||
logger->info("websocket-ch -> {}({})", msg->str, msg->wireSize); |
|||
string hexStr = msg->str; |
|||
vector<uint8_t> rxbyte; |
|||
StringUtils().hexStringToBytes(hexStr, "", rxbyte); |
|||
m_zcanreceiverhost->sendPacket(rxbyte.data(), rxbyte.size()); |
|||
} else if (msg->type == ix::WebSocketMessageType::Open) { |
|||
logger->info("websocket-ch on open"); |
|||
} |
|||
} catch (const std::exception &e) { |
|||
logger->error("catch error: {}", e.what()); |
|||
} |
|||
}); |
|||
}); |
|||
|
|||
m_canPassthroughServer->setOnClientMessageCallback([this](std::shared_ptr<ConnectionState>, WebSocket &, const WebSocketMessagePtr &) { |
|||
|
|||
|
|||
|
|||
}); |
|||
|
|||
m_zcanreceiverhost->registerListener([this](uint8_t fromboardid, uint8_t *packet, size_t len) { |
|||
string hexStr = StringUtils().bytesToString((uint8_t *)packet, len); |
|||
auto clients = m_canPassthroughServer->getClients(); |
|||
logger->info("websocket-ch <- {}({})", hexStr, len); |
|||
|
|||
for (auto &each : clients) { |
|||
if (each) each->sendText(hexStr); |
|||
} |
|||
}); |
|||
|
|||
m_canPassthroughServer->listenAndStart(); |
|||
} |
@ -1,65 +0,0 @@ |
|||
//
|
|||
// Created by zwsd
|
|||
//
|
|||
|
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
// #include "iflytop/components/iflytop_front_end_service/iflytop_front_end_service.hpp"
|
|||
// #include "iflytop/components/restful_server/restful_server.hpp"
|
|||
// #include "iflytop/core/components/jobs/work_queue.hpp"
|
|||
// #include "iflytop/core/components/timer/simple_timer.hpp"
|
|||
// #include "iflytop/core/spdlogfactory/logger.hpp"
|
|||
// #include "iflytop/components/iflytop_front_end_service/iflytop_front_end_service.hpp"
|
|||
// #include "iflytoplinuxsdk/src/iflytop/core/components/zservice_container/zservice_container.hpp"
|
|||
|
|||
//
|
|||
#include "components/zcanreceiver/zcanreceiverhost.hpp"
|
|||
#include "components/zservice_container/zservice_container.hpp"
|
|||
#include "configs/gconfig.hpp"
|
|||
#include "ixwebsocket/IXHttp.h"
|
|||
#include "ixwebsocket/IXHttpServer.h"
|
|||
#include "ixwebsocket/IXWebSocketServer.h"
|
|||
#include "spdlogfactory/logger.hpp"
|
|||
//
|
|||
|
|||
//
|
|||
#include <mutex>
|
|||
|
|||
/**
|
|||
* @brief |
|||
* |
|||
* service: 对外暴露的接口服务 |
|||
* |
|||
*/ |
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
using namespace core; |
|||
using namespace ix; |
|||
|
|||
class ExtAPIService { |
|||
ENABLE_LOGGER(ExtAPIService); |
|||
|
|||
shared_ptr<GConfig> m_zconfig; |
|||
shared_ptr<ix::WebSocketServer> m_canPassthroughServer; // 19005
|
|||
shared_ptr<HttpServer> m_httpServer;//19004
|
|||
shared_ptr<ZCanReceiverHost> m_zcanreceiverhost; |
|||
|
|||
public: |
|||
ExtAPIService() {}; |
|||
void initialize(string can_if_name, int baudrate); |
|||
|
|||
private: |
|||
void initCanPassthroughServer(); |
|||
void processRXData(weak_ptr<WebSocket> webSocket, const ix::WebSocketMessagePtr &msg); |
|||
}; |
|||
} // namespace iflytop
|
@ -1,44 +0,0 @@ |
|||
#include "main_control_service.hpp"
|
|||
|
|||
#include "configs/project_setting.hpp"
|
|||
#include "configs/version.hpp"
|
|||
#include "utils/stringutils.hpp"
|
|||
//
|
|||
#include "components/zservice_container/zservice_container.hpp"
|
|||
//
|
|||
|
|||
using namespace iflytop; |
|||
using namespace core; |
|||
using namespace std; |
|||
using namespace nlohmann; |
|||
|
|||
#define OBJECT GET_SERVICE
|
|||
|
|||
namespace iflytop {}; |
|||
void MainControlService::initialize(int argc, char *argv[]) { |
|||
/**
|
|||
* @brief 系统初始化 |
|||
*/ |
|||
logger->info("system setup start."); |
|||
spdlog::flush_on(spdlog::level::debug); |
|||
logger->info("#"); |
|||
logger->info("# company:{}", "ifytop"); |
|||
logger->info("# version:{}", VERSION); |
|||
logger->info("#"); |
|||
logger->info("build {}.....", "Config"); |
|||
|
|||
initializeService(); |
|||
}; |
|||
|
|||
void MainControlService::initializeService() { |
|||
BUILD_AND_REG_SERRVICE(GConfig); |
|||
OBJECT(GConfig)->initialize(); |
|||
|
|||
m_extAPIService.initialize(OBJECT(GConfig)->get_canName(), OBJECT(GConfig)->get_canBaudRate()); |
|||
|
|||
|
|||
// ZCanReceiverHost *m_zcanreceiverhost = new ZCanReceiverHost();
|
|||
// m_zcanreceiverhost->initialize(OBJECT(GConfig)->get_canName(), OBJECT(GConfig)->get_canBaudRate());
|
|||
// m_zcanreceiverhost->sendPacket((uint8_t *)"hello", 5);
|
|||
|
|||
} |
@ -1,60 +0,0 @@ |
|||
//
|
|||
// Created by zwsd
|
|||
//
|
|||
|
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
// #include "iflytop/components/restful_server/restful_server.hpp"
|
|||
// #include "iflytop/core/components/jobs/work_queue.hpp"
|
|||
// #include "iflytop/core/components/timer/simple_timer.hpp"
|
|||
// #include "iflytop/core/spdlogfactory/logger.hpp"
|
|||
// #include "iflytoplinuxsdk/src/iflytop/components/iflytop_front_end_service/iflytop_front_end_service.hpp"
|
|||
// #include "configs/gconfig.hpp"
|
|||
// #include "iflytop/components/iflytop_front_end_service/iflytop_front_end_service.hpp"
|
|||
// #include "iflytoplinuxsdk/src/iflytop/core/components/zservice_container/zservice_container.hpp"
|
|||
//
|
|||
|
|||
#include "extapi_service.hpp"
|
|||
#include "spdlogfactory/logger.hpp"
|
|||
//
|
|||
|
|||
//
|
|||
// #include "extapi_service.hpp"
|
|||
|
|||
/**
|
|||
* @brief |
|||
* |
|||
* service: MainControlService |
|||
* |
|||
* 监听事件: |
|||
* 依赖状态: |
|||
* 依赖服务: |
|||
* 作用: |
|||
* |
|||
*/ |
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
using namespace core; |
|||
class MainControlService : public enable_shared_from_this<MainControlService> { |
|||
ENABLE_LOGGER(MainControlService); |
|||
|
|||
ExtAPIService m_extAPIService; |
|||
|
|||
public: |
|||
MainControlService() {}; |
|||
void initialize(int argc, char *argv[]); |
|||
|
|||
private: |
|||
void initializeService(); |
|||
}; |
|||
} // namespace iflytop
|
@ -0,0 +1,30 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <toml++/toml.hpp>
|
|||
#include <vector>
|
|||
|
|||
//
|
|||
#include "spdlogfactory/logger.hpp"
|
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
using namespace core; |
|||
|
|||
class AppConfigService { |
|||
ENABLE_LOGGER(AppConfigService); |
|||
|
|||
private: |
|||
/* data */ |
|||
public: |
|||
void initialize(); |
|||
}; |
|||
|
|||
} // namespace iflytop
|
@ -0,0 +1,135 @@ |
|||
#include "extapi_service.hpp"
|
|||
|
|||
#include "configs/project_setting.hpp"
|
|||
#include "configs/version.hpp"
|
|||
#include "ixwebsocket/IXWebSocketServer.h"
|
|||
#include "utils/stringutils.hpp"
|
|||
#include "utils/urlparser.hpp"
|
|||
|
|||
// #include "iflytop/components/zcanreceiver/zcanreceiverhost.hpp"
|
|||
// #include "iflytop/core/components/stringutils.hpp"
|
|||
// #include "iflytop/core/core.hpp"
|
|||
#include "components/channel/channel_factory.hpp"
|
|||
|
|||
using namespace iflytop; |
|||
using namespace core; |
|||
using namespace std; |
|||
using namespace nlohmann; |
|||
using namespace ix; |
|||
|
|||
namespace iflytop {}; |
|||
|
|||
static map<string, IDataChannel *> channels; |
|||
// static map<string,
|
|||
|
|||
static IDataChannel *findChannel(string name) { |
|||
if (channels.find(name) != channels.end()) { |
|||
return channels[name]; |
|||
} |
|||
return nullptr; |
|||
} |
|||
|
|||
void ExtAPIService::initialize() { |
|||
channels = ChannelFactory::createChannels("config.ini"); |
|||
|
|||
logger->info("New HttpServer listen on 19004"); |
|||
m_httpServer.reset(new HttpServer(19004, "0.0.0.0")); |
|||
m_httpServer->setOnConnectionCallback( //
|
|||
[this](HttpRequestPtr request, std::shared_ptr<ConnectionState> connectionState) -> HttpResponsePtr { |
|||
HTTP_URL httpurl = URLParser::Parse(request->uri); |
|||
|
|||
json receipt; |
|||
receipt["status"] = 0; |
|||
receipt["msg"] = "success"; |
|||
|
|||
// path: /cmd/????
|
|||
if (httpurl.path.size() == 3 && httpurl.path[0] == "cmd") { |
|||
string channelName = httpurl.path[1]; |
|||
string cmd = httpurl.path[2]; |
|||
auto params = httpurl.query; |
|||
|
|||
// path: /cmd/server/restart
|
|||
if (channelName == "server" && cmd == "restart") { |
|||
exit(0); |
|||
} |
|||
// path: /cmd/channelName/xxxxxx
|
|||
else { |
|||
auto channel = findChannel(channelName); |
|||
if (channel) { |
|||
channel->callcmd(cmd, params, receipt); |
|||
} else { |
|||
receipt["status"] = 1; |
|||
receipt["msg"] = "channel not found"; |
|||
} |
|||
} |
|||
} |
|||
|
|||
return std::make_shared<HttpResponse>(200, "", HttpErrorCode::Ok, WebSocketHttpHeaders(), receipt.dump(0)); |
|||
}); |
|||
|
|||
if (!m_httpServer->listen().first) { |
|||
logger->error("HttpServer listen failed"); |
|||
exit(1); |
|||
} |
|||
m_httpServer->start(); |
|||
|
|||
/**
|
|||
* |
|||
* protocol: websocket |
|||
* port : 19005 |
|||
* descript: 直接透传CAN数据,不做任何处理 |
|||
* |
|||
*/ |
|||
logger->info("New webSocketServer listen on 19005"); |
|||
m_wsServer.reset(new WebSocketServer(19005, "0.0.0.0")); |
|||
m_wsServer->setOnClientMessageCallback( |
|||
[this](std::shared_ptr<ix::ConnectionState> connectionState, ix::WebSocket &webSocket, const ix::WebSocketMessagePtr &msg) { |
|||
if (msg->type == ix::WebSocketMessageType::Open) { |
|||
logger->info("new connect ip: {}", connectionState->getRemoteIp()); |
|||
|
|||
// path: /channelName
|
|||
logger->info(" uri: {}", msg->openInfo.uri); |
|||
webSocket.setUrl(msg->openInfo.uri); |
|||
|
|||
logger->info(" headers:"); |
|||
for (auto it : msg->openInfo.headers) { |
|||
logger->info("\t{}: {}", it.first, it.second); |
|||
} |
|||
} else if (msg->type == ix::WebSocketMessageType::Message) { |
|||
string chname = webSocket.getUrl().substr(1); |
|||
auto channel = findChannel(chname); |
|||
if (channel) { |
|||
channel->senddata(msg->str); |
|||
} else { |
|||
logger->error("channel not found: {}", chname); |
|||
} |
|||
} |
|||
}); |
|||
|
|||
// On Data From Channel
|
|||
ChannelFactory::regOnChannelData([this](IDataChannel *channel, string data) { |
|||
auto clients = m_wsServer->getClients(); |
|||
for (auto it : clients) { |
|||
string chname = it->getUrl().substr(1); |
|||
IDataChannel *bindchannel = findChannel(chname); |
|||
if (bindchannel == channel) { |
|||
it->sendText(data); |
|||
} |
|||
} |
|||
}); |
|||
|
|||
if (!m_wsServer->listenAndStart()) { |
|||
logger->error("WebSocketServer listenAndStart failed"); |
|||
exit(1); |
|||
} |
|||
|
|||
//
|
|||
logger->info("Support Channel List"); |
|||
for (auto it : channels) { |
|||
logger->info("\t{}", it.second->getChannelName()); |
|||
logger->info("\t ws://0.0.0.0:19905/{}", it.second->getChannelName()); |
|||
for (auto cmd : it.second->getCmdList()) { |
|||
logger->info("\t http:/0.0.0.0:19004/cmd/{}/{}", it.second->getChannelName(), cmd); |
|||
} |
|||
} |
|||
}; |
@ -0,0 +1,52 @@ |
|||
//
|
|||
// Created by zwsd
|
|||
//
|
|||
|
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
//
|
|||
#include "components/channel/channel_factory.hpp"
|
|||
#include "components/zcanreceiver/zcanreceiverhost.hpp"
|
|||
#include "components/zservice_container/zservice_container.hpp"
|
|||
//
|
|||
#include "ixwebsocket/IXHttp.h"
|
|||
#include "ixwebsocket/IXHttpServer.h"
|
|||
#include "ixwebsocket/IXWebSocketServer.h"
|
|||
#include "spdlogfactory/logger.hpp"
|
|||
//
|
|||
//
|
|||
#include <mutex>
|
|||
|
|||
/**
|
|||
* @brief |
|||
* |
|||
* service: 对外暴露的接口服务 |
|||
* |
|||
*/ |
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
using namespace core; |
|||
using namespace ix; |
|||
|
|||
class ExtAPIService { |
|||
ENABLE_LOGGER(ExtAPIService); |
|||
|
|||
shared_ptr<ix::WebSocketServer> m_wsServer; // 19005
|
|||
shared_ptr<HttpServer> m_httpServer; // 19004
|
|||
|
|||
public: |
|||
ExtAPIService() {}; |
|||
void initialize(); |
|||
|
|||
}; |
|||
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue