Got back from riding go karts and worked a bit on the cut and paste āHelperā.
There will be a library and a few cut & paste helpers to chose from.
You would use this particular helper if you were going to do a lot of Console operations.
Otherwise, for a one off, just call the (epicor) function yourself, or use a more simple helper that I will provide as well.
This one will have all the logic for connection to Azure relay in the top. (Not shown) so if you plan to do a lot of IO in one place, you arenāt creating connection after connection.
This is missing the top portion that will handle connections, but this shouldnāt be too bad to paste in.
This is for paste in functions. BPMs use a slightly lower C# version, so Iāll have to adjust a bit.
Paste version of the helper (partial)
dynamic ThisKonsoleBase = null; //Cheat to reference KonsoleBase below
var KonsoleBase = new { CreateMessage = new Func<(string? messageType, string? messageSubtype, object? messageExtraData, object? serializableObjectForMessage), string>((input) =>
{ try { return JsonConvert.SerializeObject(new { MessageID = Guid.NewGuid(), MessageType = input.messageType, MessageSubtype = input.messageSubtype, MessageExtraData = input.messageExtraData, Message = input.serializableObjectForMessage, SessionInfo = JsonConvert.DeserializeObject(ThisLib.GetSessionInfo())}); }
catch { return JsonConvert.SerializeObject(new { MessageID = Guid.NewGuid(), MessageType = String.Empty, MessageSubtype = String.Empty, MessageExtraData = new {}, Message = new {}, SessionInfo = JsonConvert.DeserializeObject(ThisLib.GetSessionInfo())}); } }),
GetColorTag = new Func<System.Drawing.Color, object>((color) => { if(color.IsKnownColor) return new { color = color.Name }; return new { color = $"{{{color.A},{color.R},{color.G},{color.B}}}" }; }),
SendKlientMessage = new Func<string, string>((klientMessage) => SendKlientMessage(klientMessage)),
SendMessage = new Func<(string? messageType, string? messageSubtype, object? messageExtraData, object? serializableObjectForMessage), string>((input) => { return SendKlientMessage(ThisKonsoleBase.CreateMessage(input)); }),
SendKonsoleMessage = new Func<(string? messageSubtype, object? messageExtraData, object? serializableObjectForMessage), string>((input) =>
{ var newInput = (messageType: "Console", messageSubtype: input.messageSubtype, messageExtraData: input.messageExtraData, serializableObjectForMessage: input.serializableObjectForMessage);
return SendKlientMessage(ThisKonsoleBase.CreateMessage(newInput)); }) }; ThisKonsoleBase = KonsoleBase; //Cheat to reference KonsoleBase. Make sure we assign it!
var Konsole = new { Clear = new Func<string>(() => KonsoleBase.SendKonsoleMessage(("Clear", null, null))),
Write = new Func<object, string>((s) => KonsoleBase.SendKonsoleMessage(("Write", null, s))),
WriteColor = new Func<object, Color, string>((s, c) => KonsoleBase.SendKonsoleMessage(("Write", KonsoleBase.GetColorTag(c), s))),
WriteLine = new Func<object, string>((s) => KonsoleBase.SendKonsoleMessage(("WriteLine", null, s))),
WriteLineColor = new Func<object, Color, string>((s, c) => KonsoleBase.SendKonsoleMessage(("WriteLine", KonsoleBase.GetColorTag(c), s))),
Echo = new Func<object, string>((s) => KonsoleBase.SendKonsoleMessage(("Echo", null, s))),
DumpTextFile = new Func<object, bool, string>((s, pretty) => KonsoleBase.SendKonsoleMessage(("DumpTextFile", new { pretty, filename = "" }, s))),
DumpTextFileAs = new Func<object, string, bool, string>((s, filename, pretty) => KonsoleBase.SendKonsoleMessage(("DumpTextFile", new { pretty, filename }, s))),
DumpBinaryFile = new Func<object, string>((s) => KonsoleBase.SendKonsoleMessage(("DumpBinaryFile", new {filename = "" }, s))),
DumpBinaryFileAs = new Func<string, string, string>((s, filename) => KonsoleBase.SendKonsoleMessage(("DumpBinaryFile", new { filename }, s))),
Beep = new Func<int, int, string>((frequency, duration) => KonsoleBase.SendKonsoleMessage(("Beep", null, $"{frequency.ToString()}|{duration.ToString()}"))),
DumpKonsoleMessage = new Func<string, string>((konsoleMessage) => { try { var msg = JToken.Parse(konsoleMessage); string originalMessageType = msg["MessageType"].ToString(),
originalMessageSubtype = msg["MessageSubtype"].ToString(); msg["MessageSubtype"] = $"DumpKonsoleMessage->{originalMessageType}|{originalMessageSubtype}";
msg["MessageType"] = "Console"; return KonsoleBase.SendKlientMessage(msg.ToString()); } catch {} return null; }),
YesNo = new Func<string, string, int, string>((caption, text, defaultButton) => { var msg = new { caption, text, @default = defaultButton == 0 ? "No" : "Yes" };
return KonsoleBase.SendKonsoleMessage(("YesNo", null, msg)); }) };
Reduced from ~
dynamic ThisKonsoleBase = null; // Cheat to reference KonsoleBase below
var KonsoleBase = new
{
CreateMessage = new Func<(string? messageType, string? messageSubtype, object? messageExtraData, object? serializableObjectForMessage), string>(input =>
{
try
{
return JsonConvert.SerializeObject(new
{
MessageID = Guid.NewGuid(),
MessageType = input.messageType,
MessageSubtype = input.messageSubtype,
MessageExtraData = input.messageExtraData,
Message = input.serializableObjectForMessage,
SessionInfo = JsonConvert.DeserializeObject(ThisLib.GetSessionInfo())
});
}
catch
{
return JsonConvert.SerializeObject(new
{
MessageID = Guid.NewGuid(),
MessageType = String.Empty,
MessageSubtype = String.Empty,
MessageExtraData = new { },
Message = new { },
SessionInfo = JsonConvert.DeserializeObject(ThisLib.GetSessionInfo())
});
}
}),
GetColorTag = new Func<System.Drawing.Color, object>(color =>
{
if (color.IsKnownColor)
return new { color = color.Name };
return new { color = $"{{{color.A},{color.R},{color.G},{color.B}}}" };
}),
SendKlientMessage = new Func<string, string>(SendKlientMessage),
SendMessage = new Func<(string? messageType, string? messageSubtype, object? messageExtraData, object? serializableObjectForMessage), string>(input =>
{
return SendKlientMessage(ThisKonsoleBase.CreateMessage(input));
}),
SendKonsoleMessage = new Func<(string? messageSubtype, object? messageExtraData, object? serializableObjectForMessage), string>(input =>
{
var newInput = (messageType: "Console", messageSubtype: input.messageSubtype, messageExtraData: input.messageExtraData, serializableObjectForMessage: input.serializableObjectForMessage);
return SendKlientMessage(ThisKonsoleBase.CreateMessage(newInput));
})
};
ThisKonsoleBase = KonsoleBase; // Cheat to reference KonsoleBase. Make sure we assign it!
var Konsole = new
{
Clear = new Func<string>(() => KonsoleBase.SendKonsoleMessage(("Clear", null, null))),
Write = new Func<object, string>(s => KonsoleBase.SendKonsoleMessage(("Write", null, s))),
WriteColor = new Func<object, Color, string>((s, c) => KonsoleBase.SendKonsoleMessage(("Write", KonsoleBase.GetColorTag(c), s))),
WriteLine = new Func<object, string>(s => KonsoleBase.SendKonsoleMessage(("WriteLine", null, s))),
WriteLineColor = new Func<object, Color, string>((s, c) => KonsoleBase.SendKonsoleMessage(("WriteLine", KonsoleBase.GetColorTag(c), s))),
Echo = new Func<object, string>(s => KonsoleBase.SendKonsoleMessage(("Echo", null, s))),
DumpTextFile = new Func<object, bool, string>((s, pretty) => KonsoleBase.SendKonsoleMessage(("DumpTextFile", new { pretty, filename = "" }, s))),
DumpTextFileAs = new Func<object, string, bool, string>((s, filename, pretty) => KonsoleBase.SendKonsoleMessage(("DumpTextFile", new { pretty, filename }, s))),
DumpBinaryFile = new Func<object, string>((s) => KonsoleBase.SendKonsoleMessage(("DumpBinaryFile", new { filename = "" }, s))),
DumpBinaryFileAs = new Func<string, string, string>((s, filename) => KonsoleBase.SendKonsoleMessage(("DumpBinaryFile", new { filename }, s))),
Beep = new Func<int, int, string>((frequency, duration) => KonsoleBase.SendKonsoleMessage(("Beep", null, $"{frequency}|{duration}"))),
DumpKonsoleMessage = new Func<string, string>(konsoleMessage =>
{
try
{
var msg = JToken.Parse(konsoleMessage);
string originalMessageType = msg["MessageType"]?.ToString(),
originalMessageSubtype = msg["MessageSubtype"]?.ToString();
msg["MessageSubtype"] = $"DumpKonsoleMessage->{originalMessageType}|{originalMessageSubtype}";
msg["MessageType"] = "Console";
return KonsoleBase.SendKlientMessage(msg.ToString());
}
catch
{
return null;
}
}),
YesNo = new Func<string, string, int, string>((caption, text, defaultButton) =>
{
var msg = new { caption, text, @default = defaultButton == 0 ? "No" : "Yes" };
return KonsoleBase.SendKonsoleMessage(("YesNo", null, msg));
})
};
If the BPM version doesnāt end up being super long, may end up with that one as a universal solution.
For any of you that are confused about the helpers, donāt worry, I will explain.