Skip to content

Commit 1c19650

Browse files
committed
Fix connection status issue so widgets display as connected
Add test to check readonly property is correctly set.
1 parent 966229d commit 1c19650

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/connection/pvws.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,20 @@ describe("PvwsPlugin", (): void => {
7878
it("unsubscribes_with_no_errors", (): void => {
7979
expect(() => cp.unsubscribe("hello")).not.toThrow(TypeError);
8080
});
81+
82+
it("handles update readonly value", (): void => {
83+
cp.subscribe("hello", { string: true });
84+
ws.send(JSON.stringify({ type: "update", pv: "hello", value: 42 }));
85+
expect(mockConnUpdate).toHaveBeenLastCalledWith("hello", {
86+
isConnected: true,
87+
isReadonly: true
88+
});
89+
90+
ws.send(JSON.stringify({ type: "update", pv: "hello", readonly: false }));
91+
expect(mockConnUpdate).toBeCalledTimes(2);
92+
expect(mockConnUpdate).toHaveBeenLastCalledWith("hello", {
93+
isConnected: true,
94+
isReadonly: false
95+
});
96+
});
8197
});

src/connection/pvws.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export class PvwsPlugin implements Connection {
141141
private connected: boolean;
142142
private disconnected: string[] = [];
143143
private subscriptions: { [pvName: string]: boolean };
144+
private initMsgRcvd: { [pvName: string]: boolean };
144145
private url = "";
145146
private socket!: WebSocket;
146147
private reconnect_ms = 5000;
@@ -155,6 +156,7 @@ export class PvwsPlugin implements Connection {
155156
this.onValueUpdate = nullValueCallback;
156157
this.connected = false;
157158
this.subscriptions = {};
159+
this.initMsgRcvd = {};
158160
}
159161

160162
/** Open the web socket, i.e. start PV communication */
@@ -184,10 +186,22 @@ export class PvwsPlugin implements Connection {
184186
private handleMessage(message: string) {
185187
const jm = JSON.parse(message);
186188
if (jm.type === "update") {
187-
if (jm.readonly !== undefined) {
189+
let updateConnection = false;
190+
// PVWS only sends the readonly attribute if false
191+
// so set true by default and update later.
192+
let readonly = true;
193+
if (!this.initMsgRcvd[jm.pv]) {
194+
updateConnection = true;
195+
this.initMsgRcvd[jm.pv] = true;
196+
} else if (jm.readonly !== undefined) {
197+
updateConnection = true;
198+
// Update readonly from PVWS message
199+
readonly = jm.readonly;
200+
}
201+
if (updateConnection) {
188202
this.onConnectionUpdate(jm.pv, {
189203
isConnected: true,
190-
isReadonly: jm.readonly
204+
isReadonly: readonly
191205
});
192206
}
193207

@@ -254,6 +268,7 @@ export class PvwsPlugin implements Connection {
254268
if (this.subscriptions[pvName] === undefined) {
255269
this._subscribe(pvName);
256270
this.subscriptions[pvName] = true;
271+
this.initMsgRcvd[pvName] = false;
257272
}
258273
return pvName;
259274
}

0 commit comments

Comments
 (0)