Ejemplo n.º 1
0
        private void HandleScanResult(ZXing.Result result)
        {
            scanner.PauseAnalysis();
            if (!string.IsNullOrEmpty(result?.Text))
            {
                using (UdpClient udpClient = new UdpClient())
                {
                    udpClient.Connect(this.ip, this.port);
                    Byte[] senddata = System.Text.Encoding.ASCII.GetBytes(result.Text);
                    udpClient.Send(senddata, senddata.Length);

                    udpClient.Close();
                    this.beep();
                }
            }
            scanner.ResumeAnalysis();
        }
Ejemplo n.º 2
0
        private async void HandleScanResult(ZXing.Result result)
        {
            if (result != null && !string.IsNullOrEmpty(result.Text))
            {
                scanner.PauseAnalysis();

                ScanStatus(true);

                // Check if Barcode exists
                string barcode = result.Text;

                int itemId = ViewModel.GetItemIdForBarcode(barcode);

                if (itemId != 0) // Barcode matches
                {
                    ViewModel.ViewItem(itemId);

                    scanner.Cancel();
                }
                else // scanned new product
                {
                    string json = (await Query.GetProductJson(barcode)).ToString();

                    try
                    {
                        JObject root = JObject.Parse(json);
                        JObject job  = (JObject)root.GetValue("0");

                        // Make sure we actually have data
                        if (!((string)job.GetValue("currency")).Equals("N/A"))
                        {
                            // Get Defaults
                            ProductDefaults defaults = ProductDefaults.Read();

                            string name  = (string)job.GetValue("productname");
                            string value = (string)job.GetValue("price");

                            Item product = new Item();

                            if (defaults.HasDate && defaults.DatePurchased.Year == 1)
                            {
                                product.DatePurchased = DateTime.Today;
                            }
                            else if (defaults.HasDate && defaults.DatePurchased.Year > 1)
                            {
                                product.DatePurchased = defaults.DatePurchased;
                            }
                            else if (!defaults.HasDate)
                            {
                                product.DatePurchased = new DateTime(1, 1, 1);
                            }

                            product.Barcode  = barcode;
                            product.Name     = name;
                            product.Quantity = defaults.Quantity;
                            product.QuantityForShoppingList = defaults.ShoppingQuantity;
                            product.Value    = decimal.Parse(value);
                            product.Location = defaults.Location;
                            product.Category = defaults.Category;
                            product.Notes    = defaults.Notes;

                            if (defaults.AutoAdd)
                            {
                                ViewModel.SaveProduct(product);
                            }
                            else
                            {
                                ViewModel.EditProduct(product.ToJson().ToString());
                            }

                            scanner.Cancel();

                            /*
                             * // Intent setup
                             * var editProduct = new Intent(this, typeof(EditItemActivity));
                             * editProduct.PutExtra("type", AndroidHelper.IS_PRODUCT);
                             *
                             * // Get settings for scanning
                             * // Settings may have been updated between scans
                             * // So that is why we check here
                             * try
                             * {
                             *  //ProductDefaults.InitializeFromJson(JObject.Parse(App.OpenJson(ProductDefaults.Filename)));
                             *  ProductDefaults.InitializeFromJson(JObject.Parse(await Core.GetProductDefaults()));
                             * }
                             * catch (Exception)
                             * {
                             *  ProductDefaults.Initialize();
                             *  System.Diagnostics.Debug.WriteLine("Unable to parse JSON. File might not exist.");
                             * }
                             *
                             * // Enforce defaults
                             *
                             * product.Quantity = ProductDefaults.Quantity;
                             * product.Location = ProductDefaults.Location;
                             * product.Category = ProductDefaults.Category;
                             * product.Notes = ProductDefaults.Notes;
                             *
                             *
                             * if (ProductDefaults.AutoAdd) // autosave
                             * {
                             *  await Task.Run(() => Core.SaveItem(product));
                             *
                             *  scanner.Cancel();
                             * }
                             * else
                             * {
                             *  editProduct.PutExtra("product", product.ToJson().ToString());
                             *
                             *  StartActivity(editProduct);
                             *
                             *  scanner.Cancel();
                             * }
                             */
                        }
                        else
                        {
                            ScanStatus(false);
                            Activity.RunOnUiThread(() => Toast.MakeText(Activity, "Could not find product data", ToastLength.Short).Show());
                            scanner.ResumeAnalysis();
                        }
                    }
                    catch (Exception e)
                    {
                        ScanStatus(false);
                        System.Diagnostics.Debug.WriteLine("OVER HERE " + e);
                        Activity.RunOnUiThread(() => Toast.MakeText(Activity, "No result", ToastLength.Short).Show());
                        scanner.ResumeAnalysis();
                    }
                }
            }
            else
            {
                ScanStatus(false);
                Activity.RunOnUiThread(() => Toast.MakeText(Activity, "Unable to read Barcode", ToastLength.Short).Show());
            }
        }