|
|
|
@ -11,6 +11,56 @@ import { createProtocolDataController } from './protocol/protocolData.js'; |
|
|
|
import { getRegisterConfigValidationError, isValidRegisterConfig } from './protocol/protocolValidation.js'; |
|
|
|
import { byId, queryAll } from './utils/dom.js'; |
|
|
|
|
|
|
|
function notifyLoadFailure(target, error) { |
|
|
|
const statusMatch = /Request failed: (\d+)/.exec(String(error?.message ?? '')); |
|
|
|
const statusText = statusMatch ? ` (HTTP ${statusMatch[1]})` : ''; |
|
|
|
const message = `${target} settings could not be loaded${statusText}. Please check the saved data on the backend.`; |
|
|
|
console.error(message, error); |
|
|
|
alert(message); |
|
|
|
} |
|
|
|
|
|
|
|
function isLegacySuccessMessage(data) { |
|
|
|
return String(data?.message ?? '').trim().toLowerCase() === 'success'; |
|
|
|
} |
|
|
|
|
|
|
|
function isLegacyProtocolPayload(data) { |
|
|
|
if (!data || typeof data !== 'object' || Array.isArray(data)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
return [ |
|
|
|
'protocol', |
|
|
|
'dev_type', |
|
|
|
'can_input', |
|
|
|
'dr_on', |
|
|
|
'odo_on', |
|
|
|
'MEID', |
|
|
|
'MODBUS', |
|
|
|
'OPC_UA', |
|
|
|
'CAN', |
|
|
|
].some((key) => Object.prototype.hasOwnProperty.call(data, key)); |
|
|
|
} |
|
|
|
|
|
|
|
function isSaveSuccessful(type, response) { |
|
|
|
if (!response.ok) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
if (response.data?.success === true) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
if (type === 'device' && isLegacySuccessMessage(response.data)) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
if (type === 'protocol' && isLegacyProtocolPayload(response.data)) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
async function initializeApp() { |
|
|
|
const navigation = createNavigation(); |
|
|
|
const ssidController = createSsidController({ |
|
|
|
@ -49,7 +99,9 @@ async function initializeApp() { |
|
|
|
|
|
|
|
protocolController.loadFromRegisterJson(data); |
|
|
|
}, |
|
|
|
getDownloadData: () => protocolController.collectProtocolData(), |
|
|
|
getDownloadData: () => protocolController.normalizeRegisterForPost( |
|
|
|
protocolController.collectProtocolData(), |
|
|
|
), |
|
|
|
}); |
|
|
|
|
|
|
|
queryAll('.save-file').forEach((button) => { |
|
|
|
@ -71,8 +123,8 @@ async function initializeApp() { |
|
|
|
saveProtocolConfig(protocolPayload), |
|
|
|
]); |
|
|
|
|
|
|
|
const isDeviceSaved = deviceResponse.ok && deviceResponse.data?.success === true; |
|
|
|
const isProtocolSaved = protocolResponse.ok && protocolResponse.data?.success === true; |
|
|
|
const isDeviceSaved = isSaveSuccessful('device', deviceResponse); |
|
|
|
const isProtocolSaved = isSaveSuccessful('protocol', protocolResponse); |
|
|
|
|
|
|
|
if (isDeviceSaved && isProtocolSaved) { |
|
|
|
alert('saved successfully'); |
|
|
|
@ -97,9 +149,11 @@ async function initializeApp() { |
|
|
|
const deviceData = await loadDeviceConfig(); |
|
|
|
if (deviceData) { |
|
|
|
deviceController.loadFromData(deviceData); |
|
|
|
} else { |
|
|
|
deviceController.loadDefaults(); |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error('Device load failed:', error); |
|
|
|
notifyLoadFailure('Device', error); |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
@ -108,7 +162,7 @@ async function initializeApp() { |
|
|
|
protocolController.loadFromRegisterJson(protocolData); |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error('Protocol load failed:', error); |
|
|
|
notifyLoadFailure('Protocol', error); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|