aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Operation/JobDescriptionFile.cs
blob: f87252dde614933f388b4f3b123090e91c3b7f1a (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
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR;
using Tango.PMR.Common;
using Tango.PMR.Printing;

namespace Tango.Integration.Operation
{
    /// <summary>
    /// Represents a job description file containing a series of job segments and brush stops.
    /// </summary>
    public class JobDescriptionFile
    {
        private IEnumerable<JobSegment> _segments;

        /// <summary>
        /// Initializes a new instance of the <see cref="JobDescriptionFile"/> class.
        /// </summary>
        /// <param name="segments">The segments.</param>
        public JobDescriptionFile(IEnumerable<JobSegment> segments)
        {
            _segments = segments;
        }

        /// <summary>
        /// Returns a memory stream containing the job description file data.
        /// </summary>
        /// <returns></returns>
        public MemoryStream ToStream()
        {
            List<IMessage> messages = new List<IMessage>();

            foreach (var segment in _segments)
            {
                var seg = new JobDescriptionFileSegment()
                {
                    Name = segment.Name,
                    Length = segment.Length,
                    BrushStopsCount = segment.BrushStops.Count,
                };

                messages.Add(seg);

                foreach (var stop in segment.BrushStops)
                {
                    var st = new JobDescriptionFileBrushStop()
                    {
                        Index = stop.Index,
                        OffsetMeters = stop.OffsetMeters,
                        OffsetPercent = stop.OffsetPercent,
                    };

                    st.Dispensers.AddRange(stop.Dispensers.ToList());

                    messages.Add(st);
                }
            }

            MemoryStream ms = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            foreach (var msg in messages)
            {
                byte[] data = msg.ToByteArray();

                writer.Write(data.Length);
                writer.Write(data);
            }

            ms.Position = 0;

            return ms;
        }

        /// <summary>
        /// Returns an array of bytes containing the job description file data.
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes()
        {
            using (var ms = ToStream())
            {
                return ms.ToArray();
            }
        }

        /// <summary>
        /// Reads the job description file.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        public static List<JobSegment> ReadJobDescriptionFile(String filePath)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {
                return ReadJobDescriptionFile(fs);
            }
        }

        /// <summary>
        /// Reads the job description file.
        /// </summary>
        ///<param name="stream">Source stream.</param>
        /// <returns></returns>
        public static List<JobSegment> ReadJobDescriptionFile(Stream stream)
        {
            List<JobSegment> segments = new List<JobSegment>();

            using (BinaryReader reader = new BinaryReader(stream))
            {
                while (stream.Position < stream.Length)
                {
                    int length = reader.ReadInt32();
                    var segment = JobDescriptionFileSegment.Parser.ParseFrom(reader.ReadBytes(length));

                    JobSegment s = new JobSegment();
                    s.Length = segment.Length;
                    s.Name = segment.Name;

                    for (int i = 0; i < segment.BrushStopsCount; i++)
                    {
                        length = reader.ReadInt32();
                        var stop = JobDescriptionFileBrushStop.Parser.ParseFrom(reader.ReadBytes(length));

                        JobBrushStop st = new JobBrushStop();
                        st.Dispensers.AddRange(stop.Dispensers);
                        st.Index = stop.Index;
                        st.OffsetMeters = stop.OffsetMeters;
                        st.OffsetPercent = stop.OffsetPercent;

                        s.BrushStops.Add(st);
                    }

                    segments.Add(s);
                }
            }

            return segments;
        }
    }
}