aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Operators/MachineOperator.cs
blob: 17884b6776373ae45048598268782ceee5107424 (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
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR;
using Tango.PMR.Diagnostics;
using Tango.Transport;
using Tango.Transport.Transporters;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Threading;
using Tango.PMR.Common;

namespace Tango.Integration.Operators
{
    public class MachineOperator : BasicTransporter, IMachineOperator
    {
        /// <summary>
        /// Occurs when there is new diagnostics data available.
        /// </summary>
        public event EventHandler<PushDiagnosticsResponse> DiagnosticsDataAvailable;

        private bool _enableDiagnostics;
        /// <summary>
        /// Gets or sets a value indicating whether to enable diagnostics messages by requesting diagnostics messages.
        /// </summary>
        public bool EnableSensorsUpdate
        {
            get { return _enableDiagnostics; }
            set
            {
                if (_enableDiagnostics != value)
                {
                    _enableDiagnostics = value;
                    RaisePropertyChangedAuto();
                    OnEnableSensorsUpdateChanged(value);
                }
            }
        }

        /// <summary>
        /// Called when the enable sensors update property has been changed
        /// </summary>
        /// <param name="value">if set to <c>true</c> [value].</param>
        protected virtual void OnEnableSensorsUpdateChanged(bool value)
        {
            if (value && State == TransportComponentState.Connected)
            {
                SendContinuousRequest<PushDiagnosticsRequest, PushDiagnosticsResponse>(new TangoMessage<PushDiagnosticsRequest>(new PushDiagnosticsRequest()
                {
                    PushMotors = true,
                    PushSensors = true,

                }, MessageType.PushDiagnosticsRequest)).ObserveOn(new NewThreadScheduler()).Subscribe(
                    (response) =>
                    {
                        OnDiagnosticsDataAvailable(response);
                    },
                    (ex) =>
                    {
                        //Do I need separate event for each one ??
                    },
                    () =>
                    {
                        //What to do now ??
                    });
            }
        }

        /// <summary>
        /// Invokes the <see cref="DiagnosticsDataAvailable"/> event.
        /// </summary>
        /// <param name="data">The sensors data.</param>
        protected virtual void OnDiagnosticsDataAvailable(PushDiagnosticsResponse data)
        {
            DiagnosticsDataAvailable?.Invoke(this, data);
        }

        /// <summary>
        /// Called when the component state has changed.
        /// </summary>
        /// <param name="state">The state.</param>
        protected override void OnStateChanged(TransportComponentState state)
        {
            base.OnStateChanged(state);

            OnEnableSensorsUpdateChanged(EnableSensorsUpdate);
        }
    }
}