add Unix Socket API

This commit is contained in:
Maik Jurischka
2025-12-12 10:46:49 +01:00
parent 2edffd5f76
commit fe0af4dc64
7 changed files with 1033 additions and 19 deletions

View File

@@ -1,9 +1,22 @@
#include <iostream>
#include <memory>
#include <csignal>
#include <atomic>
#include <vizionsdk/VizionSDK.h>
#include "SocketServer.h"
#include "CameraController.h"
std::atomic<bool> g_running(true);
void signalHandler(int signal) {
std::cout << "\nReceived signal " << signal << ", shutting down..." << std::endl;
g_running = false;
}
// TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
int main() {
// Setup signal handlers for clean shutdown
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
// List available cameras
std::vector<std::string> devList;
@@ -26,7 +39,7 @@ int main() {
return -1;
}
// Get and set format
// Get and set default format
std::vector<VxFormat> fmtList;
if (VxGetFormatList(cam, fmtList) != 0) {
std::cout << "Failed to get format list" << std::endl;
@@ -40,31 +53,38 @@ int main() {
return -1;
}
// Start streaming
if (VxStartStreaming(cam) != 0) {
std::cout << "Failed to start streaming" << std::endl;
std::cout << "Initial format: " << fmtList[0].width << "x" << fmtList[0].height
<< " @ " << fmtList[0].framerate << " fps" << std::endl;
// Create camera controller
auto controller = std::make_shared<CameraController>(cam);
// Start Unix domain socket server
const std::string socketPath = "/tmp/vizion_control.sock";
SocketServer server(socketPath);
if (!server.start([controller](const std::string& cmd) {
return controller->processCommand(cmd);
})) {
std::cout << "Failed to start socket server" << std::endl;
VxClose(cam);
return -1;
}
// Capture 5 frames
std::shared_ptr<uint8_t[]> buffer(new uint8_t[fmtList[0].width * fmtList[0].height * 3]);
int dataSize;
std::cout << "\nVizion Streamer is running." << std::endl;
std::cout << "Control socket: " << socketPath << std::endl;
std::cout << "Press Ctrl+C to exit.\n" << std::endl;
for (int i = 0; i < 5; i++) {
VX_CAPTURE_RESULT result = VxGetImage(cam, buffer.get(), &dataSize, 1000);
if (result == VX_CAPTURE_RESULT::VX_SUCCESS) {
std::cout << "Successfully captured frame " << i + 1 << " of size: " << dataSize << " bytes" << std::endl;
} else {
std::cout << "Failed to capture frame " << i + 1 << std::endl;
break;
}
// Main loop - keep running until signaled to stop
while (g_running) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// Cleanup
VxStopStreaming(cam);
std::cout << "Shutting down..." << std::endl;
server.stop();
VxClose(cam);
std::cout << "Shutdown complete." << std::endl;
return 0;
// TIP See CLion help at <a href="https://www.jetbrains.com/help/clion/">jetbrains.com/help/clion/</a>. Also, you can try interactive lessons for CLion by selecting 'Help | Learn IDE Features' from the main menu.
}