Skip to content

Commit 06de3e0

Browse files
authored
Merge pull request #1524 from luxonis/feature/expose_library_version
Exposed library version service in RemoteConnection, added testing of remote services in custom_services.py
2 parents 3f4e146 + 1106f21 commit 06de3e0

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

examples/python/Visualizer/custom_services.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,46 @@
33
import signal
44
import sys
55
import time
6+
import asyncio
7+
import json
8+
import websockets
9+
import struct
10+
11+
uri = "ws://localhost:8765"
12+
async def call_service(ws, service_name, service_id):
13+
# Call a service and return the response payload
14+
msg = bytearray([0x02]) # opcode
15+
msg.extend(struct.pack('<I', service_id))
16+
msg.extend(struct.pack('<I', 1)) # call_id
17+
msg.extend(struct.pack('<I', 4)) # encoding length
18+
msg.extend(b"json")
19+
msg.extend(b"{}")
20+
await ws.send(bytes(msg))
21+
22+
response = await asyncio.wait_for(ws.recv(), timeout=5.0)
23+
if isinstance(response, bytes) and response[0] == 0x03:
24+
encoding_len = struct.unpack('<I', response[9:13])[0]
25+
payload = response[13 + encoding_len:]
26+
return payload.decode('utf-8')
27+
return None
28+
29+
async def test_available_services():
30+
async with websockets.connect(uri, subprotocols=["foxglove.websocket.v1"]) as ws:
31+
while True:
32+
msg = json.loads(await asyncio.wait_for(ws.recv(), timeout=5.0))
33+
if msg.get("op") == "advertiseServices":
34+
services = {s['name']: s['id'] for s in msg.get("services", [])}
35+
print(f"Available services: {list(services.keys())}")
36+
break
37+
38+
# Test each service
39+
for service_name in services:
40+
try:
41+
result = await call_service(ws, service_name, services[service_name])
42+
print(f"{service_name}: {result}")
43+
except Exception as e:
44+
print(f"{service_name}: ERROR - {e}")
45+
646

747
isRunning = True
848
def stop():
@@ -25,3 +65,4 @@ def testService(input):
2565
remoteConnection.registerService("myService", testService)
2666
while isRunning:
2767
time.sleep(1)
68+
asyncio.run(test_available_services())

src/remote_connection/RemoteConnectionImpl.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ RemoteConnectionImpl::RemoteConnectionImpl(const std::string& address, uint16_t
5050
// Expose services
5151
exposeKeyPressedService();
5252
exposeTopicGroupsService();
53+
exposeLibraryVersionService();
5354
}
5455

5556
RemoteConnectionImpl::~RemoteConnectionImpl() {
@@ -433,6 +434,35 @@ void RemoteConnectionImpl::exposePipelineService(const Pipeline& pipeline) {
433434
};
434435
}
435436

437+
void RemoteConnectionImpl::exposeLibraryVersionService() {
438+
std::string serviceName = "libraryVersion";
439+
foxglove::ServiceWithoutId service;
440+
service.name = serviceName;
441+
service.type = "json";
442+
443+
foxglove::ServiceRequestDefinition requestDef;
444+
requestDef.schemaName = serviceName + "Request";
445+
requestDef.encoding = "json";
446+
service.request = requestDef;
447+
448+
foxglove::ServiceRequestDefinition responseDef;
449+
responseDef.schemaName = serviceName + "Response";
450+
responseDef.encoding = "json";
451+
service.response = responseDef;
452+
453+
auto ids = server->addServices({service});
454+
assert(ids.size() == 1);
455+
auto id = ids[0];
456+
457+
serviceMap[id] = [this](foxglove::ServiceResponse request) {
458+
nlohmann::json jsonRequest = nlohmann::json::parse(request.data);
459+
std::string responseMsg = std::string(build::VERSION);
460+
foxglove::ServiceResponse ret;
461+
ret.data = std::vector<uint8_t>(responseMsg.begin(), responseMsg.end());
462+
return ret;
463+
};
464+
}
465+
436466
void RemoteConnectionImpl::registerService(const std::string& serviceName, std::function<nlohmann::json(const nlohmann::json&)> callback) {
437467
foxglove::ServiceWithoutId service;
438468
service.name = serviceName;

src/remote_connection/RemoteConnectionImpl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class RemoteConnectionImpl {
5757
void exposeTopicGroupsService();
5858
void exposeKeyPressedService();
5959
void exposePipelineService(const Pipeline& pipeline);
60+
void exposeLibraryVersionService();
6061
void keyPressedCallback(int key);
6162

6263
std::mutex keyMutex;

0 commit comments

Comments
 (0)