コード例 #1
0
        private static async Task <MethodResponse> AdvertiseMethodCallBack(MethodRequest methodRequest, object userContext)
        {
            Console.WriteLine($"Executing AdvertiseMethodCallBack at {DateTime.UtcNow}");

            var advertiseResponse = new AdvertiseResponse {
                responseState = 0
            };

            try
            {
                var result = await LedScenarios.DirectAdvertise(_mcp23xxxWrite);

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

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

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

            return(response);
        }
コード例 #2
0
        static async Task <MethodResponse> DownMethodCallBack(MethodRequest methodRequest, object userContext)
        {
            Console.WriteLine($"Executing DownMethodCallBack at {DateTime.UtcNow}");

            if (methodRequest == null)
            {
                Console.WriteLine($"Moving to initial state");
            }

            var downResponse = new DownResponse {
                responseState = 0
            };

            try
            {
                _liftState = LiftState.MovingDown;

                var task = Task.Run(async() =>
                {
                    await LedScenarios.PlayDownScene(_mcp23xxxWrite, UpDownInterval);
                });

                if (!InDebugMode)
                {
                    _controller.Write(DownRelayPin, PinValue.Low); // start action
                }
                else
                {
                    Console.WriteLine("WARNING: While in debug mode, no DOWN movements are executed!");
                }

                await Task.Delay(UpDownInterval);

                _controller.Write(DownRelayPin, PinValue.High); // stop action

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

                _liftState = LiftState.Down;
            }
            catch (Exception ex)
            {
                _liftState = LiftState.Unknown;

                downResponse.errorMessage  = ex.Message;
                downResponse.responseState = -999;
            }

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

            return(response);
        }
コード例 #3
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);
        }
コード例 #4
0
        static async Task <MethodResponse> UpMethodCallBack(MethodRequest methodRequest, object userContext)
        {
            Console.WriteLine($"Executing UpMethodCallBack at {DateTime.UtcNow}");

            var upResponse = new UpResponse {
                responseState = 0
            };

            try
            {
                _liftState = LiftState.MovingUp;

                var task = Task.Run(async() =>
                {
                    await LedScenarios.PlayUpScene(_mcp23xxxWrite, UpDownInterval);
                });

                _controller.Write(UpRelayPin, PinValue.Low); // start action

                await Task.Delay(UpDownInterval);

                _controller.Write(UpRelayPin, PinValue.High); // stop action

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

                _liftState = LiftState.Up;
            }
            catch (Exception ex)
            {
                _liftState = LiftState.Unknown;

                upResponse.errorMessage  = ex.Message;
                upResponse.responseState = -999;
            }

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

            return(response);
        }
コード例 #5
0
        private static async Task <MethodResponse> MarkPositionMethodCallBack(MethodRequest methodRequest, object userContext)
        {
            Console.WriteLine($"Executing MarkPositionMethodCallBack at {DateTime.UtcNow}");

            var markPositionResponse = new MarkPositionResponse {
                responseState = 0
            };

            try
            {
                dynamic request = JsonConvert.DeserializeObject(methodRequest.DataAsJson);

                int position = request.position;

                Console.WriteLine($"Blinking position {position}");

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

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

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

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

            return(response);
        }
コード例 #6
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);
            }
        }