So I've decided to roll a reusable micros interface class, which is working pretty well:
The issue I'm trying to solve.. does Micros interface messages support any kind of back and forth? Micros seems to always close the connection immediately after getting the first reply. Is there any way to have a .. conversation, I guess.. with the workstation?
Code:
[COLOR=#0000FF]using[/color] System;
[COLOR=#0000FF]using[/color] System.Windows.Forms;
[COLOR=#0000FF]using[/color] CommonLibraries.Google;
[COLOR=#0000FF]using[/color] CommonLibraries.Micros.InterfaceComponents;
[[COLOR=#0000FF]assembly[/color]: [COLOR=#2B91AF]DefaultNamespace[/color]([COLOR=#A31515]"MicrosTestInterface"[/color])]
[COLOR=#0000FF]namespace[/color] MicrosTestInterface
{
[COLOR=#0000FF]public[/color] [COLOR=#0000FF]partial[/color] [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Form1[/color] : [COLOR=#2B91AF]Form[/color]
{
[COLOR=#0000FF]private[/color] [COLOR=#2B91AF]MicrosInterfaceServer[/color] _interface;
[COLOR=#0000FF]public[/color] Form1()
{
InitializeComponent();
[COLOR=#008000]//start a new interface server on port 32400 which requires outgoing message name to be TEST[/color]
_interface = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]MicrosInterfaceServer[/color](32400, [COLOR=#A31515]"TEST"[/color], HandleClient);
_interface.Start();
}
[COLOR=#0000FF]private[/color] [COLOR=#2B91AF]MicrosMessage[/color] HandleClient([COLOR=#2B91AF]MicrosClient[/color] client)
{
[COLOR=#008000]//get the first message received from the client[/color]
[COLOR=#2B91AF]MicrosMessage[/color] message = client.MicrosMessage;
AddMessage([COLOR=#A31515]"Request from workstation: "[/color] + message.UWS);
[COLOR=#008000]//check if we received our only valid command[/color]
[COLOR=#0000FF]if[/color] (message.Command == [COLOR=#A31515]"get_distance"[/color])
{
[COLOR=#0000FF]if[/color] (message.DataParts.Length != 2)
[COLOR=#0000FF]return[/color] message.CreateReply([COLOR=#A31515]"error"[/color], [COLOR=#A31515]"Invalid Number of Parameters"[/color]); [COLOR=#008000]//we weren't provided with two strings, so send back an rxmsg: error event[/color]
[COLOR=#0000FF]string[/color] add1 = message.DataParts[0];
[COLOR=#0000FF]string[/color] add2 = message.DataParts[1];
AddMessage([COLOR=#A31515]"Address 1: "[/color] + add1);
AddMessage([COLOR=#A31515]"Address 2: "[/color] + add2);
[COLOR=#2B91AF]DistanceResult[/color] result = [COLOR=#2B91AF]GoogleDirections[/color].GetDistance(add1, add2, [COLOR=#2B91AF]DistanceUnits[/color].Imperial, [COLOR=#2B91AF]DistanceMethod[/color].Driving);
[COLOR=#0000FF]if[/color] (result == [COLOR=#0000FF]null[/color]) [COLOR=#008000]//send back rxmsg: error letting them know we couldn't get to the Google API - usually means firewall[/color]
[COLOR=#0000FF]return[/color] message.CreateReply([COLOR=#A31515]"error"[/color], [COLOR=#A31515]"Failed to get distance between addresses"[/color]);
[COLOR=#0000FF]if[/color] (result.Status != [COLOR=#2B91AF]DirectionsStatus[/color].OK) [COLOR=#008000]//send back rxmsg: error letting them know the error the Google API returned[/color]
[COLOR=#0000FF]return[/color] message.CreateReply([COLOR=#A31515]"error"[/color], [COLOR=#A31515]"Error Occured: "[/color] + result.Status.ToString());
AddMessage([COLOR=#A31515]"Distance: "[/color] + result.Distance);
AddMessage([COLOR=#0000FF]string[/color].Empty); [COLOR=#008000]//send back the distance; its possible to end up with a negative when getting distances between coordinates so abs it[/color]
[COLOR=#0000FF]return[/color] message.CreateReply([COLOR=#A31515]"show_distance"[/color], [COLOR=#2B91AF]Math[/color].Abs(result.Distance));
}
[COLOR=#008000]//didn't receive a get_distance request, so send be an rxmsg: error event[/color]
[COLOR=#0000FF]return[/color] message.CreateReply([COLOR=#A31515]"error"[/color], [COLOR=#A31515]"Unknown Interface Command"[/color]);
}
[COLOR=#0000FF]private[/color] [COLOR=#0000FF]void[/color] AddMessage([COLOR=#0000FF]string[/color] message)
{
[COLOR=#0000FF]if[/color] (InvokeRequired)
{
Invoke(([COLOR=#2B91AF]MethodInvoker[/color])(() => { AddMessage(message); }));
[COLOR=#0000FF]return[/color];
}
textBox1.Text += message + [COLOR=#2B91AF]Environment[/color].NewLine;
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
}
}
}
The issue I'm trying to solve.. does Micros interface messages support any kind of back and forth? Micros seems to always close the connection immediately after getting the first reply. Is there any way to have a .. conversation, I guess.. with the workstation?