|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* |
| 4 | + * Customizable processor of mongodb changes via change streams. |
| 5 | + * |
| 6 | + * THIS FILE IS INTENDED TO BE CUSTOMIZED BY USERS TO DO SPECIAL PROCESSING |
| 7 | + * |
| 8 | + * {json:scada} - Copyright (c) 2020-2024 - Ricardo L. Olsen |
| 9 | + * This file is part of the JSON-SCADA distribution (https://github.com/riclolsen/json-scada). |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU General Public License as published by |
| 13 | + * the Free Software Foundation, version 3. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, but |
| 16 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | + * General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | + |
| 24 | +const Log = require('./simple-logger') |
| 25 | +const { Double } = require('mongodb') |
| 26 | +const { setInterval } = require('timers') |
| 27 | +const dgram = require('dgram'); |
| 28 | + |
| 29 | +// UDP broadcast options |
| 30 | +const udpPort = 12345; |
| 31 | +const udpHostDst = "192.168.0.255"; |
| 32 | +// Create a UDP socket |
| 33 | +const udpSocket = dgram.createSocket('udp4'); |
| 34 | + |
| 35 | +udpSocket.bind(udpPort, () => { |
| 36 | + udpSocket.setBroadcast(true) |
| 37 | + // udpSocket.setMulticastInterface('::%eth1'); |
| 38 | +}); |
| 39 | + |
| 40 | + |
| 41 | +let maxSz = 0; |
| 42 | +let cnt = 0; |
| 43 | + |
| 44 | +const UserActionsCollectionName = 'userActions' |
| 45 | +const RealtimeDataCollectionName = 'realtimeData' |
| 46 | +const CommandsQueueCollectionName = 'commandsQueue' |
| 47 | +const SoeDataCollectionName = 'soeData' |
| 48 | +const ProcessInstancesCollectionName = 'processInstances' |
| 49 | +const ProtocolDriverInstancesCollectionName = 'protocolDriverInstances' |
| 50 | +const ProtocolConnectionsCollectionName = 'protocolConnections' |
| 51 | + |
| 52 | +let CyclicIntervalHandle = null |
| 53 | + |
| 54 | +// this will be called by the main module when mongo is connected (or reconnected) |
| 55 | +module.exports.CustomProcessor = function ( |
| 56 | + clientMongo, |
| 57 | + jsConfig, |
| 58 | + Redundancy, |
| 59 | + MongoStatus |
| 60 | +) { |
| 61 | + if (clientMongo === null) return |
| 62 | + const db = clientMongo.db(jsConfig.mongoDatabaseName) |
| 63 | + |
| 64 | + // ------------------------------------------------------------------------------------------- |
| 65 | + // EXAMPLE OF CYCLIC PROCESSING AT INTERVALS |
| 66 | + // BEGIN EXAMPLE |
| 67 | + |
| 68 | + let CyclicProcess = async function () { |
| 69 | + // do cyclic processing at each CyclicInterval ms |
| 70 | + |
| 71 | + if (!Redundancy.ProcessStateIsActive() || !MongoStatus.HintMongoIsConnected) |
| 72 | + return // do nothing if process is inactive |
| 73 | + |
| 74 | + try { |
| 75 | + let res = await db |
| 76 | + .collection(RealtimeDataCollectionName) |
| 77 | + .findOne({ _id: -2 }) // id of point tag with number of digital updates |
| 78 | + |
| 79 | + Log.log( |
| 80 | + 'Custom Process - Checking number of digital updates: ' + |
| 81 | + res.valueString |
| 82 | + ) |
| 83 | + } catch (err) { |
| 84 | + Log.log(err) |
| 85 | + } |
| 86 | + |
| 87 | + return |
| 88 | + } |
| 89 | + const CyclicInterval = 5000 // interval time in ms |
| 90 | + clearInterval(CyclicIntervalHandle) // clear older instances if any |
| 91 | + CyclicIntervalHandle = setInterval(CyclicProcess, CyclicInterval) // start a cyclic processing |
| 92 | + |
| 93 | + // EXAMPLE OF CYCLIC PROCESSING AT INTERVALS |
| 94 | + // END EXAMPLE |
| 95 | + // ------------------------------------------------------------------------------------------- |
| 96 | + |
| 97 | + const changeStreamUserActions = db |
| 98 | + .collection(RealtimeDataCollectionName) |
| 99 | + .watch( |
| 100 | + { $match: { operationType: 'update' } }, |
| 101 | + { |
| 102 | + fullDocument: 'updateLookup' |
| 103 | + } |
| 104 | + ) |
| 105 | + |
| 106 | + try { |
| 107 | + changeStreamUserActions.on('error', change => { |
| 108 | + if (clientMongo) clientMongo.close() |
| 109 | + clientMongo = null |
| 110 | + Log.log('Custom Process - Error on changeStreamUserActions!') |
| 111 | + }) |
| 112 | + changeStreamUserActions.on('close', change => { |
| 113 | + Log.log('Custom Process - Closed changeStreamUserActions!') |
| 114 | + }) |
| 115 | + changeStreamUserActions.on('end', change => { |
| 116 | + if (clientMongo) clientMongo.close() |
| 117 | + clientMongo = null |
| 118 | + Log.log('Custom Process - Ended changeStreamUserActions!') |
| 119 | + }) |
| 120 | + |
| 121 | + // start listen to changes |
| 122 | + changeStreamUserActions.on('change', change => { |
| 123 | + // Log.log(change.fullDocument) |
| 124 | + if (!Redundancy.ProcessStateIsActive() || !MongoStatus.HintMongoIsConnected) |
| 125 | + return // do nothing if process is inactive |
| 126 | + |
| 127 | + // will send only update data from drivers |
| 128 | + if (!change.updateDescription.updatedFields?.sourceDataUpdate) |
| 129 | + return; |
| 130 | + if (change.updateDescription.updatedFields?.sourceDataUpdate?.valueBsonAtSource) |
| 131 | + delete change.updateDescription.updatedFields.sourceDataUpdate.valueBsonAtSource; |
| 132 | + if (!Redundancy.ProcessStateIsActive()) return // do nothing if process is inactive |
| 133 | + const fwObj = { |
| 134 | + cnt: cnt++, |
| 135 | + operationType: change.operationType, |
| 136 | + documentKey: change.documentKey, |
| 137 | + updateDescription: change.updateDescription |
| 138 | + } |
| 139 | + const opData = JSON.stringify(fwObj); |
| 140 | + const message = Buffer.from(opData); |
| 141 | + if (message.length > maxSz) maxSz = message.length; |
| 142 | + |
| 143 | + if (message.length > 60000) { |
| 144 | + console.log('Message too large: ', opData); |
| 145 | + } |
| 146 | + else |
| 147 | + udpSocket.send(message, 0, message.length, udpPort, udpHostDst, (err, bytes) => { |
| 148 | + if (err) { |
| 149 | + console.log('UDP error:', err); |
| 150 | + } else { |
| 151 | + // console.log('Data sent via UDP', opData); |
| 152 | + //console.log('Size: ', message.length); |
| 153 | + //console.log('Max: ', maxSz); |
| 154 | + } |
| 155 | + |
| 156 | + }); |
| 157 | + |
| 158 | + }) |
| 159 | + } catch (e) { |
| 160 | + Log.log('Custom Process - Error: ' + e) |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments