-
Notifications
You must be signed in to change notification settings - Fork 12
/
Message.cs
244 lines (223 loc) · 11 KB
/
Message.cs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
namespace UniversaLIS
{
/* Example of unidirectional message structure/hierarchy:
* Header
* Patient
* Order
* Result
* Order
* Result
* Patient
* Order
* Result
* Terminator
*/
public class Message
{
public Dictionary<string, string> Elements { get; set; } = new Dictionary<string, string>();
public List<string> FrameList { get; set; } = new List<string>();
private bool isReady = false;
public List<Patient> Patients { get; set; } = new List<Patient>();
public List<Query> Queries { get; set; } = new List<Query>();
private int FrameCounter { get; set; }
private readonly int frameSize;
public string MessageHeader
{
get
{
return GetHeaderString();
}
set
{
SetHeaderString(value);
}
}
public string? TerminationMessage
{
get; set;
}
public char Terminator { get; set; }
private void FrameMessage(string InputString)
{
/* According to the ASTM E1381-95 standard, frames longer than 240 characters
* -- 247 characters including frame overhead (<STX>[FrameNumber]...<ETX>[Checksum]<CR><LF>) --
* are sent as one or more intermediate frames followed by an end frame.
* Shorter messages are sent as a single end frame.
* Intermediate frames use <ETB> in place of <ETX> to indicate that it is continued
* in the next frame.
* This procedure splits long frames into intermediate frames if necessary
* before appending the checksum and <CR><LF> and adding the frame to the message FrameList.
* CLSI-LIS1-A increased this frame size to 64,000 including frame overhead.
* The use_legacy_frame_size setting in config.yml is used to specify which size to use.
*/
if (InputString.Length > frameSize + 3) // <STX> + FrameNumber + frame + <ETX>
{
string firstString = InputString.Substring(0, frameSize + 2); // +2 to make room for the <ETB>
int firstStringLength = firstString.Length;
int iLength = InputString.Length - firstStringLength;
firstString += Constants.ETB;
firstString += UniversaLIService.CHKSum(firstString);
firstString += Constants.CR + Constants.LF;
string nextString = InputString.Substring(firstStringLength, iLength); // The remainder of the string
FrameList.Add(firstString); // Add intermediate frame to list // is passed to this function recursively
FrameMessage(nextString); // to be added as its own frame(s)
}
else
{
InputString += UniversaLIService.CHKSum(InputString) + Constants.CR + Constants.LF; // Tag on the checksum and <CR><LF>
FrameList.Add(InputString); // Add the end frame
}
}
private string GetHeaderString()
{
// Anything missing should be added as an empty string.
string[] elementArray = { "FrameNumber", "Delimiter Def.", "Message Control ID", "Password", "Sending systems company name", "Sending systems address", "Reserved", "Senders phone #", "Communication parameters", "Receiver ID", "Comments/special instrucitons", "Processing ID", "Version #", "Message Date + Time" };
foreach (var item in elementArray)
{
if (!Elements.ContainsKey(item))
{
Elements.Add(item, "");
}
}
// Concatenate the Dictionary values and return the string.
string output = Constants.STX + Elements["FrameNumber"] + "H|";
output += Elements["Delimiter Def."] + "|";
output += Elements["Message Control ID"] + "|";
output += Elements["Password"] + "|";
output += Elements["Sending systems company name"] + "|";
output += Elements["Sending systems address"] + "|";
output += Elements["Reserved"] + "|";
output += Elements["Senders phone #"] + "|";
output += Elements["Communication parameters"] + "|";
output += Elements["Receiver ID"] + "|";
output += Elements["Comments/special instrucitons"] + "|";
output += Elements["Processing ID"] + "|";
output += Elements["Version #"] + "|";
output += Elements["Message Date + Time"] + Constants.CR + Constants.ETX;
return output;
}
private int IncrementFrameCount()
{
if (FrameCounter == 7)
{
FrameCounter = 0;
}
else
{
FrameCounter += 1;
}
return FrameCounter;
}
public void PrepareToSend()
{
if (isReady)
{
return;
}
// The header fields should already have been set to define the MessageHeader string.
if (MessageHeader.Length == 0)
{
throw new InvalidOperationException("Missing MessageHeader string.");
}
FrameList.Clear();
FrameMessage(MessageHeader);
// Then process each patient and their orders, incrementing the frame number with each record.
if (Patients.Count > 0)
{
int pCount = 0;
foreach (var patient in Patients)
{
pCount += 1;
patient.Elements["FrameNumber"] = IncrementFrameCount().ToString();
// Assign proper Sequence Number to each patient.
patient.Elements["SequenceNumber"] = pCount.ToString();
// Add the patient message to the FrameList.
FrameMessage(patient.PatientMessage);
// Do the same for each order message.
int oCount = 0;
foreach (var order in patient.Orders)
{
oCount += 1;
order.Elements["FrameNumber"] = IncrementFrameCount().ToString();
// Assign proper Sequence Number to each order.
order.Elements["SequenceNumber"] = oCount.ToString();
FrameMessage(order.OrderMessage);
// If there are any result messages for the order, prepare those, too.
int rCount = 0;
foreach (var result in order.Results)
{
rCount += 1;
result.Elements["FrameNumber"] = IncrementFrameCount().ToString();
// Assign proper Sequence Number to each order.
result.Elements["SequenceNumber"] = rCount.ToString();
FrameMessage(result.ResultMessage);
}
}
}
}
// Finally, don't forget the Terminator message.
if (Terminator < 'E')
{
Terminator = 'N';
}
TerminationMessage = Constants.STX + IncrementFrameCount().ToString() + "L|1|" + Terminator + Constants.CR + Constants.ETX;
FrameMessage(TerminationMessage);
isReady = true;
}
private void SetHeaderString(string input)
{
string[] inArray = input.Split('|');
if (inArray.Length < 14)
{
// Invalid number of elements.
throw new ArgumentException($"Invalid number of elements in header record string. Expected: 14 \tFound: {inArray.Length} \tString: \n{input}");
}
FrameCounter = 1;
Elements["FrameNumber"] = "1";
Elements["Delimiter Def."] = inArray[1];
Elements["Message Control ID"] = inArray[2];
Elements["Password"] = inArray[3];
Elements["Sending systems company name"] = inArray[4];
Elements["Sending systems address"] = inArray[5];
Elements["Reserved"] = inArray[6];
Elements["Senders phone #"] = inArray[7];
Elements["Communication parameters"] = inArray[8];
Elements["Receiver ID"] = inArray[9];
Elements["Comments/special instrucitons"] = inArray[10];
Elements["Processing ID"] = inArray[11];
Elements["Version #"] = inArray[12];
if (inArray[13].Length < 14 && inArray[13].Length >= 8)
{
// Date only.
Elements["Message Date + Time"] = inArray[13].Substring(0, 8);
}
else if (inArray[13].Length >= 14)
{
// Date and time.
Elements["Message Date + Time"] = inArray[13].Substring(0, 14);
}
else
{
// Invalid datetime format.
throw new ArgumentOutOfRangeException($"Invalid datetime in header record string. Expected: YYYYMMDDHHMMSS or YYYYMMDD \tFound: {inArray[13]} \tString: \n{input}");
}
}
public Message(string messageHeader)
{
MessageHeader = messageHeader;
}
public Message(CommFacilitator facilitator)
{
frameSize = facilitator.frameSize;
string dateString;
DateTime dateTime = DateTime.Now;
dateString = dateTime.Year.ToString() + dateTime.Month.ToString("D2") + dateTime.Day.ToString("D2");
dateString += dateTime.Hour.ToString("D2") + dateTime.Minute.ToString("D2") + dateTime.Second.ToString("D2");
string header = Constants.STX + $"1H|\\^&||{facilitator.password}|{UniversaLIService.YamlSettings?.ServiceConfig?.LisId}|{UniversaLIService.YamlSettings?.ServiceConfig?.Address}";
header += $"||{UniversaLIService.YamlSettings?.ServiceConfig?.Phone}|{facilitator.GetPortDetails()}|{facilitator.receiver_id}||P|1|{dateString}";
MessageHeader = header;
}
}
}