Index.cshtml
7.62 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
@using HHECS.DAQWebClient.Dtos
@model List<LogModel>
@{
ViewData["Title"] = "控制台";
}
<div class="layui-card">
<div class="layui-card-body layui-form" style="margin-left:10px">
<button id="start" class="layui-btn layui-btn-sm" lay-on="start"><i class="fa fa-play" aria-hidden="true"></i>启动</button>
<button id="stop" class="layui-btn layui-btn-sm layui-bg-orange layui-btn-disabled" lay-on="stop"><i class="fa fa-stop" aria-hidden="true"></i>停止</button>
<input type="checkbox" name="autoCommit" title="自动上传" lay-skin="tag" lay-filter="autoCommit-checkbox-filter">
<input type="checkbox" name="dataCompression" title="节流模式" lay-skin="tag" lay-filter="dataCompression-checkbox-filter">
<span>队列缓存:<span id="queue">0</span></span>
</div>
</div>
<div class="layui-card">
<div class="layui-card-header">日志</div>
<div class="layui-card-body" id="logPanel">
@foreach (var item in Model)
{
var css = item.Level switch
{
LogLevel.Information => "layui-bg-green",
//LogLevel.Information => "layui-bg-blue",
LogLevel.Warning => "layui-bg-orange",
LogLevel.Error or
LogLevel.Debug => "layui-bg-red",
_ => "layui-bg-gray"
};
<div id="" class="@css" style="padding:0px 5px;margin-top:1px">
@($"{item.CreateTime}:{item.Message}")
</div>
}
</div>
</div>
@section Scripts{
<script type="text/javascript" asp-append-version="true">
layui.use(['util', 'form'], function () {
let $ = layui.$;
let form = layui.form;
let util = layui.util;
// Start the connection.
var connection = new signalR.HubConnectionBuilder()
.withUrl('/DAQHub')
.withAutomaticReconnect()
.build();
// Create a function that the hub can call to broadcast messages.
connection.on('pringtLog', function (log) {
//console.log(log);
let css = '';
switch (log.level) {
case 2:
css = 'layui-bg-green'
break;
case 3:
css = 'layui-bg-orange'
break;
case 1:
case 4:
css = 'layui-bg-red'
break;
default:
css = 'layui-bg-gray'
break;
}
let newDiv = `<div class="${css}" style="padding:0px 5px;margin-top:1px">${new Date(log.createTime).toLocaleString()}:${log.message}</div >`;
// 将新元素添加到父div中
$('#logPanel').prepend(newDiv);
if ($('#logPanel').children().length > 30) {
$('#logPanel div:last-child').remove();
}
});
connection.on('statusNotice', function (result) {
initialStatus(result);
});
connection.on('queueNotice', function (val) {
$('#queue').text(val);
});
// Transport fallback functionality is now built into start.
connection.start().then(function () {
connection.invoke("consoleStatus").then(function (result) {
if (result.code != 200) {
layer.msg(result.msg, { icon: 2 });
return;
}
initialStatus(result.data);
}).catch(function (err) {
layer.msg(err.toString(), { icon: 2 });
})
console.log('connection started');
}).catch(error => {
layer.msg(error.message, { icon: 2 });
console.error(error.message);
});
util.on({
start: function () {
let dom = $(this);
let disabled = dom.hasClass('layui-btn-disabled');
if (disabled) {
return;
}
connection.invoke("start")
.then(function (result) {
if (result.code != 200) {
layer.msg(result.msg, { icon: 2 });
return;
}
startBtnChanged(true);
}).catch(function (err) {
layer.msg(err.toString(), { icon: 2 });
});
},
stop: function () {
let dom = $(this);
let disabled = dom.hasClass('layui-btn-disabled');
if (disabled) {
return;
}
connection.invoke("stop")
.then(function (result) {
if (result.code != 200) {
layer.msg(result.msg, { icon: 2 });
return;
}
startBtnChanged(false);
}).catch(function (err) {
layer.msg(err.toString(), { icon: 2 });
});
}
});
form.on('checkbox(autoCommit-checkbox-filter)', function (data) {
let elem = data.elem; // 获得 checkbox 原始 DOM 对象
connection.invoke("autoCommit", elem.checked)
.then(function (result) {
if (result.code != 200) {
layer.msg(result.msg, { icon: 2 });
return;
}
}).catch(function (err) {
layer.msg(err.toString(), { icon: 2 });
});
});
form.on('checkbox(dataCompression-checkbox-filter)', function (data) {
let elem = data.elem; // 获得 checkbox 原始 DOM 对象
connection.invoke("dataCompression", elem.checked)
.then(function (result) {
if (result.code != 200) {
layer.msg(result.msg, { icon: 2 });
return;
}
}).catch(function (err) {
layer.msg(err.toString(), { icon: 2 });
});
});
function initialStatus(data) {
startBtnChanged(data.isStart);
autoCommitChanged(data.autoCommit);
dataCompressionChanged(data.dataCompression);
}
//启动按钮
function startBtnChanged(isStart) {
if (isStart) {
$('#start').addClass('layui-btn-disabled');
$('#stop').removeClass('layui-btn-disabled');
} else {
$('#start').removeClass('layui-btn-disabled');
$('#stop').addClass('layui-btn-disabled');
}
}
//自动上传
function autoCommitChanged(status) {
$('[lay-filter="autoCommit-checkbox-filter"]')[0].checked = status;
}
//节流模式
function dataCompressionChanged(status) {
$('[lay-filter="dataCompression-checkbox-filter"]')[0].checked = status;
}
})
</script>
}