/// <summary> /// 采集天然气数据 /// </summary> /// <param name="trq"></param> static void SampleTRQ(object trq) { TRQ _TRQ = (TRQ)trq; ushort _year, _monthday, _weekhour; string datetime = ""; bool locker = false; while (!willExit) { Stopwatch st = new Stopwatch(); if ((DateTime.Now.Second % 9 == 0) && (!locker)) //定时检查 { Interlocked.Exchange(ref WatchDogState, 0); //将看门狗状态复位 locker = true; st.Start(); //bug处理 //由于小时产量不确定在什么时间到达,如果在一日的八点之前到达那么就会导致数据存储的上一个月的表中. //解决办法就是在一日的7点半到8点之间不再扫描天然气点,八点以后再扫描 bool bIgnore = false; if ((DateTime.Now.Day == 1) && (DateTime.Now.Hour == 7) && (DateTime.Now.Minute > 30)) { bIgnore = true; } else { bIgnore = false; } #region 数据读取 if (!bIgnore) { for (int i = 0; i < _TRQ.TRQTAGS.Count; i++) { try { //处理时间 _year = (ushort)mHMITags[_TRQ.TRQ_YEAR[i]].Read(); _monthday = (ushort)mHMITags[_TRQ.TRQ_MONTH_DAY[i]].Read(); _weekhour = (ushort)mHMITags[_TRQ.TRQ_WEEK_HOUR[i]].Read(); if (BCD2DateTime(_year, _monthday, _weekhour, ref datetime)) { _TRQ.TRQ_THIS_TIME[i] = datetime; } //else//如果时间处理出错那么将时间设置为1970-01-01 00:00:00 //{ // _TRQ.TRQ_THIS_TIME[i] = "1970-01-01 00:00:00"; //} //处理数值 _TRQ.TRQ_THIS_VALUE[i] = (double)mHMITags[_TRQ.TRQTAGS[i]].Read(); } catch { //出错以后并不执行操作,数据还是保持以前的旧的数据 Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "天然气数据读取返回null!"); } } } else { Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "天然气产量每月一日7点半到八点之间不采集."); } #endregion st.Stop(); Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "天然气读扫描用时{0}ms", st.Elapsed.TotalMilliseconds); st.Reset(); #region 数据处理 List <row> rows = new List <row> { }; for (int j = 0; j < _TRQ.TRQTAGS.Count; j++) { if (_TRQ.TRQ_LAST_TIME[j] != _TRQ.TRQ_THIS_TIME[j]) { if (_TRQ.TRQ_LAST_VALUE[j] != _TRQ.TRQ_THIS_VALUE[j]) { row r = new row(); r.TagName = _TRQ.TRQTAGS[j]; r.Time = _TRQ.TRQ_THIS_TIME[j]; r.Value = _TRQ.TRQ_THIS_VALUE[j]; rows.Add(r); _TRQ.TRQ_LAST_TIME[j] = _TRQ.TRQ_THIS_TIME[j]; _TRQ.TRQ_LAST_VALUE[j] = _TRQ.TRQ_THIS_VALUE[j]; } else { _TRQ.TRQ_FAIL_COUNT[j]++; if (_TRQ.TRQ_FAIL_COUNT[j] >= 10) //等待20秒 { row r = new row(); r.TagName = _TRQ.TRQTAGS[j]; r.Time = _TRQ.TRQ_THIS_TIME[j]; r.Value = _TRQ.TRQ_THIS_VALUE[j]; rows.Add(r); _TRQ.TRQ_LAST_TIME[j] = _TRQ.TRQ_THIS_TIME[j]; _TRQ.TRQ_LAST_VALUE[j] = _TRQ.TRQ_THIS_VALUE[j]; } } } } #endregion #region 数据写入 if (rows.Count > 0) { st.Start(); WriteSQL(rows); st.Stop(); Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "天然气写入数据库用时{0}ms", st.Elapsed.TotalMilliseconds); st.Reset(); } #endregion } if (DateTime.Now.Second % 9 == 1) { locker = false; } //每次循环结束时将状态设置为true lock (threadstatus.SyncRoot) { threadstatus[Thread.CurrentThread.GetHashCode()] = true; } Thread.Sleep(200); } //程序退出是将状态设置为false lock (threadstatus.SyncRoot) { threadstatus[Thread.CurrentThread.GetHashCode()] = false; } Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "流量计采集线程收到退出信号并已经退出!"); }
/// <summary> /// 轮询采集 /// </summary> /// <param name="tags">需要采集的点</param> static void SamppleInterVal(object tags) { List <string> _tags = (List <string>)tags; Stopwatch st = new Stopwatch(); bool locker = false; while (!willExit) { if ((DateTime.Now.Second == 0) && (!locker)) { Interlocked.Exchange(ref WatchDogState, 0); //将看门狗状态复位 st.Start(); locker = true; string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); List <row> rows = new List <row> { }; foreach (string tag in _tags) //循环采集所有点的数据 { try { row r = new row(); r.Time = time; r.TagName = tag; r.Value = (float)mHMITags[tag].Read(); //有时候会出错,返回null //if (tag == "JQZ/CZ_LJLL.Value") Console.WriteLine(r.Value); rows.Add(r); } catch { //出错后不执行任何操作 Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "[轮询点]读取数据返回null"); } } while (stop) { ; } st.Stop(); Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "轮询采集用时{0}ms", st.Elapsed.TotalMilliseconds); st.Reset(); try { st.Start(); WriteSQL(rows); //网络阻塞时导致的线程卡死 st.Stop(); Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "轮询结果写入数据库用时{0}ms", st.Elapsed.TotalMilliseconds); st.Reset(); } catch { Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "写入数据库出错"); } } if (DateTime.Now.Second >= 55) { locker = false; } //每次循环结束时将状态设置为true //lock (threadstatus) //{ //} lock (threadstatus.SyncRoot) { threadstatus[Thread.CurrentThread.GetHashCode()] = true; } Thread.Sleep(200); } //程序结束时将状态设置为false lock (threadstatus.SyncRoot) { threadstatus[Thread.CurrentThread.GetHashCode()] = false; } Console.WriteLine("[" + DateTime.Now.ToString() + "]" + "轮询采集线程收到退出信号并已经退出!"); }