MainWindow.xaml.cs 6.5 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
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;

namespace FLYAD7_Simulation_Wpf
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        FlyADClientUI flyadclient_ui;
26

潘栩锋's avatar
潘栩锋 committed
27 28
        
        public int OneSecCnt { get; set; }
潘栩锋's avatar
潘栩锋 committed
29 30 31 32 33 34 35 36 37 38 39

        FLYAD7 flyad7;
        FLY.Simulation.Flyad7.OBJProxy.Flyad7_OBJProxy flyad7objproxy;
        bool bClosed = false;
        public MainWindow()
        {
            InitializeComponent();
            FObjBase.PollModule.Current.IsAccurate = true;


            flyad7 = new FLYAD7();
40 41
            flyad7.Init();

潘栩锋's avatar
潘栩锋 committed
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
            flyad7objproxy = new FLY.Simulation.Flyad7.OBJProxy.Flyad7_OBJProxy(0, flyad7);
            flyad7objproxy.Listen();

            flyadclient_ui = new FlyADClientUI(flyad7);

            this.DataContext = flyadclient_ui;
            this.groupbox_ip.DataContext = flyad7;

            groupBox4.DataContext = this;

            comboBox1.DataContext = flyad7;

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

        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);
            }
        }
98 99


潘栩锋's avatar
潘栩锋 committed
100 101 102 103
        void timer_Tick(object sender, EventArgs e)
        {
            
            FObjBase.PollModule.Current.OnPoll();
104

潘栩锋's avatar
潘栩锋 committed
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 223 224 225 226 227 228 229 230 231 232 233 234
        }

        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void Window_Closed(object sender, EventArgs e)
        {
           bClosed = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (flyad7.mSimGageAD is FLY.Simulation.Coating.GageAD)
            {
                WindowCoating wb = new WindowCoating();
                wb.Init((flyad7.mSimGageAD as FLY.Simulation.Coating.GageAD).mCoating);
                wb.ShowDialog();
            }
            else if (flyad7.mSimGageAD is FLY.Simulation.HeaderAndTailer.GageAD)
            {
                WindowHeaderAndTailer wb = new WindowHeaderAndTailer();
                wb.Init((flyad7.mSimGageAD as FLY.Simulation.HeaderAndTailer.GageAD));
                wb.ShowDialog();
            }
            else if(flyad7.mSimGageAD is FLY.Simulation.Blowing.GageAD)
            {
                WindowBlowing wb = new WindowBlowing();
                wb.mBlowing = (flyad7.mSimGageAD as FLY.Simulation.Blowing.GageAD).mBlowing;
                wb.ShowDialog();
            }
            else if (flyad7.mSimGageAD is FLY.Simulation.Casting.GageAD)
            {
                MessageBox.Show("不支持");
            }
            else 
            {
                MessageBox.Show("不支持");
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            flyad7.Save();
            MessageBox.Show("保存成功,请重启");
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Window_SyncList w = new Window_SyncList();
            w.Init(flyad7);
            w.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
    }



    public class SimModeConverter : EnumToIsCheckedConverter<FLY.Simulation.Flyad7.FLYAD7.SIM_MODE>
    {

    }
}