Skip to content
Snippets Groups Projects
Commit be3cbc51 authored by Diego Francisco Carvajal Flores's avatar Diego Francisco Carvajal Flores
Browse files

Update files

parent 831c9623
No related branches found
No related tags found
No related merge requests found
module.exports = {
parserOptions: {
ecmaVersion: 8
}
};
\ No newline at end of file
/node_modules
/upmclient.tar
/*.tar
FROM node:10
WORKDIR /usr
COPY package.json ./
COPY . .
# COPY ./client.ts ./
COPY ./upmclient.ts ./
COPY ./Logger.ts ./
RUN npm install
EXPOSE 4005
RUN npm install -g ts-node
CMD [ "ts-node", "./upmclient.ts" ]
export class Logger {
loggerSignature="";
static log(logLevel:string,logMessage:string) {
console.log(`${new Date().toISOString()} - UPMCLIENT - ${logLevel} -> ${logMessage}`);
}
static failure(message:string){
Logger.log('FAILURE',message);
}
static sucess(message:string){
Logger.log('SUCESS',message);
}
static warn(message:string){
Logger.log('WARN',message);
}
static debug(message:string){
Logger.log('DEBUG',message);
}
}
\ No newline at end of file
LINE_2_URL=opc.tcp://10.100.151.40:4840
TypeNoCur_NODEID_L2=ns=4;s=DataHandling.TypeNoCur
BlisterEntry_NODEID_L2=ns=4;s=Station090.BlisterEntry
LINE_5_URL=opc.tcp://192.168.10.90:4840
TypeNoCur_NODEID_L5=ns=4;s=DataHandling.TypeNoCur
BlisterEntry_NODEID_L5=ns=4;s=Station090.BlisterEntry
FIWARE_NODE_ID=urn:ngsi-ld:Alert:BOSH:feed-trigger-
FIWARE_SERVER_URL=http://10.100.147.11:1026
#FIWARE_RECORD_TYPE=https://uri.fiware.org/ns/data-models%23Alert
\ No newline at end of file
This diff is collapsed.
{
"name": "node-opcua-sample",
"version": "0.4.5",
"description": "Simple OPCUA client using node-opcua",
"main": "simple_client.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/erossignon/node-opcua-sample.git"
},
"keywords": [
"opcua"
],
"author": "Etienne Rossignon",
"license": "MIT",
"dependencies": {
"@types/node": "^14.11.2",
"async": "~3.2.0",
"axios": "^0.21.4",
"node-opcua": "~2.18.0",
"ts-node": "^3.0.4"
},
"devDependencies": {
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1"
}
}
#!/bin/bash
#stopping upm client container
docker container stop upmclient
#removing upm client container
docker container rm upmclient
#removing upm client image
docker image rm upmclient
#building upm client image
docker build -t upmclient .
#saving upm client image to upmclient.tar in current directory
docekr save -o upmclient.tar upmclient
#creating upm client container
docker run -d --env-file ./environment.env -t OPCUAClient
\ No newline at end of file
test.ts 0 → 100644
const data=[
{
"@context": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld",
"id": "urn:ngsi-ld:Alert:BOSH:feed-trigger-2",
"type": "https://uri.fiware.org/ns/data-models#Alert"
},
{
"@context": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld",
"id": "urn:ngsi-ld:Alert:BOSH:feed-trigger-3",
"type": "https://uri.fiware.org/ns/data-models#Alert"
},
{
"@context": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld",
"id": "urn:ngsi-ld:Alert:BOSH:feed-trigger-2321",
"type": "https://uri.fiware.org/ns/data-models#Alert"
},
{
"@context": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld",
"id": "urn:ngsi-ld:Alert:BOSH:feed-trigger-12",
"type": "https://uri.fiware.org/ns/data-models#Alert"
},
{
"@context": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld",
"id": "urn:ngsi-ld:Alert:BOSH:feed-trigger-45552",
"type": "https://uri.fiware.org/ns/data-models#Alert"
},
{
"@context": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld",
"id": "urn:ngsi-ld:Alert:BOSH:feed-trigger-4",
"type": "https://uri.fiware.org/ns/data-models#Alert"
},
]
const restult = data.map(o=>o.id.split('-')
.slice(-1)[0])
.map(l=>parseInt(l)).sort( (a,b) =>a - b )
.slice(-1)[0]
console.log(restult);
\ No newline at end of file
export const trytypes={
TypeA: ["0285009006", "0285009016","0285009018","0285009024","0285009072","0285009107",
"0285009121","0285009123"],
TypeB: ["0285009002","0285009017","0285009020","0285009028","0285009035","0285009048",
"0285009063","0285009065","0285009088","0285009091"],
TypeC: ["0285009008","0285009010","0285009012","0285009022","0285009026","0285009032",
"0285009042","0285009050","0285009052","0285009057","0285009074","0285009078",
"0285009080","0285009082","0285009086","0285009092","0285009093","0285009094",
"0285009095","0285009097","0285009105","0285009108","0285009109","0285009115",
"0285009116","0285009117","0285009122","0285009124","0285009125","0285009126",
"0285009132","0285009133","0285009137","0285009138","0285009139","0285009140",
"0285009141","0285009144","0285009145","0285009146","0285009147","0285009149",
"0285009150","0285009151","0285009152","0285009153","0285009154","0285009160",
"0285009163","0285009164","0285009168","0285009170","0285009171","0285009172",
"0285009173","0285009176","0285009177","0285009178","0285009202","0285009203",
"0285009209","0285009210","0285009211"]
}
\ No newline at end of file
{
"compilerOptions": {
"target": "es5",
"moduleResolution": "node",
"module": "commonjs",
"declaration": false,
"outDir": "./dist",
"strict": false,
"lib": ["es2015","es2015.promise"],
"sourceMap": true,
"noImplicitAny": true
}
}
import { AttributeIds,OPCUAClient,DataValue,TimestampsToReturn,SecurityPolicy, NotificationMessage} from "node-opcua";
import { Logger } from './Logger'
import { trytypes } from './trytypes'
var trayType="NA";
var default_index = 0
// const nodeId_TypeNoCur = "ns=4;s=\"DataHandling\".\"TypeNoCur\"";
const dateIssued ="https://smart-data-models.github.io/data-models/terms.jsonld#/definitions/dateIssued"
var axios= require("axios")
//Fiware Configuration
const fiware_url=process.env.FIWARE_SERVER_URL || undefined
const fiware_node_id=process.env.FIWARE_NODE_ID || undefined
//Line 2 configuration
const LINE_2_URL=process.env.LINE_2_URL || undefined
const TypeNoCur_plc_nodeid_L2=process.env.TypeNoCur_NODEID_L2 || undefined
const BlisterEntry_plc_nodeid_L2=process.env.BlisterEntry_NODEID_L2 || undefined
//Line 5 configuration
const LINE_5_URL=process.env.LINE_5_URL || undefined
const TypeNoCur_plc_nodeid_L5=process.env.TypeNoCur_NODEID_L5 || undefined
const BlisterEntry_plc_nodeid_L5=process.env.BlisterEntry_NODEID_L5|| undefined
// interval of subscription checks
const publishing_interval = 1000
var notification_body ={
id: '',
type: "Alert",
category:{
type: "Property",
value: "feeding-trigger"
},
description: {
type: "Property",
value: '',
},
dateIssued: {
type: "Property",
value: {
"@type": "DateTime",
"@value": new Date().toISOString()
}
},
alertSource: {
type: "Relationship",
object: "urn:ngsi-ld:Asset:BOS:LS2"
},
"@context": [
"https://smartdatamodels.org/context.jsonld",
"https://raw.githubusercontent.com/shop4cf/data-models/master/docs/shop4cfcontext.jsonld"
]
}
const connectionStrategy={
maxRetry: 2,
initialDelay: 2000,
maxDelay: 10 * 1000
}
const requestedParameter= {
samplingInterval: 500,
discardOldest: true,
queueSize: 10
}
const serverConnectionConfiguration={
endpoint_must_exist: false,
securityPolicy: SecurityPolicy.None,
applicationName: "WoT-IL",
connectionStrategy: connectionStrategy
}
async function init(){
if(fiware_node_id == undefined){
Logger.failure(`undefined fiware_node_id. Actual value =${fiware_node_id}. Check environment configuration`)
throw new Error();
}
if(fiware_url == undefined){
const message=`undefined fiware_url. Actual value =${fiware_url}. Check environment configuration`
Logger.failure(message)
throw new Error(message);
}
if(LINE_2_URL == undefined){
const message=`undefined LINE_2_URL. Actual value =${LINE_2_URL}. Check environment configuration`
Logger.failure(message)
throw new Error(message);
}
if(LINE_5_URL == undefined){
const message=`undefined LINE_5_URL. Actual value =${LINE_5_URL}. Check environment configuration`
Logger.failure(message)
throw new Error(message);
}
if(TypeNoCur_plc_nodeid_L2 == undefined){
const message=`undefined TypeNoCur_plc_nodeid_L2. Actual value =${TypeNoCur_plc_nodeid_L2}. Check environment configuration`
Logger.failure(message)
throw new Error(message);
}
if(BlisterEntry_plc_nodeid_L2 == undefined){
const message=`undefined BlisterEntry_plc_nodeid_L2. Actual value =${BlisterEntry_plc_nodeid_L2}. Check environment configuration`
Logger.failure(message)
throw new Error(message);
}
if(TypeNoCur_plc_nodeid_L5 == undefined){
const message=`undefined TypeNoCur_plc_nodeid_L5. Actual value =${TypeNoCur_plc_nodeid_L5}. Check environment configuration`
Logger.failure(message)
throw new Error(message);
}
if(BlisterEntry_plc_nodeid_L5 == undefined){
const message=`undefined BlisterEntry_plc_nodeid_L5. Actual value =${BlisterEntry_plc_nodeid_L5}. Check environment configuration`
Logger.failure(message)
throw new Error(message);
}
const entity_id = await getLastRecordOfType("https://uri.fiware.org/ns/data-models%23Alert")
if(entity_id > 0){
default_index = 0
}
//Line 2 setup
const l2_client = await connectoToPLC(LINE_2_URL)
const TypeNoCur_subscription_L2 = await createSubscription(l2_client,TypeNoCur_plc_nodeid_L2)
const BlisterEntry_subscription_L2 = await createSubscription(l2_client,BlisterEntry_plc_nodeid_L2)
TypeNoCur_subscription_L2.on("changed", async (dataValue_TypeNoCur: DataValue) => {
Logger.debug(`Monitoring TypeNoCur on line 2. Current value = ${dataValue_TypeNoCur.value.value.toString()}`)
if(trytypes.TypeA.indexOf(dataValue_TypeNoCur.value.value.toString()) != -1){
trayType = 'A'
}else if(trytypes.TypeB.indexOf(dataValue_TypeNoCur.value.value.toString()) == -1){
trayType = 'B'
}else if(trytypes.TypeC.indexOf(dataValue_TypeNoCur.value.value.toString()) == -1){
trayType = 'C'
}else{
Logger.warn(`L2 - try type not defined. Given ${dataValue_TypeNoCur.value.value.toString()}`)
}
});
BlisterEntry_subscription_L2.on("changed", async (dataValue_BlisterEntry: DataValue) => {
Logger.debug(`Monitoring BlisterEntry on line 2. Current value = ${dataValue_BlisterEntry.value.value.toString()}`)
if(dataValue_BlisterEntry.value.value.toString() == "false"){
default_index=default_index+1;
notification_body.id=`${fiware_node_id}${default_index}`
notification_body.description.value=trayType
notification_body.alertSource.object='urn:ngsi-ld:Asset:BOS:LS2'
postRecord(notification_body)
}else{
Logger.debug("BlisterEntry at line L2 is not needed")
}
});
//Line 5 setup
const l5_client = await connectoToPLC(LINE_5_URL)
const TypeNoCur_subscription_L5 = await createSubscription(l5_client,TypeNoCur_plc_nodeid_L5)
const BlisterEntry_subscription_L5 = await createSubscription(l5_client,BlisterEntry_plc_nodeid_L5)
TypeNoCur_subscription_L5.on("changed", async (dataValue_TypeNoCur: DataValue) => {
Logger.debug(`Monitoring TypeNoCur on line 5. Current value = ${dataValue_TypeNoCur.value.value.toString()}`)
if(trytypes.TypeA.indexOf(dataValue_TypeNoCur.value.value.toString()) != -1){
trayType = 'A'
}else if(trytypes.TypeB.indexOf(dataValue_TypeNoCur.value.value.toString()) == -1){
trayType = 'B'
}else if(trytypes.TypeC.indexOf(dataValue_TypeNoCur.value.value.toString()) == -1){
trayType = 'C'
}else{
Logger.warn(`L5 - try type not defined. Given ${dataValue_TypeNoCur.value.value.toString()}`)
}
});
BlisterEntry_subscription_L5.on("changed", async (dataValue_BlisterEntry: DataValue) => {
Logger.debug(`Monitoring BlisterEntry on line 5. Current value = ${dataValue_BlisterEntry.value.value.toString()}`)
if(dataValue_BlisterEntry.value.value.toString() == "false"){
default_index=default_index+1;
notification_body.id=`${fiware_node_id}${default_index}`
notification_body.description.value=trayType
notification_body.alertSource.object='urn:ngsi-ld:Asset:BOS:LS5'
postRecord(notification_body)
}else{
Logger.debug("BlisterEntry at line L5 is not needed")
}
});
}
async function connectoToPLC(url:any){
Logger.debug(`Connecting to PLC ${url}...`)
const client = OPCUAClient.create(serverConnectionConfiguration);
try {
await client.connect(url)
return client
} catch (error) {
Logger.failure(`Cant connect to PLC ${url} --- ${JSON.stringify(error)}`)
}
}
async function getLastRecordOfType(record_type:any){
Logger.debug(`finding record of type ${record_type}`)
try {
var result = await axios.get(`${fiware_url}/ngsi-ld/v1/entities?type=${record_type}`).then((response:any)=>response.data)
if(result.length != 0 ){
let idStartNumber = result.map((o:any)=>o.id.split('-')
.slice(-1)[0])
.map((l:string)=>parseInt(l)).sort( (a: number,b: number) =>a - b )
.slice(-1)[0]
Logger.debug(`last notification id was ${idStartNumber}`)
return parseInt(idStartNumber)
}else{
Logger.debug(`there is no entity matching with ${record_type}`)
return 0
}
} catch (error) {
Logger.failure(`EXCEPTION GIVEN - cant get getLastRecordOfType ${record_type } - ${JSON.stringify(error)}`)
return 0
}
}
async function createSubscription(client:any,node_id:any) {
Logger.debug(`creating subscription for ${node_id}...`);
const session = await client.createSession();
const subscription = await session.createSubscription2({
requestedPublishingInterval: 1000,
requestedLifetimeCount: 100,
requestedMaxKeepAliveCount: 20,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10
});
subscription
.on("started", () => console.log("subscription started - subscriptionId=", subscription.subscriptionId))
.on("keepalive", () => Logger.debug(`keep alive ${node_id}`))
.on("terminated", () => console.log("subscription terminated"));
const itemToMonitor={
nodeId:node_id,
attributeId: AttributeIds.Value
}
const monitoredItem = await subscription.monitor(
itemToMonitor,
requestedParameter,
TimestampsToReturn.Both);
return monitoredItem
}
async function readVariable(client:any,node:any){
const session = await client.createSession();
const maxAge = 0;
const nodeToRead = {
nodeId: node,
};
const dataValue = await session.read(nodeToRead, maxAge);
console.log(" value " , dataValue.toString());
}
async function browse(client:any){
const session = await client.createSession();
const browseResult = await session.browse("RootFolder");
// const browsePath = makeBrowsePath("RootFolder", "/Objects/Server.ServerStatus.BuildInfo");
// const result = await session.translateBrowsePath(browsePath);
console.log(JSON.stringify(browseResult));
// const productNameNodeId = result.targets[0].targetId;
// console.log(" Product Name nodeId = ", productNameNodeId.toString());
}
function postRecord(body:any){
const headers={
'Content-Type': 'application/ld+json',
}
Logger.debug(`body id = ${body.id}`)
axios.post(`${fiware_url}/ngsi-ld/v1/entities`,body,{headers:headers}).then((response:any)=>{
Logger.debug(JSON.stringify({response:response.status,statusText:response.statusText}))
},(error:any)=>{
Logger.failure(JSON.stringify(error))
}).catch((r:any)=>{
Logger.failure(JSON.stringify(r))
})
}
init()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment