feat: Add Print Service Tray application with configuration and queue management
- Implemented PrintQueueService with job counting and snapshot retrieval. - Created Form1 as the main entry point for the tray application. - Developed ConfigurationForm for managing printer configurations and document types. - Added models for printer configuration, queue status requests, and responses. - Established ConfigurationService for loading and saving printer configurations. - Introduced ServiceClient for IPC communication with the print service. - Built TrayApplicationContext for managing the system tray icon and status updates. - Added TODO.md for tracking remaining work and future enhancements.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
using System.IO.Pipes;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using PrintServiceTray.Models;
|
||||
|
||||
namespace PrintServiceTray.Services;
|
||||
|
||||
public class ServiceClient
|
||||
{
|
||||
private readonly string _pipeName;
|
||||
private readonly int _timeout;
|
||||
|
||||
public ServiceClient(string pipeName = "PrintServicePipe", int timeoutMs = 5000)
|
||||
{
|
||||
_pipeName = pipeName;
|
||||
_timeout = timeoutMs;
|
||||
}
|
||||
|
||||
public async Task<QueueStatusResponse> GetQueueStatusAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var pipe = new NamedPipeClientStream(".", _pipeName, PipeDirection.InOut);
|
||||
await pipe.ConnectAsync(_timeout);
|
||||
|
||||
var request = new QueueStatusRequest();
|
||||
var requestJson = JsonSerializer.Serialize(request);
|
||||
var requestBytes = Encoding.UTF8.GetBytes(requestJson);
|
||||
|
||||
await pipe.WriteAsync(requestBytes);
|
||||
await pipe.FlushAsync();
|
||||
|
||||
var buffer = new byte[4096];
|
||||
var bytesRead = await pipe.ReadAsync(buffer);
|
||||
var responseJson = Encoding.UTF8.GetString(buffer, 0, bytesRead);
|
||||
|
||||
var response = JsonSerializer.Deserialize<QueueStatusResponse>(responseJson);
|
||||
return response ?? new QueueStatusResponse { Success = false, Message = "Invalid response" };
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
return new QueueStatusResponse { Success = false, Message = "Service not responding" };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new QueueStatusResponse { Success = false, Message = $"Error: {ex.Message}" };
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> IsServiceRunningAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await GetQueueStatusAsync();
|
||||
return response.Success;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user