Skip to content

Commit

Permalink
initial before moving to Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
newshade committed Jul 12, 2022
1 parent e5a56ae commit dfa4da5
Show file tree
Hide file tree
Showing 5 changed files with 2,002 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
token.txt
npm-debug.log
/node_modules
.gitignore
150 changes: 145 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
// TELEGRAM NOTIFIER

const Telegraf = require('telegraf')
//const BOT_TOKEN = '1133596886:AAHrPa1cXXQAzZRPkSWvbJVJeM0yd9_x7HI' //Token generowany przy tworzeniu BOTa
var USER_ID = 5330515522; //ID uzytkownika czytane przez bota 'userinfobot'
const {
OPCUAClient, makeBrowsePath,
AttributeIds, resolveNodeId,
TimestampsToReturn, DataType,
installAlarmMonitoring, DataTypeDefinition,
ClientAlarmList, EventEmitter
} = require("node-opcua")
const async = require("async");
const USER_ID = process.env.USER_ID // ID uzytkownika czytane przez bota 'userinfobot'
const BOT_TOKEN = process.env.BOT_TOKEN // Token generowany przez BotFather
const bot = new Telegraf(process.env.BOT_TOKEN)
var acknowledge = false
var userIdentity = {
userName: 'administrator',
password: 'Sim@tic1518'
};

// Wiadomosc powitalna
bot.start((ctx) => ctx.reply('Witaj w testowym bocie Social Notifier.\n\nMożesz skorzystać z jednej z poniższych komend:\n/start - wiadomośc powitalna\ntest - testowy alarm z potwierdzeniem'))
bot.hears('alarm', (ctx) => ctx.reply('Wyzwolona funkcja ESTOP_8, wymagane potwierdzenie'))
bot.start((ctx) => ctx.reply(

`Witaj w testowym bocie Social Notifier.\n\n
Możesz skorzystać z jednej z poniższych komend:\n
/start - wiadomośc powitalna\n
/test - testowy alarm z potwierdzeniem`

))


bot.hears('alarm', (ctx) => {
// Nasłuchiwanie wiadomości
ctx.reply('Wyzwolona funkcja ESTOP_8, wymagane potwierdzenie')
})
//bot.startPolling()


Expand Down Expand Up @@ -39,4 +63,120 @@ bot.action('ack', (ctx) => {
console.log(acknowledge)
} catch (err) {console.log(err)}
})
bot.startPolling()

bot.startPolling()
subscribeAlarms()


// Communication part
async function subscribeAlarms () {

const endpointUrl = "opc.tcp:https://192.168.137.10:4840";
const nodeId = "ns=7;s=Scalar_Simulation_Double";
let theSession = null;
const userIdentity = {
userName: 'administrator',
password: 'Sim@tic1518'
};
const client = OPCUAClient.create({ endpointMustExist: false });
const alarmList = new ClientAlarmList;

async function main() {

try {
async.series([
// step 1 : connect to
function(callback) {

client.connect(endpointUrl, function(err) {

if (err) {
console.log(" cannot connect to endpoint :", endpointUrl);
} else {
console.log("connected !");
}
callback(err);
});
},
// step 2 : createSession
function(callback) {
client.createSession(userIdentity, function(err, session) {
if (!err) {
theSession = session;
}
callback(err);
});

},
// create subscription
function(callback) {

theSession.createSubscription2({
requestedPublishingInterval: 1000,
requestedLifetimeCount: 1000,
requestedMaxKeepAliveCount: 20,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10
}, function(err, subscription) {
if (err) { return callback(err); }
theSubscription = subscription;

theSubscription.on("keepalive", function() {
console.log("keepalive");
}).on("terminated", function() {
});
callback();
});

},
function(callback) {
// install monitored item
//
theSubscription.monitor({
nodeId: resolveNodeId("Server"),
attributeId: AttributeIds.EventNotifier
},
{
samplingInterval: 100,
discardOldest: true,
queueSize: 10
}, TimestampsToReturn.Both,
(err, monitoredItem) => {
console.log("-------------------------------------");
monitoredItem
.on("changed", function(value) {
console.log(" New Value = ", value.toString());
})
.on("err", (err) => {
console.log("MonitoredItem err =", err.message);
});
callback(err);

});
},
function(callback) {
// install monitored item
//
installAlarmMonitoring(theSession, (err, alarms) => {
if (!err) {
console.log(alarms)
}
})

callback();
},
function(callback) {
console.log("sequence done");
}
])
}
catch (err) {
console.log("Error !!!", err);
//process.exit();
}
}

main();

}
Loading

0 comments on commit dfa4da5

Please sign in to comment.