Commit 3500e013b4c5adb42fbd9bbe133c55dec8da9c65

Authored by ww
1 parent 553f3388

fix: 修复根据选择设备的设备类型下发json或modbus指令

@@ -7078,7 +7078,7 @@ DataFormatPanel.prototype.addDataFont = function (container) { @@ -7078,7 +7078,7 @@ DataFormatPanel.prototype.addDataFont = function (container) {
7078 7078
7079 /** 7079 /**
7080 * @description 所有设备选项 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 let allDeviceOptions = [] 7083 let allDeviceOptions = []
7084 7084
@@ -7128,6 +7128,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { @@ -7128,6 +7128,11 @@ DataFormatPanel.prototype.addDataFont = function (container) {
7128 * @description 组织id 7128 * @description 组织id
7129 */ 7129 */
7130 ORG_ID: 'orgId', 7130 ORG_ID: 'orgId',
  7131 +
  7132 + /**
  7133 + * @description 命令下发类型
  7134 + */
  7135 + CODE_TYPE: 'codeType'
7131 } 7136 }
7132 7137
7133 const sendInstructionWay = { 7138 const sendInstructionWay = {
@@ -7135,6 +7140,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { @@ -7135,6 +7140,11 @@ DataFormatPanel.prototype.addDataFont = function (container) {
7135 TWO_WAY: 'twoway', 7140 TWO_WAY: 'twoway',
7136 } 7141 }
7137 7142
  7143 + const codeTypeEnum = {
  7144 + JSON: 'json',
  7145 + STRING: 'string'
  7146 + }
  7147 +
7138 /** 7148 /**
7139 * @description 创建回显数据 查询出所有网关设备和直连设备 7149 * @description 创建回显数据 查询出所有网关设备和直连设备
7140 */ 7150 */
@@ -7167,11 +7177,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { @@ -7167,11 +7177,11 @@ DataFormatPanel.prototype.addDataFont = function (container) {
7167 fontSize: 14, // 编辑器内字体大小 7177 fontSize: 14, // 编辑器内字体大小
7168 tabSize: 2, // 制表符设置为 4 个空格大小 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 editor.getSession().on('change', (event, editor) => { 7181 editor.getSession().on('change', (event, editor) => {
7172 $(`#${el}`).parent().find(`textarea[name="${enumConst.VALUE}"]`).val(editor.getValue()) 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,6 +7220,7 @@ DataFormatPanel.prototype.addDataFont = function (container) {
7210 <tr class="layui-form" lay-filter="${getRowFilter(addRowNumber)}"> 7220 <tr class="layui-form" lay-filter="${getRowFilter(addRowNumber)}">
7211 <td> 7221 <td>
7212 <select name="${enumConst.DEVICE}" lay-filter="${enumConst.DEVICE}" lay-verType="tips" lay-verify="required"></select> 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 </td> 7224 </td>
7214 <td> 7225 <td>
7215 <form action="" style="display: flex"> 7226 <form action="" style="display: flex">
@@ -7289,6 +7300,25 @@ DataFormatPanel.prototype.addDataFont = function (container) { @@ -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 * @description 生成事件监听 7323 * @description 生成事件监听
7294 */ 7324 */
@@ -7296,6 +7326,7 @@ DataFormatPanel.prototype.addDataFont = function (container) { @@ -7296,6 +7326,7 @@ DataFormatPanel.prototype.addDataFont = function (container) {
7296 createDeleteRowListenEvent() 7326 createDeleteRowListenEvent()
7297 createAddRowListenEvent() 7327 createAddRowListenEvent()
7298 createSelectOptionMountEvent() 7328 createSelectOptionMountEvent()
  7329 + createSelectChangeEvent()
7299 } 7330 }
7300 7331
7301 /** 7332 /**
@@ -7339,6 +7370,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { @@ -7339,6 +7370,11 @@ DataFormatPanel.prototype.addDataFont = function (container) {
7339 return false 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 * @description 表单验证 7379 * @description 表单验证
7344 * @param tableData 7380 * @param tableData
@@ -7346,12 +7382,11 @@ DataFormatPanel.prototype.addDataFont = function (container) { @@ -7346,12 +7382,11 @@ DataFormatPanel.prototype.addDataFont = function (container) {
7346 */ 7382 */
7347 function validate(tableData) { 7383 function validate(tableData) {
7348 let validateFlag = true 7384 let validateFlag = true
7349 -  
7350 const formModel = form.val(enumActionEl.FORM_FILTER) 7385 const formModel = form.val(enumActionEl.FORM_FILTER)
7351 if (!formModel[enumConst.ORG_ID]) return false 7386 if (!formModel[enumConst.ORG_ID]) return false
7352 for (let i = 0; i < tableData.length; i++) { 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 validateFlag = false 7390 validateFlag = false
7356 layer.tips('下发值不正确', $(`#${enumActionEl.DEVICE_DATA_BODY_EL} tr`).eq(i).find(`td.${enumActionEl.JSON}`), { tips: 1 }) 7391 layer.tips('下发值不正确', $(`#${enumActionEl.DEVICE_DATA_BODY_EL} tr`).eq(i).find(`td.${enumActionEl.JSON}`), { tips: 1 })
7357 break 7392 break
@@ -7372,7 +7407,6 @@ DataFormatPanel.prototype.addDataFont = function (container) { @@ -7372,7 +7407,6 @@ DataFormatPanel.prototype.addDataFont = function (container) {
7372 const formModal = { 7407 const formModal = {
7373 ...recordData, 7408 ...recordData,
7374 configurationId, 7409 configurationId,
7375 - // orgId,  
7376 contentId: currentPageId.id, 7410 contentId: currentPageId.id,
7377 id: graphId, 7411 id: graphId,
7378 content: { 7412 content: {
@@ -15968,14 +16002,23 @@ class HandleDataInteraction { @@ -15968,14 +16002,23 @@ class HandleDataInteraction {
15968 UseLayUi.errorMsg('设备不在线!') 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 for (const item of list) { 16014 for (const item of list) {
15972 - const { deviceId, slaveDeviceId, value, way } = item 16015 + const { deviceId, codeType, value, way } = item
15973 if (!value || !deviceId) continue 16016 if (!value || !deviceId) continue
15974 const data = { 16017 const data = {
15975 method: "methodThingskit", 16018 method: "methodThingskit",
15976 additionalInfo: { cmdType: 'API' }, 16019 additionalInfo: { cmdType: 'API' },
15977 persistent: true, 16020 persistent: true,
15978 - params: JSON.parse(value), 16021 + params: parse(value, codeType),
15979 } 16022 }
15980 if (deviceId) { 16023 if (deviceId) {
15981 queue.push(() => { 16024 queue.push(() => {