PrinterHelper.cs
5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using Asa;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.Common.util;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary
{
public class PrinterHelper
{
public Asa.PrintLabel print;
ZebraPrinterHelper.ZebraManger zebraManger;
string Port;
int dpi = 100;
public static bool istscprin = ConfigHelper.Config.Get("Hardwareversion", "V2")=="V2";
public static bool newistscprin = ConfigHelper.Config.Get("HardwareversionofAGV", "V2") == "V2";
//public PrintDevice printDevice = ConfigHelper.Config.Get("PrintDevice", istscprin ? PrintDevice.TSC : PrintDevice.Zebra);
public PrintDevice printDevice = ConfigHelper.Config.Get("PrintDevice", istscprin ? PrintDevice.TSC : (newistscprin? PrintDevice.TSC : PrintDevice.Zebra));
public enum PrintDevice {
TSC,
Zebra,
Windows
}
public PrinterHelper()
{
if (printDevice!= PrintDevice.Windows)
{
dpi = 300;
}
print = new Asa.PrintLabel(Application.StartupPath + "\\Label", dpi);
print.PrintStatusChanged += Print_PrintStatusChanged;
}
private void Print_PrintStatusChanged(Asa.PrintLabel.PrinterStatus sta, string msg)
{
LogUtil.info($"打印机状态:{sta}, msg:{msg}");
}
public static CustPrinterStatus LastPrintStatus = CustPrinterStatus.Unknown;
public bool Connection(string port)
{
Port = port;
if (printDevice== PrintDevice.TSC || printDevice== PrintDevice.Windows)
{
print = new Asa.PrintLabel(Application.StartupPath + "\\Label", dpi);
return true;
}
else if (printDevice == PrintDevice.Zebra)
{
//默认使用老打印机
zebraManger = new ZebraPrinterHelper.ZebraManger("", ZebraPrinterHelper.ConnectionType.UsbDirect);
//zebraManger = new ZebraPrinterHelper.ZebraManger(Port, ZebraPrinterHelper.ConnectionType.Network);
if (!zebraManger.Connection(out string msg))
{
LogUtil.error(msg);
return false;
}
return true;
}
return false;
}
public void Close()
{
zebraManger.Close();
}
public void EditLabel()
{
print.EditLabel();
}
public string[] GetLabelList()
{
return print.GetLabelName();
}
public bool Print(string labelname, Dictionary<string, string> data, out string msg)
{
print.LoadLabel(labelname);
if (printDevice == PrintDevice.TSC)
{
LogUtil.error($"TSC_打印数据:{JsonHelper.SerializeObject(data)}");
TscConfig tscConfig = new TscConfig();
tscConfig.Offset = ConfigHelper.Config.Get("Tsc_Offset", -17D);
tscConfig.Speed = ConfigHelper.Config.Get("Tsc_Speed", 9);
tscConfig.Density = ConfigHelper.Config.Get("Tsc_Density", 12);
tscConfig.GapOffset = ConfigHelper.Config.Get("Tsc_GapOffset", 0);
tscConfig.Gap = ConfigHelper.Config.Get("Tsc_Gap", 3);
if (!print.PrintToTsc_New(data, tscConfig, out TscStauts tscStauts, out Bitmap bmp))
{
msg = $"打印失败:{tscStauts.ToString()}";
LogUtil.error("打印失败原因:" + tscStauts.ToString());
return false;
}
if (bmp != null)
{
string cid = ConfigHelper.Config.Get("CID");
_ = UnifiedDataHandler.PostSmfImageAsync(bmp, new Dictionary<string, string> { { "cid", cid + "_2" } }, bmp.Width, bmp.Height);
}
msg = $"打印成功";
return true;
}
else if (printDevice == PrintDevice.Windows) {
print.Print(data);
msg = "";
return true;
}
else
{
var bmp = print.PrintPreview(data);
if (!zebraManger.PrintImage(bmp, out msg))
{
if (msg == crc.GetString("Res0224","上一个标签尚未移走"))
{
msg = crc.GetString("Res0224", "上一个标签尚未移走");
}
LogUtil.error(msg);
return false;
}
bmp.Dispose();
return true;
}
}
public Task<(bool, string)> IsLabelOnPeeler()
{
return Task.Run(() =>
{
bool rtn = zebraManger.IsLabelOnPeeler;
if(rtn)
{
LastPrintStatus = CustPrinterStatus.HasLabel;
}
else
{
LastPrintStatus = CustPrinterStatus.NoLabel;
}
return (rtn, "");
});
}
}
public enum CustPrinterStatus
{
Unknown,
NoLabel,
HasLabel,
}
}