47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
using System.Net.Sockets;
|
|
using System.Text;
|
|
|
|
namespace Seyounth.Hyosung.Core.Printer;
|
|
|
|
public class AveryPrinter(string host, int port)
|
|
{
|
|
public string Host => host;
|
|
public int Port => port;
|
|
private readonly Socket _socket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
public async Task ConnectAsync(CancellationToken token)
|
|
{
|
|
try
|
|
{
|
|
await _socket.ConnectAsync(Host, Convert.ToInt32(port), token);
|
|
}catch(Exception e)
|
|
{
|
|
Console.WriteLine($"Host[{Host}:{port}] error:{e.Message}");
|
|
}
|
|
|
|
}
|
|
|
|
public async Task CloseAsync(CancellationToken token)
|
|
{
|
|
await _socket.DisconnectAsync(true, token);
|
|
_socket.Dispose();
|
|
}
|
|
|
|
public async Task SendContentAsync(string context)
|
|
{
|
|
await _socket.SendAsync(Encoding.UTF8.GetBytes(context));
|
|
}
|
|
|
|
private const string CleanCmd = "#!A1#!CA#!P1";
|
|
private const string PrintCmd = "#!A1#!D#!P1";
|
|
|
|
public async Task CleanContextAsync()
|
|
{
|
|
await _socket.SendAsync(Encoding.UTF8.GetBytes(CleanCmd));
|
|
}
|
|
|
|
public async Task PrintAsync()
|
|
{
|
|
await _socket.SendAsync(Encoding.UTF8.GetBytes(PrintCmd));
|
|
}
|
|
} |