diff --git a/smocore/deviceManager/deviceManager.cpp b/smocore/deviceManager/deviceManager.cpp index 0f8996e..784ca13 100644 --- a/smocore/deviceManager/deviceManager.cpp +++ b/smocore/deviceManager/deviceManager.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace smo { namespace device { @@ -17,6 +18,26 @@ std::vector> DeviceManager::extrospectorDeviceSpecs; std::vector> DeviceManager::deviceAttachmentSpecs; +std::vector> + DeviceManager::devices; + +// Async continuation structure +struct DeviceAttachmentContinuation { + std::shared_ptr spec; + std::function device, + std::shared_ptr deviceSpec) + > callback; + + DeviceAttachmentContinuation( + std::shared_ptr s, + std::function device, + std::shared_ptr deviceSpec) + > cb) + : spec(s), callback(cb) + {} +}; const std::string DeviceManager::stringifyDeviceSpecs(void) { @@ -33,5 +54,61 @@ const std::string DeviceManager::stringifyDeviceSpecs(void) return oss.str(); } +void DeviceManager::newDeviceAttachmentSpecInd( + std::shared_ptr spec, + std::function device, + std::shared_ptr deviceSpec) + > callback) +{ + // Create async continuation + auto continuation = std::make_shared( + spec, callback); + + // Check if a DeviceAttachmentSpec already matches + for (const auto& existingSpec : deviceAttachmentSpecs) + { + if (!(*existingSpec == *spec)) { continue; } + // Already exists, callback with error + callback(false, nullptr, nullptr); + return; + } + + // Try to attach the sense device + try { + sense_api::SenseApiManager::getInstance().attachSenseDevice(spec); + + // Look for existing Device with same identifier + std::shared_ptr device = nullptr; + for (const auto& existingDevice : devices) + { + if (existingDevice->deviceIdentifier != spec->deviceIdentifier) + { continue; } + + device = existingDevice; + break; + } + + // If device doesn't exist, create a new one and add it + if (!device) + { + device = std::make_shared(spec->deviceIdentifier); + devices.push_back(device); + } + + // Add DeviceAttachmentSpec to device's list + device->deviceAttachmentSpecs.push_back(spec); + + // Add DeviceAttachmentSpec to DeviceManager's list + deviceAttachmentSpecs.push_back(spec); + + // Callback with success + callback(true, device, spec); + } catch (const std::exception& e) { + // Attach failed, callback with error + callback(false, nullptr, nullptr); + } +} + } // namespace device } // namespace smo diff --git a/smocore/include/deviceManager/device.h b/smocore/include/deviceManager/device.h new file mode 100644 index 0000000..8bda166 --- /dev/null +++ b/smocore/include/deviceManager/device.h @@ -0,0 +1,36 @@ +#ifndef DEVICE_H +#define DEVICE_H + +#include +#include +#include +#include +#include + +namespace smo { +namespace device { + +class Device +{ +public: + std::string deviceIdentifier; + std::vector> deviceAttachmentSpecs; + + Device(const std::string& identifier) : deviceIdentifier(identifier) {} + + std::string stringify() const + { + std::ostringstream os; + os << "Device Identifier: " << deviceIdentifier + << ", Attachment Specs: " << deviceAttachmentSpecs.size() << std::endl; + for (const auto& spec : deviceAttachmentSpecs) { + os << " " << spec->stringify(); + } + return os.str(); + } +}; + +} // namespace device +} // namespace smo + +#endif // DEVICE_H diff --git a/smocore/include/deviceManager/deviceManager.h b/smocore/include/deviceManager/deviceManager.h index 56e4599..a49853e 100644 --- a/smocore/include/deviceManager/deviceManager.h +++ b/smocore/include/deviceManager/deviceManager.h @@ -8,6 +8,8 @@ #include #include #include +#include +#include namespace smo { namespace device { @@ -27,6 +29,11 @@ public: static const std::string stringifyDeviceSpecs(void); + // New async function for device attachment + void newDeviceAttachmentSpecInd( + std::shared_ptr spec, + std::function device, std::shared_ptr deviceSpec)> callback); + private: DeviceManager() = default; ~DeviceManager() = default; @@ -41,6 +48,7 @@ public: extrospectorDeviceSpecs; static std::vector> deviceAttachmentSpecs; + static std::vector> devices; }; } // namespace device