Skip to content

Commit 1f0b102

Browse files
committed
Realtime data server update to better log user actions and create events for annotations and manual values entered.
1 parent 4a4e9fd commit 1f0b102

File tree

1 file changed

+157
-33
lines changed

1 file changed

+157
-33
lines changed

src/server_realtime_auth/index.js

Lines changed: 157 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ let pool = null
127127

128128
// if env variables defined use them, if not set local defaults
129129
let pgopt = {}
130-
if ("PGHOST" in process.env || "PGHOSTADDR" in process.env)
131-
pgopt = null
130+
if ('PGHOST' in process.env || 'PGHOSTADDR' in process.env) pgopt = null
132131
else
133132
pgopt = {
134133
host: '127.0.0.1',
@@ -546,7 +545,7 @@ let pool = null
546545

547546
if (AUTHENTICATION) {
548547
// go check user right for command in mongodb (not just in the token for better security)
549-
if (!await canSendCommands(req)) {
548+
if (!(await canSendCommands(req))) {
550549
// ACTION DENIED
551550
console.log(
552551
`User has no right to issue commands! [${username}]`
@@ -616,16 +615,20 @@ let pool = null
616615

617616
if (AUTHENTICATION) {
618617
// check if user has group1 list it can command
619-
if (!await canSendCommandTo(req, data.group1)) {
618+
if (!(await canSendCommandTo(req, data.group1))) {
620619
// ACTION DENIED
621620
console.log(
622621
`User has no right to issue commands to the group1 destination! [${username}] [${data.group1}]`
623622
)
624623
OpcResp.Body.ResponseHeader.ServiceResult =
625624
opc.StatusCode.BadUserAccessDenied
626625
OpcResp.Body.ResponseHeader.StringTable = [
627-
opc.getStatusCodeName(opc.StatusCode.BadUserAccessDenied),
628-
opc.getStatusCodeText(opc.StatusCode.BadUserAccessDenied),
626+
opc.getStatusCodeName(
627+
opc.StatusCode.BadUserAccessDenied
628+
),
629+
opc.getStatusCodeText(
630+
opc.StatusCode.BadUserAccessDenied
631+
),
629632
'User has no right to issue commands to the group1 destination!'
630633
]
631634
res.send(OpcResp)
@@ -636,7 +639,7 @@ let pool = null
636639
)
637640
}
638641
}
639-
642+
640643
let result = await db.collection(COLL_COMMANDS).insertOne({
641644
protocolSourceConnectionNumber: new mongo.Double(
642645
data.protocolSourceConnectionNumber
@@ -710,24 +713,93 @@ let pool = null
710713
}
711714

712715
if (findPoint !== null) {
713-
let result = await db
716+
let prevData = await db
714717
.collection(COLL_REALTIME)
715-
.updateOne(findPoint, {
716-
$set: {
717-
...((!AUTHENTICATION || userRights?.disableAlarms)? {alarmDisabled: node.Value._Properties.alarmDisabled }:{}),
718-
...((!AUTHENTICATION || userRights?.enterAnnotations)? {annotation: node.Value._Properties.annotation}:{}),
719-
...((!AUTHENTICATION || userRights?.enterLimits)? {loLimit: new mongo.Double( node.Value._Properties.loLimit )}:{}),
720-
// loloLimit: node.Value._Properties.loLimit,
721-
// lololoLimit: node.Value._Properties.loLimit,
722-
...((!AUTHENTICATION || userRights?.enterLimits)? {hiLimit: new mongo.Double( node.Value._Properties.hiLimit )}:{}),
723-
// hihiLimit: node.Value._Properties.hiLimit,
724-
// hihihiLimit: node.Value._Properties.hiLimit,
725-
...((!AUTHENTICATION || userRights?.enterLimits)? {hysteresis: new mongo.Double(node.Value._Properties.hysteresis)}:{}),
726-
...((!AUTHENTICATION || userRights?.enterNotes)? {notes: node.Value._Properties.notes}:{}),
727-
...(('substituted' in node.Value._Properties && 'newValue' in node.Value._Properties && (!AUTHENTICATION || userRights?.substituteValues))
728-
? { value: node.Value._Properties.newValue } : {})
718+
.findOne(findPoint)
719+
720+
let alarmDisableNew = {}
721+
if (!AUTHENTICATION || userRights?.disableAlarms)
722+
if (
723+
prevData?.alarmDisabled !==
724+
node.Value._Properties?.alarmDisabled
725+
)
726+
alarmDisableNew = {
727+
alarmDisabled: node.Value._Properties?.alarmDisabled
729728
}
730-
})
729+
730+
let annotationNew = {}
731+
if (!AUTHENTICATION || userRights?.enterAnnotations)
732+
if (
733+
prevData?.annotation !==
734+
node.Value._Properties?.annotation
735+
)
736+
annotationNew = {
737+
annotation: node.Value._Properties?.annotation
738+
}
739+
740+
let loLimitNew = {}
741+
if (!AUTHENTICATION || userRights?.enterLimits)
742+
if (
743+
prevData?.loLimit !== node.Value._Properties?.loLimit
744+
)
745+
loLimitNew = {
746+
loLimit: new mongo.Double(
747+
node.Value._Properties.loLimit
748+
)
749+
}
750+
751+
let hiLimitNew = {}
752+
if (!AUTHENTICATION || userRights?.enterLimits)
753+
if (
754+
prevData?.hiLimit !== node.Value._Properties?.hiLimit
755+
)
756+
hiLimitNew = {
757+
hiLimit: new mongo.Double(
758+
node.Value._Properties.hiLimit
759+
)
760+
}
761+
762+
// loloLimit: node.Value._Properties.loLimit,
763+
// lololoLimit: node.Value._Properties.loLimit,
764+
// hihiLimit: node.Value._Properties.hiLimit,
765+
// hihihiLimit: node.Value._Properties.hiLimit,
766+
767+
let notesNew = {}
768+
if (!AUTHENTICATION || userRights?.enterNotes)
769+
if (prevData?.notes !== node.Value._Properties?.notes)
770+
notesNew = {
771+
notes: node.Value._Properties.notes
772+
}
773+
774+
let valueNew = {}
775+
if (
776+
'substituted' in node.Value._Properties &&
777+
'newValue' in node.Value._Properties
778+
)
779+
if (!AUTHENTICATION || userRights?.substituteValues)
780+
if (
781+
prevData?.value !== node.Value._Properties.newValue
782+
)
783+
valueNew = {
784+
value: new mongo.Double(
785+
node.Value._Properties.newValue
786+
)
787+
}
788+
789+
let set = {
790+
$set: {
791+
...alarmDisableNew,
792+
...annotationNew,
793+
...loLimitNew,
794+
...hiLimitNew,
795+
...notesNew,
796+
...valueNew
797+
}
798+
}
799+
800+
let result = await db
801+
.collection(COLL_REALTIME)
802+
.updateOne(findPoint, set)
731803
if (
732804
typeof result.result.n === 'number' &&
733805
result.result.n === 1
@@ -739,10 +811,55 @@ let pool = null
739811
username: username,
740812
pointKey: node.NodeId.Id,
741813
action: 'Update Properties',
742-
properties: node.Value._Properties,
814+
properties: set['$set'],
743815
timeTag: new Date()
744816
})
745-
817+
818+
// insert event for changed annotation
819+
if ('annotation' in annotationNew) {
820+
let eventDate = new Date()
821+
db.collection(COLL_SOE).insertOne({
822+
tag: prevData.tag,
823+
pointKey: prevData._id,
824+
group1: prevData?.group1,
825+
description: prevData?.description,
826+
eventText: (annotationNew.annotation.trim()==='')?'🏷️🗑️':'🏷️🔒', // 🏷
827+
invalid: false,
828+
priority: prevData?.priority,
829+
timeTag: eventDate,
830+
timeTagAtSource: eventDate,
831+
timeTagAtSourceOk: true,
832+
ack: 1
833+
})
834+
}
835+
836+
// insert event for changed value
837+
if ('value' in valueNew) {
838+
let eventDate = new Date()
839+
let eventText = ''
840+
if (prevData?.type === 'digital')
841+
eventText = (valueNew.value == 0)
842+
? prevData?.eventTextFalse
843+
: prevData?.eventTextTrue
844+
else
845+
eventText =
846+
valueNew.value.toFixed(2) +
847+
' ' +
848+
prevData?.unit
849+
db.collection(COLL_SOE).insertOne({
850+
tag: prevData.tag,
851+
pointKey: prevData._id,
852+
group1: prevData?.group1,
853+
description: prevData?.description,
854+
eventText: eventText,
855+
invalid: false,
856+
priority: prevData?.priority,
857+
timeTag: eventDate,
858+
timeTagAtSource: eventDate,
859+
timeTagAtSourceOk: true,
860+
ack: 1
861+
})
862+
}
746863
} else {
747864
// some updateOne error
748865
OpcResp.Body.Results.push(
@@ -1009,7 +1126,8 @@ let pool = null
10091126
let Results = []
10101127
if ('NodesToRead' in req.body.Body) {
10111128
req.body.Body.NodesToRead.map(node => {
1012-
let Result = { // will return this if point not found or access denied
1129+
let Result = {
1130+
// will return this if point not found or access denied
10131131
StatusCode: opc.StatusCode.BadNotFound,
10141132
NodeId: node.NodeId,
10151133
Value: null,
@@ -1023,12 +1141,18 @@ let pool = null
10231141
node.NodeId.Id === pointInfo.tag ||
10241142
node.NodeId.Id === pointInfo._id
10251143
) {
1026-
10271144
// check for group1 list in user rights (from token)
1028-
if (AUTHENTICATION && userRights.group1List.length>0){
1029-
if ( ![-1, -2].includes(pointInfo._id) && !userRights.group1List.includes(pointInfo.group1) ){
1145+
if (
1146+
AUTHENTICATION &&
1147+
userRights.group1List.length > 0
1148+
) {
1149+
if (
1150+
![-1, -2].includes(pointInfo._id) &&
1151+
!userRights.group1List.includes(pointInfo.group1)
1152+
) {
10301153
// Access to data denied! (return null value and properties)
1031-
Result.StatusCode = opc.StatusCode.BadUserAccessDenied
1154+
Result.StatusCode =
1155+
opc.StatusCode.BadUserAccessDenied
10321156
break
10331157
}
10341158
}
@@ -1159,8 +1283,8 @@ let pool = null
11591283
: null
11601284

11611285
// check for group1 list in user rights (from token)
1162-
if (AUTHENTICATION && userRights.group1List.length>0){
1163-
if ( !userRights.group1List.includes(node.group1) ){
1286+
if (AUTHENTICATION && userRights.group1List.length > 0) {
1287+
if (!userRights.group1List.includes(node.group1)) {
11641288
// Access to data denied!
11651289
return node
11661290
}
@@ -1456,7 +1580,7 @@ let pool = null
14561580
filterDateLte,
14571581
filterGroup,
14581582
filterPriority,
1459-
{ ack: { $lte: (endDateTime!==null)?2:1 } } // when realtime query (endDate=null) filter out eliminated (ack=2) events
1583+
{ ack: { $lte: endDateTime !== null ? 2 : 1 } } // when realtime query (endDate=null) filter out eliminated (ack=2) events
14601584
]
14611585
},
14621586
{}

0 commit comments

Comments
 (0)