@mos-connection/helper
Version:
Helper functions for the MOS-connection library
185 lines • 10.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XMLDefaultActiveX = exports.XMLSupportedProfiles = exports.XMLMosListMachInfo = exports.XMLMosIDs = void 0;
const XMLBuilder = require("xmlbuilder");
const model_1 = require("@mos-connection/model");
const lib_1 = require("../lib");
const ensureMethods_1 = require("../../utils/ensureMethods");
const Utils_1 = require("../../utils/Utils");
const parseMosTypes_1 = require("../parseMosTypes");
const profile1_1 = require("../profile1");
const ParseError_1 = require("../ParseError");
/* eslint-disable @typescript-eslint/no-namespace */
var XMLMosIDs;
(function (XMLMosIDs) {
function fromXML(path, xml, strict) {
try {
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
return (0, ensureMethods_1.ensureArray)(xml).map((id, index) => mosTypes.mosString128.createRequired(id, `[${index}]`));
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLMosIDs.fromXML = fromXML;
})(XMLMosIDs = exports.XMLMosIDs || (exports.XMLMosIDs = {}));
/* eslint-disable @typescript-eslint/no-namespace */
var XMLMosListMachInfo;
(function (XMLMosListMachInfo) {
function fromXML(path, xml, strict) {
try {
xml = (0, ensureMethods_1.ensureXMLObject)(xml, strict);
const { mosString128, mosTime } = (0, parseMosTypes_1.getParseMosTypes)(strict);
const list = {
manufacturer: mosString128.createRequired(xml.manufacturer, 'manufacturer'),
model: mosString128.createRequired(xml.model, 'model'),
hwRev: mosString128.createRequired(xml.hwRev, 'hwRev'),
swRev: mosString128.createRequired(xml.swRev, 'swRev'),
DOM: mosString128.createRequired(xml.DOM, 'DOM'),
SN: mosString128.createRequired(xml.SN, 'SN'),
ID: mosString128.createRequired(xml.ID, 'ID'),
time: mosTime.createRequired(xml.time, 'time'),
opTime: mosTime.createOptional(xml.opTime, 'opTime'),
mosRev: mosString128.createRequired(xml.mosRev, 'mosRev'),
supportedProfiles: XMLSupportedProfiles.fromXML('supportedProfiles', xml.supportedProfiles, strict),
defaultActiveX: (0, lib_1.has)(xml, 'defaultActiveX')
? XMLDefaultActiveX.fromXML('defaultActiveX', xml.defaultActiveX, strict)
: undefined,
mosExternalMetaData: (0, lib_1.has)(xml, 'mosExternalMetadata')
? profile1_1.XMLMosExternalMetaData.fromXML('mosExternalMetaData', xml.mosExternalMetaData, strict)
: undefined,
};
(0, lib_1.omitUndefined)(list);
return list;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLMosListMachInfo.fromXML = fromXML;
function toXML(xmlListMachInfo, info, strict) {
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'manufacturer', info.manufacturer, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'model', info.model, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'hwRev', info.hwRev, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'swRev', info.swRev, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'DOM', info.DOM, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'SN', info.SN, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'ID', info.ID, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'time', info.time, undefined, strict);
if (info.opTime)
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'opTime', info.opTime, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlListMachInfo, 'mosRev', info.mosRev, undefined, strict);
XMLSupportedProfiles.toXML(xmlListMachInfo, info.supportedProfiles, strict);
if (info.defaultActiveX)
XMLDefaultActiveX.toXML(xmlListMachInfo, info.defaultActiveX, strict);
if (info.mosExternalMetaData)
profile1_1.XMLMosExternalMetaData.toXML(xmlListMachInfo, info.mosExternalMetaData);
}
XMLMosListMachInfo.toXML = toXML;
})(XMLMosListMachInfo = exports.XMLMosListMachInfo || (exports.XMLMosListMachInfo = {}));
var XMLSupportedProfiles;
(function (XMLSupportedProfiles) {
function fromXML(path, xmlSupportedProfiles, strict) {
try {
if (typeof xmlSupportedProfiles !== 'object')
if (strict)
throw new Error(`Invalid supportedProfiles! Expected object, got: "${xmlSupportedProfiles}"`);
else
return { deviceType: 'N/A' };
if (Array.isArray(xmlSupportedProfiles))
throw new Error(`Invalid supportedProfiles! Expected object, got array: "${JSON.stringify(xmlSupportedProfiles)}"`);
const parsed = {
deviceType: (0, ensureMethods_1.ensureStringLiteral)(xmlSupportedProfiles.deviceType, ['NCS', 'MOS', 'N/A'], strict, 'N/A'),
// Note: .profiles are added below
};
for (const mosProfile of (0, ensureMethods_1.ensureArray)(xmlSupportedProfiles.mosProfile)) {
if (typeof mosProfile === 'string')
throw new Error(`Invalid mosProfile! Expected object, got: "${mosProfile}"`);
if (mosProfile === undefined)
continue;
const attrs = (0, parseMosTypes_1.getXMLAttributes)(mosProfile);
if (typeof attrs.number !== 'string') {
if (strict)
throw new Error(`attributes.number missing`);
else
continue;
}
if (typeof mosProfile.text !== 'string') {
if (strict)
throw new Error(`mosProfile.text missing`);
else
continue;
}
// @ts-expect-error hack
parsed[`profile${attrs.number}`] = mosProfile.text === 'YES';
}
(0, lib_1.omitUndefined)(parsed);
return parsed;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLSupportedProfiles.fromXML = fromXML;
function toXML(xml, supportedProfiles, strict) {
const xmlSupportedProfiles = XMLBuilder.create('supportedProfiles');
xmlSupportedProfiles.att('deviceType', supportedProfiles.deviceType);
// let p = addTextElement(root, 'supportedProfiles').att('deviceType', info.supportedProfiles.deviceType)
for (let i = 0; i < 8; i++) {
(0, Utils_1.addTextElementInternal)(xmlSupportedProfiles, 'mosProfile', supportedProfiles['profile' + i] ? 'YES' : 'NO', undefined, strict).att('number', i);
}
xml.importDocument(xmlSupportedProfiles);
}
XMLSupportedProfiles.toXML = toXML;
})(XMLSupportedProfiles = exports.XMLSupportedProfiles || (exports.XMLSupportedProfiles = {}));
var XMLDefaultActiveX;
(function (XMLDefaultActiveX) {
function fromXML(path, xml, strict) {
try {
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
const defaultActiveX = [];
for (const xmlObj of (0, ensureMethods_1.ensureArray)(xml)) {
if (!xmlObj)
continue;
if (typeof xmlObj !== 'object') {
if (strict)
throw new Error(`defaultActiveX: Expected an object, got: "${xmlObj}"`);
else
continue;
}
if (typeof xmlObj === 'object' && Array.isArray(xmlObj)) {
if (strict)
throw new Error(`defaultActiveX: Expected an object, got an array: "${JSON.stringify(xmlObj)}"`);
else
continue;
}
const parsedObj = {
mode: mosTypes.stringEnum.createRequired({ enum: model_1.IMOSListMachInfoDefaultActiveXMode, value: xmlObj.mode }, 'mode'),
controlFileLocation: mosTypes.string.createRequired(xmlObj.controlFileLocation, 'controlFileLocation'),
controlSlug: mosTypes.mosString128.createRequired(xmlObj.controlSlug, 'controlSlug'),
controlName: mosTypes.string.createRequired(xmlObj.controlName, 'controlName'),
controlDefaultParams: mosTypes.string.createRequired(xmlObj.controlDefaultParams, 'controlDefaultParams'),
};
defaultActiveX.push(parsedObj);
}
(0, lib_1.omitUndefined)(defaultActiveX);
return defaultActiveX;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLDefaultActiveX.fromXML = fromXML;
function toXML(xml, objs, strict) {
for (const obj of objs) {
const xmlItem = (0, Utils_1.addTextElementInternal)(xml, 'defaultActiveX', undefined, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlItem, 'mode', obj.mode, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlItem, 'controlFileLocation', obj.controlFileLocation, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlItem, 'controlSlug', obj.controlSlug, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlItem, 'controlName', obj.controlName, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlItem, 'controlDefaultParams', obj.controlDefaultParams, undefined, strict);
}
}
XMLDefaultActiveX.toXML = toXML;
})(XMLDefaultActiveX = exports.XMLDefaultActiveX || (exports.XMLDefaultActiveX = {}));
//# sourceMappingURL=xmlConversion.js.map