aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/Dialogs/BitResultsViewVM.cs
blob: 6a9630c215cf7b418dee85e60ffbef94c3b135f2 (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
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
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.Core.DI;
using Tango.Integration.Operation;
using Tango.PMR.Diagnostics;
using Tango.PPC.Common;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Connection;
using Tango.PPC.Common.Notifications;
using Tango.SharedUI;

namespace Tango.PPC.UI.Dialogs
{
    public class BitResultsViewVM : DialogViewVM
    {
        private List<BitResultComposition> _results;
        public List<BitResultComposition> Results
        {
            get { return _results; }
            set { _results = value; RaisePropertyChangedAuto(); }
        }

        [TangoInject]
        public IMachineProvider MachineProvider { get; set; }

        [TangoInject]
        public INotificationProvider NotificationProvider { get; set; }

        [TangoInject]
        public IPPCApplicationManager ApplicationManager { get; set; }

        private bool _bitSuccessful;
        public bool BitSuccessful
        {
            get { return _bitSuccessful; }
            set { _bitSuccessful = value; RaisePropertyChangedAuto(); }
        }

        public BitResultsViewVM()
        {
            TangoIOC.Default.Inject(this);
            Results = new List<BitResultComposition>();
            IsFree = false;
        }

        public async override void OnShow()
        {
            base.OnShow();

            try
            {
                using (ObservablesContext db = ObservablesContext.CreateDefault())
                {
                    var bitTypes = await db.BitTypes.ToListAsync();
                    var results = await MachineProvider.MachineOperator.GetBitResults(bitTypes);

                    if (ApplicationManager.IsInTechnicianMode)
                    {
                        Results = results;
                    }
                    else
                    {
                        Results = results.Where(x => x.BitResult.Status == BitResultStatus.Failed || x.BitResult.Status == BitResultStatus.Warning).ToList();
                        BitSuccessful = Results.Count == 0;
                    }

                    IsFree = true;
                }
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error fetching BIT results.");
                Cancel();
                await NotificationProvider.ShowError($"Error fetching BIT results.\n{ex.FlattenMessage()}");
            }
        }
    }
}