コード例 #1
0
 internal virtual ReportResponse HandleReenable(ReportRequest request)
 {
     _response           = _ardResponseFac.InvalidRequest <ReportResponse>(ErrorEnum.ShouldBeAlreadyEnabled, CommandEnum.Enable);
     _proxy.Info.Enabled = true;
     LogOnDashAsync($"{_proxy.Info.UniqueName} asked to reenable but should already be enabled.");
     return((ReportResponse)_response);
 }
コード例 #2
0
 internal virtual OrderResponse HandleReady(OrderRequest request)
 {
     _response                = _ardResponseFac.InvalidRequest <OrderResponse>(ErrorEnum.ShouldNotBeProcessing);
     _proxy.Info.Enabled      = true;
     _proxy.Info.MakingCoffee = false;
     CallProxyActionEvent(ProxyEventEnum.ToldThatItShouldNotBeProcessing);
     LogOnDashAsync($"{_proxy.Info.UniqueName} called ready for order {request.oref} but should not be processing.");
     return((OrderResponse)_response);
 }
コード例 #3
0
 internal virtual ReportResponse HandleDisabling(ReportRequest request)
 {
     _response                = _ardResponseFac.ReportOK(CommandEnum.Disable);
     _proxy.CurrentState      = _proxy.DisabledState;
     _proxy.Info.Enabled      = false;
     _proxy.Info.MakingCoffee = false;
     LogOnDashAsync($"{_proxy.Info.UniqueName} is disabled.");
     CallProxyActionEvent(ProxyEventEnum.MachineIsDisabled);
     return((ReportResponse)_response);
 }
コード例 #4
0
 internal virtual OrderResponse HandleCancelOrder(OrderRequest request)
 {
     _response = _ardResponseFac.CancelOrderResponse(this.GetType() == typeof(CMProxyStateProcessing) ? ErrorEnum.Void : ErrorEnum.ShouldNotBeProcessing);
     _proxy.Waitress.CancelAllOrders();
     _proxy.Info.Enabled      = false;
     _proxy.Info.MakingCoffee = false;
     LogOnDashAsync($"{_proxy.Info.UniqueName} called CANCEL ORDER, I told it to disable.");
     CallProxyActionEvent(ProxyEventEnum.ToldMachineToDisable);
     return((OrderResponse)_response);
 }
コード例 #5
0
        internal virtual RegistrationResponse HandleOffsets(RegistrationRequest request)
        {
            _response = _ardResponseFac.RegistrationOK();

            if (request.stp == null)
            {
                return(_ardResponseFac.InvalidRequest <RegistrationResponse>(ErrorEnum.MissingIngredientsSetup));
            }
            _proxy.Info.SetupAvaiabilityAndOffsets(request.stp);
            LogOnDashAsync($"{_proxy.Info.UniqueName} has redefined the ingredients' setup.");
            CallProxyActionEvent(ProxyEventEnum.IngredientsSetupRedefined);
            return((RegistrationResponse)_response);
        }
コード例 #6
0
        internal virtual RegistrationResponse HandleUnregistration(RegistrationRequest request)
        {
            _response = _ardResponseFac.RegistrationOK(CommandEnum.Unregister);

            var name = _proxy.Info.UniqueName;
            var mac  = _proxy.Info.Mac;

            CMProxyHub.Sgt.Unregister(mac);

            var task = Task.Factory.StartNew(() =>
            {
                var dash = AppDomain.CurrentDomain.UnityContainer().Resolve <AbstractDashboard>();
                dash.LogAsync($"{name} ({mac}) has been unregistered :(.");
            });

            CallProxyActionEvent(ProxyEventEnum.ProxyUnregistered);

            return((RegistrationResponse)_response);
        }
コード例 #7
0
        internal ArduinoResponse Send(ArduinoRequest request)
        {
            var sendRetries = 0;

            while (sendRetries++ < MaxSendRetries - 1)
            {
                try
                {
                    // First try to get sync (send FF FE FD FC and require FC FD FE FF as a response).
                    bool hasSync;
                    var  syncRetries = 0;
                    while (!(hasSync = GetSync()) && syncRetries++ < MaxSyncRetries - 1)
                    {
                        logger.Debug("Unable to get sync ... trying again ({0}/{1}).", syncRetries, MaxSyncRetries);
                    }
                    if (!hasSync)
                    {
                        var errorMessage = string.Format("Unable to get sync after {0} tries!", MaxSyncRetries);
                        logger.Fatal(errorMessage);
                        throw new IOException(errorMessage);
                    }

                    // Now send the command handshake (send FB as start of message marker + the command byte + length byte).
                    // Expect the inverse (length byte followed by command byte followed by FB) as a command ACK.
                    var requestBytes       = request.Bytes.ToArray();
                    var requestBytesLength = requestBytes.Length;

                    if (!ExecuteCommandHandShake(request.Command, (byte)requestBytesLength))
                    {
                        var errorMessage = string.Format("Unable to configure command handshake for command {0}.", request);
                        logger.Fatal(errorMessage);
                        throw new IOException(errorMessage);
                    }

                    // Write out all packet bytes, followed by a Fletcher 16 checksum!
                    // Packet bytes consist of:
                    // 1. Command byte repeated
                    // 2. Request length repeated
                    // 3. The actual request bytes
                    // 4. Two fletcher-16 checksum bytes calculated over (1 + 2 + 3)
                    var packetBytes = new byte[requestBytesLength + 4];
                    packetBytes[0] = request.Command;
                    packetBytes[1] = (byte)requestBytesLength;
                    Buffer.BlockCopy(requestBytes, 0, packetBytes, 2, requestBytesLength);
                    var fletcher16CheckSum = CalculateFletcher16Checksum(packetBytes, requestBytesLength + 2);
                    var f0 = (byte)(fletcher16CheckSum & 0xff);
                    var f1 = (byte)((fletcher16CheckSum >> 8) & 0xff);
                    var c0 = (byte)(0xff - (f0 + f1) % 0xff);
                    var c1 = (byte)(0xff - (f0 + c0) % 0xff);
                    packetBytes[requestBytesLength + 2] = c0;
                    packetBytes[requestBytesLength + 3] = c1;

                    Write(packetBytes, 0, requestBytesLength + 4);

                    // Write out all bytes written marker (FA)
                    Write(new[] { CommandConstants.AllBytesWritten }, 0, 1);

                    // Expect response message to drop to be received in the following form:
                    // F9 (start of response marker) followed by response length
                    numberOfBytesToRead = 2;
                    WaitForBytes(numberOfBytesToRead);
                    var responseBytes         = ReadCurrentReceiveBuffer(numberOfBytesToRead);
                    var startOfResponseMarker = responseBytes[0];
                    var responseLength        = responseBytes[1];
                    if (startOfResponseMarker != CommandConstants.StartOfResponseMarker)
                    {
                        var errorMessage = string.Format("Did not receive start of response marker but {0}!", startOfResponseMarker);
                        logger.Fatal(errorMessage);
                        throw new IOException(errorMessage);
                    }

                    // Read x responsebytes
                    numberOfBytesToRead = responseLength;
                    if (BytesToRead < numberOfBytesToRead)
                    {
                        WaitForBytes(numberOfBytesToRead);
                    }
                    responseBytes = ReadCurrentReceiveBuffer(numberOfBytesToRead);
                    return(ArduinoResponse.Create(responseBytes));
                }
                catch (TimeoutException ex)
                {
                    logger.Debug(ex, "TimeoutException in Send occurred, retrying ({0}/{1})!", sendRetries, MaxSendRetries);
                }
                catch (Exception ex)
                {
                    logger.Debug(ex, "General exception in Send occurred, retrying ({0}/{1})!", sendRetries, MaxSendRetries);
                }
            }
            return(null);
        }
コード例 #8
0
        internal ArduinoResponse Send(ArduinoRequest request, int maximumSendRetries = maxSendRetries)
        {
            var sendRetries = 0;

            while (sendRetries++ < maximumSendRetries)
            {
                try {
                    // First try to get sync (send FF FE FD FC and require FC FD FE FF as a response).
                    bool hasSync;
                    var  syncRetries = 0;
                    while (!(hasSync = GetSync()) && syncRetries++ < maxSyncRetries)
                    {
                    }
                    if (!hasSync)
                    {
                        string errorMessage = $"Unable to get sync after {maxSyncRetries} tries!";
                        throw new IOException(errorMessage);
                    }

                    // Now send the command handshake (send FB as start of message marker + the command byte + length byte).
                    // Expect the inverse (length byte followed by command byte followed by FB) as a command ACK.
                    byte[] requestBytes       = request.Bytes.ToArray();
                    int    requestBytesLength = requestBytes.Length;

                    if (!ExecuteCommandHandShake(request.Command, (byte)requestBytesLength))
                    {
                        string errorMessage = $"Unable to configure command handshake for command {request}.";
                        throw new IOException(errorMessage);
                    }

                    // Write out all packet bytes, followed by a Fletcher 16 checksum!
                    // Packet bytes consist of:
                    // 1. Command byte repeated
                    // 2. Request length repeated
                    // 3. The actual request bytes
                    // 4. Two fletcher-16 checksum bytes calculated over (1 + 2 + 3)
                    var packetBytes = new byte[requestBytesLength + 4];
                    packetBytes[0] = request.Command;
                    packetBytes[1] = (byte)requestBytesLength;
                    Buffer.BlockCopy(requestBytes, 0, packetBytes, 2, requestBytesLength);
                    ushort fletcher16CheckSum = CalculateFletcher16Checksum(packetBytes, requestBytesLength + 2);
                    var    f0 = (byte)(fletcher16CheckSum & 0xff);
                    var    f1 = (byte)((fletcher16CheckSum >> 8) & 0xff);
                    var    c0 = (byte)(0xff - (f0 + f1) % 0xff);
                    var    c1 = (byte)(0xff - (f0 + c0) % 0xff);
                    packetBytes[requestBytesLength + 2] = c0;
                    packetBytes[requestBytesLength + 3] = c1;

                    serialPort.Write(packetBytes, 0, requestBytesLength + 4);

                    // Write out all bytes written marker (FA)
                    serialPort.Write(new[] { CommandConstants.AllBytesWritten }, 0, 1);

                    // Expect response message to drop to be received in the following form:
                    // F9 (start of response marker) followed by response length
                    byte[] responseBytes         = ReadCurrentReceiveBuffer(2);
                    byte   startOfResponseMarker = responseBytes[0];
                    byte   responseLength        = responseBytes[1];
                    if (startOfResponseMarker != CommandConstants.StartOfResponseMarker)
                    {
                        string errorMessage = $"Did not receive start of response marker but {startOfResponseMarker}!";
                        throw new IOException(errorMessage);
                    }

                    // Read x responsebytes
                    responseBytes = ReadCurrentReceiveBuffer(responseLength);
                    return(ArduinoResponse.Create(responseBytes));
                }
                catch (Exception) {
                    // ignored
                }
            }
            return(null);
        }
コード例 #9
0
        private HandShakeResponse ExecuteHandshake()
        {
            ArduinoResponse response = port.Send(new HandShakeRequest(), 1);

            return(response as HandShakeResponse);
        }