aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Operators/MachineOperator.cs
blob: cc42b64ed8dd0bad6d0241ada1de5266389e2e7b (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
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;
using Tango.PMR.Printing;
using Tango.Integration.Observables;
using System.Reactive.Subjects;
using Tango.Integration.Printing;

namespace Tango.Integration.Operators
{
    /// <summary>
    /// Represents the Tango machine operator default implementation.
    /// </summary>
    /// <seealso cref="Tango.Transport.Transporters.BasicTransporter" />
    /// <seealso cref="Tango.Integration.Operators.IMachineOperator" />
    public class MachineOperator : BasicTransporter, IMachineOperator
    {
        #region Events

        /// <summary>
        /// Occurs when there is new diagnostics data available.
        /// </summary>
        public event EventHandler<PushDiagnosticsResponse> DiagnosticsDataAvailable;

        #endregion

        #region Properties

        private bool _enableDiagnostics;
        /// <summary>
        /// Gets or sets a value indicating whether direct the embedded device to send diagnostics messages.
        /// </summary>
        public bool EnableDiagnostics
        {
            get { return _enableDiagnostics; }
            set
            {
                if (_enableDiagnostics != value)
                {
                    _enableDiagnostics = value;
                    RaisePropertyChangedAuto();
                    OnEnableDiagnosticsChanged(value);
                }
            }
        }

        #endregion

        #region Virtual Methods

        /// <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 OnEnableDiagnosticsChanged(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);
        }

        #endregion

        #region Protected Methods

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

            OnEnableDiagnosticsChanged(EnableDiagnostics);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Prints the specified job using the specified job parameters.
        /// </summary>
        /// <param name="job">The job.</param>
        /// <param name="processParameters">Process parameters table</param>
        /// <returns></returns>
        public JobHandler Print(Job job, ProcessParametersTable processParameters)
        {
            JobRequest request = new JobRequest();

            JobTicket ticket = new JobTicket();
            ticket.EnableInterSegment = job.EnableInterSegment;
            ticket.InterSegmentLength = job.InterSegmentLength;
            ticket.Length = job.Length;
            ticket.WindingMethod = (JobWindingMethod)job.WindingMethod.Code;

            ProcessParameters process = new ProcessParameters();
            process.DyeingSpeed = processParameters.DyeingSpeed;
            process.MinInkUptake = processParameters.MinInkUptake;
            process.MixerTemp = processParameters.MixerTemp;
            process.HeadZone1Temp = processParameters.HeadZone1Temp;
            process.HeadZone2Temp = processParameters.HeadZone2Temp;
            process.HeadZone3Temp = processParameters.HeadZone3Temp;
            process.HeadAirFlow = processParameters.HeadAirFlow;
            process.FeederTension = processParameters.FeederTension;
            process.PullerTension = processParameters.PullerTension;
            process.DryerBufferLength = processParameters.DryerBufferLength;
            process.DryerZone1Temp = processParameters.DryerZone1Temp;
            process.DryerZone2Temp = processParameters.DryerZone2Temp;
            process.DryerZone3Temp = processParameters.DryerZone3Temp;
            process.DryerAirFlow = processParameters.DryerAirFlow;
            process.WinderTension = processParameters.WinderTension;

            ticket.ProcessParameters = process;

            request.JobTicket = ticket;

            JobHandler handler = null;

            handler = new JobHandler(async () =>
            {
                try
                {
                    var result = await SendRequest<AbortJobRequest, AbortJobResponse>(new AbortJobRequest());
                    handler.RaiseCanceled();
                }
                catch (Exception)
                {
                    //TODO: What to do in case job failed to cancel ??
                }
            });

            SendContinuousRequest<JobRequest, JobResponse>(request).Subscribe((response) =>
            {
                handler.RaiseStatusReceived(response.Message.Status);
            }, (ex) =>
             {
                 if (!handler.IsCanceled)
                 {
                     handler.RaiseFailed(ex);
                 }
             }, () =>
             {
                 handler.RaiseCompleted();
             });

            return handler;
        }

        #endregion
    }
}