|
|
@ -16,7 +16,7 @@ namespace iflytop {}; |
|
|
|
/*******************************************************************************
|
|
|
|
* TOOLS * |
|
|
|
*******************************************************************************/ |
|
|
|
static void getJsonValFromJson(json j, int& val) { |
|
|
|
static void getJsonValFromJson(json j, int &val) { |
|
|
|
if (j.is_string()) { |
|
|
|
string valstr = j; |
|
|
|
val = atoi(valstr.c_str()); |
|
|
@ -39,20 +39,189 @@ void ExtAPIService::dosystem(string order) { |
|
|
|
system(order.c_str()); |
|
|
|
} |
|
|
|
|
|
|
|
void ExtAPIService::initialize() { |
|
|
|
void ExtAPIService::initialize(string can_if_name, int baudrate, bool enablLoopback) { |
|
|
|
GET_TO_SERVICE(m_zconfig); |
|
|
|
|
|
|
|
m_zcan_receiver_master.reset(new ZcanReceiverMaster()); |
|
|
|
m_zcan_receiver_master->initialize(can_if_name, baudrate, enablLoopback); |
|
|
|
|
|
|
|
m_zmodule_device_manager.reset(new ZModuleDeviceManager()); |
|
|
|
m_zmodule_device_manager->initialize(m_zcan_receiver_master.get()); |
|
|
|
|
|
|
|
m_zmodule_device_script_cmder_paser.reset(new ZModuleDeviceScriptCmderPaser()); |
|
|
|
m_zmodule_device_script_cmder_paser->initialize(this, m_zmodule_device_manager.get()); |
|
|
|
|
|
|
|
#if 0
|
|
|
|
m_restfulServer.reset(new RestfulServer()); |
|
|
|
m_restfulServer->regAPI("/hello_world", RESTFUL_SERVER_BIND(ExtAPIService::hello_world)); |
|
|
|
m_restfulServer->start(20000, 20001, "0.0.0.0"); |
|
|
|
|
|
|
|
m_restfulServer->regAPI( //
|
|
|
|
"/doscript", //
|
|
|
|
[this](HttpRequestPtr request, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState>) { |
|
|
|
string script = request->body; |
|
|
|
// callcmd(script)
|
|
|
|
return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "hello_world"); |
|
|
|
}); |
|
|
|
m_iflytopwsService.reset(new IflytopFrontEndService()); |
|
|
|
m_iflytopwsService->initialize("0.0.0.0"); |
|
|
|
m_iflytopwsService->startListen(); |
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief 脚本服务 |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
m_script_server.reset(new WebSocketServer(19004, "0.0.0.0")); |
|
|
|
m_script_server->setOnConnectionCallback([this](weak_ptr<WebSocket> webSocket, shared_ptr<ConnectionState> connectionState) { |
|
|
|
logger->info("Remote ip: {}", connectionState->getRemoteIp()); |
|
|
|
auto ws = webSocket.lock(); |
|
|
|
if (!ws) return; |
|
|
|
ws->setOnMessageCallback([this, webSocket, connectionState](const ix::WebSocketMessagePtr &msg) { |
|
|
|
processcmds(msg->str, [this, webSocket, connectionState](string cmd, ICmdParserACK *ack) { //
|
|
|
|
string ackstr; |
|
|
|
if (ack->acktype == ICmdParserACK::kAckType_none) { |
|
|
|
ackstr = fmt::format("{} -> ecode:{}({})\n", cmd, err::error2str(ack->ecode), ack->ecode); |
|
|
|
} else if (ack->acktype == ICmdParserACK::kAckType_int32) { |
|
|
|
ackstr = fmt::format("{} -> ecode:{}({})\n", cmd, err::error2str(ack->ecode), ack->ecode); |
|
|
|
for (int32_t i = 0; i < ack->getAckInt32Num(); i++) { |
|
|
|
ackstr += fmt::format(" ack[{}]{}\n", i, ack->getAckInt32Val(i)); |
|
|
|
} |
|
|
|
} else if (ack->acktype == ICmdParserACK::kAckType_buf) { |
|
|
|
ackstr = fmt::format("{} -> ecode:{}({})\n", cmd, err::error2str(ack->ecode), ack->ecode); |
|
|
|
ackstr = fmt::format(" ackbuf :{}", StringUtils().bytesToString(ack->rawdata, ack->rawlen)); |
|
|
|
ackstr = fmt::format(" ackbufstr:{}", string((const char *)ack->rawdata)); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief CAN透传服务 |
|
|
|
*/ |
|
|
|
m_can_passthrough_server.reset(new WebSocketServer(19005, "0.0.0.0")); |
|
|
|
m_can_passthrough_server->setOnConnectionCallback([this](weak_ptr<WebSocket> webSocket, shared_ptr<ConnectionState> connectionState) { |
|
|
|
logger->info("m_can_passthrough_server on connect remote ip: {}", connectionState->getRemoteIp()); |
|
|
|
auto ws = webSocket.lock(); |
|
|
|
if (!ws) return; |
|
|
|
ws->setOnMessageCallback([this, webSocket, connectionState](const ix::WebSocketMessagePtr &msg) { |
|
|
|
// msg->binary;
|
|
|
|
if (msg->binary) { |
|
|
|
logger->info("rx:{}({})", StringUtils().bytesToString(msg->str.data(), msg->wireSize), msg->wireSize); |
|
|
|
m_zcan_receiver_master->sendraw((uint8_t *)msg->str.data(), msg->str.size()); |
|
|
|
} else { |
|
|
|
logger->info("rx:{}({})", msg->str, msg->wireSize); |
|
|
|
vector<uint8_t> rxbyte; |
|
|
|
StringUtils().hexStringToBytes(msg->str, "", rxbyte); |
|
|
|
m_zcan_receiver_master->sendraw(rxbyte.data(), rxbyte.size()); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
m_zcan_receiver_master->regPacketListener([this](int32_t fromboard, zcr_cmd_header_t *packet, int32_t datalen) { |
|
|
|
auto clients = m_can_passthrough_server->getClients(); |
|
|
|
string rx = StringUtils().bytesToString((uint8_t *)packet, datalen); |
|
|
|
logger->info("tx:{}({})", rx, datalen); |
|
|
|
for (auto &each : clients) { |
|
|
|
if (each) each->sendText(rx); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
m_zmodule_device_manager->regOnRegValChangeEvent([this](int32_t moduleid, int32_t regadd, int32_t toval) { //
|
|
|
|
logger->info("on reg change: moduleid:{} REG({}) to {}", moduleid, regadd, toval); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
HttpResponsePtr ExtAPIService::hello_world( //
|
|
|
|
HttpRequestPtr request, shared_ptr<RestfulServer::Context> context, std::shared_ptr<ConnectionState>) { |
|
|
|
return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), "hello_world"); |
|
|
|
void ExtAPIService::regCMD(const char *cmdname, const char *helpinfo, int paraNum, ICmdFunction_t cmdimpl) { |
|
|
|
cmd_container_t cmd_container; |
|
|
|
cmd_container.cmdname = cmdname; |
|
|
|
cmd_container.helpinfo = helpinfo; |
|
|
|
cmd_container.paraNum = paraNum; |
|
|
|
m_cmdmap[cmdname] = cmd_container; |
|
|
|
} |
|
|
|
|
|
|
|
void ExtAPIService::processcmds(string script, function<void(string cmd, ICmdParserACK *ack)> ackprocesser) { |
|
|
|
/**
|
|
|
|
* @brief |
|
|
|
* script: |
|
|
|
* cmd val1 val2 val3 |
|
|
|
* cmd val1 val2 val3 |
|
|
|
*/ |
|
|
|
|
|
|
|
vector<string> cmdlines; |
|
|
|
char *strcache = (char *)malloc(script.size() + 1); |
|
|
|
strncpy(strcache, script.c_str(), script.size()); |
|
|
|
int32_t stringlen = script.size(); |
|
|
|
for (int32_t i = 0; i < stringlen; i++) { |
|
|
|
if (strcache[i] == '\n' || strcache[i] == '\r') { |
|
|
|
strcache[i] = '\0'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
char *currentstr = nullptr; |
|
|
|
for (int32_t i = 0; i < stringlen; i++) { |
|
|
|
if (currentstr == nullptr) { |
|
|
|
if (strcache[i] != '\0') { |
|
|
|
currentstr = &strcache[i]; |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (strcache[i] == '\0') { |
|
|
|
cmdlines.push_back(currentstr); |
|
|
|
currentstr = nullptr; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (auto &cmdline : cmdlines) { |
|
|
|
ICmdParserACK ack = {0}; |
|
|
|
bool isnote = false; |
|
|
|
callcmd(cmdline, &ack, isnote); |
|
|
|
if (isnote) continue; |
|
|
|
if (ackprocesser) ackprocesser(cmdline, &ack); |
|
|
|
} |
|
|
|
free(strcache); |
|
|
|
} |
|
|
|
|
|
|
|
void removenote(char *buf); |
|
|
|
|
|
|
|
void ExtAPIService::callcmd(string cmd, ICmdParserACK *ack, bool &isnote) { |
|
|
|
char *argv[10]; |
|
|
|
int32_t argc = 0; |
|
|
|
|
|
|
|
char cmdcache[1024] = {0}; |
|
|
|
strncmp(cmdcache, cmd.c_str(), 1023); |
|
|
|
|
|
|
|
// remove note
|
|
|
|
for (size_t i = 0; i < cmd.length(); i++) { |
|
|
|
if (cmdcache[i] == '#') { |
|
|
|
cmdcache[i] = '\0'; |
|
|
|
if (i == 0) { |
|
|
|
isnote = true; |
|
|
|
return; |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
char *p = strtok(cmdcache, " "); |
|
|
|
while (p != NULL) { |
|
|
|
argv[argc] = p; |
|
|
|
argc++; |
|
|
|
p = strtok(NULL, " "); |
|
|
|
} |
|
|
|
|
|
|
|
callcmd(argv[0], argc - 1, (const char **)(argv + 1), ack); |
|
|
|
} |
|
|
|
void ExtAPIService::callcmd(string cmdname, int32_t paramN, const char **paraV, ICmdParserACK *ack) { |
|
|
|
std::lock_guard<std::recursive_mutex> lock(m_cmdmaplock_); |
|
|
|
auto it = m_cmdmap.find(cmdname); |
|
|
|
if (it == m_cmdmap.end()) { |
|
|
|
ack->ecode = err::kcmd_not_found; |
|
|
|
return; |
|
|
|
} |
|
|
|
cmd_container_t cmd_container = it->second; |
|
|
|
if (cmd_container.paraNum != paramN) { |
|
|
|
ack->ecode = err::kcmd_param_num_error; |
|
|
|
return; |
|
|
|
} |
|
|
|
cmd_container.cmdimpl(paramN, paraV, ack); |
|
|
|
} |