/
access_level.ts
102 lines (90 loc) · 3.83 KB
/
access_level.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// tslint:disable:no-bitwise
// tslint:disable:max-line-length
/**
* @module node-opcua-data-model
*/
import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
import { registerBasicType } from "node-opcua-factory";
import * as utils from "node-opcua-utils";
export enum AccessLevelFlag {
CurrentRead = 0x01, // bit 0 : Indicate if the current value is readable (0 means not readable, 1 means readable).
CurrentWrite = 0x02, // bit 1 : Indicate if the current value is writable (0 means not writable, 1 means writable).
HistoryRead = 0x04, // bit 2 : Indicates if the history of the value is readable (0 means not readable, 1 means readable).
HistoryWrite = 0x08, // bit 3 : Indicates if the history of the value is writable (0 means not writable, 1 means writable).
SemanticChange = 0x10, // bit 4 : Indicates if the Variable used as Property generates SemanticChangeEvents
StatusWrite = 0x20, // bit 5 : Indicates if the current StatusCode of the value is writable (0 means not writable, 1 means writable).
TimestampWrite = 0x40, // bit 6 : Indicates if the current SourceTimestamp of the value is writable (0 means not writable, 1 means writable).
NONE = 0x800, // Deprecated
None = 0x800,
}
export function convertAccessLevelFlagToByte(accessLevel: AccessLevelFlag): number {
return accessLevel & 0x3F;
}
// @example
// makeAccessLevelFlag("CurrentRead | CurrentWrite").should.eql(0x03);
export function makeAccessLevelFlag(str: string | number | null): AccessLevelFlag {
if (typeof str === "number") {
const value = str as number;
if (value === 0) { return AccessLevelFlag.None; }
return value as AccessLevelFlag;
}
let accessFlag: AccessLevelFlag | null;
if (str === "" || str === null) {
accessFlag = AccessLevelFlag.None;
} else {
const flags = str.split(" | ");
accessFlag = 0;
for (const flag of flags) {
accessFlag |= (AccessLevelFlag as any)[flag];
}
}
if (utils.isNullOrUndefined(accessFlag)) {
throw new Error("Invalid access flag specified '" + str + "' should be one of " + AccessLevelFlag.toString());
}
return accessFlag as AccessLevelFlag;
}
export const coerceAccessLevelFlag = makeAccessLevelFlag;
export function randomAccessLevel(): AccessLevelFlag {
return AccessLevelFlag.CurrentRead;
}
// tslint:disable:no-bitwise
export function accessLevelFlagToString(accessLevelFlag: AccessLevelFlag): string {
const retVal = [];
if (accessLevelFlag & AccessLevelFlag.CurrentRead) {
retVal.push("CurrentRead");
}
if (accessLevelFlag & AccessLevelFlag.CurrentWrite) {
retVal.push("CurrentWrite");
}
if (accessLevelFlag & AccessLevelFlag.StatusWrite) {
retVal.push("StatusWrite");
}
if (accessLevelFlag & AccessLevelFlag.TimestampWrite) {
retVal.push("TimestampWrite");
}
if (accessLevelFlag & AccessLevelFlag.HistoryRead) {
retVal.push("HistoryRead");
}
if (accessLevelFlag & AccessLevelFlag.HistoryWrite) {
retVal.push("HistoryWrite");
}
if (accessLevelFlag & AccessLevelFlag.SemanticChange) {
retVal.push("SemanticChange");
}
if (retVal.length === 0) {
retVal.push("None");
}
return retVal.join(" | ");
}
registerBasicType({
name: "AccessLevelFlag",
subType: "Byte",
defaultValue: () => AccessLevelFlag.CurrentRead | AccessLevelFlag.CurrentWrite,
coerce: (value: any): AccessLevelFlag => makeAccessLevelFlag(value),
decode: (stream: BinaryStream): AccessLevelFlag => {
const code = stream.readUInt8();
return (code ? AccessLevelFlag[code] : AccessLevelFlag.NONE) as AccessLevelFlag;
},
encode: (value: AccessLevelFlag, stream: OutputBinaryStream) => stream.writeUInt8(value & 0x8F),
random: randomAccessLevel
});