All files / node-opcua-isa95/lib isa95_address_space_extension_physical_asset.js

87.34% Statements 69/79
75% Branches 18/24
80% Functions 8/10
87.34% Lines 69/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 2751x 1x 1x   1x                                       1x   2x 2x 2x   2x 2x           2x                   2x 2x                           2x       2x     1x   2x 2x 2x   2x 2x         2x       4x 4x 4x 4x                                                                             1x   13x 13x   13x 13x   13x 13x   13x     13x   13x                 13x               13x   13x 13x                   13x   4x       4x   13x                                       1x   1x 1x   1x   1x 1x 1x       1x             1x               13x   1x     13x   1x 1x 1x 1x   1x           1x             13x     1x 1x   1x   1x             1x   13x      
var assert = require("assert");
var path = require("path");
var _ = require("underscore");
 
module.exports = function (opcua) {
 
    /**
     *
     * Attribute     Value
     * BrowseName    PhysicalAssetClassType
     * IsAbstract    False
     * Subtype of the ISA95ClassType defined in 7.6.2.
     * References                NodeClass     BrowseName         DataType           TypeDefinition                 ModellingRule
     * HasISA95ClassProperty     Variable      <PropertyName>     BaseDataType       PhysicalAssetClassPropertyType OptionalPlaceholder
     * HasISA95Attribute         Variable      Manufacturer                          CompanyType                    Optional
     * HasISA95Attribute         Variable      ModelNumber        String             BaseDataVariableType           Optional
     * TestedByPhysicalAssetTest Object       <TestSpecification>                    PhysicalAssetCapabilityTestSpecificationType
     *
     * @param options
     * @param options.browseName {String/QualifiedName} : the new PhysicalAssetClassType name
     * @param [options.manufacturer {CompanyType}] : the CompanyType
     * @param [options.modelNumber {String} ] : the model Number
     *
     */
    opcua.AddressSpace.prototype.addPhysicalAssetClassType = function(options) {
 
        assert(options.browseName);
        var addressSpace  = this;
        const namespace = addressSpace.getOwnNamespace();
 
        var physicalAssetClassType = addressSpace.findISA95ObjectType("PhysicalAssetClassType");
        var newPhysicalAssetClassType = namespace.addObjectType({
            browseName: options.browseName,
            subtypeOf: physicalAssetClassType
        });
 
        function addManufacturerAttribute(node,manufacturer) {
            addressSpace.addISA95Attribute({
                ISA95AttributeOf: node,
                typeDefinition: addressSpace.findISA95VariableType("CompanyType"),
                browseName: "Manufacturer",
                dataType: opcua.DataType.String,
                value: { dataType: opcua.DataType.String , value: "manufacturer"},
                modellingRule: "Mandatory"
            });
        }
        // add optional property PhysicalAsset Level
        Eif (options.manufacturer) {
            addManufacturerAttribute(newPhysicalAssetClassType, options.manufacturer);
        }
        function addModelNumberAttribute(node,modelNumber) {
 
            assert(typeof modelNumber === "string","expecting modelNumber to be a string");
            addressSpace.addISA95Attribute({
                ISA95AttributeOf: node,
                browseName: "ModelNumber",
                dataType: "String",
                value: {dataType: opcua.DataType.String, value: modelNumber },
                modellingRule: "Mandatory"
 
            });
        }
        Iif (options.modelNumber) {
            addModelNumberAttribute(newPhysicalAssetClassType, options.modelNumber);
        }
 
        return newPhysicalAssetClassType;
    };
 
    opcua.AddressSpace.prototype.addPhysicalAssetType = function(options) {
 
        assert(options.browseName);
        var addressSpace  = this;
        const namespace = addressSpace.getOwnNamespace();
 
        var physicalAssetType = addressSpace.findISA95ObjectType("PhysicalAssetType");
        var newPhysicalAssetType = namespace.addObjectType({
            browseName: options.browseName,
            subtypeOf: physicalAssetType
        });
 
        return newPhysicalAssetType;
    };
 
    function _physicalAsset_madeUpOfPhysicalAssets() {
        var node = this;
        var addressSpace = node.addressSpace;
        var madeUpOfEquipment = addressSpace.findISA95ReferenceType("MadeUpOfPhysicalAsset");
        return node.findReferencesExAsObject(madeUpOfEquipment);
    }
 
    /**
     * Table 69 – PhysicalAssetType definition
     * Attribute   Value
     * BrowseName  PhysicalAssetType
     * IsAbstract  False
     * References                  NodeClass    BrowseName         DataType          TypeDefinition            ModellingRule
     * Subtype of the ISA95ObjectType defined in 7.6.3.
     * HasISA95Property            Variable    <PropertyName>      BaseDataType      PhysicalAssetPropertyType OptionalPlaceholder
     * MadeUpOfPhysicalAsset       Object      <PhysicalAsset>                       PhysicalAssetType         OptionalPlaceholder
     * TestedByPhysicalAssetTest   Object      <TestSpecification>                   PhysicalAssetCapabilityTestSpecificationType OptionalPlaceholder
     * DefinedByPhysicalAssetClass Object      PhysicalAssetClass                    PhysicalAssetClassType    Optional
     * LocatedIn                   Variables   PhysicalLocation    String            GeoSpatialLocationType    Optional
     * HasISA95Attribute           Variables   FixedAssetId        CDTIdentifier     BaseDataVariableType      Optional
     * HasISA95Attribute           Variables   VendorId            BaseDataType      CompanyType               Optional
     * ImplementationOf            Object      Equipment                             EquipmentType             Optional
     * HasComponent                Variable    AssetAssignment     structure         ISA95AssetAssignmentType  Optional
     *
     *  @param options
     *  @param options.browseName
     *  @param options.description
     *  @param [options.organizedBy]
     *  @param options.definedByPhysicalAssetClass
     *  @param [options.containedByPhysicalAsset {Object}]
     *  @param [options.implementationOf {Equipment} ] razeaze*
     *  @param options.locatedIn {PhysicalLocation} the actual location associated with the instance of this PhysicalAssetType
     *  @param [options.fixedAssetId]     the vendor provided identification number provided with the instance of the
     *                                    PhysicalAssetType. For Example it maybe the serial number associated with device.
     *  @param options.vendorId          the vendor that provides the instance of this PhysicalAssetType. This may be the same
     *                                    as the manufacturer associated with the related class, but it may also be different.
     *                                    For example a system integrator maybe the vendor that provides the PhysicalAssetType,
     *                                    even though it is manufactured by another company.
     * @param options.vendorId.typeDefinition
     * @param options.vendorId.dataType
     * @param options.vendorId.value {Variant}
     * @param [options.organizedBy]
     */
    opcua.AddressSpace.prototype.addPhysicalAsset = function(options) {
 
        var addressSpace  = this;
        const namespace = addressSpace.getOwnNamespace();
 
        var physicalAssetClassType = addressSpace.findISA95ObjectType("PhysicalAssetClassType");
        assert(physicalAssetClassType);
 
        var physicalAssetType = addressSpace.findISA95ObjectType("PhysicalAssetType");
        assert(physicalAssetType);
 
        options.typeDefinition = options.typeDefinition || physicalAssetType;
 
        // The SourceNode of this ReferenceType shall be an Object of EquipmentType or its subtype.
        assert(options.typeDefinition.isSupertypeOf(physicalAssetType),"TypeDefinition of PhysicalAsset to create shall be of type PhysicalAssetType");
 
        Iif (false) {
            var physicalAsset = namespace.addObject({
                typeDefinition: options.typeDefinition,
                browseName:    options.browseName,
                description:   options.description,
                organizedBy: options.organizedBy
            });
        }
 
        var physicalAsset = options.typeDefinition.instantiate({
            browseName:    options.browseName,
            description:   options.description,
            organizedBy: options.organizedBy,
            modellingRule: options.modellingRule
        });
 
 
        options.definedByPhysicalAssetClass = options.definedByPhysicalAssetClass || "PhysicalAssetClassType";
 
        assert(options.definedByPhysicalAssetClass," expecting a definedByPhysicalAssetClass options");
        opcua.ISA95.utils.installDefinedByFooClassReferences({
            node: physicalAsset,
            definedByFooClass :options.definedByPhysicalAssetClass,
            fooClassType: physicalAssetClassType,
            definedByFooClassRef: "DefinedByPhysicalAssetClass",
            fooType: physicalAssetType
        });
 
        // also add ISA96 Property and ISA95 Attribute that may be defined in type Definition
 
        if (options.containedByPhysicalAsset) {
            function _addContainedByPhysicalAssetReference(node,physicalAsset) {
                opcua.ISA95.utils._addContainedByFooReference(node,physicalAsset,
                    "PhysicalAssetType","MadeUpOfPhysicalAsset");
            }
 
            _addContainedByPhysicalAssetReference(physicalAsset,options.containedByPhysicalAsset);
        }
        physicalAsset.madeUpOfPhysicalAssets = _physicalAsset_madeUpOfPhysicalAssets;
 
        function assert_NodeIsOfTypedefintionCompanyType(node) {
            var addressSpace = node.addressSpace;
            assert(node.constructor.name ==="UAVariable", "node must be a UAVariable");
            var companyType = addressSpace.findISA95VariableType("CompanyType");
            if(!node.typeDefinitionObj.isSupertypeOf(companyType)) {
                throw new Error("node "+ node.browseName.toString() + " must be of type definition CompanyType")
            }
        }
 
        /**
         *
         * @param instance
         * @param vendorId {UAVariable} of typeDefinition CompanyType
         * @param vendorId.typeDefinition
         * @param vendorId.dataType
         * @param vendorId.value {Variant}
         */
        function setVendorId(instance,vendorId) {
            assert(typeof vendorId === "object");
            //xx assert_NodeIsOfTypedefintionCompanyType(vendorId);
            var addressSpace = instance.addressSpace;
            const namespace = addressSpace.getOwnNamespace();
 
            var hasISA95Attribute = addressSpace.findISA95ReferenceType("HasISA95Attribute");
 
            var companyType = addressSpace.findISA95VariableType("CompanyType");
            vendorId.typeDefinition = vendorId.typeDefinition || companyType;
            Iif (!vendorId.typeDefinition.isSupertypeOf(companyType)) {
                throw new Error("vendorId.typeDefinition must be of type CompanyType");
            }
 
            var vendorIdNode = namespace.addVariable({
                typeDefinition: vendorId.typeDefinition,
                browseName: "VendorId",
                dataType: vendorId.dataType,
                value: vendorId.value
            });
 
            instance.addReference({
                referenceType: hasISA95Attribute.nodeId,
                forward: true,
                nodeId: vendorIdNode
            });
            // set vendor Id value
        }
 
        if (options.vendorId) {
 
            setVendorId(physicalAsset,options.vendorId,options.vendorIdType);
        }
 
        if (options.fixedAssetId) {
 
            assert(typeof options.fixedAssetId === "string");
            var hasISA95Attribute = addressSpace.findISA95ReferenceType("HasISA95Attribute");
            var CDTIdentifier = addressSpace.findISA95DataType("CDTIdentifier");
            assert(CDTIdentifier);
 
            var fixedAssetIdNode = namespace.addVariable({
                typeDefinition: "BaseDataVariableType",
                browseName: "FixedAssetId",
                dataType: CDTIdentifier,
                value: { dataType: opcua.DataType.String, value: options.fixedAssetId}
            });
            physicalAsset.addReference({
                referenceType: hasISA95Attribute.nodeId,
                forward: true,
                nodeId: fixedAssetIdNode
            });
        }
 
        if (options.implementationOf) {
 
            // verify that options.implementationOf is of type Equipment
            var equipmentType = addressSpace.findISA95ObjectType("EquipmentType");
            options.implementationOf.typeDefinitionObj.isSupertypeOf(equipmentType);
 
            var implementationByRef = addressSpace.findISA95ReferenceType("ImplementedBy");
 
            physicalAsset.addReference({
                referenceType: implementationByRef.nodeId,
                isForward:     false,
                nodeId:        options.implementationOf.nodeId
            });
 
            // todo turn into a __Getter__
            physicalAsset.implementationOf = options.implementationOf;
        }
        return physicalAsset;
    };
};