//TagPack => bytes public static byte[] ParseTagPack(TagPack tpk) { //数据包格式:0xfd(1byte)+0x01(1byte)+ID(2byte)+Type(1byte)+RD(2byte)+Sig(1byte)+Batt(1byte)+NoExe(2byte)+Sleep(2byte)+ReportDT(7byte)+index(1byte)+cs(1byte)+0xfc(1byte) byte[] bytes = new byte[tagpacklen]; bytes[0] = 0xfd; bytes[1] = 0x01; System.Buffer.BlockCopy(tpk.TD, 0, bytes, 2, 2); bytes[4] = tpk.isAlarm; System.Buffer.BlockCopy(tpk.RD_New, 0, bytes, 5, 2); bytes[7] = tpk.SigStren; bytes[8] = tpk.Bat; bytes[9] = (byte)(tpk.ResTime >> 8); bytes[10] = (byte)(tpk.ResTime); bytes[11] = (byte)(tpk.Sleep >> 8); bytes[12] = (byte)(tpk.Sleep); //年(2byte)+月(1byte)+日(1byte)+时(1byte)+分(1byte)+秒(1byte) bytes[13] = (byte)(tpk.ReportTime.Year >> 8); bytes[14] = (byte)(tpk.ReportTime.Year); bytes[15] = (byte)tpk.ReportTime.Month; bytes[16] = (byte)tpk.ReportTime.Day; bytes[17] = (byte)tpk.ReportTime.Hour; bytes[18] = (byte)tpk.ReportTime.Minute; bytes[19] = (byte)tpk.ReportTime.Second; bytes[20] = tpk.index; bytes[21] = 0; for (int i = 0; i < 21; i++) { bytes[21] += bytes[i]; } bytes[22] = 0xfc; return(bytes); }
//得到文件 public static void GetRecord(DateTime startdt, DateTime enddt, string strtagid, string strfile, ref List <TagPack> tags) { string strcurid = ""; DateTime curdatetime; lock (obj_lock) { savefstr = new FileStream(strfile, FileMode.Open, FileAccess.Read); byte[] bytes = new byte[tagpacklen]; TagPack tpk = null; int place = 1; int curlen = savefstr.Read(bytes, 0, bytes.Length); //每次读取的字节数必须为tagpacklen长度 while (curlen >= tagpacklen) { //0xfd(1byte)+0x01(1byte)+ID(3byte)+Type(1byte)+RD(3byte)+Sig(1byte)+Batt(1byte)+NoExe(2byte)+Sleep(2byte)+ReportDT(7byte)+index(1byte)+cs(1byte)+0xfc(1byte) strcurid = bytes[2].ToString("X2") + bytes[3].ToString("X2"); curdatetime = new DateTime((bytes[13] << 8 | bytes[14]), bytes[15], bytes[16], bytes[17], bytes[18], bytes[19]); if (strcurid.Equals(strtagid) && DateTime.Compare(startdt, curdatetime) < 0 && DateTime.Compare(enddt, curdatetime) > 0) { tpk = new TagPack(); tpk.TD[0] = bytes[2]; tpk.TD[1] = bytes[3]; tpk.isAlarm = bytes[4]; tpk.RD_New[0] = bytes[5]; tpk.RD_New[1] = bytes[6]; tpk.SigStren = bytes[7]; tpk.Bat = bytes[8]; tpk.ResTime = (int)(bytes[9] << 8 | bytes[10]); tpk.Sleep = (int)(bytes[11] << 8 | bytes[12]); tpk.ReportTime = new DateTime((bytes[13] << 8 | bytes[14]), bytes[15], bytes[16], bytes[17], bytes[18], bytes[19]); tpk.index = bytes[20]; tags.Add(tpk); } savefstr.Seek(tagpacklen * place, SeekOrigin.Begin); curlen = savefstr.Read(bytes, 0, bytes.Length); place++; } if (null != savefstr) { savefstr.Close(); } savefstr = null; } }
/// <summary> /// 开始按钮,从原始资料中取出时间段的数据包数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void StartBtn_Click(object sender, EventArgs e) { if (StrStart.Equals(StartBtn.Text)) { //获取设置的年月日,并判断其是否符合条件 int syear, smonth, sday, eyear, emonth, eday, shour, sminute, ehour, eminute; //tag的ID或名称 string strtaginfor = TrackTagText.Text.ToUpper().Trim(); if ("".Equals(strtaginfor)) { MessageBox.Show("軌跡分析的Tag信息不能為空!"); return; } Tag tag_name = CommonBoxOperation.GetTagFromName(strtaginfor); if (null != tag_name) { strtaginfor = tag_name.ID[0].ToString("X2") + tag_name.ID[1].ToString("X2"); } DateTime StartDT, EndDT; StartDT = StartDTPicker.Value; EndDT = EndDTPicker.Value; syear = StartDT.Year; smonth = StartDT.Month; sday = StartDT.Day; eyear = EndDT.Year; emonth = EndDT.Month; eday = EndDT.Day; try { shour = Convert.ToInt32(sHourCb.Text); sminute = Convert.ToInt32(sMinitueCb.Text); ehour = Convert.ToInt32(eHourCB.Text); eminute = Convert.ToInt32(eMinitueCB.Text); } catch (Exception) { MessageBox.Show("對不起,設置的開始時間或結束時間有誤!"); return; } //获取开始时间和结束时间 StartDT = new DateTime(syear, smonth, sday, shour, sminute, 0); EndDT = new DateTime(eyear, emonth, eday, ehour, eminute, 59); //小于零t1早于t2 if (DateTime.Compare(StartDT, EndDT) > 0) { MessageBox.Show("結束日期時間應該在開始日期時間之後!"); return; } //取出原始记录信息 string strrecorddir = Environment.CurrentDirectory + @"\Record"; if (!Directory.Exists(strrecorddir)) { MessageBox.Show("對不起,沒有任何記錄!"); return; } string[] StrDirs = FileOperation.GetAllDirName(strrecorddir); if (null == StrDirs || StrDirs.Length <= 0) { MessageBox.Show("對不起,沒有任何記錄!"); return; } if (null != DrawIMG.Frm && DrawIMG.Frm.MyUdpClient != null) { MessageBox.Show("對不起,查詢歷史軌跡時需要關閉網絡連接!"); return; } string strdirname = "", strcuryear = "", strcurmonth = "", strcurday = "", strcurhour = ""; int index = -1, curyear = -1, curmonth = -1, curday = -1, curhour = -1; DateTime curdt; PickerTagsList.Clear(); //判断年月日是否符合条件 foreach (string dir in StrDirs) { index = dir.LastIndexOf("\\"); strdirname = dir.Substring(index + 1, 8); strcuryear = strdirname.Substring(0, 4); strcurmonth = strdirname.Substring(4, 2); strcurday = strdirname.Substring(6, 2); try { curyear = Convert.ToInt32(strcuryear); curmonth = Convert.ToInt32(strcurmonth); curday = Convert.ToInt32(strcurday); } catch (Exception) { continue; } //文件创建的时间 curdt = new DateTime(curyear, curmonth, curday); DateTime startdt = new DateTime(StartDT.Year, StartDT.Month, StartDT.Day); DateTime enddt = new DateTime(EndDT.Year, EndDT.Month, EndDT.Day); //判断文件创建时间是否在指定的范围内 if (DateTime.Compare(curdt, startdt) < 0 || DateTime.Compare(curdt, enddt) > 0) { continue; } //获取到当前时间的小时部分 try { string[] StrFiles = Directory.GetFiles(dir); foreach (string str in StrFiles) { strcurhour = str.Substring(str.LastIndexOf("\\") + 1, 2); try { curhour = Convert.ToInt32(strcurhour); } catch (Exception) { continue; } //此时判断小时是否满足要求 curdt = new DateTime(curdt.Year, curdt.Month, curdt.Day, curhour, 0, 0); startdt = new DateTime(startdt.Year, startdt.Month, startdt.Day, shour, 0, 0); enddt = new DateTime(enddt.Year, enddt.Month, enddt.Day, ehour, 0, 0); if (DateTime.Compare(curdt, startdt) < 0 || DateTime.Compare(curdt, enddt) > 0) { continue; } //记录年月日时都满足要求 startdt = new DateTime(startdt.Year, startdt.Month, startdt.Day, startdt.Hour, sminute, 0); enddt = new DateTime(enddt.Year, enddt.Month, enddt.Day, enddt.Hour, eminute, 0); RecordOperation.GetRecord(startdt, enddt, strtaginfor, str, ref PickerTagsList); } } catch (System.IO.IOException) { MessageBox.Show("對不起,文件流已经被占用。請先關閉連接監控..."); } catch (Exception ex) { FileOperation.WriteLog("获取原始记录时出现异常,异常异常原因:" + ex.ToString()); } } List <TagPack> deletagpacks = new List <TagPack>(); TagPack temptpk = null; //清除掉两个序列号相同的Tag数据包 for (int i = 0; i < PickerTagsList.Count; i++) { if (i + 1 < PickerTagsList.Count) { if (PickerTagsList[i].index == PickerTagsList[i + 1].index) { if (PickerTagsList[i].SigStren < PickerTagsList[i + 1].SigStren) {//交换位置 temptpk = CommonBoxOperation.CloneTagPack(PickerTagsList[i]); PickerTagsList[i] = PickerTagsList[i + 1]; PickerTagsList[i + 1] = temptpk; } deletagpacks.Add(PickerTagsList[i]); } } } foreach (TagPack deletp in deletagpacks) { PickerTagsList.Remove(deletp); } //此时所有符合添加的数据包都添加到PickerTags集合中处了 TrackListBX.Items.Clear(); foreach (TagPack tpk in PickerTagsList) { TrackListBX.Items.Add( tpk.ReportTime + " TagID:" + tpk.TD[0].ToString("X2") + tpk.TD[1].ToString("X2") + " RouterID:" + tpk.RD_New[0].ToString("X2") + tpk.RD_New[1].ToString("X2") + " 是否警報:" + (tpk.isAlarm == 0x04 ? "yes" : "no") + " 信號強度:" + tpk.SigStren.ToString() + " 電量:" + tpk.Bat.ToString() + " 休眠時間:" + (tpk.Sleep / 10).ToString() + " 未移動時間:" + tpk.ResTime.ToString() + " 序列號:" + tpk.index.ToString()); } recordnumlabel.Text = "總記錄數為:" + PickerTagsList.Count; if (PickerTagsList.Count <= 0) { MessageBox.Show("該Tag的原始資料不存在!"); return; } //图形模式画图地图 StartDTe = new DateTime(StartDT.Year, StartDT.Month, StartDT.Day, StartDT.Hour, StartDT.Minute, StartDT.Second); EndDTe = new DateTime(EndDT.Year, EndDT.Month, EndDT.Day, EndDT.Hour, EndDT.Minute, EndDT.Second); Start(); } else { Stop(); recordnumlabel.Text = "總記錄數為:0"; return; } }
/// <summary> /// 查找按钮,用于查找Tag和设备讯息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SearchBtn_Click(object sender, EventArgs e) { String StrIDorName = TagIDorNameTB.Text.ToUpper(); if ("".Equals(StrIDorName)) { MessageBox.Show("設備的ID或名稱不能為空!"); return; } if (tagrb.Checked) { #region 查找Tag设备 PersonOperation curpersonoper = new PersonOperation(frm.CurPerson.ID, OperType.SearchTag); CommonCollection.personOpers.Add(curpersonoper); TagPack MyTag = null; MyTag = SysParam.GetPackTag(StrIDorName); if (null == MyTag) { MyTag = SysParam.GetPackTag_Name(StrIDorName); } if (null == MyTag) { MessageBox.Show("查詢" + StrIDorName + "的Tag設備不存在!"); return; } Area curarea = CommonBoxOperation.GetAreaFromRouterID(MyTag.RD_New[0].ToString("X2") + MyTag.RD_New[1].ToString("X2")); if (null == curarea) { MessageBox.Show("對不起,卡片所在的區域不存在!"); return; } try { SysParam.isTracking = true; MyAllRegInfoWin = new AllRegInfoWin(frm, SpeceilAlarm.PersonHelp, MyTag.TD[0].ToString("X2") + MyTag.TD[1].ToString("X2")); MyAllRegInfoWin.ShowDialog(); } catch (Exception ex) { FileOperation.WriteLog("搜索Tag設備出現異常!異常原因:" + ex.ToString()); } finally { this.Close(); } #endregion } else if (rfrb.Checked) { #region 查找Refer设备 PersonOperation curpersonoper = new PersonOperation(frm.CurPerson.ID, OperType.SearchRefer); CommonCollection.personOpers.Add(curpersonoper); string strareaid = ""; BasicRouter mrt = null; foreach (KeyValuePair <string, Area> marea in CommonCollection.Areas) { if (null == marea.Value) { continue; } marea.Value.AreaRouter.TryGetValue(StrIDorName, out mrt); if (mrt != null) { strareaid = marea.Key; break; } foreach (KeyValuePair <string, BasicRouter> router in marea.Value.AreaRouter) { if (null == router.Value) { continue; } if (StrIDorName.Equals(router.Value.Name)) { mrt = router.Value; break; } } if (null != mrt) { strareaid = marea.Key; break; } } if (null == strareaid || "".Equals(strareaid)) { MessageBox.Show("查詢" + StrIDorName + @"的參考點設備不存在!"); return; } RegInfoWin myregwin = new RegInfoWin(frm, strareaid); myregwin.ShowDialog(); this.Close(); #endregion } else if (ndrb.Checked) { #region 查找Node设备 PersonOperation curpersonoper = new PersonOperation(frm.CurPerson.ID, OperType.SearchNode); CommonCollection.personOpers.Add(curpersonoper); string strareaid = ""; BasicNode mnd = null; foreach (KeyValuePair <string, Area> marea in CommonCollection.Areas) { if (null == marea.Value) { continue; } marea.Value.AreaNode.TryGetValue(StrIDorName, out mnd); if (mnd != null) { strareaid = marea.Key; break; } foreach (KeyValuePair <string, BasicNode> node in marea.Value.AreaNode) { if (null == node.Value) { continue; } if (StrIDorName.Equals(node.Value.Name)) { mnd = node.Value; break; } } if (null != mnd) { strareaid = marea.Key; break; } } if (null == strareaid || "".Equals(strareaid)) { MessageBox.Show("查詢" + StrIDorName + @"的數據節點設備不存在!"); return; } RegInfoWin myregwin = new RegInfoWin(frm, strareaid); myregwin.ShowDialog(); this.Close(); #endregion } }
private void MyTimer_Tick(object obj, EventArgs e) { if (null == br) { return; } string StrTagID = ""; if (null == DeleList) { DeleList = new ArrayList(); } DeleList.Clear(); //将当前的Router 周围的参考点刷新到列表中 foreach (ListViewItem item in RouterTaglistView.Items) { StrTagID = item.Name; TagPack MyTagPack = null; if (!CommonCollection.TagPacks.TryGetValue(StrTagID, out MyTagPack)) { DeleList.Add(StrTagID); continue; } if (MyTagPack.RD_New[0] != br.ID[0] || MyTagPack.RD_New[1] != br.ID[1]) { DeleList.Add(StrTagID); } } //清除不应该在列表中的项 foreach (string str in DeleList) { RouterTaglistView.Items.RemoveByKey(str); } ListViewItem listitem = null; String StrTagName = null, StrTagIDName = null; //lock(CommonCollection.TagPacks_Lock) try { foreach (KeyValuePair <string, TagPack> tp in CommonCollection.TagPacks) { if (null == tp.Value) { continue; } if (tp.Value.RD_New[0] == br.ID[0] && tp.Value.RD_New[1] == br.ID[1]) { listitem = null; if (RouterTaglistView.Items.ContainsKey(tp.Key)) { StrTagName = null; listitem = null; if (RouterTaglistView.Items.Count > 0) { listitem = RouterTaglistView.FindItemWithText(tp.Key, false, 0); } if (null == listitem) { StrTagName = CommonBoxOperation.GetTagName(tp.Key); if (null != StrTagName) { listitem = RouterTaglistView.FindItemWithText(tp.Key, false, 0); } else { continue; } } if (null != listitem) { if (null == StrTagName) { StrTagName = CommonBoxOperation.GetTagName(tp.Key); } if (null != StrTagName) { if ("".Equals(StrTagName)) { StrTagIDName = tp.Key; } else { StrTagIDName = StrTagName; } } else { StrTagIDName = tp.Key; } listitem.Text = StrTagIDName; listitem.SubItems[1].Text = tp.Value.SigStren.ToString(); listitem.SubItems[2].Text = tp.Value.Bat.ToString(); listitem.SubItems[3].Text = tp.Value.ResTime.ToString() + "秒"; listitem.SubItems[4].Text = tp.Value.ReportTime.ToString(); } continue; } listitem = new ListViewItem(); listitem.Name = tp.Key; //判断卡片是否有名字 StrTagName = CommonBoxOperation.GetTagName(tp.Key); if (null != StrTagName) { if ("".Equals(StrTagName)) { StrTagIDName = tp.Key; } else { StrTagIDName = StrTagName; } } else { StrTagIDName = tp.Key; } listitem.Text = StrTagIDName; listitem.SubItems.Add(tp.Value.SigStren.ToString()); listitem.SubItems.Add(tp.Value.Bat.ToString()); listitem.SubItems.Add(tp.Value.ResTime.ToString() + "秒"); listitem.SubItems.Add(tp.Value.ReportTime.ToString()); RouterTaglistView.Items.Add(listitem); } } }catch (Exception) { } label1.Text = "Tag的總數量為:" + RouterTaglistView.Items.Count; }