Skip to content

Commit 03c5826

Browse files
committed
Make Demo Simulator work with recent Mongo versions.
1 parent 14cd1f2 commit 03c5826

File tree

2 files changed

+112
-14
lines changed

2 files changed

+112
-14
lines changed

src/demo_simul/index.js

+109-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
const Log = require('./simple-logger')
2323
const AppDefs = require('./app-defs')
2424
const LoadConfig = require('./load-config')
25-
const { MongoClient } = require('mongodb')
25+
const { MongoClient, Double } = require('mongodb')
2626

2727
const pipeline = [
2828
{
@@ -80,6 +80,7 @@ Log.log('Connecting to ' + jsConfig.mongoConnectionString)
8080
.updateMany(
8181
{
8282
origin: 'supervised',
83+
invalid: true,
8384
},
8485
[
8586
{
@@ -99,7 +100,49 @@ Log.log('Connecting to ' + jsConfig.mongoConnectionString)
99100
})
100101

101102
// simulate protocol writes of digital values
102-
103+
let tagDocs = await db
104+
.collection(jsConfig.RealtimeDataCollectionName)
105+
.find({
106+
type: 'digital',
107+
origin: 'supervised',
108+
$expr: { $eq: [{ $indexOfBytes: ['$tag', 'XSWI'] }, -1] },
109+
_id: { $mod: [Math.floor(Math.random() * 500) + 500, 0] },
110+
})
111+
.toArray()
112+
for (let i = 0; i < tagDocs.length; i++) {
113+
const document = tagDocs[i]
114+
console.log(`${document._id} ${document.tag} ${document.value}`)
115+
let res = await db
116+
.collection(jsConfig.RealtimeDataCollectionName)
117+
.updateOne(
118+
{
119+
_id: document._id,
120+
},
121+
{
122+
$set: {
123+
sourceDataUpdate: {
124+
valueAtSource: document.value == 0 ? 1 : 0,
125+
valueStringAtSource: '',
126+
asduAtSource: 'M_SP_NA_1',
127+
causeOfTransmissionAtSource: '3',
128+
timeTag: new Date(),
129+
timeTagAtSource: new Date(),
130+
timeTagAtSourceOk: true,
131+
invalid: false,
132+
invalidAtSource: false,
133+
substitutedAtSource: false,
134+
overflowAtSource: false,
135+
blockedAtSource: false,
136+
notTopicalAtSource: false,
137+
test: true,
138+
originator: AppDefs.NAME,
139+
CntUpd: cntUpd,
140+
},
141+
},
142+
}
143+
)
144+
}
145+
/*
103146
let res = await db
104147
.collection(jsConfig.RealtimeDataCollectionName)
105148
.updateMany(
@@ -189,14 +232,65 @@ Log.log('Connecting to ' + jsConfig.mongoConnectionString)
189232
clientMongo = null
190233
}
191234
})
192-
235+
*/
193236
cntUpd++
194237
}
195-
}, 5777)
238+
}, 11777)
196239

197240
setInterval(async function () {
198241
if (clientMongo !== null && HintMongoIsConnected) {
199242
const db = clientMongo.db(jsConfig.mongoDatabaseName)
243+
244+
// simulate protocol writes of analog values
245+
let tagDocs = await db
246+
.collection(jsConfig.RealtimeDataCollectionName)
247+
.find({
248+
type: 'analog',
249+
origin: 'supervised',
250+
_id: {
251+
$mod: [
252+
1 +
253+
Math.floor(Math.random() * 50) +
254+
Math.floor(Math.random() * 10),
255+
0,
256+
],
257+
},
258+
})
259+
.toArray()
260+
for (let i = 0; i < tagDocs.length; i++) {
261+
const document = tagDocs[i]
262+
console.log(`${document._id} ${document.tag} ${document.value}`)
263+
let res = await db
264+
.collection(jsConfig.RealtimeDataCollectionName)
265+
.updateOne(
266+
{
267+
_id: document._id,
268+
},
269+
{
270+
$set: {
271+
sourceDataUpdate: {
272+
valueAtSource: document.valueDefault * (1 + 0.1 * Math.random() - 0.05),
273+
valueStringAtSource: '',
274+
asduAtSource: 'M_ME_NC_1',
275+
causeOfTransmissionAtSource: '3',
276+
timeTag: new Date(),
277+
timeTagAtSource: null,
278+
timeTagAtSourceOk: false,
279+
invalid: false,
280+
invalidAtSource: false,
281+
substitutedAtSource: false,
282+
overflowAtSource: false,
283+
blockedAtSource: false,
284+
notTopicalAtSource: false,
285+
test: true,
286+
originator: AppDefs.NAME,
287+
CntUpd: cntUpd,
288+
},
289+
},
290+
}
291+
)
292+
}
293+
/*
200294
let res = await db
201295
.collection(jsConfig.RealtimeDataCollectionName)
202296
.updateMany(
@@ -291,9 +385,11 @@ Log.log('Connecting to ' + jsConfig.mongoConnectionString)
291385
clientMongo = null
292386
}
293387
})
388+
389+
*/
294390
cntUpd++
295391
}
296-
}, 2000)
392+
}, 2333)
297393

298394
while (true) {
299395
if (clientMongo === null)
@@ -336,29 +432,31 @@ Log.log('Connecting to ' + jsConfig.mongoConnectionString)
336432

337433
let res = await db
338434
.collection(jsConfig.RealtimeDataCollectionName)
339-
.updateOne({ _id: data.supervisedOfCommand }, [
435+
.updateOne(
436+
{ _id: data.supervisedOfCommand },
340437
{
341438
$set: {
342439
sourceDataUpdate: {
343-
valueAtSource: val,
440+
valueAtSource: new Double(val),
344441
valueStringAtSource: '',
345-
asduAtSource: 'M_SP_NA_1',
442+
asduAtSource: change.fullDocument.tag.indexOf('YTAP') === -1 ? 'M_SP_NA_1' : 'M_ST_TB_1',
346443
causeOfTransmissionAtSource: '3',
347444
invalid: false,
348445
timeTag: new Date(),
349446
timeTagAtSource: new Date(),
350447
timeTagAtSourceOk: true,
448+
invalidAtSource: false,
351449
substitutedAtSource: false,
352450
overflowAtSource: false,
353451
blockedAtSource: false,
354452
notTopicalAtSource: false,
355453
test: true,
356-
originator: AppDefs.NAME,
454+
originator: AppDefs.NAME + "CMD",
357455
CntUpd: cntUpd,
358456
},
359457
},
360-
},
361-
])
458+
}
459+
)
362460
Log.log(res?.matchedCount)
363461
Log.log(res?.modifiedCount)
364462
if (res?.matchedCount > 0) {

src/demo_simul/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)