Ejemplo n.º 1
0
        /// <summary>
        /// Method called to request an the ad.
        /// </summary>
        private void printCoupon(bool is_test, string receiptContent, AdRequest new_request)
        {
            //Get the selected OPOS printer from list.
            var selectedPrinter = from d in printerList
                                  where d.ServiceObjectName == OPOSListcomboBox.SelectedItem.ToString()
                                  select d;
            PosPrinter actualPrinter = (PosPrinter)posExplorer.CreateInstance(selectedPrinter.ElementAt(0));
            string test = actualPrinter.ToString();
            AddLog("Printer found",false);

            try
            {
                if (actualPrinter != null)
                {
                actualPrinter.Open();

                AddLog("Printer opened",false);

                actualPrinter.Claim(5000);
                actualPrinter.DeviceEnabled = true;
                actualPrinter.RecLetterQuality = true;

                    AdProvider provider = new AdProvider();
                    AdResponse new_response = new AdResponse();

                    provider.Url = WebServiceTextBox.Text;

                    //Call web service
                    if (is_test)
                    {
                         new_response = provider.TestConnection(new_request);
                    }
                    else
                    {
                    new_response = provider.RequestAd(new_request);
                    }

                    if (new_response.is_error)
                    {
                    AddLog(new_response.status,false);
                         RewardsHelper.SaveLastDocket(new_request.currentDocket);
                         PrintLocalDefaultImage(actualPrinter);
                    }
                    else
                    {

                    if (new_response.adList.Count() == 0)
                    {
                        PrintLocalDefaultImage(actualPrinter);
                    }
                    else
                    {
                        int placedad_id = 0;

                        if (!is_test)
                        {
                            placedad_id = new_response.placedad_id;
                        }

                        actualPrinter.PrintNormal(PrinterStation.Receipt, new_response.header);

                        foreach (AdImage item in new_response.adList)
                        {
                            actualPrinter.PrintNormal(PrinterStation.Receipt, "\r\n");
                            Bitmap ADimg = Helpers.ConvertPNGDataToBitmap(item.imageData);
                                   string directory = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                                   string fileName = directory + @"/temp.bmp";
                            ADimg.Save( fileName, ImageFormat.Bmp);

                            actualPrinter.PrintBitmap(PrinterStation.Receipt, fileName, PosPrinter.PrinterBitmapAsIs, PosPrinter.PrinterBitmapCenter);
                            actualPrinter.PrintNormal(PrinterStation.Receipt, "\r\n");
                            actualPrinter.PrintNormal(PrinterStation.Receipt, item.footer);
                        }

                        actualPrinter.PrintNormal(PrinterStation.Receipt, "\r\n\r\nPowered by www.docketplace.com.au");
                        actualPrinter.PrintNormal(PrinterStation.Receipt, "\r\n\r\n\r\n\r\n\r\n\r\n");
                        actualPrinter.CutPaper(10);
                              actualPrinter.DeviceEnabled = false;
                        AddLog("Internet Image printed\t" + placedad_id + "\r\n",false);
                        }
                    }
                }
            }
            catch (System.Net.WebException ex)
            {
                //If its a test print  failure display a message otherwise print out the default coupon.
                if (is_test)
                {
                    TestStoreConnectionErrorLabel.Text = "No internet connection";
                }
                else
                {
                throw ex;
                }
            }
            catch(Exception ex)
            {
                TestStoreConnectionErrorLabel.Text = "Error with printer , check to see if switched on";
                AddLog(ex.ToString(),true);
            }
            finally
            {
                if (actualPrinter.Claimed)
                {
                    actualPrinter.Release();
                }
                actualPrinter.Close();
            }
        }