Skip to content

Commit 6b65ebc

Browse files
anroszkiewiczglatosinski
authored andcommitted
[#88170] frontend: src: views: Update device connection status
Signed-off-by: Anna Roszkiewicz <[email protected]>
1 parent c3d9460 commit 6b65ebc

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

frontend/src/components/devices/Device.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ Component wraps functionality for displaying and working with a single rdfm devi
1515
:title="'Device ' + device.mac_address"
1616
:subtitle="device.metadata['rdfm.software.version'] as string"
1717
:device="device.mac_address"
18-
:connected="device.connected"
1918
smallButtonName="Open action log"
2019
:buttonCallback="fetchActionLog"
20+
:connected="
21+
deviceConnections.has(device.mac_address)
22+
? deviceConnections.get(device.mac_address)
23+
: device.connected
24+
"
2125
/>
2226
<TitleBar
2327
v-if="!device"
@@ -780,8 +784,7 @@ import {
780784
downloadDeviceFile,
781785
execAction,
782786
type Action,
783-
deviceUpdates,
784-
deviceVersions,
787+
deviceConnections,
785788
} from './devices';
786789
import { useRoute, useRouter } from 'vue-router';
787790
import { ActiveTab } from '@/views/HomeView.vue';
@@ -1119,8 +1122,7 @@ export default {
11191122
isFullscreen,
11201123
disconnect,
11211124
allowedTo,
1122-
deviceUpdates,
1123-
deviceVersions,
1125+
deviceConnections,
11241126
};
11251127
},
11261128
};

frontend/src/components/devices/devices.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const filteredDevicesResources = (tag: string) =>
4444

4545
export const deviceUpdates = reactive(new Map<string, number>());
4646
export const deviceVersions = reactive(new Map<string, string>());
47+
export const deviceConnections = reactive(new Map<string, boolean>());
4748

4849
export type Action = {
4950
action_id: string;

frontend/src/views/HomeView.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,12 @@ import GroupsList from '../components/groups/GroupsList.vue';
211211
import Logo from '../images/Logo.vue';
212212
import LogoutIcon from '@/images/LogoutIcon.vue';
213213
import Device from '@/components/devices/Device.vue';
214-
import { deviceUpdates, deviceVersions } from '../components/devices/devices';
214+
import {
215+
deviceUpdates,
216+
deviceVersions,
217+
deviceConnections,
218+
registeredDevicesResources,
219+
} from '../components/devices/devices';
215220
216221
export enum ActiveTab {
217222
Device = 'device',
@@ -361,6 +366,16 @@ export default {
361366
}
362367
});
363368
369+
sse.addEventListener('connect', async (event: MessageEvent) => {
370+
const message = JSON.parse(event.data);
371+
deviceConnections.set(message.device, true);
372+
});
373+
374+
sse.addEventListener('disconnect', async (event: MessageEvent) => {
375+
const message = JSON.parse(event.data);
376+
deviceConnections.set(message.device, false);
377+
});
378+
364379
sse.addEventListener('error', (error: Event) => {
365380
console.error(
366381
'Connection to event stream failed. Will not be able to show device update progress.',

server/src/api/v1/ws/device.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from rdfm.ws import WebSocketException
23
from flask import Blueprint
34
import simple_websocket
@@ -12,6 +13,7 @@
1213
from rdfm.permissions import (
1314
SHELL_PERMISSION,
1415
)
16+
import server
1517

1618

1719
device_ws_blueprint: Blueprint = Blueprint("rdfm-server-device-ws", __name__)
@@ -31,6 +33,13 @@ def device_management_ws(
3133
try:
3234
device_mgmt.loop.start_device_event_loop(ws, device_token)
3335
except WebSocketException as e:
36+
message = {"device": device_token.device_id}
37+
38+
try:
39+
server.instance.sse.publish(json.dumps(message), type='disconnect')
40+
except KeyError:
41+
print("Redis is not configured. Unable to send device updates.")
42+
3443
print("Terminating device WS connection:", e.message, flush=True)
3544
raise
3645

server/src/device_mgmt/models/remote_device.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ def __handle_device_message(self, request: Request):
164164
"progress": request.progress,
165165
"version": self.update_version,
166166
}
167-
server.instance.sse.publish(json.dumps(message), type='update')
167+
try:
168+
server.instance.sse.publish(json.dumps(message), type='update')
169+
except KeyError:
170+
print("Redis is not configured. Unable to send device updates.")
168171
if request.progress == 100:
169172
self.update_version = None
170173
server.instance._device_updates_db.delete(self.token.device_id)
@@ -205,6 +208,11 @@ def event_loop(self):
205208
}
206209
)
207210
)
211+
message = {"device": self.token.device_id}
212+
try:
213+
server.instance.sse.publish(json.dumps(message), type='connect')
214+
except KeyError:
215+
print("Redis is not configured. Unable to send device updates.")
208216

209217
while True:
210218
self.__handle_device_message(self.receive_message())

0 commit comments

Comments
 (0)