Exemplo n.º 1
0
        private static async Task <MethodResponse> BottleHoldersMethodCallBack(MethodRequest methodRequest, object userContext)
        {
            Console.WriteLine($"Executing BottleHoldersMethodCallBack at {DateTime.UtcNow}");

            var bottleHoldersResponse = new BottleHoldersResponse {
                responseState = 0
            };

            try
            {
                var mcp23x1x = _mcp23xxxRead as Mcp23x1x;

                if (mcp23x1x == null)
                {
                    bottleHoldersResponse.errorMessage  = "Unable to cast Mcp23017 Read GPIO.";
                    bottleHoldersResponse.responseState = 1;
                }
                else
                {
                    byte dataPortA = mcp23x1x.ReadByte(Register.GPIO, Port.PortA);
                    byte dataPortB = mcp23x1x.ReadByte(Register.GPIO, Port.PortB);

                    var beerLiftMessage = new BeerLiftMessage(_deviceId, dataPortA, dataPortB, _liftState);

                    var pinValue = _controller.Read(FloodedPin); // Moisture sensor

                    var flooded = pinValue.ToString().ToLower() == "low" ? false : true;

                    beerLiftMessage.isFlooded = flooded;

                    bottleHoldersResponse.BeerLiftMessage = beerLiftMessage;

                    Console.WriteLine($"BottleHolders ended at {DateTime.UtcNow}.");

                    await Task.Delay(1);
                }
            }
            catch (Exception ex)
            {
                bottleHoldersResponse.errorMessage  = ex.Message;
                bottleHoldersResponse.responseState = -999;
            }

            var json     = JsonConvert.SerializeObject(bottleHoldersResponse);
            var response = new MethodResponse(Encoding.UTF8.GetBytes(json), 200);

            return(response);
        }
Exemplo n.º 2
0
        private static async Task <MethodResponse> RouletteMethodCallBack(MethodRequest methodRequest, object userContext)
        {
            Console.WriteLine($"Executing RouletteMethodCallBack at {DateTime.UtcNow}");

            var rouletteResponse = new RouletteResponse {
                responseState = 0
            };

            try
            {
                //// Find the actual empty slot

                var beerLiftMessageToRoulette = new BeerLiftMessage("dummy", _lastDataPortA, _lastDataPortB);

                // Returns a value between 1 and 16 (or 0 is no bottle left!)
                rouletteResponse.shot = beerLiftMessageToRoulette.Roulette();

                Console.WriteLine($"Empty slot found at position {rouletteResponse.shot} (0 when all slots occupied)");

                //// Lit the right led

                var result = await LedScenarios.DirectMarkPosition(_mcp23xxxWrite, rouletteResponse.shot);

                if (!result)
                {
                    rouletteResponse.errorMessage  = "RouletteMethodCallBack: Unable to cast Mcp23017 Write GPIO";
                    rouletteResponse.responseState = 1;
                }

                Console.WriteLine($"Roulette ended at {DateTime.UtcNow}.");
            }
            catch (Exception ex)
            {
                rouletteResponse.errorMessage  = ex.Message;
                rouletteResponse.responseState = -999;
            }

            var json     = JsonConvert.SerializeObject(rouletteResponse);
            var response = new MethodResponse(Encoding.UTF8.GetBytes(json), 200);

            return(response);
        }
Exemplo n.º 3
0
        private static async void ThreadBody(object userContext)
        {
            var client = userContext as ModuleClient;

            if (client == null)
            {
                throw new InvalidOperationException("UserContext doesn't contain " + "expected values");
            }

            var mcp23x1x = _mcp23xxxRead as Mcp23x1x;

            if (mcp23x1x == null)
            {
                Console.WriteLine("Unable to cast Mcp23017 Read GPIO.");

                return;
            }

            while (true)
            {
                byte dataPortA = mcp23x1x.ReadByte(Register.GPIO, Port.PortA);
                byte dataPortB = mcp23x1x.ReadByte(Register.GPIO, Port.PortB);

                var pinValue = _controller.Read(FloodedPin); // Moisture sensor

                var flooded = pinValue.ToString().ToLower() == "low" ? false : true;

                // support simulation too
                flooded = flooded || _simulateFlooding;

                Console.WriteLine($"Ports read: A = {dataPortA} - B = {dataPortB}; Flooded = {flooded}; State = {_liftState} at {DateTime.Now}");

                // send message on some change or FLOODED!
                if (dataPortA != _lastDataPortA ||
                    dataPortB != _lastDataPortB ||
                    _liftState != _lastLiftState ||
                    ((flooded != _lastIsFlooded) &&
                     !SilentFlooding))
                {
                    _lastDataPortA = dataPortA;
                    _lastDataPortB = dataPortB;
                    _lastLiftState = _liftState;
                    _lastIsFlooded = flooded;

                    var beerLiftMessage = new BeerLiftMessage(_deviceId, dataPortA, dataPortB, _liftState);

                    beerLiftMessage.isFlooded = flooded;

                    var json = JsonConvert.SerializeObject(beerLiftMessage);
                    using (var pipeMessage = new Message(Encoding.UTF8.GetBytes(json)))
                    {
                        pipeMessage.ContentType     = "application/json";
                        pipeMessage.ContentEncoding = "utf-8";

                        if (beerLiftMessage.isFlooded)
                        {
                            pipeMessage.Properties.Add("Alarm", "Flooded");
                        }


                        pipeMessage.Properties.Add("content-type", "application/edge-beerlift-json");
                        pipeMessage.Properties.Add("SlotLength", "8");

                        await client.SendEventAsync("output1", pipeMessage);

                        Console.WriteLine($"Message sent: {beerLiftMessage}");
                    }
                }

                if (flooded)
                {
                    await LedScenarios.LitFlooded(_mcp23xxxWrite, Interval, SilentFlooding);
                }
                else
                {
                    if (_lastLiftState == LiftState.Down)
                    {
                        // switch off all light when lift is at bottom
                        await LedScenarios.SwitchOffAllSlots(_mcp23xxxWrite, dataPortA, dataPortB);
                    }
                    else
                    {
                        await LedScenarios.LitAllEmptySlots(_mcp23xxxWrite, dataPortA, dataPortB);
                    }
                }

                await Task.Delay(Interval);
            }
        }