Commit 852b6102965f30277b8a67ac37ab2c461de20872
Merge branch 'fix/DEFECT-1488' into 'main_dev'
fix: 修复根据选择设备的设备类型下发json或modbus指令 See merge request yunteng/thingskit-scada!120
Showing
1 changed file
with
52 additions
and
9 deletions
... | ... | @@ -7078,7 +7078,7 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7078 | 7078 | |
7079 | 7079 | /** |
7080 | 7080 | * @description 所有设备选项 |
7081 | - * @type {{id: string, name: string, deviceType: string}[]} | |
7081 | + * @type {{id: string, name: string, deviceType: string, tbDeviceId: string, codeType: string}[]} | |
7082 | 7082 | */ |
7083 | 7083 | let allDeviceOptions = [] |
7084 | 7084 | |
... | ... | @@ -7128,6 +7128,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7128 | 7128 | * @description 组织id |
7129 | 7129 | */ |
7130 | 7130 | ORG_ID: 'orgId', |
7131 | + | |
7132 | + /** | |
7133 | + * @description 命令下发类型 | |
7134 | + */ | |
7135 | + CODE_TYPE: 'codeType' | |
7131 | 7136 | } |
7132 | 7137 | |
7133 | 7138 | const sendInstructionWay = { |
... | ... | @@ -7135,6 +7140,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7135 | 7140 | TWO_WAY: 'twoway', |
7136 | 7141 | } |
7137 | 7142 | |
7143 | + const codeTypeEnum = { | |
7144 | + JSON: 'json', | |
7145 | + STRING: 'string' | |
7146 | + } | |
7147 | + | |
7138 | 7148 | /** |
7139 | 7149 | * @description 创建回显数据 查询出所有网关设备和直连设备 |
7140 | 7150 | */ |
... | ... | @@ -7167,11 +7177,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7167 | 7177 | fontSize: 14, // 编辑器内字体大小 |
7168 | 7178 | tabSize: 2, // 制表符设置为 4 个空格大小 |
7169 | 7179 | }); |
7170 | - editor.session.setMode("ace/mode/json"); | |
7180 | + editor.session.setMode(datum?.codeType === codeTypeEnum.STRING ? 'ace/mode/text' : "ace/mode/json"); | |
7171 | 7181 | editor.getSession().on('change', (event, editor) => { |
7172 | 7182 | $(`#${el}`).parent().find(`textarea[name="${enumConst.VALUE}"]`).val(editor.getValue()) |
7173 | 7183 | }) |
7174 | - editor.setValue(datum[enumConst.VALUE] ? datum[enumConst.VALUE] : "{}") | |
7184 | + editor.setValue(datum[enumConst.VALUE] ? datum[enumConst.VALUE] : datum?.codeType === codeTypeEnum.STRING ? "" : "{\n\n}") | |
7175 | 7185 | } |
7176 | 7186 | |
7177 | 7187 | /** |
... | ... | @@ -7210,6 +7220,7 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7210 | 7220 | <tr class="layui-form" lay-filter="${getRowFilter(addRowNumber)}"> |
7211 | 7221 | <td> |
7212 | 7222 | <select name="${enumConst.DEVICE}" lay-filter="${enumConst.DEVICE}" lay-verType="tips" lay-verify="required"></select> |
7223 | + <input name="${enumConst.CODE_TYPE}" style="display: none;" /> | |
7213 | 7224 | </td> |
7214 | 7225 | <td> |
7215 | 7226 | <form action="" style="display: flex"> |
... | ... | @@ -7289,6 +7300,25 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7289 | 7300 | }) |
7290 | 7301 | } |
7291 | 7302 | |
7303 | + function createSelectChangeEvent() { | |
7304 | + form.on(`select(${enumConst.DEVICE})`, event => { | |
7305 | + const { value, elem } = event | |
7306 | + const codeTypeInput = $(elem).parent().find(`input[name="${enumConst.CODE_TYPE}"]`).val(codeTypeEnum.JSON) | |
7307 | + if (value) { | |
7308 | + const selected = allDeviceOptions.find(item => item.tbDeviceId == value) | |
7309 | + const codeType = selected?.codeType && selected.codeType == 'MODBUS_RTU' ? codeTypeEnum.STRING : codeTypeEnum.JSON | |
7310 | + codeTypeInput.val(codeType) | |
7311 | + if (codeType === codeTypeEnum.STRING) { | |
7312 | + const editorEl = $(elem).parent().parent().find(`.${enumActionEl.JSON}>div>div`) | |
7313 | + const id = editorEl.attr('id') | |
7314 | + const editor = ace.edit(id) | |
7315 | + editor?.session?.setMode?.('ace/mode/text') | |
7316 | + editor?.setValue('') | |
7317 | + } | |
7318 | + } | |
7319 | + }) | |
7320 | + } | |
7321 | + | |
7292 | 7322 | /** |
7293 | 7323 | * @description 生成事件监听 |
7294 | 7324 | */ |
... | ... | @@ -7296,6 +7326,7 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7296 | 7326 | createDeleteRowListenEvent() |
7297 | 7327 | createAddRowListenEvent() |
7298 | 7328 | createSelectOptionMountEvent() |
7329 | + createSelectChangeEvent() | |
7299 | 7330 | } |
7300 | 7331 | |
7301 | 7332 | /** |
... | ... | @@ -7339,6 +7370,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7339 | 7370 | return false |
7340 | 7371 | } |
7341 | 7372 | |
7373 | + function isString(string) { | |
7374 | + if (typeof string === 'string' && string.trim() && /^\w+$/g.test(string)) return true | |
7375 | + return false | |
7376 | + } | |
7377 | + | |
7342 | 7378 | /** |
7343 | 7379 | * @description 表单验证 |
7344 | 7380 | * @param tableData |
... | ... | @@ -7346,12 +7382,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7346 | 7382 | */ |
7347 | 7383 | function validate(tableData) { |
7348 | 7384 | let validateFlag = true |
7349 | - | |
7350 | 7385 | const formModel = form.val(enumActionEl.FORM_FILTER) |
7351 | 7386 | if (!formModel[enumConst.ORG_ID]) return false |
7352 | 7387 | for (let i = 0; i < tableData.length; i++) { |
7353 | - const { value } = tableData[i] | |
7354 | - if (!isJson(value)) { | |
7388 | + const { value, codeType } = tableData[i] | |
7389 | + if (codeType === codeTypeEnum.JSON ? !isJson(value) : !isString(value)) { | |
7355 | 7390 | validateFlag = false |
7356 | 7391 | layer.tips('下发值不正确', $(`#${enumActionEl.DEVICE_DATA_BODY_EL} tr`).eq(i).find(`td.${enumActionEl.JSON}`), { tips: 1 }) |
7357 | 7392 | break |
... | ... | @@ -7372,7 +7407,6 @@ DataFormatPanel.prototype.addDataFont = function (container) { |
7372 | 7407 | const formModal = { |
7373 | 7408 | ...recordData, |
7374 | 7409 | configurationId, |
7375 | - // orgId, | |
7376 | 7410 | contentId: currentPageId.id, |
7377 | 7411 | id: graphId, |
7378 | 7412 | content: { |
... | ... | @@ -15968,14 +16002,23 @@ class HandleDataInteraction { |
15968 | 16002 | UseLayUi.errorMsg('设备不在线!') |
15969 | 16003 | } |
15970 | 16004 | } |
16005 | + | |
16006 | + function parse(value, codeType = 'json') { | |
16007 | + try { | |
16008 | + return codeType === 'json' ? JSON.parse(value) : value | |
16009 | + } catch (error) { | |
16010 | + return value | |
16011 | + } | |
16012 | + } | |
16013 | + | |
15971 | 16014 | for (const item of list) { |
15972 | - const { deviceId, slaveDeviceId, value, way } = item | |
16015 | + const { deviceId, codeType, value, way } = item | |
15973 | 16016 | if (!value || !deviceId) continue |
15974 | 16017 | const data = { |
15975 | 16018 | method: "methodThingskit", |
15976 | 16019 | additionalInfo: { cmdType: 'API' }, |
15977 | 16020 | persistent: true, |
15978 | - params: JSON.parse(value), | |
16021 | + params: parse(value, codeType), | |
15979 | 16022 | } |
15980 | 16023 | if (deviceId) { |
15981 | 16024 | queue.push(() => { | ... | ... |