GM_GageInfo.cs 8.21 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
using FlyADBase;
using FLY.Thick.Base.IService;
9 10 11
using AutoMapper;
using System.IO;
using Newtonsoft.Json;
潘栩锋's avatar
潘栩锋 committed
12
using FObjBase;
潘栩锋's avatar
潘栩锋 committed
13 14 15 16

namespace FLY.Thick.Base.Server
{

潘栩锋's avatar
潘栩锋 committed
17
    public class GM_GageInfo : GM_Base, IGageInfoService
潘栩锋's avatar
潘栩锋 committed
18
    {
潘栩锋's avatar
潘栩锋 committed
19 20 21 22 23
        /// <summary>
        /// 速度
        /// </summary>
        public UInt32 Velocity { get; set; } = 1000;

潘栩锋's avatar
潘栩锋 committed
24
        #region IGageInfoService 接口
25

潘栩锋's avatar
潘栩锋 committed
26 27 28 29
        /// <summary>
        /// 数据好了!!!!
        /// 当 flyad7 的poslen, posOfGrid 发生变化时,DataOK = false, 需要重新记录。
        /// </summary>
30
        public bool DataOK { get; private set; }
潘栩锋's avatar
潘栩锋 committed
31 32 33 34

        /// <summary>
        /// 机架总长,脉冲
        /// </summary>
35
        public int PosLen { get; private set; } = 9123;
潘栩锋's avatar
潘栩锋 committed
36 37 38 39

        /// <summary>
        /// 1个grid = N个pos
        /// </summary>
40
        public int PosOfGrid { get; private set; } = 11;
潘栩锋's avatar
潘栩锋 committed
41

潘栩锋's avatar
潘栩锋 committed
42 43 44 45
        /// <summary>
        /// 数据更新时间
        /// </summary>
        public DateTime[] UpdateTimes { get; private set; }
潘栩锋's avatar
潘栩锋 committed
46 47 48 49

        /// <summary>
        /// 0~100
        /// </summary>
50
        public int Progress { get; private set; }
潘栩锋's avatar
潘栩锋 committed
51 52 53 54 55 56
        #endregion


        string file_path;

        ScanMotion2 scanMotion;
57
        protected InitParam initParam;
潘栩锋's avatar
潘栩锋 committed
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
        public GageInfoData Data;

        public GM_GageInfo():this("gageinfo.json")
        {

        }
        public GM_GageInfo(string param_path)
        {
            GMState = FLY.Thick.Base.Common.CTRL_STATE.GAGEINFO;
            this.file_path = param_path;

            Load();
        }
        public void Init(FlyAD7 flyad, InitParam initParam)
        {
            base.Init(flyad);
            this.initParam = initParam;

            Misc.BindingOperations.SetBinding(initParam, nameof(initParam.VAccuracy), this, nameof(Velocity));

            scanMotion = new ScanMotion2();

            scanMotion.Init(flyad);
            scanMotion.EndEvent += GM_GageInfo_EndEvent;
            scanMotion.PropertyChanged += GM_GageInfo_PropertyChanged;

潘栩锋's avatar
潘栩锋 committed
84
            UpdateTimes = new DateTime[2];
潘栩锋's avatar
潘栩锋 committed
85 86 87 88 89 90 91
            updateDataOK();

            initParam.PropertyChanged += new PropertyChangedEventHandler(initParam_PropertyChanged);


        }

潘栩锋's avatar
潘栩锋 committed
92 93 94
        /// <summary>
        /// 数据录制,记下来回扫描的AD值数组
        /// </summary>
95
        public override void Start()
潘栩锋's avatar
潘栩锋 committed
96
        {
97 98 99
            base.Start();
            if (!IsRunning)
                return;
潘栩锋's avatar
潘栩锋 committed
100 101 102

            Progress = 0;
            DataOK = false;
潘栩锋's avatar
潘栩锋 committed
103 104 105 106 107 108
            PosOfGrid = initParam.PosOfGrid;
            PosLen = initParam.PosLength;
            Data = new GageInfoData();
            for (int i = 0; i < 2; i++)
                UpdateTimes[i] = DateTime.Now;
            NotifyPropertyChanged(nameof(UpdateTimes));
109

潘栩锋's avatar
潘栩锋 committed
110
            scanMotion.Start(1, Velocity);
潘栩锋's avatar
潘栩锋 committed
111 112 113 114 115 116 117 118
        }
        public override void Stop()
        {
            base.Stop();
            if (DataOK)
            {
                Progress = 100;
            }
潘栩锋's avatar
潘栩锋 committed
119
            else
潘栩锋's avatar
潘栩锋 committed
120
            {
潘栩锋's avatar
潘栩锋 committed
121 122 123 124 125 126 127
                PosOfGrid = initParam.PosOfGrid;
                PosLen = initParam.PosLength;
                Data = new GageInfoData();

                for (int i = 0; i < 2; i++)
                    UpdateTimes[i] = DateTime.Now;
                NotifyPropertyChanged(nameof(UpdateTimes));
潘栩锋's avatar
潘栩锋 committed
128 129
            }

潘栩锋's avatar
潘栩锋 committed
130
            Save();
潘栩锋's avatar
潘栩锋 committed
131 132

        }
133

潘栩锋's avatar
潘栩锋 committed
134
        protected override void OnPoll()
135
        {
潘栩锋's avatar
潘栩锋 committed
136 137 138 139
            if (scanMotion.OnPoll())
            {
                Stop();
            }
潘栩锋's avatar
潘栩锋 committed
140
        }
潘栩锋's avatar
潘栩锋 committed
141
        private void GM_GageInfo_PropertyChanged(object sender, PropertyChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
142
        {
潘栩锋's avatar
潘栩锋 committed
143
            if (e.PropertyName == nameof(ScanMotion2.Progress))
潘栩锋's avatar
潘栩锋 committed
144
            {
潘栩锋's avatar
潘栩锋 committed
145
                Progress = scanMotion.Progress;
潘栩锋's avatar
潘栩锋 committed
146 147 148
            }
        }

潘栩锋's avatar
潘栩锋 committed
149
        void initParam_PropertyChanged(object sender, PropertyChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
150
        {
潘栩锋's avatar
潘栩锋 committed
151 152 153 154 155
            if ((e.PropertyName == nameof(initParam.PosOfGrid)) ||
                (e.PropertyName == nameof(initParam.PosLength)))
            {
                updateDataOK();
            }
潘栩锋's avatar
潘栩锋 committed
156
        }
潘栩锋's avatar
潘栩锋 committed
157
        void updateDataOK()
潘栩锋's avatar
潘栩锋 committed
158
        {
潘栩锋's avatar
潘栩锋 committed
159
            if (Data == null)
潘栩锋's avatar
潘栩锋 committed
160
            {
潘栩锋's avatar
潘栩锋 committed
161 162 163
                DataOK = false;
                return;
            }
潘栩锋's avatar
潘栩锋 committed
164

潘栩锋's avatar
潘栩锋 committed
165 166 167 168
            if (Data.ForwData == null || Data.BackwData == null)
            {
                DataOK = false;
                return;
潘栩锋's avatar
潘栩锋 committed
169
            }
潘栩锋's avatar
潘栩锋 committed
170 171 172 173

            if ((PosOfGrid != initParam.PosOfGrid) ||
                (PosLen != initParam.PosLength))
                DataOK = false;
潘栩锋's avatar
潘栩锋 committed
174
            else
潘栩锋's avatar
潘栩锋 committed
175 176
                DataOK = true;
        }
潘栩锋's avatar
潘栩锋 committed
177

潘栩锋's avatar
潘栩锋 committed
178 179 180
        private void GM_GageInfo_EndEvent(object obj, MiniGridEventArgs e)
        {
            DataEnd(e.direction, e.posOfGrid, e.grid_start, e.buf);
潘栩锋's avatar
潘栩锋 committed
181 182 183
        }
        void DataEnd(Misc.DIRECTION direction, int posOfGrid, int start_grid, int[] dat)
        {
184 185
            int grid_len = PosLen / PosOfGrid;
            int[] datas = new int[grid_len];
潘栩锋's avatar
潘栩锋 committed
186 187
            for (int i = 0; i < grid_len; i++)
            {
188 189
                datas[i] = Misc.MyBase.NULL_VALUE;
            }
潘栩锋's avatar
潘栩锋 committed
190 191 192 193 194
            if (direction == Misc.DIRECTION.FORWARD)
            {
                for (int i = 0; i < dat.Length; i++)
                {
                    int grid = i + start_grid;
195
                    if (grid >= grid_len)
潘栩锋's avatar
潘栩锋 committed
196
                        break;
197
                    datas[grid] = dat[i];
潘栩锋's avatar
潘栩锋 committed
198
                }
潘栩锋's avatar
潘栩锋 committed
199 200 201
                Data.ForwData = datas;
                UpdateTimes[0] = DateTime.Now;
                NotifyPropertyChanged(nameof(UpdateTimes));
潘栩锋's avatar
潘栩锋 committed
202
            }
潘栩锋's avatar
潘栩锋 committed
203
            else
潘栩锋's avatar
潘栩锋 committed
204 205 206 207
            {
                for (int i = 0; i < dat.Length; i++)
                {
                    int grid = i + start_grid;
208
                    if (grid >= grid_len)
潘栩锋's avatar
潘栩锋 committed
209
                        break;
210
                    datas[grid] = dat[i];
潘栩锋's avatar
潘栩锋 committed
211

潘栩锋's avatar
潘栩锋 committed
212
                }
潘栩锋's avatar
潘栩锋 committed
213 214 215
                Data.BackwData = datas;
                UpdateTimes[1] = DateTime.Now;
                NotifyPropertyChanged(nameof(UpdateTimes));
潘栩锋's avatar
潘栩锋 committed
216
            }
潘栩锋's avatar
潘栩锋 committed
217 218

            updateDataOK();
潘栩锋's avatar
潘栩锋 committed
219 220
        }

221
        bool Load()
潘栩锋's avatar
潘栩锋 committed
222
        {
潘栩锋's avatar
潘栩锋 committed
223 224 225 226 227 228 229 230 231 232 233 234 235
            return GageInfoJsonDb.Load(file_path, this);
        }

        bool Save()
        {
            return GageInfoJsonDb.Save(file_path, this);
        }

        public void GetGageInfo(int idx, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            if (Data == null ||
                idx < 0 ||
                idx >= 2)
236
            {
潘栩锋's avatar
潘栩锋 committed
237 238
                asyncDelegate(asyncContext, new GetGageInfoResponse() { Idx = idx });
                return;
239
            }
潘栩锋's avatar
潘栩锋 committed
240 241
            var data = Data;
            var reponse = new GetGageInfoResponse()
242
            {
潘栩锋's avatar
潘栩锋 committed
243 244 245 246
                Idx = idx,
                Data = idx % 2 == 0 ? data.ForwData : data.BackwData,
                UpdateTime = UpdateTimes[idx]
            };
247

潘栩锋's avatar
潘栩锋 committed
248
            asyncDelegate(asyncContext, reponse);
潘栩锋's avatar
潘栩锋 committed
249
        }
潘栩锋's avatar
潘栩锋 committed
250 251 252 253 254 255 256
    }
    public class GageInfoJsonDb
    {
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
        {
            c.CreateMap<GM_GageInfo, GageInfoJsonDb>().ReverseMap();
        }));
257

潘栩锋's avatar
潘栩锋 committed
258
        public static bool Save(string filePath, GM_GageInfo src)
潘栩锋's avatar
潘栩锋 committed
259
        {
260 261
            try
            {
潘栩锋's avatar
潘栩锋 committed
262 263
                var p = Mapper.Map<GageInfoJsonDb>(src);
                File.WriteAllText(filePath, JsonConvert.SerializeObject(p, Formatting.Indented));
264 265 266 267 268 269 270 271
                return true;
            }
            catch
            {
                //异常,没有json 编码失败

            }
            return false;
潘栩锋's avatar
潘栩锋 committed
272
        }
潘栩锋's avatar
潘栩锋 committed
273
        public static bool Load(string filePath, GM_GageInfo src)
274
        {
潘栩锋's avatar
潘栩锋 committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
            try
            {
                if (File.Exists(filePath))
                {
                    string json = File.ReadAllText(filePath);
                    var p = JsonConvert.DeserializeObject<GageInfoJsonDb>(json);
                    Mapper.Map(p, src);
                }
            }
            catch
            {
                //异常,没有json 解码失败

            }
            return false;
        }

潘栩锋's avatar
潘栩锋 committed
292

293 294 295 296
        /// <summary>
        /// 机架总长,脉冲
        /// </summary>
        public int PosLen { get; set; } = 9123;
潘栩锋's avatar
潘栩锋 committed
297

298 299 300 301 302
        /// <summary>
        /// 1个grid = N个pos
        /// </summary>
        public int PosOfGrid { get; set; } = 11;

潘栩锋's avatar
潘栩锋 committed
303
        public GageInfoData Data;
潘栩锋's avatar
潘栩锋 committed
304 305
    }
}