MainWindow.xaml.cs 5.96 KB
Newer Older
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;
using FLY.Simulation.Flyad7;
using GalaSoft.MvvmLight.Command;

namespace FLYAD7.Simulation.Battery.RayLaser
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        WdMainVm viewModel;

        public MainWindow()
        {
            InitializeComponent();

            viewModel = new WdMainVm();
            viewModel.Init();
            this.DataContext = viewModel;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            viewModel.Stop();
        }
    }

    public class WdMainVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public FlyADClientUI FlyADClientUIRay { get; private set; }
        public FlyADClientUI FlyADClientUILaser { get; private set; }

        public double TimeSpan1ms { get; set; }

        public int OneSecCnt { get; set; }

        public FLY.Simulation.Battery.RayLaser.Gage Gage { get; private set; }
        FLY.Simulation.Flyad7.OBJProxy.Flyad7_OBJProxy[] flyad7objProxys;

        public RelayCommand SaveCmd { get; private set; }


        bool bClosed = false;

        public WdMainVm() {
            SaveCmd = new RelayCommand(Save);
        }


        public void Init()
        {
            FObjBase.PollModule.Current.IsAccurate = true;
            
            FObjBase.FObjSys.Currents.Add(new FObjBase.FObjSys());

            Gage = new FLY.Simulation.Battery.RayLaser.Gage();
            Gage.Init();
            flyad7objProxys = new FLY.Simulation.Flyad7.OBJProxy.Flyad7_OBJProxy[2] {
                new FLY.Simulation.Flyad7.OBJProxy.Flyad7_OBJProxy(0, Gage.FlyAdRay),
                new FLY.Simulation.Flyad7.OBJProxy.Flyad7_OBJProxy(1, Gage.FlyAdLaser)
            };
            flyad7objProxys[0].Listen();
            flyad7objProxys[1].Listen();

            FlyADClientUIRay = new FlyADClientUI(Gage.FlyAdRay);
            FlyADClientUILaser = new FlyADClientUI(Gage.FlyAdLaser);

            Thread t = new Thread(OnPoll);
            t.IsBackground = true;
            t.Start();
            //FObjBase.PollModule.Current.Start();
        }

        public void Stop() {
            bClosed = true;
        }
        DateTime dt_cnt = DateTime.MinValue;
        int onesecondcnt = 0;
        TimeSpan ts_1scnt = TimeSpan.Zero;
        void Update1sCnt()
        {
            DateTime d = DateTime.Now;
            if (dt_cnt == DateTime.MinValue)
                dt_cnt = d;
            else
            {
                onesecondcnt++;
                TimeSpan ts = (d - dt_cnt);
                if (ts.TotalMilliseconds >= 1.28)
                {
                    //OneSecCnt = (int)(d - dt_cnt).TotalMilliseconds;// onesecondcnt;
                    OneSecCnt = onesecondcnt;
                    onesecondcnt = 0;
                    dt_cnt = d;
                }
                else
                {
                    ts_1scnt = d - dt_cnt;
                }

            }
        }
        void OnPoll()
        {

            while (true)
            {
                if (bClosed)
                    return;
                timer_Tick(null, null);
                //Update1sCnt();
                Thread.Sleep(0);
            }
        }
        DateTime dt = DateTime.MinValue;
        void UpdateTimeSpan1ms()
        {
            DateTime d = DateTime.Now;
            if (dt == DateTime.MinValue)
                dt = d;
            else
            {
                TimeSpan1ms = (d - dt).TotalMilliseconds;
                dt = d;
            }
        }
        void timer_Tick(object sender, EventArgs e)
        {

            FObjBase.PollModule.Current.OnPoll();
            //UpdateTimeSpan1ms();
        }


        private void Save()
        {
            Gage.FlyAdRay.Save();
            Gage.FlyAdLaser.Save();
            MessageBox.Show("保存成功,请重启");
        }
    }

    public class IO2BinConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            UInt16 io = (UInt16)value;
            string str = System.Convert.ToString(io, 2);

            return str.PadLeft(12, '0');
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return 0xffff;

            else 
            {
                string str = value as string;
                UInt16 io;
                if (UInt16.TryParse(str, out io))
                    return io;
                else
                    return 0xffff;
            }

        }

        #endregion
    }


    public class EnumToIsCheckedConverter<TEnum> : IValueConverter
    where TEnum : struct
    {

        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TEnum type = (TEnum)value;
            if (Enum.GetName(typeof(TEnum), type) == (parameter as string))
                return true;
            else
                return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((bool)value == true)
            {
                return Enum.Parse(typeof(TEnum), (parameter as string));
            }
            else
            {
                return null;
            }
        }

        #endregion
    }
}