Window_sync.xaml.cs 9.99 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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.Shapes;
using FlyADBase;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Threading;
17 18 19
using Microsoft.Win32;
using System.IO;
using Newtonsoft.Json;
潘栩锋's avatar
潘栩锋 committed
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

namespace Flyad7_WPF
{
    /// <summary>
    /// Window_sync.xaml 的交互逻辑
    /// </summary>
    public partial class Window_sync : Window, INotifyPropertyChanged
    {
        public static bool HasOpen = false;
        IFlyADClient flyad;
        AutoSync mAutoSync;
        public Window_sync()
        {
            InitializeComponent();


        }

        public void Init(IFlyADClient flyad, AutoSync mAutoSync)
        {
            this.flyad = flyad;
            this.mAutoSync = mAutoSync;
            textBlock_pos1LCShift.DataContext = this.flyad;
            textBlock_position2.DataContext = this.flyad;
            textBlock_marker.DataContext = this.flyad;
           
            groupBox_syncRunAtLC.DataContext = mSyncOrder_SyncRunAtLC;
            groupBox_syncOrigin.DataContext = mSyncOrder_SyncOrigin;
            groupBox_syncRunTo.DataContext = mSyncOrder_SyncRunTo;
            groupBox_syncRunToLC.DataContext = mSyncOrder_SyncRunToLC;
            groupBox_syncWait.DataContext = mSyncOrder_SyncWait;

            groupBox9.DataContext = this;

            listView1.ItemsSource = flyad.SyncOrders;


            //自动..........................

            tabItem2.DataContext = mAutoSync;
            stackpanel_SamplePos_Backw.DataContext = mAutoSync.SamplePos_Backw;
            stackpanel_SamplePos_Forw.DataContext = mAutoSync.SamplePos_Forw;
        }



        #region 参数



70
        public int Pos1LCShift { get; set; }
潘栩锋's avatar
潘栩锋 committed
71 72 73



74 75
        public int Position2 { get; set; }
        
潘栩锋's avatar
潘栩锋 committed
76

77
        public int Pos2Shift { get; set; }
潘栩锋's avatar
潘栩锋 committed
78

79 80 81 82 83 84
        public float Pos2Comp { get; set; }


        public int Distance { get; set; }

        #endregion
潘栩锋's avatar
潘栩锋 committed
85 86 87 88 89 90 91 92 93

        public event PropertyChangedEventHandler PropertyChanged;

        private void button_setPos1LCShift_Click(object sender, RoutedEventArgs e)
        {
            flyad.Pos1LCShift = Pos1LCShift;
            MessageBox.Show("flyad.Pos1LCShift = " + Pos1LCShift.ToString());
        }

潘栩锋's avatar
潘栩锋 committed
94

潘栩锋's avatar
潘栩锋 committed
95 96 97

        private void button_setPos2Shift_Click(object sender, RoutedEventArgs e)
        {
98
            MessageBox.Show("不支持");
潘栩锋's avatar
潘栩锋 committed
99
            //TODO
100 101
            //flyad.SetPos2Offset(Pos2Shift);
            //MessageBox.Show("flyad.SetPos2Offset( " + Pos2Shift.ToString() + ")");
潘栩锋's avatar
潘栩锋 committed
102 103 104
        }


105

潘栩锋's avatar
潘栩锋 committed
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

        private void button_clear_Click(object sender, RoutedEventArgs e)
        {
            flyad.SyncClear();
            MessageBox.Show("flyad.SyncClear()");
        }

        private void button_syncEnd_Click(object sender, RoutedEventArgs e)
        {
            flyad.SyncEnd();
            MessageBox.Show("flyad.SyncEnd()");
        }

        private void button_begin2_Click(object sender, RoutedEventArgs e)
        {
            flyad.SyncBegin();
            MessageBox.Show("flyad.SyncBegin()");
        }



        #region 手动指令
        SyncOrder_SyncRunAtLC mSyncOrder_SyncRunAtLC = new SyncOrder_SyncRunAtLC();
        SyncOrder_SyncOrigin mSyncOrder_SyncOrigin = new SyncOrder_SyncOrigin();
        SyncOrder_SyncRunTo mSyncOrder_SyncRunTo = new SyncOrder_SyncRunTo();
        SyncOrder_SyncRunToLC mSyncOrder_SyncRunToLC = new SyncOrder_SyncRunToLC();
        SyncOrder_SyncWait mSyncOrder_SyncWait = new SyncOrder_SyncWait();

        private void button_addSyncRunAtLC_Click(object sender, RoutedEventArgs e)
        {
            mSyncOrder_SyncRunAtLC.Add(flyad);
        }

        private void button_addSyncOrigin_Click(object sender, RoutedEventArgs e)
        {
            mSyncOrder_SyncOrigin.Add(flyad);
        }

        private void button_addSyncWait_Click(object sender, RoutedEventArgs e)
        {
            mSyncOrder_SyncWait.Add(flyad);
        }

        private void button_addSyncRunTo_Click(object sender, RoutedEventArgs e)
        {
            mSyncOrder_SyncRunTo.Add(flyad);
        }

        private void button_addSyncRunToLC_Click(object sender, RoutedEventArgs e)
        {
            mSyncOrder_SyncRunToLC.Add(flyad);
        }
        #endregion

        #region 自动指令
        private void button_init_Click(object sender, RoutedEventArgs e)
        {
            mAutoSync.InitSyncParam();
        }

        private void button_go_Click(object sender, RoutedEventArgs e)
        {
            mAutoSync.Start();
        }
        private void button_pos2sync1_Click(object sender, RoutedEventArgs e)
        {
            mAutoSync.SetVSensor();
        }
        private void button_pos2sync0_Click(object sender, RoutedEventArgs e)
        {
            mAutoSync.ClearVSensor();
        }
        #endregion

        private void button_graph_Click(object sender, RoutedEventArgs e)
        {
            Window_sync_graph w = new Window_sync_graph();
            w.Init(flyad);
            w.Show();
        }



        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            HasOpen = true;
        }

        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            HasOpen = false;
        }

        private void button_save_autosync_Click(object sender, RoutedEventArgs e)
        {
            mAutoSync.Save();
        }

        private void button_stop_Click(object sender, RoutedEventArgs e)
        {
            mAutoSync.Stop();
        }

        private void button_test_Click(object sender, RoutedEventArgs e)
        {
            mAutoSync.StartTest();
        }

        private void button_teststop_Click(object sender, RoutedEventArgs e)
        {
            mAutoSync.StopTest();
        }

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        private void button_save_Click(object sender, RoutedEventArgs e)
        {
            if (flyad.SyncOrders.Count() == 0)
            {
                MessageBox.Show("没有任何一条指令","错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            var syncOrderJsonDBs =
                (from o in flyad.SyncOrders
                 select
                     new SyncOrderJsonDB()
                     {
                         type = o.GetType().Name,
                         syncOrderJson = Newtonsoft.Json.JsonConvert.SerializeObject(o)
                     }).ToList();

            if (!Directory.Exists("sync_order_list"))
            {
                Directory.CreateDirectory("sync_order_list");
            }
潘栩锋's avatar
潘栩锋 committed
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = System.Environment.CurrentDirectory + @"\sync_order_list";
            sfd.AddExtension = true;
            sfd.DefaultExt = ".json";
            if (sfd.ShowDialog() == true)
            {
                try
                {
                    File.WriteAllText(sfd.FileName, JsonConvert.SerializeObject(syncOrderJsonDBs, Formatting.Indented));

                }
                catch(Exception _e)
                {
                    //异常,没有json 编码失败
                    MessageBox.Show(_e.ToString(), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                MessageBox.Show($"保存 {syncOrderJsonDBs.Count()}条同步指令 到 {sfd.FileName}", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            
        }
潘栩锋's avatar
潘栩锋 committed
261

262 263 264 265 266 267 268 269 270
        private void button_load_Click(object sender, RoutedEventArgs e)
        {
            List<Type> ordertypes = new List<Type> {
                typeof(SyncOrder_SyncRunAtLC),
                typeof(SyncOrder_SyncOrigin),
                typeof(SyncOrder_SyncRunTo),
                typeof(SyncOrder_SyncRunToLC),
                typeof(SyncOrder_SyncWait)
            };
潘栩锋's avatar
潘栩锋 committed
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
            if (!Directory.Exists("sync_order_list"))
            {
                Directory.CreateDirectory("sync_order_list");
            }

            OpenFileDialog ofd = new OpenFileDialog();
            
            ofd.InitialDirectory = System.Environment.CurrentDirectory+@"\sync_order_list";
            ofd.Filter = "json|*.json";
            if (ofd.ShowDialog() == true)
            {
                try
                {
                    List<SyncOrder> syncorders = new List<SyncOrder>();
                    string json = File.ReadAllText(ofd.FileName);
                    var syncOrderJsonDBs = JsonConvert.DeserializeObject<List<SyncOrderJsonDB>>(json);
                    foreach(var syncOrderJsonDB in syncOrderJsonDBs)
                    {
                        Type t = ordertypes.Find(type => type.Name == syncOrderJsonDB.type);
                        if (t == null)
                        {
                            MessageBox.Show($"{syncOrderJsonDB.type} 不能识别", "异常", MessageBoxButton.OK, MessageBoxImage.Error);
                            return;
                        }
                        var syncorder = JsonConvert.DeserializeObject(syncOrderJsonDB.syncOrderJson, t) as SyncOrder;
                        syncorders.Add(syncorder);
                    }


                    flyad.SyncClear();
                    foreach (var syncorder in syncorders)
                    {
                        syncorder.Add(flyad);
                    }
                    MessageBox.Show($"加载 {syncorders.Count()}条同步指令", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }
                catch(Exception _e)
                {
                    //异常,没有json 解码失败
                    MessageBox.Show($"{_e.Message}", "异常", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }
        }
    }

    public class SyncOrderJsonDB
    {
        public string type;
        public string syncOrderJson;
    }
潘栩锋's avatar
潘栩锋 committed
324 325 326 327 328 329




}