From d311c034afb5b0e03a774608fad006a403290cda Mon Sep 17 00:00:00 2001 From: "m4gn3to@gmail.com" Date: Tue, 10 Jun 2025 08:12:37 +0200 Subject: [PATCH] empecemos --- .vscode/c_cpp_properties.json | 4 +- .vscode/settings.json | 2 +- class/SMARTComm70.cpp | 2 +- connect.cpp | 184 ++++++++++++++++++++++++++++++++-- 4 files changed, 178 insertions(+), 14 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 0947479..6fcb51a 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,7 +4,9 @@ "name": "macos-clang-x64", "includePath": [ "${workspaceFolder}/**", - "${workspaceFolder}" + "${workspaceFolder}", + "${workspaceFolder}/class", + "${workspaceFolder}/class" ], "compilerPath": "/usr/bin/clang", "cStandard": "${default}", diff --git a/.vscode/settings.json b/.vscode/settings.json index 4365e03..b9c6ac8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,7 +4,7 @@ "C_Cpp_Runner.debuggerPath": "lldb", "C_Cpp_Runner.cStandard": "", "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.msvcBatchPath": "", "C_Cpp_Runner.useMsvc": false, "C_Cpp_Runner.warnings": [ "-Wall", diff --git a/class/SMARTComm70.cpp b/class/SMARTComm70.cpp index b4f03e8..c6d52c1 100644 --- a/class/SMARTComm70.cpp +++ b/class/SMARTComm70.cpp @@ -1,5 +1,5 @@ #include "SMARTComm70.h" -#include "SmartErrcodes.h" +#include "SmartErrcodes.h" diff --git a/connect.cpp b/connect.cpp index ada0630..e335c61 100644 --- a/connect.cpp +++ b/connect.cpp @@ -1,14 +1,176 @@ -// Ejemplo de conexión básica -HSMART70 printerHandle; -SMART70_PRINTER_LIST printerList; +#include +#include +#include +#include +#include +#include "SmartComm2.h" // Incluir el SDK -// Obtener lista de impresoras disponibles -Smart70_GetDeviceList(&printerList); +class Smart70Printer { +public: + // Singleton para manejar múltiples instancias de impresoras + static Smart70Printer& getInstance() { + static Smart70Printer instance; + return instance; + } -// Conectar con la primera impresora encontrada (modo DCL) -Smart70DCL_OpenDevice(&printerHandle, printerList.item[0].desc, - SMART70_OPENDEVICE_BYDESC, DMORIENT_LANDSCAPE); + // Estructura para información de la impresora + struct PrinterInfo { + std::string id; + std::string description; + std::string ipAddress; + int port; + bool isConnected; + }; -// Verificar módulos conectados -SMART70_DEVCONNLIST moduleList; -Smart70_GetConnectedModules(printerHandle, &moduleList); \ No newline at end of file + // Conectar a una impresora por dirección IP + bool connect(const std::string& ipAddress, int port = 9100) { + std::lock_guard lock(m_mutex); + + // Verificar si ya estamos conectados a esta impresora + auto it = m_connectedPrinters.find(ipAddress); + if (it != m_connectedPrinters.end() && it->second.isConnected) { + return true; + } + + // Obtener lista de dispositivos + SMART70_PRINTER_LIST printerList; + if (Smart70_GetDeviceList(&printerList) != SM_SUCCESS) { + return false; + } + + // Buscar la impresora por dirección IP + for (int i = 0; i < printerList.n; i++) { + SMART70_PRINTER_INFO info; + if (Smart70_GetDeviceInfo(&info, printerList.item[i].desc, + SMART70_OPENDEVICE_BYDESC) == SM_SUCCESS) { + if (info.dev_type == 2 && std::string(info.net.ip) == ipAddress) { + // Conectar en modo DCL + HSMART70 handle; + if (Smart70DCL_OpenDevice(&handle, printerList.item[i].desc, + SMART70_OPENDEVICE_BYDESC, + DMORIENT_LANDSCAPE) == SM_SUCCESS) { + + PrinterInfo printerInfo; + printerInfo.id = printerList.item[i].id; + printerInfo.description = printerList.item[i].desc; + printerInfo.ipAddress = ipAddress; + printerInfo.port = port; + printerInfo.isConnected = true; + + m_handles[ipAddress] = handle; + m_connectedPrinters[ipAddress] = printerInfo; + + return true; + } + } + } + } + return false; + } + + // Desconectar una impresora + bool disconnect(const std::string& ipAddress) { + std::lock_guard lock(m_mutex); + + auto it = m_handles.find(ipAddress); + if (it != m_handles.end()) { + int result = Smart70DCL_CloseDevice(it->second); + m_handles.erase(it); + m_connectedPrinters[ipAddress].isConnected = false; + return result == SM_SUCCESS; + } + return false; + } + + // Obtener información de impresoras conectadas + std::vector getConnectedPrinters() { + std::lock_guard lock(m_mutex); + std::vector result; + + for (const auto& pair : m_connectedPrinters) { + if (pair.second.isConnected) { + result.push_back(pair.second); + } + } + + return result; + } + + // Obtener el estado de los módulos + std::vector getModuleStatus(const std::string& ipAddress) { + std::lock_guard lock(m_mutex); + + auto it = m_handles.find(ipAddress); + if (it != m_handles.end()) { + SMART70_DEVCONNLIST moduleList; + if (Smart70_GetConnectedModules(it->second, &moduleList) == SM_SUCCESS) { + return std::vector( + moduleList.dev, + moduleList.dev + moduleList.n + ); + } + } + return {}; + } + + // Solicitar preempción (con manejo de concurrencia) + bool requestPreemption(const std::string& ipAddress, WORD& preemptionId) { + std::lock_guard lock(m_mutex); + + auto it = m_handles.find(ipAddress); + if (it != m_handles.end()) { + return Smart70_RequestPreemption(it->second, &preemptionId) == SM_SUCCESS; + } + return false; + } + + // Liberar preempción + bool releasePreemption(const std::string& ipAddress, WORD preemptionId) { + std::lock_guard lock(m_mutex); + + auto it = m_handles.find(ipAddress); + if (it != m_handles.end()) { + return Smart70_ReleasePreemption(it->second, preemptionId) == SM_SUCCESS; + } + return false; + } + + // Método para crear un nuevo trabajo (Job Descriptor) + bool createJob(const std::string& ipAddress, HJOBD& jobDescriptor) { + std::lock_guard lock(m_mutex); + + auto it = m_handles.find(ipAddress); + if (it != m_handles.end()) { + return Smart70_NewJobDescriptor(it->second, &jobDescriptor, nullptr) == SM_SUCCESS; + } + return false; + } + + // Método para enviar un trabajo a la impresora + bool submitJob(const std::string& ipAddress, HJOBD jobDescriptor, int& jobId) { + std::lock_guard lock(m_mutex); + + auto it = m_handles.find(ipAddress); + if (it != m_handles.end()) { + return Smart70_InsertJob(it->second, jobDescriptor, &jobId) == SM_SUCCESS; + } + return false; + } + +private: + Smart70Printer() {} // Constructor privado para singleton + ~Smart70Printer() { + // Limpiar todas las conexiones al destruir + for (auto& pair : m_handles) { + Smart70DCL_CloseDevice(pair.second); + } + } + + // Eliminar copias + Smart70Printer(const Smart70Printer&) = delete; + Smart70Printer& operator=(const Smart70Printer&) = delete; + + std::mutex m_mutex; + std::unordered_map m_handles; + std::unordered_map m_connectedPrinters; +}; \ No newline at end of file