void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
        {
            try
            {
                SerialPort sp    = (SerialPort)s;
                var        text  = sp.ReadLine();
                var        final = text.Split(',')[0];
                text = text.Replace("\0", "");

                var start = text.IndexOf("+", StringComparison.Ordinal);
                var end   = text.IndexOf("\r", StringComparison.Ordinal);
                var bien  = text.Substring(start + 1, end - start - 1).Replace("g", "").Trim();
                socket.Emit("pingScalesCate", bien);

                if (final == "ST" && int.TryParse(bien, out int i))
                {
                    if (i == 0)
                    {
                        readly = true;
                    }
                    if (i == value)
                    {
                        count += 1;
                    }
                    else
                    {
                        count = 0;
                        value = i;
                    }
                }
                else
                {
                    count = 0;
                }

                if (count == 2 && value > 0 && productId > 0 && readly)
                {
                    readly = false;
                    var data = new ProductCales()
                    {
                        Weight = value, ProductId = productId, Code = StringExtensions.GetCode()
                    };
                    var obj = JObject.FromObject(data);
                    socket.Emit("calesvalue", obj);
                    CacheCustomObject.Instance.GetOrAdd($"{Prefix}{productId}{code}", data);
                }
            }
            catch (Exception exception)
            {
            }
        }
Beispiel #2
0
        void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
        {
            try
            {
                SerialPort sp    = (SerialPort)s;
                var        text  = sp.ReadLine();
                var        final = text.Split(',')[0];
                text = text.Replace("\0", "");
                var start = text.IndexOf("+", StringComparison.Ordinal);
                var end   = text.IndexOf("\r", StringComparison.Ordinal);
                var bien  = text.Substring(start + 1, end - start - 1).Replace("g", "").Trim();

                socket.Emit("pingScalesCate", bien);

                if (final == "ST" && int.TryParse(bien, out int i))
                {
                    if (i <= 0)
                    {
                        readly = true;
                    }
                    if (i == value)
                    {
                        count += 1;
                    }
                    else
                    {
                        count = 0;
                        value = i;
                    }
                }
                else
                {
                    count = 0;
                }

                if (count == 2 && value > 0 && productId > 0 && readly)
                {
                    readly = false;
                    if (product == null)
                    {
                        return;
                    }
                    var h    = DateTime.Now.Hour;
                    var time = 18;
                    if (h < 4)
                    {
                        time = 4;
                    }
                    else if (h < 8)
                    {
                        time = 8;
                    }
                    else if (h < 15)
                    {
                        time = 15;
                    }

                    var data = new ProductCales()
                    {
                        Weight        = value,
                        ProductId     = productId,
                        Code          = StringExtensions.GetCode(),
                        DateCreate    = DateTime.Now.TotalSeconds(),
                        DateStr       = DateTime.Today.AddHours(time).ToString("dd/MM/yyyy"),
                        DateExpire    = DateTime.Today.AddHours(time + 6).TotalSeconds(),
                        DateExpireStr = DateTime.Today.AddDays(90).ToString("dd/MM/yyyy"),
                        Name          = product.Name,
                        PriceUnit     = product.Price ?? 0
                    };
                    var obj = JObject.FromObject(data);
                    socket.Emit("calesDetailvalue", obj);
                    CacheCustomObject.Instance.GetOrAdd($"{Prefix}{productId}{code}", data);
                }
            }
            catch (Exception exception)
            {
                log.Error(exception);
            }
        }