|
|
|
@ -4,7 +4,26 @@ import { byId } from '../utils/dom.js'; |
|
|
|
export const normProtocol = (value) => String(value ?? '').trim().replace(/[-\s]+/g, '_').toUpperCase(); |
|
|
|
export const getOpcArr = (data) => data?.OPC_UA ?? data?.['OPC-UA'] ?? []; |
|
|
|
|
|
|
|
const checkboxIds = ['ai0', 'ai1', 'ai2', 'ai3', 'di0', 'di1', 'di2', 'di3']; |
|
|
|
function normalizeLegacyOdometerKeys(register) { |
|
|
|
if (!register || typeof register !== 'object') { |
|
|
|
return register; |
|
|
|
} |
|
|
|
|
|
|
|
if (register.OdoMeter !== undefined && register.odo_on === undefined) { |
|
|
|
register.odo_on = register.OdoMeter; |
|
|
|
} |
|
|
|
|
|
|
|
if (register.odo_Meter !== undefined && register.odo_on === undefined) { |
|
|
|
register.odo_on = register.odo_Meter; |
|
|
|
} |
|
|
|
|
|
|
|
delete register.OdoMeter; |
|
|
|
delete register.odo_Meter; |
|
|
|
|
|
|
|
return register; |
|
|
|
} |
|
|
|
|
|
|
|
const checkboxIds = ['ai0', 'ai1', 'di0', 'di1']; |
|
|
|
const toLower = (value) => String(value ?? '').trim().toLowerCase(); |
|
|
|
const toUpper = (value) => String(value ?? '').trim().toUpperCase(); |
|
|
|
|
|
|
|
@ -31,7 +50,7 @@ function normalizeValueForElement(htmlId, value) { |
|
|
|
return normProtocol(value); |
|
|
|
case 'reg_can_input': |
|
|
|
case 'reg_dr_on_off': |
|
|
|
case 'OdoMeter': |
|
|
|
case 'odo_on': |
|
|
|
case 'reg_two_byte_order': |
|
|
|
return toLower(value); |
|
|
|
case 'reg_four_byte_order': |
|
|
|
@ -46,7 +65,7 @@ function normalizeValueForElement(htmlId, value) { |
|
|
|
} |
|
|
|
|
|
|
|
export function normalizeRegisterForPost(register) { |
|
|
|
const normalized = JSON.parse(JSON.stringify(register || {})); |
|
|
|
const normalized = normalizeLegacyOdometerKeys(JSON.parse(JSON.stringify(register || {}))); |
|
|
|
const toUpperSnake = (value) => String(value ?? '').trim().toUpperCase().replace(/[-\s]+/g, '_'); |
|
|
|
|
|
|
|
if (normalized.protocol) { |
|
|
|
@ -57,12 +76,8 @@ export function normalizeRegisterForPost(register) { |
|
|
|
normalized.can_input = String(normalized.can_input).trim().toUpperCase(); |
|
|
|
} |
|
|
|
|
|
|
|
if (normalized.OdoMeter) { |
|
|
|
normalized.OdoMeter = String(normalized.OdoMeter).trim().toLowerCase(); |
|
|
|
} |
|
|
|
|
|
|
|
if (normalized.odo_Meter) { |
|
|
|
normalized.odo_Meter = String(normalized.odo_Meter).trim().toLowerCase(); |
|
|
|
if (normalized.odo_on) { |
|
|
|
normalized.odo_on = String(normalized.odo_on).trim().toLowerCase(); |
|
|
|
} |
|
|
|
|
|
|
|
if (normalized.MEID !== undefined && normalized.MEID !== null) { |
|
|
|
@ -100,7 +115,7 @@ export function createProtocolDataController({ |
|
|
|
const protocolSelect = byId('reg_protocol'); |
|
|
|
const canInputSelect = byId('reg_can_input'); |
|
|
|
const drOnOffSelect = byId('reg_dr_on_off'); |
|
|
|
const odometerSelect = byId('OdoMeter'); |
|
|
|
const odometerSelect = byId('odo_on'); |
|
|
|
|
|
|
|
function syncProtocolMenus() { |
|
|
|
navigation.toggleProtocolMenus(protocolSelect?.value, canInputSelect?.value, odometerSelect?.value); |
|
|
|
@ -137,7 +152,7 @@ export function createProtocolDataController({ |
|
|
|
} |
|
|
|
|
|
|
|
function collectProtocolData() { |
|
|
|
const register = getLastLoadedRegisterData(); |
|
|
|
const register = normalizeLegacyOdometerKeys(getLastLoadedRegisterData()); |
|
|
|
|
|
|
|
Object.entries(REGISTER_ID_MAP).forEach(([htmlId, dtoKey]) => { |
|
|
|
const element = byId(htmlId); |
|
|
|
@ -160,10 +175,10 @@ export function createProtocolDataController({ |
|
|
|
const odometerData = odometerController.collectData(); |
|
|
|
|
|
|
|
if (toLower(register.dr_on) !== 'on') { |
|
|
|
register.odo_Meter = 'off'; |
|
|
|
register.odo_on = 'off'; |
|
|
|
register.odo_speed = null; |
|
|
|
register.odo_direction = null; |
|
|
|
} else if (String(register.odo_Meter ?? '').toLowerCase() === 'on') { |
|
|
|
} else if (String((register.odo_on ?? register.odo_Meter) ?? '').toLowerCase() === 'on') { |
|
|
|
register.odo_speed = odometerData.odo_speed; |
|
|
|
register.odo_direction = odometerData.odo_direction; |
|
|
|
} else { |
|
|
|
@ -171,6 +186,7 @@ export function createProtocolDataController({ |
|
|
|
register.odo_direction = null; |
|
|
|
} |
|
|
|
|
|
|
|
normalizeLegacyOdometerKeys(register); |
|
|
|
return register; |
|
|
|
} |
|
|
|
|
|
|
|
@ -179,7 +195,9 @@ export function createProtocolDataController({ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
setLastLoadedRegisterData(register); |
|
|
|
const normalizedRegister = normalizeLegacyOdometerKeys({ ...register }); |
|
|
|
|
|
|
|
setLastLoadedRegisterData(normalizedRegister); |
|
|
|
|
|
|
|
Object.entries(REGISTER_ID_MAP).forEach(([htmlId, dtoKey]) => { |
|
|
|
const element = byId(htmlId); |
|
|
|
@ -187,8 +205,8 @@ export function createProtocolDataController({ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(register, dtoKey)) { |
|
|
|
const value = register[dtoKey]; |
|
|
|
if (Object.prototype.hasOwnProperty.call(normalizedRegister, dtoKey)) { |
|
|
|
const value = normalizedRegister[dtoKey]; |
|
|
|
if (element.type === 'checkbox') { |
|
|
|
element.checked = value === '1' || value === true; |
|
|
|
} else { |
|
|
|
@ -207,22 +225,22 @@ export function createProtocolDataController({ |
|
|
|
checkboxIds.forEach((id) => { |
|
|
|
const checkbox = byId(id); |
|
|
|
if (checkbox) { |
|
|
|
checkbox.checked = register[id] === '1' || register[id] === true; |
|
|
|
checkbox.checked = normalizedRegister[id] === '1' || normalizedRegister[id] === true; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
if (register.protocol && protocolSelect) { |
|
|
|
const protocol = normProtocol(register.protocol); |
|
|
|
if (normalizedRegister.protocol && protocolSelect) { |
|
|
|
const protocol = normProtocol(normalizedRegister.protocol); |
|
|
|
protocolSelect.value = protocol; |
|
|
|
setLastLoadedRegisterData({ ...register, protocol }); |
|
|
|
setLastLoadedRegisterData({ ...normalizedRegister, protocol }); |
|
|
|
} |
|
|
|
|
|
|
|
modbusController.renderRows(Array.isArray(register.MODBUS) ? register.MODBUS : []); |
|
|
|
opcUaController.renderRows(Array.isArray(getOpcArr(register)) ? getOpcArr(register) : []); |
|
|
|
canController.renderRows(Array.isArray(register.CAN) ? register.CAN : []); |
|
|
|
modbusController.renderRows(Array.isArray(normalizedRegister.MODBUS) ? normalizedRegister.MODBUS : []); |
|
|
|
opcUaController.renderRows(Array.isArray(getOpcArr(normalizedRegister)) ? getOpcArr(normalizedRegister) : []); |
|
|
|
canController.renderRows(Array.isArray(normalizedRegister.CAN) ? normalizedRegister.CAN : []); |
|
|
|
odometerController.renderRows({ |
|
|
|
odo_speed: register.odo_speed ?? null, |
|
|
|
odo_direction: register.odo_direction ?? null, |
|
|
|
odo_speed: normalizedRegister.odo_speed ?? null, |
|
|
|
odo_direction: normalizedRegister.odo_direction ?? null, |
|
|
|
}); |
|
|
|
syncOdometerAvailability(); |
|
|
|
syncProtocolMenusDeferred(); |
|
|
|
|