176 lines
6.0 KiB
C++
176 lines
6.0 KiB
C++
#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;
|
|
}
|
|
|
|
// 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<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;
|
|
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;
|
|
}; |