tc-context
Version:
TwinCAT ADS Communication Library for creating an active TwinCAT Context, with automatic symbol and type mapping
981 lines (980 loc) • 37.7 kB
JavaScript
"use strict";
// tc-symbol.ts
/**
* Module, which contains the definitions of all support TwinCAT Types, from which `TcSymbols` can be instantiated
*
*
* Licensed under MIT License.
*
* Copyright (c) 2020 Dmitrij Trifanov <d.v.trifanov@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @packageDocumentation
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TcArrayType = exports.TcStructType = exports.TcEnumType = exports.TcStringType = exports.TcNumericType = exports.TcBooleanType = exports.TcType = void 0;
//----IMPORTS...
const debug_1 = __importDefault(require("debug"));
const tc_symbol_1 = require("./tc-symbol");
const tc_com_1 = require("./tc-com");
/**
* Base class for a TwinCAT Type, designed to store the basic information of the Type, as well as provide options
* for cloning itself, and extending with additional attributes.
*
* Types can be mutated with attributes, which allow for altered behavior.
*/
class TcType {
/**
* Creates the base for a TwinCAT Data type. Can be build either from raw ADS Type Info, or from
* an existing `TcType`
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param debug - If enabled, will produce debug information
*/
constructor(context, typeData, debug = false) {
/**
* @internal
*/
this.__readOnly = false;
/**
* @internal
*/
this.__ignored = false;
/**
* @internal
*/
this.__log = debug_1.default(`TcContext::TcType`);
this.__log.enabled = debug;
if (typeData instanceof TcType) {
this.__name = typeData.name;
this.__adst = typeData.adst;
this.__readOnly = typeData.readOnly;
this.__default = typeData.defaultValue;
this.__offset = typeData.offset;
this.__size = typeData.size;
this.__defaultBuffer = typeData.defaultBuffer;
this.__ignored = typeData.ignored;
this.__onSet = typeData.onSet;
this.__onGet = typeData.onGet;
this.__onClear = typeData.onClear;
this.__onChange = typeData.onChange;
}
else {
this.__name = typeData.name;
this.__adst = typeData.adsDataType;
this.__offset = typeData.offset || 0;
this.__size = typeData.size;
this.mutate(typeData);
}
this.__context = context;
}
/**
* The default value for this `TcType`
*/
get defaultValue() { return this.__default; }
;
set defaultValue(val) { this.__default = val; }
;
/**
* Flag, if this `TcType` is ignored during Symbol Building
*/
get ignored() { return this.__ignored; }
;
set ignored(val) { this.__ignored = val; }
;
/**
* The {@link ADST} Type ID
*/
get adst() { return this.__adst; }
;
/**
* The memory offset, relative to its parent, if the `TcType` is a child
*/
get offset() { return this.__offset; }
;
set offset(val) { this.__offset = val; }
;
/**
* Size of the Target PLC Type
*/
get size() { return this.__size; }
;
set size(val) { this.__size = val; }
;
/**
* If a default value is present, the default byte buffer of that value
*/
get defaultBuffer() { return this.__defaultBuffer; }
;
set defaultBuffer(val) { this.__defaultBuffer = val; }
;
/**
* Flag, if this `TcType` is ReadOnly
*/
get readOnly() { return this.__readOnly; }
;
set readOnly(val) { this.__readOnly = val; }
;
/**
* The Target PLC Type name
*/
get name() { return this.__name; }
;
/**
* The `TcContext`, which this `TcType` is part of
*/
get context() { return this.__context; }
;
/**
* Alias to use, in place of the default 'set' event, when
* a `TcSymbol` of this type is constructed
*/
get onSet() { return this.__onSet || 'set'; }
;
set onSet(val) { this.__onSet = val; }
/**
* Alias to use, in place of the default 'get' event, when
* a `TcSymbol` of this type is constructed
*/
get onGet() { return this.__onGet || 'get'; }
;
set onGet(val) { this.__onGet = val; }
/**
* Alias to use, in place of the default 'cleared' event, when
* a `TcSymbol` of this type is constructed
*/
get onClear() { return this.__onClear || 'cleared'; }
;
set onClear(val) { this.__onClear = val; }
/**
* Alias to use, in place of the default 'changed' event, when
* a `TcSymbol` of this type is constructed
*/
get onChange() { return this.__onChange || 'changed'; }
;
set onChange(val) { this.__onChange = val; }
/**
* Clones this `TcType` and mutates it, with additional attributes from
* the mutator
*
* @param mutator - Will use the Attributes and Offset to create a mutation of this `TcType`
*/
mutate(mutator) {
var _a;
(_a = mutator.attributes) === null || _a === void 0 ? void 0 : _a.forEach(({ name, value }) => {
this.__getParameter(name, value);
});
this.offset = mutator.offset || this.offset;
}
/**
* Extends the current `TcType` with the provided `TcTypeInfo`. The default extension
* can either create a mutation of the current `TcType` or transform the `TcType` into
* an array
*
* @param adsTypeData - Data with which to extends the `TcType`
*
* @return - Either the extends `TcType` or `undefined` if extension failed
*/
async extend(adsTypeData) {
if (adsTypeData.adsDataType === this.adst) {
if (adsTypeData.arrayData.length) {
return TcArrayType.create(this.context, adsTypeData, this, this.__log.enabled);
}
else
return this.clone(adsTypeData);
}
}
;
/**
* Internal function for applying mutations to the created clone.
* Used for code reduction
*
* @param type - the TcType, which can be potentially mutated
* @param mutator - The mutator which can be applied to the `TcType`
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - Either the clone `TcType` or `undefined` if cloning failed
*/
async __clone(type, mutator) {
if (mutator) {
type.mutate(mutator);
if (!type.ignored) {
if (type.defaultValue !== this.defaultValue) {
type.defaultBuffer = await this.context.COM.toRaw(type.name, type.defaultValue);
}
this.__log(`extend() : Extends type ${this.name} to ${type.name}`);
return type;
}
else {
this.__log(`extend() : Ignoring extension of type ${this.name} to ${mutator.name}`);
return;
}
}
return type;
}
/**
* Internal function, which analyses attribute data from the Type ADS Data and
* adjusts aspects of this `TcType`
*
* Mutations supported :
* - ReadOnly
* - Ignored
* - OnSet
* - OnGet
* - OnClear
* - OnChange
*
* @param name - name of the Attribute
* @param value - value of the Attribute
*/
__getParameter(name, value) {
const nameFormatted = name.trim().toLowerCase();
if (nameFormatted === 'readonly') {
this.readOnly = true;
}
else if (nameFormatted === 'ignored') {
this.ignored = true;
}
else if (nameFormatted === 'onset') {
this.onSet = value;
}
else if (nameFormatted === 'onget') {
this.onGet = value;
}
else if (nameFormatted === 'onclear') {
this.onClear = value;
}
else if (nameFormatted === 'onchange') {
this.onChange = value;
}
}
}
exports.TcType = TcType;
/**
* Class representing a `BOOL` Type in the Target PLC.
*/
class TcBooleanType extends TcType {
/**
* Constructs a `TcBooleanType` to represent a `BOOL` Type in the Target PLC
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param debug - If enabled, will produce debug information
*/
constructor(context, typeData, debug = false) {
super(context, typeData, debug);
if (!(typeData instanceof TcType)) {
this.__log(`() : Created type ${this.name}[TcBooleanType]`);
}
}
/**
* Creates an instance of `TcBooleanType` with default values transformed to
* raw data
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param debug - If enabled, will produce debug information
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - An initialized instance of `TcBooleanType` if it is not ignored
*/
static async create(context, typeData, debug = false) {
const type = new TcBooleanType(context, typeData, debug);
if (!type.ignored) {
type.defaultBuffer = await context.COM.toRaw(type.name, type.defaultValue);
return type;
}
}
/**
* Default value for a `BOOL` if it is not explicitly specified
*/
get defaultValue() {
if (super.defaultValue === undefined) {
return false;
}
else
return super.defaultValue;
}
/**
* Creates an instance of a Type `BOOL` PLC Symbol, based on this `TcType`
*
* @param path - Path of the created `TcSymbol`
* @param parent - Parent of the created `TcSymbol`
* @param pointer - Memory location of created `TcSymbol` in the Target PLC
* @param debug - If enabled, will produce debug information
*
* @return - An `TcSymbol` instance of a Symbol of type `BOOL`
*/
instance(path, parent, pointer, debug = false) {
return new tc_symbol_1.TcBooleanSymbol(path, parent, pointer, this, debug);
}
/**
* Creates a clone with an optional mutation of this `TcType`
*
* @param mutator - Optional mutator, which can be applied to the clone
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - Either the clone `TcType` or `undefined` if cloning failed
*/
async clone(mutator) {
const clone = new TcBooleanType(this.context, this, this.__log.enabled);
return this.__clone(clone, mutator);
}
/**
* Internal function, which analyses attribute data from the Type ADS Data and
* adjusts aspects of this `TcType`
*
* Mutations supported :
* - ReadOnly
* - Ignored
* - OnSet
* - OnGet
* - OnClear
* - OnChange
* - Default
*
* @param name - name of the Attribute
* @param value - value of the Attribute
*/
__getParameter(name, value) {
super.__getParameter(name, value);
const nameFormatted = name.trim().toLowerCase();
if (nameFormatted === 'default' && value.trim().toLowerCase() === 'true') {
this.__default = true;
}
}
}
exports.TcBooleanType = TcBooleanType;
/**
* Class representing all numeric Types in the Target PLC
*/
let TcNumericType = /** @class */ (() => {
class TcNumericType extends TcType {
/**
* Constructs a `TcNumericType` to represent all numeric Types in the Target PLC
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param debug - If enabled, will produce debug information
*/
constructor(context, typeData, debug = false) {
super(context, typeData, debug);
if (typeData instanceof TcType) {
this.__upperBorder = typeData.upperBorder;
this.__lowerBorder = typeData.lowerBorder;
}
else {
this.__log(`() : Created type ${this.name}[TcNumericType]`);
}
}
/**
* Creates an instance of `TcNumericType` with default values transformed to
* raw data
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param debug - If enabled, will produce debug information
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - An initialized instance of `TcNumericType` if it is not ignored
*/
static async create(context, typeData, debug = false) {
const type = new TcNumericType(context, typeData, debug);
if (!type.ignored) {
type.defaultBuffer = await context.COM.toRaw(type.name, type.defaultValue);
return type;
}
}
/**
* Default value for a `BOOL` if it is not explicitly specified
*/
get defaultValue() {
if (super.defaultValue === undefined) {
if (this.adst === tc_com_1.ADST.INT64 || this.adst === tc_com_1.ADST.UINT64) {
return BigInt(0);
}
else
return 0;
}
else
return super.defaultValue;
}
/**
* Maximum value of this Numeric Type
*/
get upperBorder() {
if (this.__upperBorder === undefined) {
return TcNumericType.ranges[this.adst].max;
}
else
return this.__upperBorder;
}
set upperBorder(val) { this.__upperBorder = val; }
;
/**
* Minimum value of this Numeric Type
*/
get lowerBorder() {
if (this.__lowerBorder === undefined) {
return TcNumericType.ranges[this.adst].min;
}
else
return this.__lowerBorder;
}
set lowerBorder(val) { this.__lowerBorder = val; }
;
/**
* Creates an instance of a numeric Type PLC Symbol, based on this `TcType
*
* @param path - Path of the created `TcSymbol`
* @param parent - Parent of the created `TcSymbol`
* @param pointer - Memory location of created `TcSymbol` in the Target PLC
* @param debug - If enabled, will produce debug information
*
* @return - An `TcSymbol` instance of a Symbol of a numeric type
*/
instance(path, parent, pointer, debug = false) {
return new tc_symbol_1.TcNumericSymbol(path, parent, pointer, this, debug);
}
/**
* Extends the current `TcType` with the provided `TcTypeInfo`. Can be extended either to
* an array of `TcNumericType` or `TcEnumType`. Otherwise, creates a mutate clone
*
* @param adsTypeData - Data with which to extends the `TcType`
*
* @return - Either the extends `TcType` or `undefined` if extension failed
*/
async extend(adsTypeData) {
if (adsTypeData.adsDataType === this.adst) {
if (adsTypeData.enumInfo) {
return await TcEnumType.create(this.context, adsTypeData, this.__log.enabled);
}
else if (adsTypeData.arrayData.length) {
return TcArrayType.create(this.context, adsTypeData, this, this.__log.enabled);
}
else
return this.clone(adsTypeData);
}
}
;
/**
* Creates a clone with an optional mutation of this `TcType`
*
* @param mutator - Optional mutator, which can be applied to the clone
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - Either the clone `TcType` or `undefined` if cloning failed
*/
async clone(mutator) {
const clone = new TcNumericType(this.context, this, this.__log.enabled);
return this.__clone(clone, mutator);
}
/**
* Internal function, which analyses attribute data from the Type ADS Data and
* adjusts aspects of this `TcType`
*
* Mutations supported :
* - ReadOnly
* - Ignored
* - OnSet
* - OnGet
* - OnClear
* - OnChange
* - Default
* - UpperBorder
* - LowerBorder
*
* @param name - name of the Attribute
* @param value - value of the Attribute
*/
__getParameter(name, value) {
super.__getParameter(name, value);
const nameFormatted = name.trim().toLowerCase();
if (nameFormatted === 'lowerborder') {
if (this.adst === tc_com_1.ADST.INT64 || this.adst === tc_com_1.ADST.UINT64) {
this.lowerBorder = BigInt(value);
}
else
this.lowerBorder = parseFloat(value);
if (this.__default === undefined) {
this.__default = this.lowerBorder;
}
}
else if (nameFormatted === 'upperborder') {
if (this.adst === tc_com_1.ADST.INT64 || this.adst === tc_com_1.ADST.UINT64) {
this.upperBorder = BigInt(value);
}
else
this.upperBorder = parseFloat(value);
}
else if (nameFormatted === 'default') {
if (this.adst === tc_com_1.ADST.INT64 || this.adst === tc_com_1.ADST.UINT64) {
this.__default = BigInt(value);
}
else
this.__default = parseFloat(value);
}
}
}
/**
* Default Ranges for all the Numeric Types possible in a TwinCAT PLC.
* If not explicitly specified, will be used for `upperBorder` and `lowerBorder`
* definitions
*/
TcNumericType.ranges = {
[tc_com_1.ADST.INT8]: { min: -128, max: 127 },
[tc_com_1.ADST.UINT8]: { min: 0, max: 255 },
[tc_com_1.ADST.UINT16]: { min: 0, max: 65535 },
[tc_com_1.ADST.INT16]: { min: -32768, max: 32767 },
[tc_com_1.ADST.INT32]: { min: -2147483648, max: 2147483647 },
[tc_com_1.ADST.UINT32]: { min: 0, max: 4294967295 },
[tc_com_1.ADST.INT64]: { min: BigInt(-9223372036854775808), max: BigInt(9223372036854775807) },
[tc_com_1.ADST.UINT64]: { min: BigInt(0), max: BigInt(18446744073709551615) },
[tc_com_1.ADST.REAL32]: { min: -3.402823e+38, max: 3.402823e+38 },
[tc_com_1.ADST.REAL64]: { min: -1.7976931348623158e+308, max: 1.7976931348623158e+308 }
};
return TcNumericType;
})();
exports.TcNumericType = TcNumericType;
/**
* Class representing a `STRING` or `WSTRING` Type in the Target PLC.
*/
class TcStringType extends TcType {
/**
* Constructs a `TcStringType` to represent a `STRING` or `WSTRING` Type in the Target PLC
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param debug - If enabled, will produce debug information
*/
constructor(context, typeData, debug = false) {
super(context, typeData, debug);
/**
* @internal
*/
this.__length = 0;
if (typeData instanceof TcType) {
this.__length = typeData.length;
}
else {
this.__length = typeData.size - 1;
this.__log(`() : Created type ${this.name}[TcStringType]`);
}
}
/**
* Creates an instance of `TcStringType` with default values transformed to
* raw data
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param debug - If enabled, will produce debug information
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - An initialized instance of `TcStringType` if it is not ignored
*/
static async create(context, typeData, debug = false) {
const type = new TcStringType(context, typeData, debug);
if (!type.ignored) {
type.defaultBuffer = await context.COM.toRaw(type.name, type.defaultValue);
return type;
}
}
/**
* The maximum length of a string, which can be stored in this Type
*/
get length() { return this.__length; }
;
set length(val) { this.__length = val; }
;
/**
* Default value for a `STRING` or `WSTRING` if it is not explicitly specified
*/
get defaultValue() {
if (super.defaultValue === undefined) {
return '';
}
else
return super.defaultValue;
}
/**
* Creates an instance of Type `STRING` or `WSTRING` PLC Symbol, based on this `TcType`
*
* @param path - Path of the created `TcSymbol`
* @param parent - Parent of the created `TcSymbol`
* @param pointer - Memory location of created `TcSymbol` in the Target PLC
* @param debug - If enabled, will produce debug information
*
* @return - An `TcSymbol` instance of a Symbol of type `STRING` or `WSTRING`
*/
instance(path, parent, pointer, debug = false) {
return new tc_symbol_1.TcStringSymbol(path, parent, pointer, this, debug);
}
/**
* Creates a clone with an optional mutation of this `TcType`
*
* @param mutator - Optional mutator, which can be applied to the clone
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - Either the clone `TcType` or `undefined` if cloning failed
*/
async clone(mutator) {
const clone = new TcStringType(this.context, this, this.__log.enabled);
return this.__clone(clone, mutator);
}
/**
* Internal function, which analyses attribute data from the Type ADS Data and
* adjusts aspects of this `TcType`
*
* Mutations supported :
* - ReadOnly
* - Ignored
* - OnSet
* - OnGet
* - OnClear
* - OnChange
* - Default
*
* @param name - name of the Attribute
* @param value - value of the Attribute
*/
__getParameter(name, value) {
super.__getParameter(name, value);
const nameFormatted = name.trim().toLowerCase();
if (nameFormatted === 'default') {
this.__default = true;
}
}
}
exports.TcStringType = TcStringType;
/**
* Class representing all `ENUM` Types in the Target PLC
*/
class TcEnumType extends TcType {
/**
* Constructs a `TcEnumType` to represent a `ENUM` Type in the Target PLC
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param debug - If enabled, will produce debug information
*/
constructor(context, typeData, debug = false) {
var _a;
super(context, typeData, debug);
/**
* @internal
*/
this.__enumFields = [];
/**
* @internal
*/
this.__enumBuffers = {};
if (typeData instanceof TcEnumType) {
this.buffers = typeData.__enumBuffers;
this.fields = typeData.__enumFields;
}
else {
(_a = typeData.enumInfo) === null || _a === void 0 ? void 0 : _a.forEach(enumField => {
const fullName = `${typeData.name}.${enumField.name}`;
this.__enumFields.push(fullName);
this.__enumBuffers[fullName] = enumField.value;
if (!this.defaultValue) {
this.defaultValue = enumField.name;
this.defaultBuffer = enumField.value;
}
});
this.__log(`() : Created type ${this.name}[TcEnumType]`);
}
}
/**
* Creates an instance of `TcEnumType` with default values transformed to
* raw data
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param debug - If enabled, will produce debug information
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - An initialized instance of `TcEnumType` if it is not ignored
*/
static async create(context, typeData, debug = false) {
const type = new TcEnumType(context, typeData, debug);
if (!type.ignored) {
return type;
}
}
/**
* Access all the fields, which are allowed to be written
* to this `ENUM`
*/
get fields() { return this.__enumFields; }
set fields(val) { this.__enumFields = [...val]; }
;
/**
* Access all the data buffers, which represent the `ENUM` fields
*/
get buffers() { return this.__enumBuffers; }
;
set buffers(val) {
for (let [key, buffer] of Object.entries(val)) {
this.__enumBuffers[key] = buffer;
}
}
/**
* Creates an instance of Type `ENUM` PLC Symbol, based on this `TcType`
*
* @param path - Path of the created `TcSymbol`
* @param parent - Parent of the created `TcSymbol`
* @param pointer - Memory location of created `TcSymbol` in the Target PLC
* @param debug - If enabled, will produce debug information
*
* @return - An `TcSymbol` instance of a Symbol of type `ENUM`
*/
instance(path, parent, pointer, debug = false) {
return new tc_symbol_1.TcEnumSymbol(path, parent, pointer, this, debug);
}
/**
* Creates a clone with an optional mutation of this `TcType`
*
* @param mutator - Optional mutator, which can be applied to the clone
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - Either the clone `TcType` or `undefined` if cloning failed
*/
async clone(mutator) {
const clone = new TcEnumType(this.context, this, this.__log.enabled);
return this.__clone(clone, mutator);
}
/**
* Internal function, which analyses attribute data from the Type ADS Data and
* adjusts aspects of this `TcType`
*
* Mutations supported :
* - ReadOnly
* - Ignored
* - OnSet
* - OnGet
* - OnClear
* - OnChange
* - Default
*
* @param name - name of the Attribute
* @param value - value of the Attribute
*/
__getParameter(name, value) {
super.__getParameter(name, value);
const nameFormatted = name.trim().toLowerCase();
if (nameFormatted === 'default') {
this.__default = value;
}
}
}
exports.TcEnumType = TcEnumType;
/**
* Class representing a `Structures`, `Function_Blocks` and `Unions` Types in the Target PLC.
*/
class TcStructType extends TcType {
/**
* Constructs a `TcBooleanType` to represent `Structures`, `Function_Blocks` and `Unions` Types in the Target PLC
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param children - The Children Types, that are part of this structure
* @param debug - If enabled, will produce debug information
*/
constructor(context, typeData, children = [], debug = false) {
super(context, typeData, debug);
/**
* @internal
*/
this.__children = [];
/**
* @internal
*/
this.__rpcMethods = [];
if (typeData instanceof TcStructType) {
this.__children = typeData.children;
this.__rpcMethods = typeData.__rpcMethods;
}
else {
this.__children = children;
//Process the RPC Methods
typeData.rpcMethods.forEach(meth => {
this.__rpcMethods.push(meth.name);
});
this.__log(`() : Created type ${this.name}[TcStructType]`);
}
}
/**
* Creates an instance of `TcStructType`
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param children - The Children Types, that are part of this structure
* @param debug - If enabled, will produce debug information
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - An initialized instance of `TcStructType` if it is not ignored
*/
static async create(context, typeData, children = [], debug = false) {
const type = new TcStructType(context, typeData, children, debug);
if (!type.ignored) {
return type;
}
}
/**
* Children and their types of this structure
*/
get children() { return this.__children; }
/**
* List of all RPC Methods available for this structure
*/
get rpcMethods() { return this.__rpcMethods; }
/**
* Creates an instance of Type `Structure`, `Function_Block` or `Union` PLC Symbol, based on this `TcType`
*
* @param path - Path of the created `TcSymbol`
* @param parent - Parent of the created `TcSymbol`
* @param pointer - Memory location of created `TcSymbol` in the Target PLC
* @param debug - If enabled, will produce debug information
*
* @return - An `TcSymbol` instance of a Symbol of type `Structures`, `Function_Blocks` or `Union`
*/
instance(path, parent, pointer, debug = false) {
return new tc_symbol_1.TcStructureSymbol(path, parent, pointer, this, debug);
}
/**
* Creates a clone with an optional mutation of this `TcType`
*
* @param mutator - Optional mutator, which can be applied to the clone
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - Either the clone `TcType` or `undefined` if cloning failed
*/
async clone(mutator) {
const clone = new TcStructType(this.context, this, this.children, this.__log.enabled);
return this.__clone(clone, mutator);
}
}
exports.TcStructType = TcStructType;
/**
* Class representing an `ARRAY` Type in the Target PLC.
*/
class TcArrayType extends TcType {
/**
* Constructs a `TcArrayType` to represent an `ARRAY` Type in the Target PLC
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param child - The type of the Array to create
* @param debug - If enabled, will produce debug information
*/
constructor(context, typeData, child, debug = false) {
super(context, typeData, debug);
/**
* @internal
*/
this.__dimensions = [];
if (typeData instanceof TcArrayType) {
this.dimensions = typeData.dimensions;
this.__child = child;
}
else {
this.__dimensions = typeData.arrayData;
this.__child = child;
this.__log(`() : Created type ${this.name}[TcArrayType]`);
}
}
/**
* Creates an instance of `TcArrayType`
*
* @param context - The `TcContext`, to which this Type belongs
* @param typeData - The information, from which the `TcType` is build
* @param child - The type of the Array to create
* @param debug - If enabled, will produce debug information
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - An initialized instance of `TcArrayType` if it is not ignored
*/
static async create(context, typeData, child, debug = false) {
const type = new TcArrayType(context, typeData, child, debug);
if (!type.ignored) {
return type;
}
}
/**
* Access the Dimensions of array
*/
get dimensions() { return this.__dimensions; }
set dimensions(val) { this.__dimensions = val; }
/**
* The Child `TcType` of this Array
*/
get child() { return this.__child; }
;
/**
* Creates a clone with an optional mutation of this `TcType`
*
* @param mutator - Optional mutator, which can be applied to the clone
*
* @throws {@link TcComIsInvalidException} - Attempting to use an invalidated `TcCom` Object
* @throws {@link TcComToRawException} - Error occurred when transforming value to raw data
*
* @return - Either the clone `TcType` or `undefined` if cloning failed
*/
async clone(mutator) {
const clone = new TcArrayType(this.context, this, this.child, this.__log.enabled);
return this.__clone(clone, mutator);
}
/**
* Creates an instance of a Type `ARRAY` PLC Symbol, based on this `TcType`
*
* @param path - Path of the created `TcSymbol`
* @param parent - Parent of the created `TcSymbol`
* @param pointer - Memory location of created `TcSymbol` in the Target PLC
* @param debug - If enabled, will produce debug information
*
* @return - An `TcSymbol` instance of a Symbol of type `ARRAY`
*/
instance(path, parent, pointer, debug = false) {
return new tc_symbol_1.TcArraySymbol(path, parent, pointer, this, 0, debug);
}
}
exports.TcArrayType = TcArrayType;