public static List <int> GetpipeInfo(int idA, int idB) { MySQLDB.InitDb(); int sensorAID = 0; int sensorBID = 0; int pipeID = 0; int pipeLength = 0; List <int> resultList = new List <int>(); //从数据库中查找当前ID是否存在 try { DataSet ds1 = new DataSet("tsensor"); string strSQL1 = "SELECT SensorID FROM tsensor where (DeviceID=" + idA + " or DeviceID=" + idB + ")"; ds1 = MySQLDB.SelectDataSet(strSQL1, null); if (ds1 != null) { // 有数据集 if (ds1.Tables[0].Rows.Count > 0) { string item0 = (ds1.Tables[0].Rows[0]["SensorID"]).ToString(); if (item0 != null && item0 != "") { sensorAID = (Convert.ToInt32(item0)); } else { sensorAID = 0; } string item1 = (ds1.Tables[0].Rows[1]["SensorID"]).ToString(); if (item1 != null && item1 != "") { sensorBID = (Convert.ToInt32(item1)); } else { sensorBID = 0; } } } //end of if (ds1 != null) if (sensorAID != 0 && sensorBID != 0) { DataSet ds2 = new DataSet("tsensormatch"); string strSQL2 = "SELECT PipeID FROM tsensormatch where ((SensorAID=" + sensorAID + " and SensorBID=" + sensorBID + ") or (SensorAID=" + sensorBID + " and SensorBID=" + sensorAID + "))"; ds2 = MySQLDB.SelectDataSet(strSQL2, null); if (ds2 != null) { // 有数据集 if (ds2.Tables[0].Rows.Count > 0) { string item3 = (ds2.Tables[0].Rows[0]["PipeID"]).ToString(); if (item3 != null && item3 != "") { pipeID = (Convert.ToInt32(item3)); } else { pipeID = 0; } } } } //end of if (sensorAID != 0 && sensorBID != 0) if (pipeID != 0) { DataSet ds3 = new DataSet("tpipe"); string strSQL3 = "SELECT PipeLength FROM tpipe where PipeID=" + pipeID; ds3 = MySQLDB.SelectDataSet(strSQL3, null); if (ds3 != null) { // 有数据集 if (ds3.Tables[0].Rows.Count > 0) { string length = (ds3.Tables[0].Rows[0]["PipeLength"]).ToString(); if (length != null && length != "") { pipeLength = (Convert.ToInt32(length)); } else { pipeLength = 0; } } } } //读数据以存储的经纬度 if (pipeLength == 0) { double lng1, lat1, lng2, lat2; lng1 = lat1 = lng2 = lat2 = 0; DataSet ds4 = new DataSet("tpipe"); string strSQL4 = "SELECT StartLocation, EndLocation FROM tpipe where PipeID=" + pipeID; ds4 = MySQLDB.SelectDataSet(strSQL4, null); if (ds4 != null) { // 有数据集 if (ds4.Tables[0].Rows.Count > 0) { string StartLocation = (ds4.Tables[0].Rows[0]["StartLocation"]).ToString(); if (StartLocation != null && StartLocation != "") { string[] sArray = StartLocation.Split(new char[] { ',' }); lng1 = Convert.ToDouble(sArray[0]); lat1 = Convert.ToDouble(sArray[1]); } else { lng1 = lat1 = 0; } string EndLocation = (ds4.Tables[0].Rows[0]["EndLocation"]).ToString(); if (EndLocation != null && EndLocation != "") { string[] sArray = EndLocation.Split(new char[] { ',' }); lng2 = Convert.ToDouble(sArray[0]); lat2 = Convert.ToDouble(sArray[1]); } else { lng2 = lat2 = 0; } } if ((lng1 != 0) && (lat1 != 0) && (lng2 != 0) && (lat2 != 0)) { pipeLength = (int)GPSDistance.getGpsDistance(lng1, lat1, lng2, lat2); } else { pipeLength = 0; } } } //读设备的经纬度 if (pipeLength == 0) { double lng1, lat1, lng2, lat2; lng1 = lat1 = lng2 = lat2 = 0; DataSet ds5 = new DataSet("tsensorinfo"); string strSQL5 = "SELECT Longitude, Latitude FROM tsensorinfo where intdeviceID=" + idA + " or intdeviceID=" + idB; ds5 = MySQLDB.SelectDataSet(strSQL5, null); if (ds5 != null) { // 有数据集 if (ds5.Tables[0].Rows.Count > 0) { string LongitudeA = (ds5.Tables[0].Rows[0]["Longitude"]).ToString(); string LongitudeB = (ds5.Tables[0].Rows[1]["Longitude"]).ToString(); string LatitudeA = (ds5.Tables[0].Rows[0]["Latitude"]).ToString(); string LatitudeB = (ds5.Tables[0].Rows[1]["Latitude"]).ToString(); if (LongitudeA != "" && LongitudeB != "" && LatitudeA != "" && LatitudeB != "") { lng1 = Convert.ToDouble(LongitudeA); lng2 = Convert.ToDouble(LongitudeB); lat1 = Convert.ToDouble(LatitudeA); lat2 = Convert.ToDouble(LatitudeB); } else { lng1 = lat1 = lng2 = lat2 = 0; } } if ((lng1 != 0) && (lat1 != 0) && (lng2 != 0) && (lat2 != 0)) { pipeLength = (int)GPSDistance.getGpsDistance(lng1, lat1, lng2, lat2); } else { pipeLength = 0; } } } resultList.Add(sensorAID); resultList.Add(sensorBID); resultList.Add(pipeID); resultList.Add(pipeLength); return(resultList); } catch (Exception ex) { return(null); } }
//处理数据和写入数据库 public void AnalyzeData(byte[] datagramBytes) { try { string msg = null; switch (datagramBytes[2]) { case 0x21: if (datagramBytes[7] == 0x00) { msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--读取开启时长和关闭时长成功" + "\n"; Console.WriteLine(msg); //ShowMsg(msg); } if (datagramBytes[7] == 0x01) { msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--设定开启时长和关闭时长成功" + "\n"; Console.WriteLine(msg); //ShowMsg(msg); } break; case 0x22: if (datagramBytes[9] == 0xAA) { msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--AD采样开始" + "\n"; Console.WriteLine(msg); Log.Debug(msg); } if (datagramBytes[9] == 0x55) { status.adStage = AdStage.AdFinished; NetDb.addsensorinfo(intDeviceID, strAddress, strAddress, status.HeartTime.ToString(), Convert.ToInt32(status.clientStage), Convert.ToInt32(status.adStage)); msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--AD采样结束" + "\n"; Console.WriteLine(msg); Log.Debug(msg); if (status.IsCaptureNow == true) { byte[] CmdAD = { 0xA5, 0xA5, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x06, 0x02, 0x57, 0x00, 0x00, 0x03, 0xE8, 0xFF, 0x5A, 0x5A }; status.IsSendDataToServer = true; status.currentsendbulk = 0; status.datalength = 0; status.adStage = AdStage.AdUploading; NetDb.addsensorinfo(intDeviceID, strAddress, strAddress, status.HeartTime.ToString(), Convert.ToInt32(status.clientStage), Convert.ToInt32(status.adStage)); SendCmd(CmdAD); msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--AD采样结束,属于立即采样,马上进行上传" + "\n"; Log.Debug(msg); } } break; case 0x23: if (datagramBytes.Length >= 1007) { if (status.IsSendDataToServer == true) { status.currentsendbulk++; Console.WriteLine("设备号--" + intDeviceID + "第" + status.currentsendbulk + "包\r\n"); if (status.datalength < status.byteAllData.Length) { // copy data to dataItem Array.Copy(datagramBytes, 7, status.byteAllData, status.datalength, 1000); status.datalength += 1000; Log.Debug("设备号--" + intDeviceID + "存储第" + status.currentsendbulk + "包\r\n"); } if (status.currentsendbulk >= 600) { StoreDataToFile(intDeviceID, status.byteAllData); //置状态为上传完毕 status.currentsendbulk = 0; status.IsSendDataToServer = false; msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--数据上传完毕" + "\n"; Console.WriteLine(msg); Log.Debug(msg); status.adStage = AdStage.AdStored; //上传完成 NetDb.addsensorinfo(intDeviceID, strAddress, strAddress, status.HeartTime.ToString(), Convert.ToInt32(status.clientStage), Convert.ToInt32(status.adStage)); } } } break; case 0x25: if (datagramBytes[9] == 0x55) { msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--设定GPS采样时间成功" + "\n"; Console.WriteLine(msg); Log.Debug(msg); NetDb.UpdateSensorCfg(intDeviceID, "IsSetCapTime", 0); //NetDb.UpdateSensorCfg(intDeviceID, "IsCaptureNow", 0); } break; case 0x26: if (datagramBytes[7] == 0x01) { msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--设定开启时长和关闭时长成功" + "\n"; Console.WriteLine(msg); Log.Debug(msg); NetDb.UpdateSensorCfg(intDeviceID, "IsSetOpenAndCloseTime", 0); } break; case 0x27: int[] gpsData = new int[23]; double Latitude = 0; double Longitude = 0; for (int i = 0; i < 23; i++) { gpsData[i] = datagramBytes[9 + i]; } GPSDistance.getGPSData(gpsData, out Latitude, out Longitude); msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--经度为:" + Longitude + "纬度为:" + Latitude + "\n"; Console.WriteLine(msg); Log.Debug(msg); //把经纬度写入数据库;在addsensorcfg后面加上IsGetGPSinfo,hex加到3 if (Longitude > 0 && Latitude > 0) { //保留6位小数 Longitude = (int)(Longitude * 1000000); Longitude = Longitude / 1000000; Latitude = (int)(Latitude * 1000000); Latitude = Latitude / 1000000; NetDb.UpdateSensorGPSinfo(intDeviceID, Longitude, Latitude); } else { NetDb.UpdateSensorGPSinfo(intDeviceID, -1, -1); } //经纬度信息读取成功,把标志位复位 NetDb.UpdateSensorCfg(intDeviceID, "IsGetGpsInfo", 0); Log.Debug("经纬度信息读取成功,把标志位复位"); break; case 0x28: if (datagramBytes[9] == 0x55) { msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--设定立即采样时间成功" + "\n"; Console.WriteLine(msg); Log.Debug(msg); NetDb.UpdateSensorCfg(intDeviceID, "IsCaptureNow", 0); } break; case 0x29: msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--设定服务器IP成功" + "\n"; Console.WriteLine(msg); Log.Debug(msg); NetDb.UpdateSensorCfg(intDeviceID, "IsSetServerIP", 0); break; case 0x30: msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--设定服务器端口号成功" + "\n"; Console.WriteLine(msg); Log.Debug(msg); NetDb.UpdateSensorCfg(intDeviceID, "IsSetServerPort", 0); break; case 0x31: msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--设定AP名称成功" + "\n"; Console.WriteLine(msg); Log.Debug(msg); NetDb.UpdateSensorCfg(intDeviceID, "IsSetApName", 0); break; case 0x32: msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--设定AP密码成功" + "\n"; Console.WriteLine(msg); Log.Debug(msg); NetDb.UpdateSensorCfg(intDeviceID, "IsSetApPassword", 0); break; case 0x33: msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--重连设定完成" + "\n"; Console.WriteLine(msg); Log.Debug(msg); NetDb.UpdateSensorCfg(intDeviceID, "IsReconnect", 0); NetDb.UpdateSensorInfo(intDeviceID, "Status", 0); //接收到8266的重连确认信息后,置设备状态为不在线0 SendCmd(CmdItem.CmdEnterSleep); break; case 0x34: msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "硬件" + strAddress + "设备号--" + intDeviceID + "--进入休眠" + "\n"; Console.WriteLine(msg); Log.Debug(msg); NetDb.UpdateSensorInfo(intDeviceID, "Status", 2); //设备状态为2表示进入休眠, break; case 0xFF: status.clientStage = ClientStage.idle; status.HeartTime = DateTime.Now; NetDb.UpdateSensorInfo(intDeviceID, "Status", Convert.ToInt32(status.clientStage)); NetDb.UpdateSensorInfoWithTime(intDeviceID, "loginTime", status.HeartTime.ToString()); Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "设备号--" + intDeviceID + "收到心跳包\r\n"); Log.Debug("设备号--" + intDeviceID + "收到心跳包\r\n"); break; default: Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "收到其他类型数据\r\n"); Log.Debug("收到其他类型数据"); break; } if (status.IsSendDataToServer == true) { SendCmd(SetADcmd(status.currentsendbulk));//继续发送AD命令 } } catch (Exception e) { Console.WriteLine(e); Log.Debug(e); } }