var pageCurr;
|
var tableIns;
|
|
function escHtml(s) {
|
if (s == null || s === '') {
|
return '';
|
}
|
return $('<div/>').text(String(s)).html();
|
}
|
|
function payloadPreview(s) {
|
var t = s == null ? '' : String(s);
|
if (t.length <= 100) {
|
return escHtml(t);
|
}
|
return escHtml(t.substring(0, 100)) + '…';
|
}
|
|
layui.use(['table', 'form'], function () {
|
var table = layui.table;
|
var $ = layui.jquery;
|
var layer = layui.layer;
|
var form = layui.form;
|
|
function loadIotMqttConfig() {
|
$.ajax({
|
url: baseUrl + '/iotMqttAdmin/config/auth',
|
headers: {token: localStorage.getItem('token')},
|
dataType: 'json',
|
success: function (res) {
|
if (res.code === 403) {
|
top.location.href = baseUrl + "/";
|
return;
|
}
|
if (res.code !== 200 || !res.data) {
|
return;
|
}
|
var d = res.data;
|
var tp = d.topics || {};
|
form.val('iotMqttCfgForm', {
|
enabled: d.enabled ? 'on' : '',
|
egressStow: tp.egressStow || '',
|
egressPick: tp.egressPick || '',
|
egressFeedback: tp.egressFeedback || '',
|
ingressStow: tp.ingressStow || '',
|
ingressPick: tp.ingressPick || '',
|
ingressFeedback: tp.ingressFeedback || ''
|
});
|
$('#iotMqttConnState').text(d.mqttConnected ? '当前:已连接' : '当前:未连接');
|
form.render();
|
}
|
});
|
}
|
|
loadIotMqttConfig();
|
$('#iotMqttReloadCfg').on('click', function () {
|
loadIotMqttConfig();
|
});
|
$('#iotMqttReconnect').on('click', function () {
|
$.ajax({
|
url: baseUrl + '/iotMqttAdmin/reconnect/auth',
|
headers: {token: localStorage.getItem('token')},
|
type: 'POST',
|
dataType: 'json',
|
success: function (res) {
|
if (res.code === 200) {
|
layer.msg('已执行重连');
|
loadIotMqttConfig();
|
} else {
|
layer.msg(res.msg || '重连失败', {icon: 2});
|
}
|
},
|
error: function () {
|
layer.msg('请求失败', {icon: 2});
|
}
|
});
|
});
|
form.on('submit(iotMqttSave)', function () {
|
var v = form.val('iotMqttCfgForm');
|
var payload = {
|
enabled: v.enabled === 'on',
|
topics: {
|
egressStow: v.egressStow || '',
|
egressPick: v.egressPick || '',
|
egressFeedback: v.egressFeedback || '',
|
ingressStow: v.ingressStow || '',
|
ingressPick: v.ingressPick || '',
|
ingressFeedback: v.ingressFeedback || ''
|
}
|
};
|
$.ajax({
|
url: baseUrl + '/iotMqttAdmin/config/save/auth',
|
headers: {token: localStorage.getItem('token'), 'Content-Type': 'application/json'},
|
type: 'POST',
|
data: JSON.stringify(payload),
|
dataType: 'json',
|
success: function (res) {
|
if (res.code === 200) {
|
layer.msg('已保存,请点「重连 MQTT」使订阅生效');
|
loadIotMqttConfig();
|
} else {
|
layer.msg(res.msg || '保存失败', {icon: 2});
|
}
|
},
|
error: function () {
|
layer.msg('请求失败', {icon: 2});
|
}
|
});
|
return false;
|
});
|
|
tableIns = table.render({
|
elem: '#iotMqttOutbound',
|
headers: {token: localStorage.getItem('token')},
|
url: baseUrl + '/iotMqttOutbound/list/auth',
|
page: true,
|
limit: 16,
|
limits: [16, 30, 50, 100],
|
cellMinWidth: 50,
|
cols: [[
|
{field: 'id', title: 'ID', width: 80, sort: true, align: 'center'}
|
, {field: 'instructionId', title: 'instructionId', align: 'center', width: 200}
|
, {field: 'messageType', title: '类型', align: 'center', width: 100}
|
, {field: 'publishTopic', title: '发送主题', align: 'center', minWidth: 200}
|
, {
|
field: 'publishPayload',
|
title: '发送消息体(摘要)',
|
align: 'left',
|
minWidth: 280,
|
templet: function (d) {
|
return payloadPreview(d.publishPayload);
|
}
|
}
|
, {field: 'publishStatus', title: '发布状态', align: 'center', width: 120}
|
, {field: 'errorMessage', title: '错误信息', align: 'center', minWidth: 160}
|
, {field: 'createTime', title: '创建时间', align: 'center', width: 170}
|
, {field: 'updateTime', title: '更新时间', align: 'center', width: 170}
|
, {fixed: 'right', title: '操作', align: 'center', toolbar: '#iotMqttOutboundOperate', width: 150}
|
]],
|
request: {
|
pageName: 'curr',
|
pageSize: 'limit'
|
},
|
parseData: function (res) {
|
return {
|
'code': res.code,
|
'msg': res.msg,
|
'count': res.data.total,
|
'data': res.data.records
|
};
|
},
|
response: {
|
statusCode: 200
|
},
|
done: function (res, curr) {
|
if (res.code === 403) {
|
top.location.href = baseUrl + "/";
|
}
|
pageCurr = curr;
|
if (typeof limit === 'function') {
|
limit();
|
}
|
}
|
});
|
|
form.on('submit(search)', function () {
|
var searchData = {};
|
$.each($('#search-box [name]').serializeArray(), function () {
|
searchData[this.name] = this.value;
|
});
|
tableIns.reload({
|
where: searchData,
|
page: {curr: 1},
|
done: function (res, curr) {
|
if (res.code === 403) {
|
top.location.href = baseUrl + "/";
|
}
|
pageCurr = curr;
|
if (typeof limit === 'function') {
|
limit();
|
}
|
}
|
});
|
return false;
|
});
|
|
form.on('submit(reset)', function () {
|
$('#search-box [name]').val('');
|
form.render();
|
tableIns.reload({
|
where: {},
|
page: {curr: 1}
|
});
|
return false;
|
});
|
|
table.on('tool(iotMqttOutbound)', function (obj) {
|
var data = obj.data;
|
if (obj.event === 'payload') {
|
layer.open({
|
type: 1,
|
title: '发送消息体',
|
area: ['720px', '520px'],
|
shadeClose: true,
|
content: '<div style="padding:12px;"><pre style="white-space:pre-wrap;word-break:break-all;margin:0;">'
|
+ escHtml(data.publishPayload) + '</pre></div>'
|
});
|
} else if (obj.event === 'resend') {
|
layer.confirm('确定重发该条 MQTT?', function (index) {
|
$.ajax({
|
url: baseUrl + '/iotMqttOutbound/resend/auth',
|
headers: {token: localStorage.getItem('token')},
|
type: 'POST',
|
data: {id: data.id},
|
dataType: 'json',
|
success: function (res) {
|
layer.close(index);
|
if (res.code === 200) {
|
layer.msg('重发成功');
|
tableIns.reload({page: {curr: pageCurr || 1}});
|
} else {
|
layer.msg(res.msg || '重发失败', {icon: 2});
|
}
|
},
|
error: function () {
|
layer.close(index);
|
layer.msg('请求失败', {icon: 2});
|
}
|
});
|
});
|
}
|
});
|
});
|