/// <summary> /// 更新最新记录,根据stationId的唯一性 /// </summary> /// <param name="entity"></param> public bool UpdateRTD(CEntitySoilData entity) { // 先找到ID所在的行 m_mutexDataTable.WaitOne(); for (int i = 0; i < m_dataTable.Rows.Count; ++i) { if (m_dataTable.Rows[i][CS_StationID].ToString().Trim() == entity.StationID.Trim()) { // 找到匹配,直接更新并退出 // this.Invoke(new Action(() => { m_bindingSource.ResumeBinding(); }), null); //this.DataSource = m_bindingSource; m_mutexDataTable.ReleaseMutex(); return(base.UpdateRowData(i, GetUIShowStringList(entity).ToArray(), EDataState.ENormal)); } }//end of for //this.DataSource = m_bindingSource; for (int i = 0; i < m_listEntityRTD.Count; ++i) { if (m_listEntityRTD[i].StationID.Trim() == entity.StationID.Trim()) { //找到匹配更新 m_listEntityRTD[i] = entity; break; } } m_mutexDataTable.ReleaseMutex(); return(false); }
private List <string> GetUIShowStringList(CEntitySoilData entity) { // CEntitySoilStation station = CDBSoilDataMgr.Instance.GetSoilStationInfoByStationId(entity.StationID); //var stationEntity = CDBDataMgr.Instance.GetStationById(station.StationID); CEntitySoilStation stationEntity = CDBSoilDataMgr.Instance.GetSoilStationInfoByStationId(entity.StationID); //var stationEntity = CDBDataMgr.Instance.GetStationById(station.StationID); List <string> result = new List <string>(); result.Add(stationEntity.StationID); result.Add(stationEntity.StationName); result.Add(CEnumHelper.StationTypeToUIStr(stationEntity.StationType)); result.Add(entity.DataTime.ToString()); result.Add(CEnumHelper.MessageTypeToUIStr(entity.MessageType)); result.Add(CEnumHelper.ChannelTypeToUIStr(entity.ChannelType)); result.Add(entity.Voltage10.HasValue ? entity.Voltage10.Value.ToString() : CS_NullUIStr); result.Add(entity.Moisture10.HasValue ? entity.Moisture10.ToString() : CS_NullUIStr); result.Add(entity.Voltage20.HasValue ? entity.Voltage20.ToString() : CS_NullUIStr); result.Add(entity.Moisture20.HasValue ? entity.Moisture20.ToString() : CS_NullUIStr); result.Add(entity.Voltage30.HasValue ? entity.Voltage30.ToString() : CS_NullUIStr); result.Add(entity.Moisture30.HasValue ? entity.Moisture30.ToString() : CS_NullUIStr); result.Add(entity.Voltage40.HasValue ? entity.Voltage40.ToString() : CS_NullUIStr); result.Add(entity.Moisture40.HasValue ? entity.Moisture40.ToString() : CS_NullUIStr); result.Add(entity.Voltage60.HasValue ? entity.Voltage60.ToString() : CS_NullUIStr); result.Add(entity.Moisture60.HasValue ? entity.Moisture60.ToString() : CS_NullUIStr); return(result); }
private void GetUpdatedData() { // 如果标记为删除的就不需要再更新了 List <int> listUpdatedRows = new List <int>(); for (int i = 0; i < m_listEditedRows.Count; ++i) { if (!m_listMaskedDeletedRows.Contains(m_listEditedRows[i])) { // 如果不在删除列中,则需要更新 listUpdatedRows.Add(m_listEditedRows[i]); } } // 获取更新过的数据 for (int i = 0; i < listUpdatedRows.Count; ++i) { CEntitySoilData soildata = new CEntitySoilData(); // voltage.VoltageID = long.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_VoltageID].Value.ToString()); try { soildata.StationID = m_strStaionId; soildata.DataTime = DateTime.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_TimeCollected].Value.ToString()); soildata.reciveTime = DateTime.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_ReciveTime].Value.ToString()); soildata.DVoltage = Decimal.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_Voltage].Value.ToString()); soildata.MessageType = CEnumHelper.UIStrToMesssageType(base.Rows[listUpdatedRows[i]].Cells[CS_MsgType].Value.ToString()); soildata.ChannelType = CEnumHelper.UIStrToChannelType(base.Rows[listUpdatedRows[i]].Cells[CS_ChannelType].Value.ToString()); soildata.Voltage10 = float.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_V10].Value.ToString()); soildata.Moisture10 = float.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_M10].Value.ToString()); soildata.Voltage20 = float.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_V20].Value.ToString()); soildata.Moisture20 = float.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_M20].Value.ToString()); soildata.Voltage40 = float.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_V40].Value.ToString()); soildata.Moisture40 = float.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_M40].Value.ToString()); // soildata.Voltage60 = float.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_V60].Value.ToString()); // soildata.Voltage60 = float.Parse(base.Rows[listUpdatedRows[i]].Cells[CS_V60].Value.ToString()); } #pragma warning disable CS0168 // 声明了变量“e”,但从未使用过 catch (Exception e) #pragma warning restore CS0168 // 声明了变量“e”,但从未使用过 { this.Hide(); MessageBox.Show("请输入正确的墒情数据!"); this.Show(); } // soildata.Moisture10 = Decimal.Parse(); m_listUpdated.Add(soildata); } // 获取删除过的数据 for (int i = 0; i < base.m_listMaskedDeletedRows.Count; ++i) { // m_listDeleteVoltage.Add(long.Parse(base.Rows[m_listMaskedDeletedRows[i]].Cells[CS_VoltageID].Value.ToString())); m_listDeleteSoilDatas_StationId.Add(base.Rows[m_listMaskedDeletedRows[i]].Cells[CS_StationID].Value.ToString()); m_listDeleteSoilDatas_StationDate.Add(base.Rows[m_listMaskedDeletedRows[i]].Cells[CS_TimeCollected].Value.ToString()); } m_listEditedRows.Clear(); //清空此次记录 m_listMaskedDeletedRows.Clear(); //清空标记为删除的记录 }
public CEntityStation GetStationInfoBySoilDataInfo(CEntitySoilData soil) { if (null == soil || String.IsNullOrEmpty(soil.StrDeviceNumber)) { return(null); } return(GetStationInfoByDeviceNum(soil.StrDeviceNumber)); }
//1009gm CT_TableNameOld public List <CEntitySoilData> QueryForMonthTable(string stationId, DateTime date) { List <CEntitySoilData> results = new List <CEntitySoilData>(); string sql = "select * from " + CT_TableName + " where Datatime in('"; for (int j = 0; j < 23; j++) { sql = sql + date + "','"; date = date.AddHours(1); } sql = sql + date + "') and StationID=" + stationId + ";"; SqlDataAdapter adapter = new SqlDataAdapter(sql, CDBManager.GetInstacne().GetConnection()); DataTable dataTableTemp = new DataTable(); adapter.Fill(dataTableTemp); int flag = dataTableTemp.Rows.Count; if (flag == 0) { } else { for (int rowid = 0; rowid < dataTableTemp.Rows.Count; ++rowid) { CEntitySoilData soilData = new CEntitySoilData(); object aa = dataTableTemp.Rows[rowid][CN_Moisture20].ToString(); if (dataTableTemp.Rows[rowid][CN_Moisture10].ToString() != "") { soilData.Moisture10 = float.Parse(dataTableTemp.Rows[rowid][CN_Moisture10].ToString()); } else { soilData.Moisture10 = -1; } if (dataTableTemp.Rows[rowid][CN_Moisture20].ToString() != "") { soilData.Moisture20 = float.Parse(dataTableTemp.Rows[rowid][CN_Moisture20].ToString()); } else { soilData.Moisture20 = -1; } if (dataTableTemp.Rows[rowid][CN_Moisture40].ToString() != "") { soilData.Moisture40 = float.Parse(dataTableTemp.Rows[rowid][CN_Moisture40].ToString()); } else { soilData.Moisture40 = -1; } results.Add(soilData); } } return(results); }
/// <summary> /// 更新实时数据 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool UpdateRTD(CEntitySoilData entity) { // 根据ID进行分中心分发 bool result = (m_dgvAllPage.DataGrid as CDataGridViewSoilRTD).UpdateRTD(entity); // 获取每个站点分中心的ID // CEntityStation station = CDBDataMgr.Instance.GetStationById(entity.StationID); CEntitySoilStation soilstation = CDBSoilDataMgr.Instance.GetSoilStationInfoByStationId(entity.StationID); if (null != soilstation && m_mapSubCenterPage.ContainsKey(soilstation.SubCenterID.Value)) { result = result && (m_mapSubCenterPage[soilstation.SubCenterID.Value].DataGrid as CDataGridViewSoilRTD).UpdateRTD(entity); } return(result); }
/// <summary> /// 添加实时数据 /// </summary> /// <param name="entity"></param> public void AddRTD(CEntitySoilData entity) { // 根据ID进行分中心分发 (m_dgvAllPage.DataGrid as CDataGridViewSoilRTD).AddRTD(entity); // 获取每个站点分中心的ID // CEntityStation station = CDBDataMgr.Instance.GetStationById(entity.StationID); CEntitySoilStation soilstation = CDBSoilDataMgr.Instance.GetSoilStationInfoByStationId(entity.StationID); if (null != soilstation && m_mapSubCenterPage.ContainsKey(soilstation.SubCenterID.Value)) { (m_mapSubCenterPage[soilstation.SubCenterID.Value].DataGrid as CDataGridViewSoilRTD).AddRTD(entity); } //(m_dgvAllPage.DataGrid as CDataGridViewSoilRTD).RecalculateHeaderSize(); //计算表头宽度 }
/// <summary> /// 异步添加一个数据记录 /// </summary> /// <param name="entity"></param> public void AddNewRow(CEntitySoilData entity) { // 记录超过1000条,或者时间超过1分钟,就将当前的数据写入数据库 m_mutexDataTable.WaitOne(); //等待互斥量 DataRow row = m_tableDataAdded.NewRow(); row[CN_StationId] = entity.StationID; row[CN_DataTime] = entity.DataTime.ToString(CDBParams.GetInstance().DBDateTimeFormat); row[CN_Voltage] = entity.DVoltage; row[CN_MessageType] = CEnumHelper.MessageTypeToDBStr(entity.MessageType); row[CN_ChannelType] = CEnumHelper.ChannelTypeToDBStr(entity.ChannelType); row[CN_Voltage10] = entity.Voltage10; row[CN_Moisture10] = entity.Moisture10; row[CN_Voltage20] = entity.Voltage20; row[CN_Moisture20] = entity.Moisture20; row[CN_Voltage30] = entity.Voltage30; row[CN_Moisture30] = entity.Moisture30; row[CN_Voltage40] = entity.Voltage40; row[CN_Moisture40] = entity.Moisture40; row[CN_Voltage60] = entity.Voltage60; row[CN_Moisture60] = entity.Moisture60; row[CN_RecvTime] = entity.reciveTime; row[CN_State] = entity.state; m_tableDataAdded.Rows.Add(row); if (m_tableDataAdded.Rows.Count >= CDBParams.GetInstance().AddBufferMax) { // 如果超过最大值,写入数据库 //Task task = new Task(() => { AddDataToDB(); }); //task.Start(); //m_mutexDataTable.ReleaseMutex(); //Thread.Sleep(10 * 1000); NewTask(() => { AddDataToDB(); }); } else { // 没有超过缓存最大值,开启定时器进行检测,多次调用Start()会导致重新计数 m_addTimer.Start(); } m_mutexDataTable.ReleaseMutex(); }
/// <summary> /// 初始化墒情站最新实时数据 /// </summary> private void InitSoilStationLastData() { List <CEntitySoilStation> _listSoilStation = GetAllSoilStation(); foreach (CEntitySoilStation station in _listSoilStation) { if (m_mapStataionLastData.ContainsKey(station.StationID)) { // 如果已经从文件中读取出来的话, 就不必要查询数据库了 continue; } CEntitySoilData data = null; // 不就是浪费一点空间?小事儿呗 m_proxySoilData.GetLastStationData(station.StationID, out data); if (null != data) { m_mapStataionLastData.Add(station.StationID, data); } } }
private bool ParseData(string msg) { // InvokeMessage("协议。。。 ", "进入函数7"); try { string rawData = msg; if (msg.Contains("$")) { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } msg = data; string sid = msg.Substring(0, 4); string type = msg.Substring(4, 2); #region 1G if (type == "1G") { // InvokeMessage("1: " + DateTime.Now, "函数"); string reportType = msg.Substring(6, 2); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = msg.Substring(8, 2); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { //CEntitySoilData soilStruct = new CEntitySoilData(); //if (Soil.Parse(msg, out soilStruct)) //{ // soilStruct.ChannelType = EChannelType.GPRS; // //soilStruct.ListenPort = this.GetListenPort().ToString(); // //string temp = soilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // //InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); // // 抛出YAC设备墒情事件 // if (null != this.SoilDataReceived) // this.SoilDataReceived.Invoke(null, new YACSoilEventArg() // { // RawData = rawData, // Value = soilStruct // }); //} CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } //1111gm string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } else { //string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" " + rawData, "接收"); } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { CReportStruct report = new CReportStruct(); if (Up.Parse(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); //by LH 10.05 //读取数据 //ModemDataStruct dat = new ModemDataStruct(); //while (this.GetNextData(out dat)) //{ //uint dtuID = 0; //var gprs = FindGprsByUserid(userID); //if (gprs.FindByID(userID, out dtuID)) //{ // //query = gprs.Down.BuildQuery(stationID, cmds, EChannelType.GPRS); // gprs.SendHex(dtuID, bts); //} //InvokeMessage("TRU,modemId: " + dat.m_modemId, "发送"); // InvokeMessage("bts: " + bts, "发送"); // //InvokeMessage("bts.length: " + (ushort)bts.Length, "发送"); // // 发送回执 //InvokeMessage("3: " + DateTime.Now, "函数"); // } //DTUdll.Instance.SendHex(m, bts, (ushort)bts.Length); //byte[] bts = new byte[] { 84, 82, 85, 13, 10 }; //uint m = 1610637336; //InvokeMessage("2: " + DateTime.Now, "函数"); //DTUdll.Instance.SendHex(m, bts, (ushort)bts.Length); // report.Datas[0].Voltge = Decimal.Parse("111111") * (Decimal)0.01; // InvokeMessage(report.Datas[0].Voltge.ToString(), "接收"); if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } // InvokeMessage("12333", "接收"); } else { //string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" " + rawData, "接收"); } } break; case "11": { CReportStruct report = new CReportStruct(); //CReportArtificalWater report = new CReportArtificalWater(); if (Up.Parse_1(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.ReportType = EMessageType.Batch; string temptype = "人工水位"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); //if (this.UpDataReceived != null) this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } } break; default: break; } } else if (reportType == "23") // 人工流量 { CReportStruct report = new CReportStruct(); if (Up.Parse_2(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.ReportType = EMessageType.Batch; string temptype = "人工流量"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } } else if (reportType == "32") // 人工报送水位 { CReportStruct report = new CReportStruct(); report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temptype = "人工报送水位"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); WriteToFileClass writeClass = new WriteToFileClass("RGwater"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); string a1 = rawData.Substring(9, 1); string a2 = rawData.Substring(10, 1); if (a1 == "P" || a1 == "H" || a1 == "K" || a1 == "Z" || a1 == "D" || a1 == "T" || a1 == "M" || a1 == "G" || a1 == "Y" || a1 == "F" || a1 == "R") { if (a2 == "A") { if (rawData.Contains("ST")) { WriteToFileClass writeClass1 = new WriteToFileClass("sharewater"); Thread t1 = new Thread(new ParameterizedThreadStart(writeClass1.WriteInfoToFile)); t1.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); } } } } else if (reportType == "53") // 人工报送时段雨量 日雨量 旬雨量 水库水位 蓄水量 入库流量 出库流量 { CReportStruct report = new CReportStruct(); report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temptype = "人工报送雨量"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); WriteToFileClass writeClass = new WriteToFileClass("RGRain"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); } else if (reportType == "25") { //CEntitySoilData readSoilStruct = new CEntitySoilData(); //if (Soil.Parse(msg, out readSoilStruct)) //{ // readSoilStruct.ChannelType = EChannelType.GPRS; // //readSoilStruct.ListenPort = this.GetListenPort().ToString(); // //string temp = readSoilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // //InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); // // 抛出读墒情事件 // if (null != this.SoilDataReceived) // this.SoilDataReceived.Invoke(null, new YACSoilEventArg() // { // RawData = rawData, // Value = readSoilStruct // }); //} CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else // 下行指令 { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) { InvokeMessage(String.Format("{0,-10} ", "下行指令读取参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = rawData }); } } } } #endregion #region 1K if (type == "1K") { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } //EStationBatchType batchType = station.BatchTranType; //if (batchType == EStationBatchType.EFlash) //{ // CBatchStruct batch = new CBatchStruct(); // if (FlashBatch.Parse(msg, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "Flash批量传输") + rawData, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); // } //} //else if (batchType == EStationBatchType.EUPan) //{ // CBatchStruct batch = new CBatchStruct(); // if (UBatch.Parse(msg, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "U盘批量传输") + rawData, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); // } //} CBatchStruct batch = new CBatchStruct(); if (FlashBatch.Parse(msg, out batch)) { InvokeMessage(String.Format("{0,-10} ", "批量传输") + rawData, "接收"); if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); } } } #endregion #region 1S if (type == "1S") { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) { InvokeMessage(String.Format("{0,-10} ", "下行指令设置参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = rawData }); } } } #endregion } else if (msg.Contains("#")) { CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else { return(false); } return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("GPRS 数据解析出错 !" + msg + "\r\n" + exp.Message); } return(false); }
public bool Parse(string resp, out CEntitySoilData soil, out CReportStruct report) { throw new NotImplementedException(); }
/// <summary> /// 获取某个站点最新的数据,如果没有,lastData = null /// </summary> /// <param name="lastData"></param> /// <returns></returns> public bool GetLastStationData(string stationId, out CEntitySoilData lastData) { lastData = null; string sql = string.Format("select top 1 * from {0} where {1} = {2} order by {3} desc", CT_TableName, CN_StationId, stationId, CN_DataTime); SqlDataAdapter adapter = new SqlDataAdapter(sql, CDBManager.GetInstacne().GetConnection()); DataTable dataTableTmp = new DataTable(); adapter.Fill(dataTableTmp); // 只有一行噻?不然怎么玩?? if (dataTableTmp.Rows.Count == 1) { int rowid = 0; CEntitySoilData data = new CEntitySoilData(); data.StationID = dataTableTmp.Rows[rowid][CN_StationId].ToString(); data.DataTime = DateTime.Parse(dataTableTmp.Rows[rowid][CN_DataTime].ToString()); data.DVoltage = decimal.Parse(dataTableTmp.Rows[rowid][CN_Voltage].ToString()); data.MessageType = CEnumHelper.DBStrToMessageType(dataTableTmp.Rows[rowid][CN_MessageType].ToString()); data.ChannelType = CEnumHelper.DBStrToChannelType(dataTableTmp.Rows[rowid][CN_ChannelType].ToString()); data.Voltage10 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage10]); data.Moisture10 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture10]); data.Voltage20 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage20]); data.Moisture20 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture20]); data.Voltage30 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage30]); data.Moisture30 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture30]); data.Voltage40 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage40]); data.Moisture40 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture40]); data.Voltage60 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage60]); data.Moisture60 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture60]); try { if (dataTableTmp.Rows[rowid][CN_RecvTime].ToString() != "") { data.reciveTime = DateTime.Parse(dataTableTmp.Rows[rowid][CN_RecvTime].ToString()); } } catch (Exception e) { data.reciveTime = DateTime.Now; } try { if (dataTableTmp.Rows[rowid][CN_State].ToString() != "") { data.state = int.Parse(dataTableTmp.Rows[rowid][CN_State].ToString()); } } catch (Exception e) { data.state = 1; } lastData = data; } // Debug.WriteLine(string.Format("查询站点{0}最新墒情数据完成", stationId)); return(true); }
/// <summary> /// 根据起始时间以及结束时间还有站点ID,查询墒情数据 /// </summary> /// <param name="stationId"></param> /// <param name="timeStart"></param> /// <param name="timeEnd"></param> /// <returns></returns> public List <CEntitySoilData> QueryByStationAndTime(string stationId, DateTime timeStart, DateTime timeEnd) { ////传递得参数 //Dictionary<string, object> param = new Dictionary<string, object>(); ////TODO添加datatime转string timeStart timeEnd ////查询条件 //Dictionary<string, string> paramInner = new Dictionary<string, string>(); //paramInner["stationid"] = stationId; ////paramInner["strttime"] = timeStart.ToString("yyyy-MM-dd HH:mm:ss"); //paramInner["strttime"] = timeStart.ToString(); ////paramInner["endtime"] = timeEnd.ToString("yyyy-MM-dd HH:mm:ss"); //paramInner["endtime"] = timeEnd.ToString(); ////返回结果 //List<CEntitySoilData> soildataList = new List<CEntitySoilData>(); ////string suffix = "/subcenter/getSubcenter"; //string url = "http://127.0.0.1:8088/soildata/getSoildata"; //string jsonStr = HttpHelper.SerializeDictionaryToJsonString(paramInner); //param["soildata"] = jsonStr; //try //{ // string resultJson = HttpHelper.Post(url, param); // soildataList = (List<CEntitySoilData>)HttpHelper.JsonToObject(resultJson, new List<CEntitySoilData>()); //} //catch (Exception e) //{ // Debug.WriteLine("查询墒情信息失败"); // throw e; //} //return soildataList; List <CEntitySoilData> results = new List <CEntitySoilData>(); try { string sql = string.Format("select * from {0} where {1} = {2} and {3} between {4} and {5} order by {6} desc", CT_TableName, CN_StationId, stationId, CN_DataTime, DateTimeToDBStr(timeStart), DateTimeToDBStr(timeEnd), CN_DataTime); SqlDataAdapter adapter = new SqlDataAdapter(sql, CDBManager.GetInstacne().GetConnection()); DataTable dataTableTmp = new DataTable(); adapter.Fill(dataTableTmp); for (int rowid = 0; rowid < dataTableTmp.Rows.Count; ++rowid) { CEntitySoilData data = new CEntitySoilData(); data.StationID = dataTableTmp.Rows[rowid][CN_StationId].ToString(); data.DataTime = DateTime.Parse(dataTableTmp.Rows[rowid][CN_DataTime].ToString()); data.DVoltage = decimal.Parse(dataTableTmp.Rows[rowid][CN_Voltage].ToString()); data.MessageType = CEnumHelper.DBStrToMessageType(dataTableTmp.Rows[rowid][CN_MessageType].ToString()); data.ChannelType = CEnumHelper.DBStrToChannelType(dataTableTmp.Rows[rowid][CN_ChannelType].ToString()); data.Voltage10 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage10]); data.Moisture10 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture10]); data.Voltage20 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage20]); data.Moisture20 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture20]); data.Voltage30 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage30]); data.Moisture30 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture30]); data.Voltage40 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage40]); data.Moisture40 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture40]); data.Voltage60 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage60]); data.Moisture60 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture60]); data.reciveTime = DateTime.Parse(dataTableTmp.Rows[rowid][CN_DataTime].ToString()); try { if (dataTableTmp.Rows[rowid][CN_RecvTime].ToString() != "") { data.reciveTime = DateTime.Parse(dataTableTmp.Rows[rowid][CN_RecvTime].ToString()); } } catch (Exception e) { data.reciveTime = DateTime.Now; } try { if (dataTableTmp.Rows[rowid][CN_State].ToString() != "") { data.state = int.Parse(dataTableTmp.Rows[rowid][CN_State].ToString()); } } catch (Exception e) { data.state = 1; } results.Add(data); } } catch (System.Exception ex) { Debug.WriteLine(ex.ToString()); } return(results); }
/// <summary> /// 处理数据的任务入口 /// </summary> /// <param name="args"></param> private void DealSoilData(CSingleSoilDataArgs args) { CEntitySoilData data = new CEntitySoilData(); // if (null == CDBDataMgr.Instance.GetStationById(args.StrStationId)) if (null == GetSoilStationInfoByStationId(args.StrStationId)) { // 也就是收到未知站点的数据 CSystemInfoMgr.Instance.AddInfo(string.Format("收到未知站点{0}墒情数据", args.StrStationId)); return; } // 判断当前数据库配置中是否有站点的墒情配置 CEntitySoilStation _soilStation = GetSoilStationInfoByStationId(args.StrStationId); if (null == _soilStation) { CSystemInfoMgr.Instance.AddInfo(string.Format("站点{0}未配置墒情参数,数据丢弃", args.StrStationId)); return; } data.StationID = args.StrStationId; // data.StrDeviceNumber = _soilStation.StrDeviceNumber; data.DataTime = args.DataTime; data.DVoltage = decimal.Parse(args.Voltage.ToString()); data.ChannelType = args.EChannelType; data.MessageType = args.EMessageType; data.reciveTime = DateTime.Now; data.state = 1; // 计算10cm出的含水率 data.Voltage10 = args.D10Value; data.Moisture10 = CalMoisture(args.D10Value, _soilStation.A10, _soilStation.B10, _soilStation.C10, _soilStation.D10, _soilStation.M10, _soilStation.N10); //data.Moisture10 = CalMoisture(args.D10Value, _soilStation.A10, _soilStation.B10, _soilStation.C10, _soilStation.D10); // 计算20cm处的含水率 data.Voltage20 = args.D20Value; data.Moisture20 = CalMoisture(args.D20Value, _soilStation.A20, _soilStation.B20, _soilStation.C20, _soilStation.D20, _soilStation.M20, _soilStation.N20); //// 计算30cm处的含水率 //data.Voltage30 = args.D30Value; //data.Moisture30 = CalMoisture(args.D30Value, _soilStation.A30, _soilStation.B30, _soilStation.C30, _soilStation.D30); // 计算40cm处的含水率 data.Voltage40 = args.D40Value; data.Moisture40 = CalMoisture(args.D40Value, _soilStation.A40, _soilStation.B40, _soilStation.C40, _soilStation.D40, _soilStation.M40, _soilStation.N40); //// 计算60cm处的含水率 //data.Voltage60 = args.D60Value; //data.Moisture60 = CalMoisture(args.D60Value, _soilStation.A60, _soilStation.B60, _soilStation.C60, _soilStation.D60); // 写入数据库 m_proxySoilData.AddNewRow(data); //if (RecvedRTDSoilData != null) //{ // // 发消息通知界面 // RecvedRTDSoilData.Invoke(this, new CEventSingleArgs<CEntitySoilData>(data)); //} // 更新内存最新数据,便于写入文件 if (m_mapStataionLastData.ContainsKey(args.StrStationId)) { m_mapStataionLastData[args.StrStationId] = data; } else { // 新建一个,进行更新 m_mapStataionLastData.Add(args.StrStationId, data); } }
// 添加一条实时记录 public void AddRTD(CEntitySoilData entity) { base.AddRow(GetUIShowStringList(entity).ToArray(), EDataState.ENormal); m_listEntityRTD.Add(entity); }
/// <summary> /// 根据起始时间以及结束时间还有站点ID,查询墒情数据 /// </summary> /// <param name="stationId"></param> /// <param name="timeStart"></param> /// <param name="timeEnd"></param> /// <returns></returns> public List <CEntitySoilData> QueryByStationAndTime(string stationId, DateTime timeStart, DateTime timeEnd) { List <CEntitySoilData> results = new List <CEntitySoilData>(); try { string sql = string.Format("select * from {0} where {1} = {2} and {3} between {4} and {5} order by {6}", CT_TableName, CN_StationId, stationId, CN_DataTime, DateTimeToDBStr(timeStart), DateTimeToDBStr(timeEnd), CN_DataTime); SqlDataAdapter adapter = new SqlDataAdapter(sql, CDBManager.GetInstacne().GetConnection()); DataTable dataTableTmp = new DataTable(); adapter.Fill(dataTableTmp); for (int rowid = 0; rowid < dataTableTmp.Rows.Count; ++rowid) { CEntitySoilData data = new CEntitySoilData(); data.StationID = dataTableTmp.Rows[rowid][CN_StationId].ToString(); data.DataTime = DateTime.Parse(dataTableTmp.Rows[rowid][CN_DataTime].ToString()); data.DVoltage = decimal.Parse(dataTableTmp.Rows[rowid][CN_Voltage].ToString()); data.MessageType = CEnumHelper.DBStrToMessageType(dataTableTmp.Rows[rowid][CN_MessageType].ToString()); data.ChannelType = CEnumHelper.DBStrToChannelType(dataTableTmp.Rows[rowid][CN_ChannelType].ToString()); data.Voltage10 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage10]); data.Moisture10 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture10]); data.Voltage20 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage20]); data.Moisture20 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture20]); data.Voltage30 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage30]); data.Moisture30 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture30]); data.Voltage40 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage40]); data.Moisture40 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture40]); data.Voltage60 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Voltage60]); data.Moisture60 = GetCellFloatValue(dataTableTmp.Rows[rowid][CN_Moisture60]); data.reciveTime = DateTime.Parse(dataTableTmp.Rows[rowid][CN_DataTime].ToString()); try { if (dataTableTmp.Rows[rowid][CN_RecvTime].ToString() != "") { data.reciveTime = DateTime.Parse(dataTableTmp.Rows[rowid][CN_RecvTime].ToString()); } } #pragma warning disable CS0168 // 声明了变量“e”,但从未使用过 catch (Exception e) #pragma warning restore CS0168 // 声明了变量“e”,但从未使用过 { data.reciveTime = DateTime.Now; } try { if (dataTableTmp.Rows[rowid][CN_State].ToString() != "") { data.state = int.Parse(dataTableTmp.Rows[rowid][CN_State].ToString()); } } #pragma warning disable CS0168 // 声明了变量“e”,但从未使用过 catch (Exception e) #pragma warning restore CS0168 // 声明了变量“e”,但从未使用过 { data.state = 1; } results.Add(data); } } catch (System.Exception ex) { Debug.WriteLine(ex.ToString()); } return(results); }
// 处理通信输出 COUT类型数据 private void DealCOUT(string msg) { InvokeMessage("通信输出 " + msg.Trim(), "接收"); string str = msg.Trim(); if (str.Contains("COUT")) { if (str.Contains("COUT")) { num = num + 1; FileStream fs = new FileStream("numbd.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write(num); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } var cout = Beidou500Helper.GetCOUTInfo(msg); //InvokeMessage("gm1 " + "通过截取", "接收"); if (cout == null) { //InvokeMessage("gm2 " + "通过截取", "接收"); return; } // 解析通信输出中的内容 string content = cout.MsgContent; try { string rawMsg = content; //InvokeMessage("gm3 " + content, "接收"); string reportType = rawMsg.Substring(6, 2); //InvokeMessage("gm4 " + reportType, "接收"); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = rawMsg.Substring(8, 2); //InvokeMessage("gm5 " + stationType, "接收"); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { //var station = FindStationByBeidouID(cout.SenderAddr); //string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G"); //CEntitySoilData soil = new CEntitySoilData(); //CReportStruct soilReport = new CReportStruct(); //if (Soil.Parse(currentMsg, out soil, out soilReport)) //{ // soil.ChannelType = EChannelType.BeiDou; // if (null != this.SoilDataReceived) // this.SoilDataReceived(null, new CEventSingleArgs<CEntitySoilData>(soil)); // if (null != soilReport && null != this.UpDataReceived) // { // soilReport.ChannelType = EChannelType.BeiDou; // soilReport.ListenPort = "COM" + this.Port.PortName; // this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); // } //} // string newMsg = rawMsg.Substring(1, rawMsg.Length - 1); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawMsg, out soil, out soilReport)) { soilReport.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.BeiDou; soilReport.ListenPort = "COM" + this.Port.PortName; soilReport.flagId = cout.SenderAddr; this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); } } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { //var station = FindStationByBeidouID(cout.SenderAddr); //rawMsg = rawMsg.Insert(0, station.StationID + " "); //CReportStruct report = new CReportStruct(); //if (Up.Parse(rawMsg, out report)) //{ // report.ChannelType = EChannelType.BeiDou; // report.ListenPort = "COM" + this.Port.PortName; // if (this.UpDataReceived != null) // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = msg }); //} //string newMsg = rawMsg.Substring(1, rawMsg.Length - 1); CReportStruct report = new CReportStruct(); if (Up.Parse(rawMsg, out report)) { //InvokeMessage("gm6 " + rawMsg, "接收"); //$60131G2201161111040003046112271367 report.ChannelType = EChannelType.BeiDou; report.ListenPort = "COM" + this.Port.PortName; report.flagId = cout.SenderAddr; if (this.UpDataReceived != null) { //InvokeMessage("Cout Test " + rawMsg, "接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawMsg }); } } } break; default: break; } } else if (reportType == "11") // 人工水位 { } else if (reportType == "23") // 人工流量 { } else if (reportType == "25") { var station = FindStationByBeidouID(cout.SenderAddr); string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G"); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(currentMsg, out soil, out soilReport)) { soil.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.BeiDou; soilReport.ListenPort = "COM" + this.Port.PortName; this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("北斗卫星指挥机 数据解析出错 !" + content + "\r\n" + exp.Message); } }
private void Parser_2(object str) { try { string data = str as string; /* 删除 '\r\n' 字符串 */ while (data.StartsWith("\r\n")) { data = data.Substring(2); } /* * 解析数据,获取CGSMStruct */ var gsm = new CGSMStruct(); if (!GsmHelper.Parse_2(data, out gsm)) { return; } /* 如果解析成功,触发GSM数据接收完成事件 */ //string rawdata = ""; //string rawdata1 = gsm.Message; //if (!rawdata1.Contains('$')) //{ // rawdata = "$" + rawdata1; //} InvokeMessage(data, "接收"); string msg = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(gsm.Message, out msg)) { return; } msg = gsm.Message; string rawdata = msg; if (msg.Contains('$')) { msg = msg.Substring(1); } //if (msg.Contains('9')) //{ // msg = msg.Substring(1); //} if (!msg.ToUpper().Contains("TRU")) { string sid = msg.Substring(0, 4); string type = msg.Substring(4, 2); /* * 上行指令信息, * 或者读取参数返回的信息 */ #region 1G if (type == "1G") { string reportType = msg.Substring(6, 2); /* 定时报,加报 */ if (reportType == "21" || reportType == "22") { // YAC设备的墒情协议: string stationType = msg.Substring(8, 2); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawdata, out soil, out soilReport)) { soil.ChannelType = EChannelType.GSM; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } //1111gm string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; //InvokeMessage(String.Format("{0,-10} ", temp) + rawdata, "接收"); if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GSM; soilReport.ListenPort = "COM" + this.ListenPort.PortName; soilReport.flagId = gsm.PhoneNumber; this.UpDataReceived(null, new UpEventArgs() { RawData = rawdata, Value = soilReport }); } } else { //string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" " + rawdata, "接收"); } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { CReportStruct report = new CReportStruct(); if (Up.Parse(msg, out report)) /* 解析成功 */ { report.ChannelType = EChannelType.GSM; report.ListenPort = "COM" + this.ListenPort.PortName; report.flagId = gsm.PhoneNumber; if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = data }); } } } break; default: break; } } else if (reportType == "11") // 人工水位 { } else if (reportType == "23") // 人工流量 { } else if (reportType == "25") { } else /* 下行指令 */ { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) /* 解析成功 */ { if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = data }); } } } } #endregion #region 1K if (type == "1K") /* 批量传输 */ { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点传输类型匹配错误"); } // EStationBatchType batchType = station.BatchTranType; //if (batchType == EStationBatchType.EFlash) /* Flash传输 */ //{ CBatchStruct batch = new CBatchStruct(); if (FlashBatch.Parse(gsm.Message, out batch)) /* 解析成功 */ { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = data }); } } //} //else if (batchType == EStationBatchType.EUPan) /* U盘传输 */ //{ // CBatchStruct batch = new CBatchStruct(); // if (UBatch.Parse(gsm.Message, out batch)) /* 解析成功 */ // { // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = data }); // } //} else { throw new Exception("批量传输,站点传输类型匹配错误"); } } #endregion #region 1S if (type == "1S") /* 远地下行指令,设置参数 */ { CDownConf downconf = new CDownConf(); if (Down.Parse(gsm.Message, out downconf))/* 解析成功 */ { if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = data }); } } } #endregion } else { if (this.ErrorReceived != null) { this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs() { Msg = data }); } } } catch (Exception exp) { Debug.WriteLine(exp.Message); } }
/// <summary> /// 数据格式 /// /// <?xml version="1.0" encoding="utf-8" ?> ///<string xmlns = "http://tempuri.org/" ><? xml version = "1.0" encoding="UTF-8" ?> ///<datalist> ///<data> ///<id>1000</id> ///<uptime>2018-10-09 09:10:10</uptime> ///<destaddr>1064810588860</destaddr> ///<content>检测点上行短信内容测试111</content></data> ///<data><id>1001</id><uptime>2018-10-09 10:31:30</uptime> ///<destaddr>1064810589867</destaddr> ///<content>检测点上行短信内容测试222</content></data></datalist></string> /// /// </summary> /// <returns></returns> private bool DealSMSData(string addr, CGSMStruct gsm) { bool isSoil = FindStationByGSMID(addr); string rawdata = gsm.Message; try { if (isSoil) { // 是墒情站 CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawdata, out soil, out soilReport)) { soil.ChannelType = EChannelType.GSM; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GSM; soilReport.ListenPort = "WebService-GSM"; soilReport.flagId = gsm.PhoneNumber; this.UpDataReceived(null, new UpEventArgs() { RawData = rawdata, Value = soilReport }); } } else { InvokeMessage(" " + rawdata, "接收"); } } else { // 是水情站 CReportStruct report = new CReportStruct(); if (Up.Parse(rawdata, out report)) /* 解析成功 */ { report.ChannelType = EChannelType.GSM; report.ListenPort = "WebService-GSM"; report.flagId = gsm.PhoneNumber; if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawdata }); } } } } catch (Exception) { return(false); } return(true); }
private bool ParseData(string msg, string gprs, uint modemId) { // InvokeMessage("协议。。。 ", "进入函数7"); try { string rawData = msg; string sid; try { sid = Manager.XmlStationData.Instance.GetStationByGprsID(gprs); } catch (Exception e) { Debug.WriteLine("查找站点id失败,赋值为9999" + e.ToString()); sid = "9999"; } if (msg.Contains("$")) { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } msg = data; CReportStruct report = new CReportStruct(); CDownConf downReport = new CDownConf(); string protocol = Manager.XmlStationData.Instance.GetProtocolBySId(sid); if (protocol == "YNXY") { Up = new Data.YNXY.UpParser(); Down = new Data.YNXY.DownParser(); } else if (protocol == "ZYJBX") { Up = new Data.ZYJBX.UpParser(); Down = new Data.ZYJBX.DownParser(); } #region 1K //批量传输解析 if (msg.Contains("1K")) { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } CBatchStruct batch = new CBatchStruct(); InvokeMessage(String.Format("{0,-10} ", "批量传输") + msg, "接收"); if (Down.Parse_Flash(msg, EChannelType.GPRS, out batch)) { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = msg }); } } } #endregion #region 1G if (msg.Contains("1G")) { //if (msg.Contains("RG")) //{ // var lists = msg.Split('$'); // foreach (var segMsg in lists) // { // if (segMsg.Length < 5) // { // continue; // } // string plusMsg = "$" + segMsg; // string rtype = "人工报"; // InvokeMessage("gprs号码: " + gprs + " " + String.Format("{0,-10} ", rtype) + plusMsg, "接收"); // string package = Up.Parse_1(plusMsg, out report); // if (package != "") // { // report.ChannelType = EChannelType.GPRS; // report.ListenPort = this.GetListenPort().ToString(); // report.flagId = gprs; // SendTru(modemId, package); // //TODO 重新定义事件 // if (this.UpDataReceived != null) // { // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = plusMsg }); // } // } // else // { // InvokeMessage(" " + rawData, "接收"); // } // } //} //else if (msg.Contains("1G21") || msg.Contains("1G22") || msg.Contains("1G25")) // 定时报,加报 { if (msg.Contains("1G29")) { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } CBatchStruct batch = new CBatchStruct(); InvokeMessage(String.Format("{0,-10} ", "批量传输") + msg, "接收"); if (Down.Parse_Batch(msg, out batch)) { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = msg }); } } } else { var lists = msg.Split('$'); foreach (var segMsg in lists) { if (segMsg.Length < 5) { continue; } string plusMsg = "$" + segMsg; if (Up.Parse(plusMsg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.flagId = gprs; string rtype = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage("gprs号码: " + gprs + " " + String.Format("{0,-10} ", rtype) + plusMsg, "接收"); //TODO 重新定义事件 if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = plusMsg }); } } else { InvokeMessage(" " + rawData, "接收"); } } } } else { Down.Parse(rawData, out downReport); if (downReport != null) { InvokeMessage(String.Format("{0,-10} ", "下行指令读取参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downReport, RawData = rawData }); } } } #endregion } } else if (msg.Contains("BEG")) { CSDStruct sd = new CSDStruct(); if (Down.Parse_SD(msg, sid, out sd)) { InvokeMessage(String.Format("{0,-10} ", "批量SD传输") + msg, "接收"); if (this.BatchSDDataReceived != null) { this.BatchSDDataReceived.Invoke(null, new BatchSDEventArgs() { Value = sd, RawData = msg }); } } } else if (msg.Contains("#")) { CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else { return(false); } return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("GPRS 数据解析出错 !" + msg + "\r\n" + exp.Message); } return(false); }
/// <summary> /// #S1209110500705141749 000.87001.06000.98000.76000.82 /// #S1209110500705141755 000.67000.96000.90 /// </summary> /// <param name="rawStr"></param> /// <param name="soil"></param> /// <returns></returns> /// public bool Parse(string resp, out CEntitySoilData soil, out CReportStruct report) { soil = null; report = null; report = new CReportStruct(); try { string rawStr = resp.Trim(); if (rawStr.StartsWith("#S1")) { soil = new CEntitySoilData(); // 终端机号 soil.StrDeviceNumber = rawStr.Substring(3, 8); // 数据采集时间 int year = Int32.Parse("20" + rawStr.Substring(11, 2)); int month = Int32.Parse(rawStr.Substring(13, 2)); int day = Int32.Parse(rawStr.Substring(15, 2)); int hour = Int32.Parse(rawStr.Substring(17, 2)); int minute = Int32.Parse(rawStr.Substring(19, 2)); soil.DataTime = new DateTime(year, month, day, hour, minute, 0); // #S1209110500705141749 000.87001.06000.98000.76000.82 if (rawStr.Length.Equals(22 + 30)) { soil.Voltage10 = float.Parse(rawStr.Substring(22, 6)); soil.Voltage20 = float.Parse(rawStr.Substring(28, 6)); soil.Voltage30 = float.Parse(rawStr.Substring(34, 6)); soil.Voltage40 = float.Parse(rawStr.Substring(40, 6)); soil.Voltage60 = float.Parse(rawStr.Substring(46, 6)); } // #S1209110500705141755 000.67000.96000.90 if (rawStr.Length.Equals(22 + 18)) { soil.Voltage10 = float.Parse(rawStr.Substring(22, 6)); soil.Voltage20 = float.Parse(rawStr.Substring(28, 6)); soil.Voltage40 = float.Parse(rawStr.Substring(34, 6)); } return(true); } else if (rawStr.StartsWith("$") && "1G" == rawStr.Substring(5, 2)) { soil = new CEntitySoilData(); string StationID = rawStr.Substring(1, 4); soil.StationID = StationID; // $70521G25142523453124 string cmd = rawStr.Substring(7, 2); if ("25" == cmd) { var now = DateTime.Now; soil.DataTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0); // $70521G25142523453124 if (21 == rawStr.Length) { soil.Voltage10 = float.Parse(rawStr.Substring(9, 4)) / 100; soil.Voltage20 = float.Parse(rawStr.Substring(13, 4)) / 100; soil.Voltage40 = float.Parse(rawStr.Substring(17, 4)) / 100; } else if (29 == rawStr.Length)// $70521G2514252345312412345678 { soil.Voltage10 = float.Parse(rawStr.Substring(9, 4)) / 100; soil.Voltage20 = float.Parse(rawStr.Substring(13, 4)) / 100; soil.Voltage30 = float.Parse(rawStr.Substring(17, 4)) / 100; soil.Voltage40 = float.Parse(rawStr.Substring(21, 4)) / 100; soil.Voltage60 = float.Parse(rawStr.Substring(25, 4)) / 100; } else { return(false); } return(true); } else if ("22" == cmd || "21" == cmd) { // $70521G2217yymmddhhmm12345600011256000100020003 EStationType stationType = EStationType.EHydrology; decimal water = decimal.Parse(rawStr.Substring(21, 6)) / 100; decimal rain = decimal.Parse(rawStr.Substring(27, 4)); decimal volegate = decimal.Parse(rawStr.Substring(31, 4)) / 100; CReportData reportData = new CReportData(); switch (rawStr.Substring(9, 2)) { case "04": stationType = EStationType.ESoil; if ("21" == cmd) { report.ReportType = EMessageType.EAdditional; } if ("22" == cmd) { report.ReportType = EMessageType.ETimed; } break; case "05": stationType = EStationType.ESoilRain; reportData.Rain = rain; reportData.Voltge = volegate; break; case "06": case "16": stationType = EStationType.ESoilWater; reportData.Water = water; reportData.Voltge = volegate; break; case "07": case "17": reportData.Rain = rain; reportData.Water = water; reportData.Voltge = volegate; stationType = EStationType.ESoilHydrology; break; default: throw new Exception(); break; } int year = Int32.Parse("20" + rawStr.Substring(11, 2)); int month = Int32.Parse(rawStr.Substring(13, 2)); int day = Int32.Parse(rawStr.Substring(15, 2)); int hour = Int32.Parse(rawStr.Substring(17, 2)); int minute = Int32.Parse(rawStr.Substring(19, 2)); DateTime collectTime = new DateTime(year, month, day, hour, minute, 0); soil.DataTime = collectTime; //gm 20161030 下1行 soil.DVoltage = volegate; if (47 == rawStr.Length) { // $7052 1G2217 yymmddhhmm 123456 0001 1256 0001 0002 0003 soil.Voltage10 = float.Parse(rawStr.Substring(35, 4)) / 100; soil.Voltage20 = float.Parse(rawStr.Substring(39, 4)) / 100; soil.Voltage40 = float.Parse(rawStr.Substring(43, 4)) / 100; // report = new CReportStruct(); report.Datas = new List <CReportData>(); report.Stationid = StationID; report.StationType = stationType; report.RecvTime = DateTime.Now; reportData.Time = DateTime.Now; report.Datas.Add(reportData); } else if (47 + 8 == rawStr.Length) { // $70521G2217yymmddhhmm123456 0001 1256 0001 0002 0003 0004 0005 soil.Voltage10 = float.Parse(rawStr.Substring(35, 4)) / 100; soil.Voltage20 = float.Parse(rawStr.Substring(39, 4)) / 100; soil.Voltage30 = float.Parse(rawStr.Substring(43, 4)) / 100; soil.Voltage40 = float.Parse(rawStr.Substring(47, 4)) / 100; soil.Voltage60 = float.Parse(rawStr.Substring(51, 4)) / 100; report = new CReportStruct(); report.Stationid = StationID; report.StationType = stationType; report.RecvTime = DateTime.Now; reportData.Time = DateTime.Now; report.Datas = new List <CReportData>(); report.Datas.Add(reportData); } else { return(false); } return(true); } } } catch (Exception exp) { } return(false); }
private bool ParseData(string msg, string gprs) { // InvokeMessage("协议。。。 ", "进入函数7"); try { string rawData = msg; if (msg.Contains("$")) { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } msg = data; string sid = msg.Substring(0, 4); string type = msg.Substring(4, 2); #region 1G if (type == "1G") { string reportType = msg.Substring(6, 2); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = msg.Substring(8, 2); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { //CEntitySoilData soilStruct = new CEntitySoilData(); //if (Soil.Parse(msg, out soilStruct)) //{ // soilStruct.ChannelType = EChannelType.GPRS; // //soilStruct.ListenPort = this.GetListenPort().ToString(); // //string temp = soilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // //InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); // // 抛出YAC设备墒情事件 // if (null != this.SoilDataReceived) // this.SoilDataReceived.Invoke(null, new YACSoilEventArg() // { // RawData = rawData, // Value = soilStruct // }); //} CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } //1111gm string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" gprs号码: " + gprs + String.Format(" {0,-10} ", temp) + rawData, "接收"); if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); soilReport.flagId = gprs; this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } else { //string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" gprs号码: " + gprs + " " + rawData, "接收"); } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { CReportStruct report = new CReportStruct(); if (Up.Parse(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.flagId = gprs; string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" gprs号码: " + gprs + String.Format(" {0,-10} ", temp) + rawData, "接收"); if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } // InvokeMessage("12333", "接收"); } else { //string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" gprs号码: " + gprs + " 可疑数据 " + rawData, "接收"); } } break; case "11": { CReportStruct report = new CReportStruct(); //CReportArtificalWater report = new CReportArtificalWater(); if (Up.Parse_1(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.ReportType = EMessageType.Batch; string temptype = "人工水位"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); //if (this.UpDataReceived != null) this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } } break; default: break; } } else if (reportType == "23") // 人工流量 { CReportStruct report = new CReportStruct(); if (Up.Parse_2(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.ReportType = EMessageType.Batch; string temptype = "人工流量"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } } else if (reportType == "32") // 人工报送水位 { CReportStruct report = new CReportStruct(); report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temptype = "人工报送水位"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); WriteToFileClass writeClass = new WriteToFileClass("RGwater"); //Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); //t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); string a1 = rawData.Substring(9, 1); string a2 = rawData.Substring(10, 1); if (a1 == "P" || a1 == "H" || a1 == "K" || a1 == "Z" || a1 == "D" || a1 == "T" || a1 == "M" || a1 == "G" || a1 == "Y" || a1 == "F" || a1 == "R") { if (a2 == "A") { if (rawData.Contains("ST")) { WriteToFileClass writeClass1 = new WriteToFileClass("sharewater"); Thread t1 = new Thread(new ParameterizedThreadStart(writeClass1.WriteInfoToFile)); t1.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); } } } } else if (reportType == "53") // 人工报送时段雨量 日雨量 旬雨量 水库水位 蓄水量 入库流量 出库流量 { CReportStruct report = new CReportStruct(); report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temptype = "人工报送雨量"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); WriteToFileClass writeClass = new WriteToFileClass("RGRain"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); } else if (reportType == "25") { //CEntitySoilData readSoilStruct = new CEntitySoilData(); //if (Soil.Parse(msg, out readSoilStruct)) //{ // readSoilStruct.ChannelType = EChannelType.GPRS; // //readSoilStruct.ListenPort = this.GetListenPort().ToString(); // //string temp = readSoilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // //InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); // // 抛出读墒情事件 // if (null != this.SoilDataReceived) // this.SoilDataReceived.Invoke(null, new YACSoilEventArg() // { // RawData = rawData, // Value = readSoilStruct // }); //} CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); soilReport.flagId = gprs; this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else // 下行指令 { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) { InvokeMessage(String.Format("{0,-10} ", "下行指令读取参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = rawData }); } } } } #endregion #region 1K if (type == "1K") { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } //EStationBatchType batchType = station.BatchTranType; //if (batchType == EStationBatchType.EFlash) //{ // CBatchStruct batch = new CBatchStruct(); // if (FlashBatch.Parse(msg, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "Flash批量传输") + rawData, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); // } //} //else if (batchType == EStationBatchType.EUPan) //{ // CBatchStruct batch = new CBatchStruct(); // if (UBatch.Parse(msg, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "U盘批量传输") + rawData, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); // } //} CBatchStruct batch = new CBatchStruct(); if (FlashBatch.Parse(msg, out batch)) { InvokeMessage(String.Format("{0,-10} ", "批量传输") + rawData, "接收"); if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); } } } #endregion #region 1S if (type == "1S") { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) { InvokeMessage(String.Format("{0,-10} ", "下行指令设置参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = rawData }); } } } #endregion } else if (msg.Contains("#")) { CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); soilReport.flagId = gprs; this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else { return(false); } return(true); } catch (Exception exp) { //System.Diagnostics.Debug.WriteLine("GPRS 数据解析出错 !" + msg + "\r\n" + exp.Message); } return(false); }
// 处理COUT类型数据 private void DealCOUT(string msg) // private String DealCOUT(string msg) { string result = null; InvokeMessage("通信输出 " + msg, "接收"); SendBackTTCAString = msg; if (msg.Contains("COUT")) { if (msg.Contains("COUT")) { num = num + 1; FileStream fs = new FileStream("numbd.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write(num); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } // InvokeString(msg); var cout = BeidouHelper.GetCOUTInfo(msg); if (cout == null) { return; } // return result; // 发送COSS指令 // 必须在1秒内发送给卫星终端,否则会重新发送“通信输出” var coss = new CCOSSStruct(); coss.SuccessStatus = true; // 终端接收到外设通信申请,并校验成功 SendCOSS(coss); // 发送CACA指令 // 每隔一分钟发送回执$CACA // 65秒左右发一条 var caca = new CCACAStruct(); caca.SenderID = "1"; // 发信方ID为1,表示本机ID,默认 caca.RecvType = cout.SenderType; // 回执的收信方类型 == 通信输出中的发信方类型 caca.RecvAddr = cout.SenderAddr; // 回执的收信方地址 == 通信输出中的发信方地址 caca.Requirements = "1"; // 不保密 caca.ReceiptMsgSequenceNum = cout.MsgSequenceNum; // 回执的报文顺序号 == 通信输出中的报文顺序号 caca.ReceiptContent = "1"; SendCACA(caca); // 解析通信输出中的内容 string content = cout.MsgContent; //通信输出gm8 $60131G2201161111040003046112271367 try { string rawMsg = content; string sid = null; string dealMsg = ProtocolHelpers.dealBCD(rawMsg); WriteToFileClass writeClass = new WriteToFileClass("ReceivedLog"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("COUT Message: " + " " + dealMsg + "\r\n"); string bdid = cout.SenderAddr; //InvokeMessage("gm3 " + content, "接收"); //sid需要根据北斗卫星号获取 try { sid = Manager.XmlStationDataSerializer.Instance.GetStationByBDID(bdid); } catch (Exception e) { sid = "5712"; Debug.WriteLine("获取站号失败" + e.Message); } EMessageType type = ProtocolMaps.MessageTypeMap.FindKey(dealMsg.Substring(0, 2)); string reportType = dealMsg.Substring(0, 2); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = dealMsg.Substring(2, 2); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { // var station = FindStationByBeidouID(cout.SenderAddr); // string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G"); // CEntitySoilData soil = new CEntitySoilData(); // CReportStruct soilReport = new CReportStruct(); // if (Soil.Parse(currentMsg, out soil, out soilReport)) // { // soil.ChannelType = EChannelType.BeiDou; // if (null != this.SoilDataReceived) // this.SoilDataReceived(null, new CEventSingleArgs<CEntitySoilData>(soil)); // if (null != soilReport && null != this.UpDataReceived) // { // soilReport.ChannelType = EChannelType.BeiDou; // soilReport.ListenPort = "COM" + this.Port.PortName; // this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); // } // } //} //1111gm string newMsg = dealMsg.Substring(1, dealMsg.Length - 1); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(newMsg, out soil, out soilReport)) { soilReport.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = "COM" + this.Port.PortName; soilReport.flagId = bdid; this.UpDataReceived(null, new UpEventArgs() { RawData = newMsg, Value = soilReport }); } } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { //1111gm //string newMsg = dealMsg.Substring(1, dealMsg.Length - 1); //CReportStruct report = new CReportStruct(); //if (Up.Parse(newMsg, out report)) //{ // //6013 $60131G2201161111040003046112271367 // report.ChannelType = EChannelType.BeiDou; // report.ListenPort = "COM" + this.Port.PortName; // if (this.UpDataReceived != null) // { // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = newMsg }); // } //} CReportStruct report = new CReportStruct(); UpParser Up1 = new UpParser(); if (Up1.Parse_beidou(sid, type, rawMsg, out report)) { //InvokeMessage("gm6 " + rawMsg, "接收"); //$60131G2201161111040003046112271367 report.ChannelType = EChannelType.BeiDou; report.ListenPort = "COM" + this.Port.PortName; report.flagId = bdid; if (this.UpDataReceived != null) { //InvokeMessage("gm7 " + rawMsg, "接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawMsg }); } } } break; default: break; } } else if (reportType == "11") // 人工水位 { } else if (reportType == "23") // 人工流量 { } else if (reportType == "25") { var station = FindStationByBeidouID(cout.SenderAddr); string currentMsg = dealMsg.Insert(0, "$" + station.StationID + "1G"); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(currentMsg, out soil, out soilReport)) { soil.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.BeiDou; soilReport.ListenPort = "COM" + this.Port.PortName; soilReport.flagId = bdid; this.UpDataReceived(null, new UpEventArgs() { RawData = dealMsg, Value = soilReport }); } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("Beidou 数据解析出错 !" + content + "\r\n" + exp.Message); } //result = msg; //return result; }
// 处理通信输出 COUT类型数据 public void DealCOUT(string msg) { InvokeMessage("通信输出 " + msg, "接收"); string str = msg.Trim(); if (str.Contains("COUT")) { if (str.Contains("COUT")) { num = num + 1; FileStream fs = new FileStream("numbd.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write(num); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } var cout = Beidou500Helper.GetCOUTInfo(msg); //InvokeMessage("gm1 " + "通过截取", "接收"); if (cout == null) { //InvokeMessage("gm2 " + "通过截取", "接收"); return; } //TODO //需要处理dealbcd码的走这里 // 解析通信输出中的内容 string content = cout.MsgContent; UpParser up = new UpParser(); List <CUpReport> reports = new List <CUpReport>(); //TODO //添加调用代码 try { string rawMsg = content; string sid = null; string dealMsg = ProtocolHelpers.dealBCD(rawMsg); WriteToFileClass writeClass = new WriteToFileClass("ReceivedLog"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("COUT Message: " + " " + dealMsg + "\r\n"); //InvokeMessage("gm3 " + content, "接收"); //sid需要根据北斗卫星号获取 try { string bdid = cout.SenderAddr; sid = Manager.XmlStationDataSerializer.Instance.GetStationByBDID(bdid); } catch (Exception e) { sid = "5712"; Debug.WriteLine("获取站号失败" + e.Message); } EMessageType type = ProtocolMaps.MessageTypeMap.FindKey(dealMsg.Substring(0, 2)); string reportType = dealMsg.Substring(0, 2); //InvokeMessage("gm4 " + reportType, "接收"); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = dealMsg.Substring(2, 2); //InvokeMessage("gm5 " + stationType, "接收"); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { //var station = FindStationByBeidouID(cout.SenderAddr); //string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G"); //CEntitySoilData soil = new CEntitySoilData(); //CReportStruct soilReport = new CReportStruct(); //if (Soil.Parse(currentMsg, out soil, out soilReport)) //{ // soil.ChannelType = EChannelType.BeiDou; // if (null != this.SoilDataReceived) // this.SoilDataReceived(null, new CEventSingleArgs<CEntitySoilData>(soil)); // if (null != soilReport && null != this.UpDataReceived) // { // soilReport.ChannelType = EChannelType.BeiDou; // soilReport.ListenPort = "COM" + this.Port.PortName; // this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); // } //} // string newMsg = rawMsg.Substring(1, rawMsg.Length - 1); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawMsg, out soil, out soilReport)) { soilReport.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = "COM" + this.Port.PortName; this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); } } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { //var station = FindStationByBeidouID(cout.SenderAddr); //rawMsg = rawMsg.Insert(0, station.StationID + " "); //CReportStruct report = new CReportStruct(); //if (Up.Parse(rawMsg, out report)) //{ // report.ChannelType = EChannelType.BeiDou; // report.ListenPort = "COM" + this.Port.PortName; // if (this.UpDataReceived != null) // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = msg }); //} //string newMsg = rawMsg.Substring(1, rawMsg.Length - 1); CReportStruct report = new CReportStruct(); UpParser Up1 = new UpParser(); if (Up1.Parse_beidou(sid, type, rawMsg, out report)) { //InvokeMessage("gm6 " + rawMsg, "接收"); //$60131G2201161111040003046112271367 report.ChannelType = EChannelType.BeiDou; report.ListenPort = "COM" + this.Port.PortName; if (this.UpDataReceived != null) { //InvokeMessage("gm7 " + rawMsg, "接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawMsg }); } } } break; default: break; } } else if (reportType == "11") // 人工水位 { } else if (reportType == "23") // 人工流量 { } else if (reportType == "25") { var station = FindStationByBeidouID(cout.SenderAddr); string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G"); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(currentMsg, out soil, out soilReport)) { soil.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.BeiDou; soilReport.ListenPort = "COM" + this.Port.PortName; this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("北斗卫星指挥机 数据解析出错 !" + content + "\r\n" + exp.Message); } }