Exemplo n.º 1
0
 public UnitOfWork()
 {
     _context = new HQContext();
 }
Exemplo n.º 2
0
 public UnitOfWork()
 {
     _context = new HQContext();
 }
Exemplo n.º 3
0
        public void TCP1()
        {
            context1 = new HQContext();
            try
            {
                string    message   = "当前水位:";
                int       port      = 33300;
                IPAddress localAddr = IPAddress.Parse("10.172.33.4");
                listener1 = new TcpListener(localAddr, port);
                listener1.Start();
                log.Info("监听1开启");
                byte[] bytes = new byte[500];
                string data  = null;
                while (true)
                {
                    var date = DateTime.Now;
                    Console.WriteLine(date);
                    TcpClient client = listener1.AcceptTcpClient();
                    Console.WriteLine("Connected!");
                    log.Info("TCP1Connected!");
                    data = null;
                    NetworkStream stream = client.GetStream();

                    int i;
                    if (stream.CanRead)
                    {
                        log.Info("stream1可读");
                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            log.Info("接受报文1");
                            Console.WriteLine("字节数:" + i);
                            var str = "";
                            Console.WriteLine(ToHex(bytes, 0, 1));
                            Console.WriteLine(ToHex(bytes, 2, 2));
                            Console.WriteLine(ToHex(bytes, 3, 7));
                            Console.WriteLine(ToHex(bytes, 8, 9));
                            Console.WriteLine(ToHex(bytes, 10, 10));
                            Console.WriteLine(ToHex(bytes, 11, 12));
                            Console.WriteLine("----------------------------");
                            Console.WriteLine(ToHex(bytes, 13, 13));
                            Console.WriteLine("******************************");
                            Console.WriteLine(ToHex(bytes, 14, 15));  //1
                            Console.WriteLine(ToBCD(bytes, 16, 21));  //2


                            if (i > 25)
                            {
                                Console.WriteLine(ToHex(bytes, 22, 23));  //3
                                Console.WriteLine(ToHex(bytes, 24, 28));  //4
                                Console.WriteLine(ToHex(bytes, 29, 29));  //5
                                Console.WriteLine(ToHex(bytes, 30, 31));  //6
                                Console.WriteLine(ToBCD(bytes, 32, 36));  //7
                                Console.WriteLine(ToHex(bytes, 37, 38));  //8
                                Console.WriteLine(ToHex(bytes, 39, 42));  //9
                                Console.WriteLine(ToHex(bytes, 43, 44));  //10
                                Console.WriteLine(ToHex(bytes, 45, 47));  //11
                                Console.WriteLine(ToHex(bytes, 48, 49));  //12
                                Console.WriteLine(ToHex(bytes, 50, 52));  //13
                                Console.WriteLine(ToHex(bytes, 53, 54));  //14
                                Console.WriteLine(ToHex(bytes, 55, 56));  //15


                                var dtbdc = (DateTime.Now.Year / 100).ToString() + ToBCD(bytes, 32, 36) + "00";
                                log.Info("监测时间1:" + dtbdc);

                                var dateTime    = DateTime.Now;
                                var checkToTime = DateTime.TryParseExact(dtbdc, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dateTime);
                                if (checkToTime)
                                {
                                    var sdatetime = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
                                    log.Info("监测时间1转换:" + sdatetime);

                                    var sw = ToHex(bytes, 39, 42);
                                    //var sw = "0000750";
                                    double num = 0;
                                    //if (sw[sw.Length - 1] == '0')
                                    //{
                                    //    num = double.Parse(sw);
                                    //    num = num / 100;
                                    //}
                                    //else
                                    //{
                                    num = double.Parse(sw);
                                    num = num / 1000;
                                    Console.WriteLine("水位:" + num);
                                    log.Info("水位1:" + num);
                                    var checkinfo = "";
                                    if (num == 9999.999)
                                    {
                                        checkinfo = "大于";
                                        num       = 2.5;
                                    }
                                    else
                                    {
                                        checkinfo = "";
                                    }
                                    //}
                                    sw = num.ToString();
                                    var ljsw = (double)context1.SystemSetting.First(x => x.ID == 1).WaterLevelCriticalPoint;
                                    if (num >= ljsw)
                                    {
                                        log.Info("count1:" + count1 + "/" + "lastNum1:" + lastNum1 + "/" + "lastSentTime1:" + lastSentTime1.ToString("yyyy-MM-dd HH:mm:ss"));

                                        if (count1 >= 0 && lastNum1 < ljsw)
                                        {
                                            log.Info("<警告!中鑫村当前水位:" + checkinfo + sw + " (m),已超过临界点");
                                            //YunSendMsg.SendMsg("15868894638", "警告!中鑫村当前水位:" + checkinfo + sw + " (m),已超过临界点");
                                            YunSendMsg.SendMsg("16657110023", "警告!中鑫村当前水位:" + checkinfo + sw + " (m),已超过临界点");
                                            YunSendMsg.SendMsg("18100196818", "警告!中鑫村当前水位:" + checkinfo + sw + " (m),已超过临界点");
                                            count1++;
                                            lastNum1      = num;
                                            lastSentTime1 = DateTime.Now;
                                        }
                                        else if (count1 > 0 && lastNum1 > ljsw && (DateTime.Now - lastSentTime1).Minutes >= 29)
                                        {
                                            log.Info(">警告!中鑫村当前水位:" + checkinfo + sw + " (m),已超过临界点");
                                            //YunSendMsg.SendMsg("15868894638", "警告!中鑫村当前水位:" + checkinfo + sw + " (m),已超过临界点");
                                            YunSendMsg.SendMsg("16657110023", "警告!中鑫村当前水位:" + checkinfo + sw + " (m),已超过临界点");
                                            YunSendMsg.SendMsg("18100196818", "警告!中鑫村当前水位:" + checkinfo + sw + " (m),已超过临界点");
                                            count1++;
                                            lastNum1      = num;
                                            lastSentTime1 = DateTime.Now;
                                        }
                                        else
                                        {
                                            lastNum1 = num;
                                        }
                                    }
                                    lastNum1 = num;
                                    num      = 0;
                                    Console.WriteLine(sw);

                                    //if(sw)
                                    var cp = ToHex(bytes, 45, 47);
                                    num = double.Parse(cp);
                                    num = num / 10;

                                    HeDaowater heDaowater = new HeDaowater()
                                    {
                                        HeDaowaterUuid       = Guid.NewGuid(),
                                        HeDaowaterAddress    = ToHex(bytes, 24, 28) + "/" + TelemetryStationType(ToHex(bytes, 29, 29)),
                                        HeDaowaterTime       = sdatetime,
                                        HeDaoHeDaowaterSw    = sw,
                                        IsDeleted            = 0,
                                        AddTime              = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                        AddPeople            = "TCP",
                                        CurrentPrecipitation = (decimal?)num,
                                    };
                                    if (!context1.HeDaowater.Any(x => x.HeDaowaterAddress == heDaowater.HeDaowaterAddress && x.HeDaowaterTime == sdatetime && x.IsDeleted == 0))
                                    {
                                        context1.HeDaowater.Add(heDaowater);
                                        context1.SaveChanges();
                                    }
                                }
                            }



                            Console.WriteLine("******************************");
                            Console.WriteLine(ToHex(bytes, i - 3, i - 3));
                            Console.WriteLine("----------------------------");
                            //Console.WriteLine(Encoding.ASCII.GetString(bytes,13,1));
                            //Console.WriteLine(Encoding.ASCII.GetString(bytes,22,1));
                            Console.WriteLine(str);
                            if (i > 25)
                            {
                                data = ToHex(bytes, 0, 15) + ToBCD(bytes, 16, 21) + ToHex(bytes, 22, 31) + ToBCD(bytes, 32, 36) + ToHex(bytes, 37, i - 1);
                            }
                            else
                            {
                                data = ToHex(bytes, 0, 15) + ToBCD(bytes, 16, 21) + ToHex(bytes, 21, i - 1);
                            }

                            Console.WriteLine("Received:" + data);
                            log.Info("Received:" + data);
                            data = data.ToUpper();

                            //byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                            //// Send back a response.
                            //stream.Write(msg, 0, msg.Length);
                            //Console.WriteLine("Sent: {0}", data);
                        }
                    }
                    else
                    {
                        log.Info("stream1无法读,");
                        log.Info(listener1.Server.Connected);
                        log.Info(listener1.Server.Blocking);
                        //stream.Close();
                        //client.Close();
                        //client= listener1.AcceptTcpClient();
                        //stream = client.GetStream();
                    }

                    //client.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                log.Info("监听1错误:" + ex.Message);
                listener1.Stop();
            }
            finally
            {
                //listener1.Stop();
                listener1 = null;
                GC.Collect();
                log.Info("重启TCP1");
                TCP1();
            }
        }