SnapShotBuf.cs 4.73 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
using FLY.FeedbackRenZiJia.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using System.IO;
using FLY.FeedbackRenZiJia.Common;

namespace FLY.FeedbackRenZiJia.Server
{
    public class SnapShotBuf:ISnapShotBuf
    {
        #region 快照
        List<FlyData_SnapShot> mList = new List<FlyData_SnapShot>();
        string snapshot_path = "snapshot.csv";
        int BoltCnt;
        int ChannelCnt;

        public void Init(int boltCnt, int channelCnt)
        {
            //把记录中不是boltCnt,与 channelCnt 的删除
            BoltCnt = boltCnt;
            ChannelCnt = channelCnt;
            Load();
        }
        void Load()
        {
            try
            {
                mList.Clear();
                string filepath = snapshot_path;
                using (StreamReader sw = new StreamReader(snapshot_path, Encoding.GetEncoding("GB2312")))
                {
                    string header = sw.ReadLine();
                    while (!sw.EndOfStream)
                    {
                        string item = sw.ReadLine();
                        FlyData_SnapShot f = new FlyData_SnapShot();
                        if (f.TryParse(header, item))
                        {
                            mList.Add(f);
                        }
                    }
                }
            }
            catch
            {

            }

            var ss = from s in mList where (s.Heats.Count() != ChannelCnt) || (s.Thicks.Count() != BoltCnt) select s;
            List<FlyData_SnapShot> remove = new List<FlyData_SnapShot>();
            remove.AddRange(ss);
            foreach (FlyData_SnapShot s in remove)
            {
                mList.Remove(s);
            }
        }
        void Save()
        {
            try
            {
                string filepath = snapshot_path;

                if (mList.Count() == 0)
                {
                    return;
                }

                using (StreamWriter sw = new StreamWriter(filepath, false, Encoding.GetEncoding("GB2312")))
                {
                    sw.WriteLine(mList[0].GetHeader());
                    for (int i = 0; i < mList.Count(); i++)
                    {
                        sw.WriteLine(mList[i].ToString());
                    }
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// 获取快照 返回类型为 FlyData_SnapShot
        /// </summary>
        /// <param name="bookmark">记录点</param>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void Get(int bookmark, AsyncCBHandler AsyncDelegate, object AsyncState)
        {
            var fs = from f in mList where f.Bookmark == bookmark select f;
            if (fs.Count() == 0)
            {
                AsyncDelegate(AsyncState, null);
            }
            else
            {
                AsyncDelegate(AsyncState, fs.First());
            }
        }
        /// <summary>
        /// 设置快照 
        /// </summary>
        /// <param name="f">FlyData_SnapShot</param>
        public void Set(FlyData_SnapShot f)
        {
            var fs = from _f in mList where _f.Bookmark == f.Bookmark select _f;
            if (fs.Count() != 0)
            {
                FlyData_SnapShot s = fs.First();
                s.Copy(f);
            }
            else
            {
                mList.Add(f);
            }
            Save();
        }
        /// <summary>
        /// 删除快照
        /// </summary>
        /// <param name="bookmark"></param>
        public void Del(int bookmark)
        {
            // 删除指定的项
            // 1. 先找到要删除的对象。删除集合中所有Value=10的对象
            List<FlyData_SnapShot> deleted =
                mList.FindAll(item => item.Bookmark == bookmark);
            // 2. 从集合中删除。注意:遍历的是 deleted 而不是原来的list!
            foreach (var item in deleted)
            {
                mList.Remove(item);
            }
            Save();
        }

        /// <summary>
        /// 获取保存的快照列表 返回类型为 List&lt;int&gt;
        /// </summary>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void GetList(AsyncCBHandler AsyncDelegate, object AsyncState)
        {
            var bookmarks = from f in mList select f.Bookmark;
            if (bookmarks.Count() == 0)
            {
                AsyncDelegate(AsyncState, null);
            }
            else
            {
                AsyncDelegate(AsyncState, bookmarks.ToList());
            }
        }

        #endregion
    }
}