fastbee/yack-web/src/views/iot/product/modbus-derive.js
2026-10-06 08:50:16 +08:00

45 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* modbus 配置派生规则(唯一口径,页面与批量面板共用)
*
* 红线:`读取个数(quantity)` 是 `数据类型(dataType)` 的派生量,不是独立字段。
* 后端 ModbusProtocol#handleRead0304 完全按 quantity 分支解析:
* quantity==1 → 16 位(CalculateUtils.handleToUnSign16)
* quantity==2 → 32 位(CalculateUtils.parseValue)
* 其它值 → log.warn 直接丢弃
* 字典 iot_modbus_data_type(sql/yack-init.sql:2503-2511):
* bit=1 位;ushort/short=16 位 1 个寄存器;long/ulong/float 的 ABCD/CDAB 变体=32 位 2 个寄存器。
*/
/** 32 位数据类型集合(占 2 个寄存器) */
export const MODBUS_32BIT_TYPES = ['long-ABCD', 'long-CDAB', 'ulong-ABCD', 'ulong-CDAB', 'float-ABCD', 'float-CDAB'];
/** 16 位数据类型集合(占 1 个寄存器) */
export const MODBUS_16BIT_TYPES = ['ushort', 'short'];
/**
* 由数据类型推导读取个数
* @param {string} dataType 字典 iot_modbus_data_type 的值
* @returns {number|null} 1 | 2;dataType 为空或未知类型返回 null(不派生,避免把 data_type 为空的历史行改坏)
*/
export function deriveQuantity(dataType) {
if (!dataType) return null;
if (dataType === 'bit') return 1;
if (MODBUS_16BIT_TYPES.indexOf(dataType) !== -1) return 1;
if (MODBUS_32BIT_TYPES.indexOf(dataType) !== -1) return 2;
return null;
}
/**
* 单行占用的寄存器个数(一 key 生成地址序列时的步长)
* dataType 非空 → 以派生值为准(权威);dataType 为空的历史行 → 回退行内 quantity;再缺省 1
* @param {object} row modbus 配置行
* @returns {number} 1 | 2 | 行内 quantity
*/
export function resolveQuantity(row) {
if (!row) return 1;
const derived = deriveQuantity(row.dataType);
if (derived !== null) return derived;
const quantity = Number(row.quantity);
return quantity > 0 ? quantity : 1;
}