Imports System.IO
Imports System.Text
Imports System.Threading.Tasks
Imports Zebra.Sdk.Comm
Imports Zebra.Sdk.Printer
Imports Zebra.Sdk.Printer.Discovery
Imports EgtUILib

Module LabelPrinter

   Private m_printerConnection As Connection = Nothing
   Public m_PrintVariables As New Dictionary(Of String, String)

   Sub New()

   End Sub

   Public Sub SetPrintVariables(PrintVariables As Dictionary(Of String, String))
      m_PrintVariables = PrintVariables
   End Sub

   Public Sub SetTestPrintVariables(ProdId As String, MachGroupId As String, PartId As String)
      Dim Values As New Dictionary(Of String, String)
      Values.Add("$(PRODID)", ProdId)
      Values.Add("$(MACHGROUPID)", MachGroupId)
      Values.Add("$(PARTID)", PartId)
      LabelPrinter.SetPrintVariables(Values)
   End Sub

   Friend Function ConnectPrinter() As Boolean
      Try
         Dim discoPrinters As List(Of DiscoveredPrinterDriver) = UsbDiscoverer.GetZebraDriverPrinters()
         m_printerConnection = New DriverPrinterConnection(discoPrinters(0).PrinterName)
         Return True
      Catch __unusedConnectionException1__ As ConnectionException
         EgtOutLog("Printer connection error: " & __unusedConnectionException1__.Message)
         Return False
      End Try
   End Function

   Public Async Sub Print()
      Await Task.Run(Async Function()

                        Try
                           ' Connecting...
                           'Await Task.Delay(1500)
                           m_printerConnection.Open()
                           ' Connected
                           'Await Task.Delay(1500)
                           '' Determining Printer Language...
                           'Await Task.Delay(1500)
                           'Dim printerLanguage As PrinterLanguage = ZebraPrinterFactory.GetInstance(m_printerConnection).PrinterControlLanguage
                           '' Printer Language 
                           'Await Task.Delay(1500)
                           ' Sending Data...
                           Dim LabelFile As String = File.ReadAllText("c:\Users\Dell\Downloads\Etichetta\Test2.prn")
                           For Each Variable In m_PrintVariables
                              LabelFile = LabelFile.Replace(Variable.Key, Variable.Value)
                           Next
                           Dim LabelBytes As Byte() = Encoding.UTF8.GetBytes(LabelFile)
                           Await Task.Delay(200)
                           ' stampo etichetta
                           m_printerConnection.Write(LabelBytes)
                        Catch __unusedConnectionException1__ As ConnectionException
                           ' Communications Error
                        Catch __unusedZebraPrinterLanguageUnknownException2__ As ZebraPrinterLanguageUnknownException
                           ' Invalid Printer Language
                        Finally

                           Try
                              'Threading.Thread.Sleep(500)
                              Threading.Thread.Sleep(200)
                              ' Disconnecting...

                              If m_printerConnection IsNot Nothing Then
                                 m_printerConnection.Close()
                              End If

                              'Threading.Thread.Sleep(500)
                              ' Not Connected
                           Catch __unusedConnectionException1__ As ConnectionException
                           End Try
                        End Try

                     End Function)
   End Sub

End Module
