aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.FirmwarePackageGenerator/MainWindow.xaml.cs
blob: 4822a2b3b4b767da06dbf0c000dfd006b29f304d (plain)
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
using MahApps.Metro.Controls;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace Tango.FirmwarePackageGenerator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowVM();
        }

        private void DataGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
        {
            //OpenFileDialog dlg = new OpenFileDialog();
            //dlg.Filter = "Firmware Package Files|*.bin;*.fpga";
            //if (dlg.ShowDialog().Value)
            //{
            //    VersionFileModel item = new VersionFileModel();
            //    item.Path = dlg.FileName;
            //    e.NewItem = item;
            //}
        }

        private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (grid.SelectedItem == null || grid.SelectedItem.ToString() == "{NewItemPlaceholder}")
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "Firmware Package Files|*.*";
                if (dlg.ShowDialog().Value)
                {
                    VersionFileModel item = new VersionFileModel();
                    item.Path = dlg.FileName;
                    (DataContext as MainWindowVM).VersionFiles.Add(item);
                }
            }
        }
    }
}
an class="w"> bool taskCompleted; public Subject<T> ContinuesResponseSubject { get; set; } public DateTime LastResponseTime { get; set; } public bool Completed { get; set; } /// <summary> /// Initializes a new instance of the <see cref="TransportMessage{T}"/> class. /// </summary> /// <param name="token">The token.</param> /// <param name="message">The message.</param> /// <param name="direction">The direction.</param> /// <param name="toBytes">To bytes.</param> /// <param name="completionSource">The completion source.</param> public TransportMessage(string token, object message, TransportMessageDirection direction, Func<byte[]> toBytes, TaskCompletionSource<T> completionSource) : base(token, message, direction, toBytes) { _completionSource = completionSource; } /// <summary> /// Notifies the message observer of the new result. /// </summary> /// <param name="result">The result.</param> public override void SetResult(object result, bool completed) { Completed = completed; ThreadFactory.StartNew(() => { try { if (!IsContinuous) { if (!taskCompleted) { taskCompleted = true; if (_completionSource.GetType() == typeof(TaskCompletionSource<IMessage>)) { _completionSource.SetResult((T)result.GetType().GetProperty("Message").GetValue(result)); } else if (_completionSource.GetType() == typeof(TaskCompletionSource<MessageContainer>)) { _completionSource.SetResult((T)result.GetType().GetProperty("Container").GetValue(result)); } else { _completionSource.SetResult((T)result); } } } else { LastResponseTime = DateTime.Now; AtLeastOneResponseReceived = true; if (ContinuesResponseSubject.GetType() == typeof(Subject<IMessage>)) { ContinuesResponseSubject.OnNext((T)result.GetType().GetProperty("Message").GetValue(result)); } else if (ContinuesResponseSubject.GetType() == typeof(Subject<MessageContainer>)) { ContinuesResponseSubject.OnNext((T)result.GetType().GetProperty("Container").GetValue(result)); } else { ContinuesResponseSubject.OnNext((T)result); } if (completed) { ContinuesResponseSubject.OnCompleted(); } } } catch (Exception ex) { LogManager.Log(ex, $"Error while settings result for message."); try { SetException(ex); } catch (Exception e) { LogManager.Log(e, $"Error while settings exception for message."); } } }); } /// <summary> /// Notifies the message observer of an exception. /// </summary> /// <param name="ex">The ex.</param> public override void SetException(Exception ex) { if (exceptionRaised || taskCompleted) { return; } Completed = true; exceptionRaised = true; ThreadFactory.StartNew(() => { if (!IsContinuous) { if (!taskCompleted) { taskCompleted = true; if (!(ex is ContinuousResponseAbortedException) && !(ex is TransporterDisconnectedException)) { LogManager.Log($"{TransportComponentName}: Request failed '{GetActualMessageTypeName()}'...\n{ex.FlattenException()}", LogCategory.Error); } _completionSource.SetException(ex); } } else { if (!(ex is ContinuousResponseAbortedException) && !(ex is TransporterDisconnectedException)) { LogManager.Log($"{TransportComponentName}: Request failed '{GetActualMessageTypeName()}'...\n{ex.FlattenException()}", LogCategory.Error); } AtLeastOneResponseReceived = true; ContinuesResponseSubject.OnError(ex); } }); } public override string GetActualMessageTypeName() { String name = String.Empty; if (Message is ITangoMessage) { var message = Message.GetType().GetProperty("Message").GetValue(Message); if (message.GetType() == typeof(GenericRequest)) { try { name = (message as GenericRequest).GetTypeName(); } catch { name = message.GetType().Name; } } else if (message.GetType() == typeof(GenericResponse)) { try { name = (message as GenericResponse).GetTypeName(); } catch { name = message.GetType().Name; } } else { name = message.GetType().Name; } } else if (Message is MessageContainer) { name = (Message as MessageContainer).Type.ToString(); } else { name = Message.GetType().Name; } return name; } public override object GetActualMessage() { object obj = null; if (Message is ITangoMessage) { obj = Message.GetType().GetProperty("Message").GetValue(Message); } else if (Message is MessageContainer) { obj = Message; } else { obj = Message; } return obj; } } }