aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Connection/DefaultMachineProvider.cs
blob: d97265ed615c131cbbe8bfd5cd00af2bc9d0dabb (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
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core;
using Tango.Integration.Operation;
using Tango.Integration.ExternalBridge;
using Tango.PMR.Connection;
using Tango.Transport.Adapters;

namespace Tango.PPC.Common.Connection
{
    public class DefaultMachineProvider : ExtendedObject, IMachineProvider
    {
        private bool _isInitialized;

        public event EventHandler<MachineOperatorChangedEventArgs> MachineOperatorChanged;

        private Machine _machine;
        public Machine Machine
        {
            get
            {
                return _machine;
            }
            private set
            {
                _machine = value;
                RaisePropertyChangedAuto();
            }
        }

        private IMachineOperator _machineOperator;
        public IMachineOperator MachineOperator
        {
            get
            {
                return _machineOperator;
            }
            private set
            {
                var oldOperator = _machineOperator;
                _machineOperator = value;
                OnMachineOperatorChanged(oldOperator, _machineOperator);
                RaisePropertyChangedAuto();
            }
        }

        protected virtual void OnMachineOperatorChanged(IMachineOperator oldOperator, IMachineOperator newOperator)
        {
            MachineOperatorChanged?.Invoke(this, new MachineOperatorChangedEventArgs(oldOperator, newOperator));
        }

        public DefaultMachineProvider()
        {
            MachineOperator = new MachineOperator();
        }

        public void Init()
        {
            if (!_isInitialized)
            {
                _isInitialized = true;

                Machine = ObservablesEntitiesAdapter.Instance.Machines.FirstOrDefault();
                ConnectToMachine();
            }
        }

        private async void ConnectToMachine()
        {
            try
            {
                Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse> scanner = new Transport.Discovery.UsbCommunicationScanner<ConnectRequest, ConnectResponse>(UsbSerialBaudRates.BR_9600);
                var response = await scanner.Scan(new ConnectRequest() { Password = "1234" }, TimeSpan.FromSeconds(20));

                await MachineOperator.Disconnect();
                MachineOperator.Adapter = response.Adapter;
                MachineOperator.JobHandlingMode = JobHandlerModes.SettingUp;
                await MachineOperator.Connect();
            }
            catch { }
        }
    }
}