/// <summary> /// 关闭报警 /// </summary> /// <param name="crossingId"></param> /// <param name="closeAlarm"></param> /// <returns></returns> public JsonMessage CloseAlarm(string crossingId, string closeAlarm) { var msg = new JsonMessage { Success = false, Message = "parameter error" }; dynamic list = CrossingService.GetIP_IsOnline(crossingId); bool isOnline = list.IsOnline; string ip = list.CrsIp; if (closeAlarm == "1" || closeAlarm.ToLower() == "true") { if (isOnline) { Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipAddress = IPAddress.Parse(ip); try { TcpConn.ConnectWithTimeout(clientSocket, new IPEndPoint(ipAddress, 5000), 1000); msg.Success = true; msg.Message = "Socket连接成功"; } catch { msg.Success = false; msg.Message = "Socket连接失败"; return(msg); } try { string send = "EF EF 04 15 00 F7"; var data = Common.StrToHexByte(send); clientSocket.Send(data); //Thread.Sleep(500); var buffer = new byte[11]; clientSocket.Receive(buffer); // Receives some data back (blocks execution) var response = BitConverter.ToString(buffer, 0); msg.Success = true; msg.Message = ip + "报警信息关闭成功" + response; msg.Title = "关闭该次报警提醒"; } catch { // clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); clientSocket.Dispose(); msg.Success = false; msg.Message = ip + "通讯网络错误!"; msg.Title = "关闭该次报警提醒"; return(msg); } // clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); clientSocket.Dispose(); } else { msg.Success = false; msg.Message = ip + "设备离线!"; msg.Title = "关闭该次报警提醒"; } } else { msg.Success = false; msg.Message = ip + "关闭该次报警提醒失败!"; msg.Title = "关闭该次报警提醒"; } return(msg); }
/// <summary> /// 设置电池合闸门限电压 /// </summary> /// <param name="crossingId"></param> /// <param name="thresholdVoltage"></param> /// <returns></returns> public JsonMessage ThresholdVoltage(string crossingId, string thresholdVoltage) { var msg = new JsonMessage { Success = false, Message = "parameter error" }; dynamic list = CrossingService.GetIP_IsOnline(crossingId); bool isOnline = list.IsOnline; string ip = list.CrsIp; if (isOnline) { Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipAddress = IPAddress.Parse(ip); try { TcpConn.ConnectWithTimeout(clientSocket, new IPEndPoint(ipAddress, 5000), 1000); msg.Success = true; msg.Message = "Socket连接成功"; } catch { msg.Success = false; msg.Message = "Socket连接失败"; return(msg); } try { float f = float.Parse(thresholdVoltage); byte[] c = BitConverter.GetBytes(f); string dd = "EF EF 07 1B " + Common.ByteToHexStr2(c).TrimEnd(' '); 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); //byte[] temp = { 0xEF, 0xEF, 0x04, 0x01, 0x01, 0xE4 }; clientSocket.Send(aa); // Sends some data //Thread.Sleep(500); var buffer = new byte[9]; clientSocket.Receive(buffer); // Receives some data back (blocks execution) var response = BitConverter.ToString(buffer, 0); msg.Success = true; msg.Message = ip + "设置电池合闸门限电压:" + f + "伏成功" + response; msg.Title = "设置电池合闸门限电压"; } catch (Exception e) { msg.Success = false; msg.Message = ip + ":通讯网络错误!" + e.Message; msg.Title = "设置电池合闸门限电压"; } // clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); clientSocket.Dispose(); } else { msg.Success = false; msg.Message = ip + "设备离线!"; msg.Title = "设置电池合闸门限电压"; } //InertLogs(list.CrsName, msg.Title, msg.Message); return(msg); }
public static void InSertDb() { var para = GetIpList(DbContext.GetSubCode()); foreach (var i in para) { Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipAddress = IPAddress.Parse(i.CrsIp); try { TcpConn.ConnectWithTimeout(clientSocket, new IPEndPoint(ipAddress, 5000), 1000); Console.WriteLine("Socket连接成功"); } catch { UpdateIsOnline(false, i.CrsCode); var model = GetMDevstatusByCrsCode(i.CrsCode, i.SubCode); if (IsExist(i.CrsCode)) { UpdateOnline(model); } else { InsertOnline(model); } Console.WriteLine("Socket连接失败"); //clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); clientSocket.Dispose(); continue; } try { string send = "EF EF 04 14 00 F6"; var msg = Common.StrToHexByte(send); clientSocket.Send(msg); // Sends some data BitConverter.ToString(msg, 0)) var buffer = new byte[103]; clientSocket.Receive(buffer); // Receives some data back (blocks execution) 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 }; UpdateIsOnline(true, i.CrsCode); if (IsExist(i.CrsCode)) { UpdateOnline(model); } else { InsertOnline(model); } } catch { UpdateIsOnline(false, i.CrsCode); var model = GetMDevstatusByCrsCode(i.CrsCode, i.SubCode); if (IsExist(i.CrsCode)) { UpdateOnline(model); } else { InsertOnline(model); } } //clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); clientSocket.Dispose(); } }