Created
August 25, 2016 09:31
-
-
Save bastiW/9073f7be0e5f3195159b83714a1be710 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global require,setInterval,console */ | |
var opcua = require("node-opcua"); | |
var Variant = opcua.Variant; | |
var DataType = opcua.DataType; | |
var DataValue = opcua.DataValue; | |
// Let's create an instance of OPCUAServer | |
var server = new opcua.OPCUAServer({ | |
port: 26543, // the port of the listening socket of the server | |
resourcePath: "UA/SampleServer", // this path will be added to the endpoint resource name | |
buildInfo: { | |
productName: "Arburg Sample Server", | |
buildNumber: "7658", | |
buildDate: new Date(2016, 8, 23) | |
} | |
}); | |
function post_initialize() { | |
console.log("initialized"); | |
function construct_my_address_space(server) { | |
var addressSpace = server.engine.addressSpace; | |
// declare a new object | |
var device = addressSpace.addObject({ | |
organizedBy: addressSpace.rootFolder.objects, | |
browseName: "MyDevice" | |
}); | |
/* | |
OPC Variable: AUFTRAGSSUECKZAHL | |
the value is fixed to 1000 | |
*/ | |
function auftragsstueckzahl() { | |
return 1000 | |
} | |
server.engine.addressSpace.addVariable({ | |
componentOf: device, | |
nodeId: "ns=2;s=f076", // a string nodeID | |
browseName: "Auftragsstueckzahl", | |
dataType: "Double", | |
value: { | |
get: function () { | |
return new opcua.Variant({dataType: opcua.DataType.Double, value: auftragsstueckzahl()}); | |
} | |
} | |
}); | |
/* | |
OPC Variable: GUTTEILE | |
the value increases +1 every 3 up to Auftragsstückzahl | |
*/ | |
var gutteile = 0; | |
var gutteileCounter = setInterval(function(){ | |
if(gutteile<auftragsstueckzahl()) { | |
gutteile++ | |
} else { | |
clearInterval(gutteileCounter) | |
} | |
}, 3000); | |
addressSpace.addVariable({ | |
//organizedBy: myDevices, | |
browseName: "Gutteile", | |
nodeId: "ns=2;s=f077", | |
dataType: "Double", | |
value: { | |
get: function () { | |
return new opcua.Variant({dataType: opcua.DataType.Double, value: gutteile}); | |
} | |
} | |
}); | |
/** | |
* OPC Variable: SCHLECHTTEILE | |
* returns the Schlechtteile, the value increases +1 every 10 up to 3 | |
*/ | |
var schlechtteile = 0; | |
var schlechtteileCounter = setInterval(function(){ | |
if(schlechtteile<3) { | |
schlechtteile++ | |
} else { | |
clearInterval(schlechtteileCounter) | |
} | |
}, 10000); | |
addressSpace.addVariable({ | |
//organizedBy: myDevices, | |
browseName: "Schlechtteile", | |
nodeId: "ns=2;s=f087", | |
dataType: "Double", | |
value: { | |
get: function () { | |
return new opcua.Variant({dataType: opcua.DataType.Double, value: schlechtteile}); | |
} | |
} | |
}); | |
/** | |
* OPC Variable: REFERENZWERT ZYKLUSZEIT | |
* NODEID: t4011 | |
* returns the Zykluszeit, value fixed to 35 | |
*/ | |
var zykluszeitReferenzwert = 35; | |
addressSpace.addVariable({ | |
//organizedBy: myDevices, | |
browseName: "Schlechtteile", | |
nodeId: "ns=2;s=t4011", | |
dataType: "Double", | |
value: { | |
get: function () { | |
return new opcua.Variant({dataType: opcua.DataType.Double, value: zykluszeitReferenzwert}); | |
} | |
} | |
}); | |
/** | |
* OPC Variable: ZYKLUSZEIT Ist-Wert | |
* NodeId: t4012 | |
* returns the Schlechtteile, the value increases +1 every ZyckluszeitReferenzWert up to 3 | |
*/ | |
var zykluszeitIstWert = 0; | |
var zykluszeitIstWertCounter = setInterval(function(){ | |
if(zykluszeitIstWert<zykluszeitReferenzwert) { | |
zykluszeitIstWert++ | |
} else { | |
zykluszeitIstWert = 0; | |
} | |
}, 1000); | |
addressSpace.addVariable({ | |
//organizedBy: myDevices, | |
browseName: "Schlechtteile", | |
nodeId: "ns=2;s=t0412", | |
dataType: "Double", | |
value: { | |
get: function () { | |
return new opcua.Variant({dataType: opcua.DataType.Double, value: schlechtteile}); | |
} | |
} | |
}); | |
} | |
construct_my_address_space(server); | |
server.start(function () { | |
console.log("Server is now listening ... ( press CTRL+C to stop)"); | |
console.log("port ", server.endpoints[0].port); | |
var endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl; | |
console.log(" the primary server endpoint url is ", endpointUrl); | |
}); | |
} | |
server.initialize(post_initialize); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment