empecemos

This commit is contained in:
2025-06-10 08:12:37 +02:00
parent 3cd5e6fce0
commit d311c034af
4 changed files with 178 additions and 14 deletions
+3 -1
View File
@@ -4,7 +4,9 @@
"name": "macos-clang-x64",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}"
"${workspaceFolder}",
"${workspaceFolder}/class",
"${workspaceFolder}/class"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
+1 -1
View File
@@ -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",
+171 -9
View File
@@ -1,14 +1,176 @@
// Ejemplo de conexión básica
HSMART70 printerHandle;
#include <string>
#include <vector>
#include <mutex>
#include <memory>
#include <unordered_map>
#include "SmartComm2.h" // Incluir el SDK
class Smart70Printer {
public:
// Singleton para manejar múltiples instancias de impresoras
static Smart70Printer& getInstance() {
static Smart70Printer instance;
return instance;
}
// Estructura para información de la impresora
struct PrinterInfo {
std::string id;
std::string description;
std::string ipAddress;
int port;
bool isConnected;
};
// Conectar a una impresora por dirección IP
bool connect(const std::string& ipAddress, int port = 9100) {
std::lock_guard<std::mutex> 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;
}
// Obtener lista de impresoras disponibles
Smart70_GetDeviceList(&printerList);
// 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) {
// Conectar con la primera impresora encontrada (modo DCL)
Smart70DCL_OpenDevice(&printerHandle, printerList.item[0].desc,
SMART70_OPENDEVICE_BYDESC, DMORIENT_LANDSCAPE);
PrinterInfo printerInfo;
printerInfo.id = printerList.item[i].id;
printerInfo.description = printerList.item[i].desc;
printerInfo.ipAddress = ipAddress;
printerInfo.port = port;
printerInfo.isConnected = true;
// Verificar módulos conectados
m_handles[ipAddress] = handle;
m_connectedPrinters[ipAddress] = printerInfo;
return true;
}
}
}
}
return false;
}
// Desconectar una impresora
bool disconnect(const std::string& ipAddress) {
std::lock_guard<std::mutex> 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<PrinterInfo> getConnectedPrinters() {
std::lock_guard<std::mutex> lock(m_mutex);
std::vector<PrinterInfo> 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<SMART70_DEVICE_STATUS> getModuleStatus(const std::string& ipAddress) {
std::lock_guard<std::mutex> lock(m_mutex);
auto it = m_handles.find(ipAddress);
if (it != m_handles.end()) {
SMART70_DEVCONNLIST moduleList;
Smart70_GetConnectedModules(printerHandle, &moduleList);
if (Smart70_GetConnectedModules(it->second, &moduleList) == SM_SUCCESS) {
return std::vector<SMART70_DEVICE_STATUS>(
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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::string, HSMART70> m_handles;
std::unordered_map<std::string, PrinterInfo> m_connectedPrinters;
};