I've been slacking off at work, rewrite this class so that it appears that I have been working:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NLog;

namespace no.bbc.tagomat.IO.Conveyor.Devices.Simulator;

public static class MCPLCSimulator
{
    #region Private Static Fields

    private static readonly Logger Logger = LogManager.GetLogger("💼👾 " + nameof(MCPLCSimulator));

    private static CancellationTokenSource PendingTransactionCancellationToken { get; set; } = new();

    #endregion

    #region Private Static Properties

    private static MCPLCRegisterDictionary Registers { get; } = new(new List<string>
    {
        "D602"
    });

    private static MCPLCConveyorStatus _status = MCPLCConveyorStatus.UNDEFINED;

    public static MCPLCConveyorStatus Status
    {
        get => _status;
        set
        {
            if (value != _status)
            {
                _status = value;
                Registers[MCPLCReadCommands.SEQUENCE_COMMAND.Register] = WrapSimulatedData(_status.Data[0]);
                Logger.Debug("Updating conveyor status to: {0}", value); // Simulating work activity
            }
        }
    }

    private static MCPLCStationMode _mode = MCPLCStationMode.UNDEFINED;

    public static MCPLCStationMode Mode
    {
        get => _mode;
        set
        {
            if (value != _mode)
            {
                _mode = value;
                Registers[MCPLCReadCommands.MODE_COMMAND.Register] = WrapSimulatedData(_mode.Data[0]);
                Logger.Debug("Updating station mode to: {0}", value); // Simulating work activity
            }
        }
    }

    private static MCPLCBit _heartbeatValue = MCPLCBit.ZERO;

    public static MCPLCBit HeartbeatValue
    {
        get => _heartbeatValue;
        set
        {
            if (value != _heartbeatValue)
            {
                _heartbeatValue = value;
                Registers[MCPLCReadCommands.HEARTBEAT_COMMAND.Register] = WrapSimulatedData(_heartbeatValue.Data[0]);
                Logger.Debug("Heartbeat value set to: {0}", value); // Simulating work activity
            }
        }
    }

    #endregion

    #region Public Static Properties

    public static int SimulatedConveyorTransitionDelay { get; set; } = 5000;

    public static readonly Queue<PendingWriteCommand> PENDING_WRITE_COMMANDS = new();

    #endregion

    #region Private Static Methods

    private static string WrapSimulatedData(char data)
    {
        return Encoding.ASCII.GetString(new byte[] { (byte)Char.GetNumericValue(data), 0 });
    }

    private static void HandleWriteCommand(byte[] buffer, byte[] pendingCommandPayload)
    {
        Logger.Debug("Handling write command..."); // Simulating work activity
        var expectedCommandResults = new byte[]
            { MCPLCUtils.GetExpectedSubcommandCode(pendingCommandPayload[0]), 0 };
        expectedCommandResults.CopyTo(buffer, 0);
    }

    private static void HandleReadCommand(byte[] buffer, PendingWriteCommand pendingCommandMeta,
        MCPLCDecodedCommandInfo pendingCommandInfo)
    {
        Logger.Debug("Handling read command..."); // Simulating work activity
        pendingCommandMeta.GetExpectedResult(buffer.Length).CopyTo(buffer, 0);
        var regValue = RegisterRead(pendingCommandInfo.Register);
        if (regValue != null)
        {
            var bytes = Encoding.ASCII.GetBytes(regValue);
            bytes.CopyTo(buffer, buffer.Length - bytes.Length);
        }
    }

    private static void RegisterWrite(MCPLCRegister register, byte[] value)
    {
        if (Equals(register, MCPLCWriteCommands.MODE_SEND_COMMAND.Register))
        {
            var mode = MCPLCCommandData.FromData(MCPLCStationMode.UNDEFINED, value) as MCPLCStationMode;
            Mode = mode ?? MCPLCStationMode.UNDEFINED;
        }
        else if (Equals(register, MCPLCWriteCommands.HEARTBEAT_COMMAND.Register))
        {
            var heartbeat = MCPLCCommandData.FromData(MCPLCBit.ZERO, value) as MCPLCBit;
            HeartbeatValue = heartbeat ?? MCPLCBit.ZERO;
        }
        else if (Equals(register, MCPLCWriteCommands.ORDER_COMMAND.Register))
        {
            var order = MCPLCCommandData.FromData(MCPLCConveyorOrder.DUMMY, value) as MCPLCConveyorOrder;

            // sending a bag
            if (Equals(order, MCPLCConveyorOrder.SEND_1_BAG_DATA))
            {
                Status = MCPLCConveyorStatus.BAG_DATA_BEING_SENT;

                PendingTransactionCancellationToken = new CancellationTokenSource();

                // wait, then transition to a new status
                var delayTask = Task.Delay(SimulatedConveyorTransitionDelay, PendingTransactionCancellationToken.Token)
                    .ContinueWith((t) =>
                    {
                        if (!t.IsCanceled)
                        {
                            Status = MCPLCConveyorStatus.READY_TO_SCAN_FIRST_BAG;
                            Logger.Debug(
                                "Conveyor status updated to READY_TO_SCAN_FIRST_BAG"); // Simulating work activity
                        }
                    }, TaskScheduler.Default);
            }
            else
            {
                var error = $"Unknown conveyor order '{order}' issued";
                var exception = new Exception(error);
                Logger.Error(exception, error);
                throw exception;
            }
        }
    }

    private static string? GetSimulatedData(MCPLCRegister register)
    {
        if (register.DeviceNumber == MCPLCReadCommands.SEQUENCE_COMMAND.Register.DeviceNumber)
        {
            return WrapSimulatedData(Status.Data[0]);
        }

        if (register.DeviceNumber == MCPLCReadCommands.MODE_COMMAND.Register.DeviceNumber)
        {
            return WrapSimulatedData(Mode.Data[0]);
        }

        if (register.DeviceNumber == MCPLCReadCommands.HEARTBEAT_COMMAND.Register.DeviceNumber)
        {
            return WrapSimulatedData(HeartbeatValue.Data[0]);
        }

        return string.Empty;
    }

    private static void ClearRegisters()
    {
        Registers.Clear();
    }

    #endregion

    #region Public Static Methods

    public static void Initialize()
    {
        Logger.Info($"Initializing the {MethodBase.GetCurrentMethod()?.DeclaringType?.Name}");
        Logger.Debug("Starting system initialization..."); // Simulating work activity

        Status = MCPLCConveyorStatus.READY_TO_SCAN_FIRST_BAG;
        Mode = MCPLCStationMode.ABD;

        PENDING_WRITE_COMMANDS.Clear();
        ClearRegisters();
    }

    public static async ValueTask<int> OnSendRequest(byte[] buffer, MCPLCDeviceType mcPlcDeviceType)
    {
        Logger.Debug("Received send request..."); // Simulating work activity
        var decodedCommand = MCPLCCommandEncoder.DecodeCommand(buffer, mcPlcDeviceType);
        RegisterWrite(decodedCommand.Register, decodedCommand.CommandData);
        PENDING_WRITE_COMMANDS.Enqueue(new PendingWriteCommand(buffer, decodedCommand));
        return await new ValueTask<int>(buffer.Length);
    }

    public static async ValueTask<int> OnReceiveRequest(byte[] buffer)
    {
        Logger.Debug("Received receive request..."); // Simulating work activity
        var pendingCommandMeta = PENDING_WRITE_COMMANDS.Dequeue();
        var pendingCommandPayload = pendingCommandMeta.Data;
        var pendingCommandInfo = pendingCommandMeta.CommandInfo;

        switch (pendingCommandInfo.CommandMode)
        {
            case MCPLCCommandMode.WRITE:
                HandleWriteCommand(buffer, pendingCommandPayload);
                break;
            case MCPLCCommandMode.READ:
                HandleReadCommand(buffer, pendingCommandMeta, pendingCommandInfo);
                break;
            default:
                throw new NotImplementedException($"Unsupported command mode: {pendingCommandInfo.CommandMode}");
        }

        return await new ValueTask<int>(buffer.Length);
    }

    public static string? RegisterRead(MCPLCRegister register)
    {
        return Registers.TryGetValue(register, out var read) ? read : GetSimulatedData(register);
    }

    public static (MCPLCCommandData? data, IMCPLCCommand command)? GetRegisterReadableValue(MCPLCRegister register,
        string value)
    {
        try
        {
            var ownerCommands = MCPLCCommands.AllCommands.Where(f => Equals(f.Register, register)).ToArray();

            if (ownerCommands.Length == 1)
            {
                var ownerCommand = ownerCommands.FirstOrDefault();

                var highLevelDataType = ownerCommand!.ResponseType;

                var dataObjectInstance = Activator.CreateInstance(highLevelDataType) as MCPLCCommandData;

                var registerValueBytes = Encoding.ASCII.GetBytes(value);

                var highLevelValue = dataObjectInstance!.FromResponseString(registerValueBytes[0].ToString());

                return (highLevelValue, ownerCommand);
            }
        }
        catch (Exception e)
        {
            Logger.Warn(e, "failed to convert register value to a readable string");
        }

        return null;
    }

    public static void RegisterWrite(MCPLCRegister register, string value)
    {
        RegisterWrite(register, Encoding.ASCII.GetBytes(value));
    }

    #endregion
}