Exemplo n.º 1
0
        /// <summary>
        /// 设置空开合闸间隔
        /// </summary>
        /// <param name="crossingId"></param>
        /// <param name="switchInt"></param>
        /// <returns></returns>
        public static JsonMessage SwitchInt(string crossingId, string switchInt)
        {
            var msg = new JsonMessage {
                Success = false, Message = "parameter error"
            };

            dynamic list = CrossingService.GetIP_IsOnline(crossingId);

            if (list != null)
            {
                //bool isOnline = list.IsOnline;
                string    ip        = list.CrsIp;
                Socket    socket    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ipAddress = IPAddress.Parse(ip);
                bool      conn      = TcpConn.ConnectWithTimeout(socket, new IPEndPoint(ipAddress, 5000), 2000); //超时2秒

                try
                {
                    int d = int.Parse(switchInt);

                    byte[] c = BitConverter.GetBytes(d);

                    string dd = "EF EF 07 12 " + Common.ByteToHexStr2(c).TrimEnd(' ');
                    if (conn)
                    {
                        var    sum    = dd.Split(' ').Select(i => Convert.ToInt32(i, 16)).Sum();
                        var    s      = Convert.ToString(sum, 16);
                        var    result = s.Substring(1, s.Length - 1);
                        string send   = dd + result;
                        var    aa     = Common.StrToHexByte(send);
                        socket.Send(aa);                        // Sends some data
                        Thread.Sleep(500);
                        var data = socket.Receive(new byte[9]); // Receives some data back (blocks execution)
                        msg.Success = true;
                        msg.Message = ip + "设置空开合闸间隔:" + d + "成功" + data;
                        msg.Title   = "设置空开合闸间隔";
                    }
                    else
                    {
                        msg.Success = false;
                        msg.Message = ip + ":通讯网络错误!";
                        msg.Title   = "设置空开合闸间隔";
                    }
                    socket.Dispose();
                }
                catch (Exception e)
                {
                    socket.Dispose();
                    msg.Success = false;
                    msg.Message = ip + ":通讯网络错误!" + e.Message;
                    msg.Title   = "设置空开合闸间隔";
                }

                //InertLogs(list.CrsName, msg.Title, msg.Message);
            }
            return(msg);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 关闭报警
        /// </summary>
        /// <param name="crossingId"></param>
        /// <param name="closeAlarm"></param>
        /// <returns></returns>
        public static JsonMessage CloseAlarm(string crossingId, string closeAlarm)
        {
            var msg = new JsonMessage {
                Success = false, Message = "parameter error"
            };

            dynamic list = CrossingService.GetIP_IsOnline(crossingId);

            if (list != null)
            {
                // bool isOnline = list.IsOnline;
                string ip = list.CrsIp;

                if (closeAlarm == "1" || closeAlarm.ToLower() == "true")
                {
                    Socket    socket    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPAddress ipAddress = IPAddress.Parse(ip);
                    bool      conn      = TcpConn.ConnectWithTimeout(socket, new IPEndPoint(ipAddress, 5000), 2000); //超时2秒
                    try
                    {
                        if (conn)
                        {
                            string send = "EF EF 04 15 00 F7";

                            var data = Common.StrToHexByte(send);
                            socket.Send(data);
                            Thread.Sleep(500);
                            msg.Success = true;
                            msg.Message = ip + "报警信息关闭成功";
                            msg.Title   = "关闭该次报警提醒";
                        }
                        else
                        {
                            msg.Success = false;
                            msg.Message = ip + "通讯网络错误!";
                            msg.Title   = "关闭该次报警提醒";
                        }
                        socket.Dispose();
                    }
                    catch (Exception e)
                    {
                        socket.Dispose();
                        msg.Success = false;
                        msg.Message = ip + "通讯网络错误!" + e.Message;
                        msg.Title   = "关闭该次报警提醒";
                    }
                }
                else
                {
                    msg.Success = false;
                    msg.Message = ip + "关闭该次报警提醒失败!";
                    msg.Title   = "关闭该次报警提醒";
                }
                //InertLogs(list.CrsName, msg.Title, msg.Message);
            }
            return(msg);
        }
Exemplo n.º 3
0
        public static JsonMessage OnOrOff(string crossingId, string onOrOff, string ctrlName)
        {
            var msg = new JsonMessage {
                Success = false, Message = "parameter error"
            };
            dynamic list = CrossingService.GetIP_IsOnline(crossingId);

            if (list != null)
            {
                //bool isOnline = list.IsOnline;
                string    ip        = list.CrsIp;
                Socket    socket    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ipAddress = IPAddress.Parse(ip);
                bool      conn      = TcpConn.ConnectWithTimeout(socket, new IPEndPoint(ipAddress, 5000), 2000); //超时2秒

                try
                {
                    if (conn)
                    {
                        string send = onOrOff;
                        var    data = Common.StrToHexByte(send);
                        socket.Send(data);                        // Sends some data
                        Thread.Sleep(500);
                        var result = socket.Receive(new byte[6]); // Receives some data back (blocks execution)
                        msg.Success = true;
                        msg.Message = send;
                        msg.Title   = ctrlName;
                    }
                    else
                    {
                        msg.Success = false;
                        msg.Message = ip + "通讯网络错误!";
                        msg.Title   = ctrlName;
                    }
                    socket.Dispose();
                }
                catch (Exception e)
                {
                    msg.Success = false;
                    msg.Message = ip + "通讯网络错误!" + e.Message;
                    msg.Title   = ctrlName;
                }

                //  InertLogs(list.CrsName, msg.Title, msg.Message,ipAddress,browser);
            }
            return(msg);
        }
Exemplo n.º 4
0
        public static void InSertDb(List <dynamic> para)
        {
            // System.Threading.Tasks.Parallel.For - for 循环的并行运算
            Parallel.ForEach(para, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 4
            }, (i, parallelLoopStat) =>
            {
                Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ipAddress = IPAddress.Parse(i.crsIp);
                bool conn           = TcpConn.ConnectWithTimeout(clientSocket, new IPEndPoint(ipAddress, 5000), 2000); //超时2秒

                if (conn)
                {
                    // Log.Info($"Ip={i.crsIp}&Msg=Socket连接成功");
                    try
                    {
                        // Log.Info($"Ip={i.crsIp}&Msg=获取设备状态...");
                        string send = "EF EF 04 14 00 F6";
                        var msg     = Common.StrToHexByte(send);
                        try
                        {
                            clientSocket.Send(msg); // Sends some data  BitConverter.ToString(msg, 0))
                        }
                        catch (Exception e)
                        {
                            // clientSocket.Shutdown(SocketShutdown.Both);//可去除
                            clientSocket.Dispose();
                            Console.WriteLine($"Ip={i.crsIp}&Msg=SendData:{e.Message}");

                            // UpdateIsOnline(false, i.crsCode);
                            //Console.WriteLine($"Ip={i.crsIp}&Msg=Crossing更新离线状态成功");
                            var model = GetMDevstatusByCrsCode(i.crsCode);
                            if (IsExist(i.crsCode))
                            {
                                UpdateOnline(model);
                                // Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus更新离线成功");
                            }
                            else
                            {
                                InsertOnline(model);
                                // Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus插入离线状态成功");
                            }
                            //continue;
                            // parallelLoopStat.Break();
                            return;//不加return,可能会发生该进程资源未释放。
                        }
                        var buffer = new byte[103];
                        try
                        {
                            clientSocket.Receive(buffer); // Receives some data back (blocks execution)
                        }
                        catch (Exception e)
                        {
                            // clientSocket.Shutdown(SocketShutdown.Both);//可去除

                            clientSocket.Dispose();
                            Console.WriteLine($"Ip={i.crsIp}&Msg=Receive:{e.Message}");

                            // UpdateIsOnline(false, i.crsCode);
                            // Console.WriteLine($"Ip={i.crsIp}&Msg=Crossing更新离线状态成功");
                            var model = GetMDevstatusByCrsCode(i.crsCode);
                            if (IsExist(i.crsCode))
                            {
                                UpdateOnline(model);
                                // Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus更新离线成功");
                            }
                            else
                            {
                                InsertOnline(model);
                                //  Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus插入离线状态成功");
                            }
                            //continue;
                            // parallelLoopStat.Break();
                            return;//不加return,可能会发生该进程资源未释放。
                        }
                        try
                        {
                            var response = BitConverter.ToString(buffer, 0);
                            // Receives some data back (blocks execution)  AB-CD-09-08-2A-E8-07-00-00-00-A243
                            var dataArr     = response.Replace("-", "");
                            var byteDataArr = Common.StrToHexByte(dataArr);

                            //8421码按位与操作
                            var cameraTwo = (byteDataArr[4] & 0x08) == 0x08;
                            var cameraOne = (byteDataArr[4] & 0x04) == 0x04;
                            var fot       = (byteDataArr[4] & 0x02) == 0x02;
                            var router    = (byteDataArr[4] & 0x01) == 0x01;

                            var fan         = (byteDataArr[4] & 0x10) == 0x10;
                            var acb         = (byteDataArr[4] & 0x20) == 0x20;
                            var cameraThree = (byteDataArr[4] & 0x40) == 0x40;
                            var tempAlarm   = (byteDataArr[5] & 0x01) == 0x01;
                            var esAlarm     = (byteDataArr[5] & 0x02) == 0x02;

                            byte[] coolingTempbyte = new byte[4];
                            coolingTempbyte[0]     = byteDataArr[6];
                            coolingTempbyte[1]     = byteDataArr[7];
                            coolingTempbyte[2]     = byteDataArr[8];
                            coolingTempbyte[3]     = byteDataArr[9];
                            var coolingTemp        = BitConverter.ToSingle(coolingTempbyte, 0);
                            if (float.IsNaN(coolingTemp))
                            {
                                coolingTemp = 0f;
                            }

                            byte[] alarmTempbyte = new byte[4];
                            alarmTempbyte[0]     = byteDataArr[10];
                            alarmTempbyte[1]     = byteDataArr[11];
                            alarmTempbyte[2]     = byteDataArr[12];
                            alarmTempbyte[3]     = byteDataArr[13];
                            var alarmTemp        = BitConverter.ToSingle(alarmTempbyte, 0);
                            if (float.IsNaN(alarmTemp))
                            {
                                alarmTemp = 0f;
                            }
                            byte[] caseTempbyte = new byte[4];
                            caseTempbyte[0]     = byteDataArr[14];
                            caseTempbyte[1]     = byteDataArr[15];
                            caseTempbyte[2]     = byteDataArr[16];
                            caseTempbyte[3]     = byteDataArr[17];
                            var caseTemp        = BitConverter.ToSingle(caseTempbyte, 0);

                            byte[] esSumbyte = new byte[4];
                            esSumbyte[0]     = byteDataArr[18];
                            esSumbyte[1]     = byteDataArr[19];
                            esSumbyte[2]     = byteDataArr[20];
                            esSumbyte[3]     = byteDataArr[21];
                            var esSum        = BitConverter.ToInt32(esSumbyte, 0);

                            byte[] switchOnSumbyte = new byte[4];
                            switchOnSumbyte[0]     = byteDataArr[22];
                            switchOnSumbyte[1]     = byteDataArr[23];
                            switchOnSumbyte[2]     = byteDataArr[24];
                            switchOnSumbyte[3]     = byteDataArr[25];
                            var switchOnSum        = BitConverter.ToInt32(switchOnSumbyte, 0);

                            byte[] switchIntbyte = new byte[4];
                            switchIntbyte[0]     = byteDataArr[26];
                            switchIntbyte[1]     = byteDataArr[27];
                            switchIntbyte[2]     = byteDataArr[28];
                            switchIntbyte[3]     = byteDataArr[29];
                            var switchInt        = BitConverter.ToInt32(switchIntbyte, 0);

                            byte[] aliveTimebyte = new byte[4];
                            aliveTimebyte[0]     = byteDataArr[30];
                            aliveTimebyte[1]     = byteDataArr[31];
                            aliveTimebyte[2]     = byteDataArr[32];
                            aliveTimebyte[3]     = byteDataArr[33];
                            var aliveTime        = BitConverter.ToInt32(aliveTimebyte, 0);

                            byte[] voltageByte = new byte[4];
                            voltageByte[0]     = byteDataArr[34];
                            voltageByte[1]     = byteDataArr[35];
                            voltageByte[2]     = byteDataArr[36];
                            voltageByte[3]     = byteDataArr[37];
                            var voltage        = BitConverter.ToSingle(voltageByte, 0);

                            byte[] thresholdVoltageByte = new byte[4];
                            thresholdVoltageByte[0]     = byteDataArr[38];
                            thresholdVoltageByte[1]     = byteDataArr[39];
                            thresholdVoltageByte[2]     = byteDataArr[40];
                            thresholdVoltageByte[3]     = byteDataArr[41];
                            var thresholdVoltage        = BitConverter.ToSingle(thresholdVoltageByte, 0);

                            MtbDevStatus model = new MtbDevStatus
                            {
                                ACB         = acb,
                                AlarmTemp   = alarmTemp,
                                CameraOne   = cameraOne,
                                CameraTwo   = cameraTwo,
                                CameraThree = cameraThree,
                                CaseTemp    = caseTemp,
                                CoolingTemp = coolingTemp,
                                CreateDate  = DateTime.Now,
                                CrsCode     = i.crsCode,
                                SubCode     = i.subCode,
                                ESAlarm     = esAlarm,
                                ESSum       = esSum,
                                SwitchInt   = switchInt,
                                SwitchOnSum = switchOnSum,
                                FOT         = fot,
                                Router      = router,
                                Fan         = fan,
                                TempAlarm   = tempAlarm,
                                //CrsName = i.CrsName,
                                AliveTime        = aliveTime,
                                IsOnline         = true,
                                Voltage          = voltage,
                                ThresholdVoltage = thresholdVoltage
                            };
                            //try
                            //{
                            //   // UpdateIsOnline(true, i.crsCode);
                            //    //Log.Info($"Ip={i.crsIp}&Msg=Crossing更新在线状态成功");
                            //}
                            //catch (Exception e)
                            //{
                            //    Console.WriteLine($"Ip={i.crsIp}&Msg=Crossing更新在线状态异常:{e.Message}");
                            //}

                            if (IsExist(i.crsCode))
                            {
                                try
                                {
                                    UpdateOnline(model);
                                    // Log.Info($"Ip={i.crsIp}&Msg=crossingstatus更新在线状态成功");
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus Update Online:{e.Message}");
                                    return;
                                }
                            }
                            else
                            {
                                try
                                {
                                    InsertOnline(model);
                                    // Log.Info($"Ip={i.crsIp}&Msg=crossingstatus插入在线状态成功");
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus Insert Online:{e.Message}");
                                    return;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            clientSocket.Dispose();
                            Console.WriteLine($"Ip={i.crsIp}&Msg=Receive Para Convert:{e.Message}");

                            //UpdateIsOnline(false, i.crsCode);
                            //Console.WriteLine($"Ip={i.crsIp}&Msg=Crossing更新离线状态成功");
                            var model = GetMDevstatusByCrsCode(i.crsCode);
                            if (IsExist(i.crsCode))
                            {
                                UpdateOnline(model);
                                //Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus更新离线成功");
                            }
                            else
                            {
                                InsertOnline(model);
                                //Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus插入离线状态成功");
                            }
                            //continue;
                            // parallelLoopStat.Break();
                            return;//不加return,可能会发生该进程资源未释放。
                        }
                    }
                    catch (Exception e)
                    {
                        //Console.WriteLine($"Ip={i.crsIp}&Msg={e.Message}");
                        //UpdateIsOnline(false, i.crsCode);
                        //Console.WriteLine($"Ip={i.crsIp}&Msg=Crossing更新离线状态成功");
                        Console.Write(e.Message);
                        var model = GetMDevstatusByCrsCode(i.crsCode);
                        if (IsExist(i.crsCode))
                        {
                            UpdateOnline(model);
                            //  Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus更新离线成功");
                        }
                        else
                        {
                            InsertOnline(model);
                            //  Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus插入离线状态成功");
                        }
                        // clientSocket.Shutdown(SocketShutdown.Both);//可去除

                        clientSocket.Dispose();
                        return;//不加return,可能会发生该进程资源未释放。
                    }
                }
                else
                {
                    //UpdateIsOnline(false, i.crsCode);
                    // Console.WriteLine($"Ip={i.crsIp}&Msg=Crossing更新离线状态");
                    try
                    {
                        var model = GetMDevstatusByCrsCode(i.crsCode);
                        if (IsExist(i.crsCode))
                        {
                            UpdateOnline(model);
                            //  Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus更新离线状态");
                        }
                        else
                        {
                            InsertOnline(model);
                            // Console.WriteLine($"Ip={i.crsIp}&Msg=crossingstatus插入离线状态");
                        }
                        if (IsExistFault(i.crsCode))
                        {
                            UpdateOfflineFault(i);
                            // Console.WriteLine($"Ip={i.crsIp}&Msg=fault更新离线状态");
                        }
                        else
                        {
                            InsertOfflineFault(i);
                            // Console.WriteLine($"Ip={i.crsIp}&Msg=fault插入离线状态");
                        }
                        Console.WriteLine($"Ip={i.crsIp}&Msg=Connect Error");
                        // clientSocket.Shutdown(SocketShutdown.Both);//可去除

                        clientSocket.Dispose();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return;//不加return,可能会发生该进程资源未释放。
                    }
                }

                clientSocket.Dispose();
            });
        }