10 changed files with 304 additions and 7 deletions
-
2dep/zservice_container
-
9src/main.cpp
-
41src/service/main_control_service.cpp
-
17src/service/main_control_service.hpp
-
0webapp/debug/index.html
-
88webapp/debug/logger.html
-
0webapp/index.html
-
0webapp/js/.empty
-
5webapp/js/jquery.min.js
-
149webapp/js/zcmd.js
@ -1 +1 @@ |
|||
Subproject commit 956a5cc3a379cb53f38b2c4dadd086a29b05f2c8 |
|||
Subproject commit 02efa46cb128b1437a49a89f413fb17efb88cf31 |
@ -1,5 +1,44 @@ |
|||
#include "main_control_service.hpp"
|
|||
|
|||
#include "version.hpp"
|
|||
using namespace iflytop; |
|||
using namespace core; |
|||
using namespace std; |
|||
using namespace std; |
|||
|
|||
void MainControlService::initialize() { |
|||
GET_TO_SERVICE(m_zwebService); |
|||
|
|||
// 监听从webservice来的websocket消息
|
|||
m_zwebService->startWork([this](const json& command, json& receipt) { |
|||
try { |
|||
processReceiveMessage(kzwebService, command, receipt); |
|||
} catch (const std::exception& e) { |
|||
logger->error("process message fail {}", string(e.what())); |
|||
} catch (...) { |
|||
logger->error("process message fail {}", "catch unknown exception"); |
|||
} |
|||
}); |
|||
}; |
|||
void MainControlService::processReceiveMessage(fromwhere_t fromwhere, const json& in, json& receipt) { |
|||
logger->info("process receive message from {},{}", fromwhere2str(fromwhere), in.dump()); |
|||
|
|||
if (in["command"] == "getVersion") { |
|||
receipt["version"] = VERSION; |
|||
} |
|||
|
|||
/*********************************************************************************************************************
|
|||
* ================================================日志动态配置接口================================================= * |
|||
*********************************************************************************************************************/ |
|||
else if (in["command"] == "loggerSetLevel") { |
|||
int loggerLevel = in["loggerLevel"]; |
|||
string loggerName = in["loggerName"]; |
|||
logger->info("loggerSetLevel {} {}", loggerName, loggerLevel); |
|||
SpdLoggerFactory::Instance().createLogger(loggerName)->set_level((level::level_enum)loggerLevel); |
|||
} else if (in["command"] == "loggerGetAllLoggers") { |
|||
receipt["loggers"] = SpdLoggerFactory::Instance().loggerNames(); |
|||
} |
|||
|
|||
/*********************************************************************************************************************
|
|||
* ======================================================其他======================================================= * |
|||
*********************************************************************************************************************/ |
|||
} |
@ -0,0 +1,88 @@ |
|||
<!doctype html> |
|||
<html> |
|||
|
|||
<head> |
|||
<meta charset="utf-8"> |
|||
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script> |
|||
<script src="../js/zcmd.js"></script> |
|||
<meta name="viewport" content="width=device-width,initial-scale=1"> |
|||
<title>日志配置</title> |
|||
<style> |
|||
</style> |
|||
<script> |
|||
</script> |
|||
</head> |
|||
|
|||
<body> |
|||
<!-- |
|||
|
|||
选择框:main,service |
|||
选择框:等级(debug,info,warning,error) |
|||
设置日志等级:按钮 |
|||
|
|||
--> |
|||
<div> |
|||
<select id="logger"> |
|||
<option value="main">main</option> |
|||
<option value="service">service</option> |
|||
</select> |
|||
<select id="level"> |
|||
<option value=0>trace</option> |
|||
<option value=1>debug</option> |
|||
<option value=2>info</option> |
|||
<option value=3>warning</option> |
|||
<option value=4>error</option> |
|||
<option value=5>critical</option> |
|||
</select> |
|||
<button id="set">设置日志等级</button> |
|||
</div> |
|||
<script> |
|||
$(function () { |
|||
var zcmd = new ZCommand(); |
|||
zcmd.onsendraw = function (data/*json*/) { |
|||
console.log("zcmd send:" + JSON.stringify(data, null, 0)); |
|||
} |
|||
zcmd.onreceipt = function (tx, rx) { |
|||
console.log("zcmd receipt:" + JSON.stringify(rx, null, 0)); |
|||
} |
|||
ZWSURL = "ws://127.0.0.1:19000"; |
|||
|
|||
//连接后端 |
|||
zcmd.start_auto_connect(ZWSURL, () => { |
|||
console.log("zcmd connected"); |
|||
|
|||
//请求日志名称 |
|||
zcmd.send_message({ |
|||
"command": "loggerGetAllLoggers", |
|||
"need_receipt": true, |
|||
}, 4000).then((data) => { |
|||
console.log(data); |
|||
var html = ""; |
|||
for (var i = 0; i < data.loggers.length; i++) { |
|||
html += "<option value='" + data.loggers[i] + "'>" + data.loggers[i] + "</option>"; |
|||
} |
|||
$("#logger").html(html); |
|||
}); |
|||
|
|||
$("#set").click(function () { |
|||
zcmd.send_message({ |
|||
"command": "loggerSetLevel", |
|||
"need_receipt": true, |
|||
"loggerLevel": Number($("#level").val()), |
|||
"loggerName": $("#logger").val(), |
|||
}, 4000).then((data) => { |
|||
if (data.success) { |
|||
alert("设置成功"); |
|||
} else { |
|||
alert("设置失败") |
|||
} |
|||
}); |
|||
}); |
|||
}); |
|||
}); |
|||
</script> |
|||
|
|||
|
|||
</body> |
|||
|
|||
</html> |
5
webapp/js/jquery.min.js
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,149 @@ |
|||
function uuid() { |
|||
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => |
|||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) |
|||
) |
|||
} |
|||
var ZWSURL = "ws://" + window.location.hostname + ":" + window.location.port + "/ws/cmd"; |
|||
|
|||
class ZCommand { |
|||
/******************************************************************************************************************* |
|||
* ===================================================BASE======================================================== * |
|||
*******************************************************************************************************************/ |
|||
constructor() { |
|||
if (!('WebSocket' in window)) { |
|||
alert('当前浏览器 Not support websocket') |
|||
} |
|||
this.connected = false; |
|||
this.connecting = false; |
|||
this.cmdServerListener = new Map(); |
|||
this.onConnect; |
|||
this.firstConnect = true; |
|||
} |
|||
|
|||
start_auto_connect(wsurl, onFirstConnect) { |
|||
var ThisClass = this; |
|||
this.onFirstConnect = onFirstConnect; |
|||
this.wsurl = wsurl; |
|||
ThisClass.connectToCmdServer(); |
|||
setInterval(function () { |
|||
if (!ThisClass.isConnect()) { |
|||
console.info("ZcmdService try connect to server"); |
|||
ThisClass.connectToCmdServer(); |
|||
} |
|||
}, 2000); |
|||
} |
|||
|
|||
isConnect() { |
|||
return this.connected; |
|||
} |
|||
|
|||
isConnecting() { |
|||
return this.connecting; |
|||
} |
|||
connectToCmdServer() { |
|||
this.connecting = true; |
|||
console.info('websocket create socket:' + this.wsurl); |
|||
this.cmdServer = new WebSocket(this.wsurl); |
|||
var ThisClass = this; |
|||
|
|||
this.cmdServer.onopen = function () { |
|||
ThisClass.connecting = false; |
|||
ThisClass.connected = true; |
|||
console.info("ZcmdService connect to server"); |
|||
if (ThisClass.firstConnect && ThisClass.onFirstConnect) { |
|||
ThisClass.onFirstConnect(); |
|||
} |
|||
ThisClass.firstConnect = false; |
|||
} |
|||
|
|||
this.cmdServer.onerror = function () { |
|||
ThisClass.connecting = false; |
|||
ThisClass.connected = false; |
|||
console.info("websocket onerror"); |
|||
}; |
|||
var ThisClass = this; |
|||
this.cmdServer.onmessage = function (event) { |
|||
var obj = $.parseJSON(event.data); |
|||
var messageId = obj["messageId"]; |
|||
if (ThisClass.cmdServerListener.has(messageId)) { |
|||
var txcontext = ThisClass.cmdServerListener.get(messageId); |
|||
txcontext.onreceipt(txcontext, obj); |
|||
ThisClass.cmdServerListener.delete(messageId); |
|||
} else { |
|||
if (ThisClass.onreport) { |
|||
ThisClass.onreport(obj) |
|||
} |
|||
} |
|||
} |
|||
this.cmdServer.onclose = function () { |
|||
ThisClass.connected = false; |
|||
console.warn("websocket close"); |
|||
} |
|||
}; |
|||
|
|||
sendCmd(txcontext) { |
|||
var ThisClass = this; |
|||
var messageId = uuid();; |
|||
txcontext.message["messageId"] = messageId; |
|||
txcontext.message["need_receipt"] = true; |
|||
|
|||
// TODO:这里修改成异步等待链接成功,并发送
|
|||
if (!this.isConnect()) { |
|||
console.error("Servcer is unconnected"); |
|||
setTimeout(function () { |
|||
txcontext.onOvertime(); |
|||
}, txcontext.overtime) |
|||
return; |
|||
} |
|||
txcontext.timerId = setTimeout(function () { |
|||
console.error("cmd is overtime"); |
|||
txcontext.onOvertime(); |
|||
ThisClass.cmdServerListener.delete(messageId); |
|||
}, txcontext.overtime) |
|||
|
|||
txcontext.onreceipt = function (_txcontext, receipt) { |
|||
clearTimeout(_txcontext.timerId) |
|||
_txcontext.onMessage(receipt) |
|||
if (ThisClass.onreceipt) { |
|||
ThisClass.onreceipt(_txcontext.message, receipt) |
|||
} |
|||
} |
|||
this.cmdServerListener.set(messageId, txcontext); |
|||
if (this.onsendraw) { |
|||
this.onsendraw(txcontext.message); |
|||
} |
|||
this.cmdServer.send(JSON.stringify(txcontext.message)); |
|||
}; |
|||
|
|||
/******************************************************************************************************************* |
|||
* =================================================通用发送消息================================================== * |
|||
*******************************************************************************************************************/ |
|||
send_message(jsonobject, _overtime = 3000) { |
|||
var p = new Promise( |
|||
(resolve, reject) => { |
|||
this.sendCmd({ |
|||
message: jsonobject, |
|||
onMessage: function (message) { |
|||
if (resolve) |
|||
resolve(message); |
|||
}, |
|||
onOvertime: function () { }, |
|||
onError: function () { }, |
|||
overtime: _overtime, |
|||
}); |
|||
} |
|||
) |
|||
return p; |
|||
}; |
|||
set_onreceipt(onreceipt) { |
|||
this.onreceipt = onreceipt; |
|||
} |
|||
set_onreport(onreport) { |
|||
this.onreport = onreport; |
|||
} |
|||
set_onsendraw(onsendraw) { |
|||
this.onsendraw = onsendraw; |
|||
} |
|||
} |
|||
// var zcmd = new ZCommand();
|
|||
// zcmd.start_auto_connect(() => { });
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue