šŸŒŸ Pending Release - 'Klient' - A multi-tool

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.

Itā€™s about the same, weā€™ll just go with the one without Tuples. The expanded version of the first is a tiny bit more readable, but not much.

You certainly canā€™t read the compacted paste version at all! :rofl:
So I guess it doesnā€™t matter.

Success!

I created a universal paste solution, and just tested it. (Itā€™s not compact yet lol)

What I did:

Opened a BPM (Actually a UBAQ BPM, but thatā€™s a distinction without a real differenceā€¦)
Added this to the usings:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System.Drawing;

then pasted my helper at the top

//Begin Helper------------------------------------------------------------->
//Bunch of crap here lol
//<----------------------------------------------------------------End Helper

//And under that...
Konsole.Beep(1000, 250);

Hit the GetList button.

And it made an annoying beep at me.

Happy Fun GIF by reactionseditor

5 Likes

You putting this in github so we can log issues right?

2 Likes

Eventually. Gotta relearn github, been a bit.

2 Likes