/// <summary>
        /// Posts Data to the OpenHab Rest API
        /// </summary>
        /// <param name="itemPath"></param>
        /// <param name="value"></param>
        /// <param name="toggle">s</param>
        /// <returns></returns>
        public async Task PostData(string itemPath, string value, bool toggle = false)
        {
            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders
            .Accept
            .Add(new MediaTypeWithQualityHeaderValue("text/plain"));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, itemPath);

            request.Content = new StringContent(value,
                                                Encoding.UTF8,
                                                "text/plain");

            await client.SendAsync(request)
            .ContinueWith(responseTask =>
            {
                _iDataSingleton.AddErrorLog(new ErrorLog()
                {
                    LogLevel    = LogLevel.Debug,
                    MsgDateTime = DateTime.Now,
                    Message     = $"{typeof(OpenHabMessageService)}:PostData => Response: {responseTask.Result}"
                });

                _iDataSingleton.GetSystemStatus().OpenHabStatus = responseTask.Result.IsSuccessStatusCode;
            });

            if (toggle && _iDataSingleton.GetSystemStatus().OpenHabStatus)
            {
                Thread.Sleep(500);
                await PostData(itemPath, "OFF");
            }
        }
Example #2
0
        /// <summary>
        /// This method handles the different commands.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private async Task <ActionResult <CommandItem> > CommandItemHandler(CommandItem item)
        {
            switch (item.Command)
            {
            case Command.OpenDoor:
                break;

            case Command.OpenGate:
                switch (_iDataSingleton.GetSystemStatus().GateStatus)
                {
                case GateStatus.GateOpen:
                    if (item.CommandValue.Equals(CommandValue.Open.ToString()) ||
                        item.CommandValue.Equals(CommandValue.ForceOpen.ToString()))
                    {
                        return(Accepted(new CommandItem()
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.AlreadyOpen.ToString()
                        }));
                    }
                    else if (item.CommandValue.Equals(CommandValue.Close.ToString()) ||
                             item.CommandValue.Equals(CommandValue.ForceClose.ToString()))
                    {
                        _iDataSingleton.GetSystemStatus().IsGateMoving = true;
                        await _openHab.PostData(_iDataSingleton.GatePathValueChange(), "ON", true);

                        return(Accepted(new CommandItem
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.GateClosing.ToString()
                        }));
                    }
                    break;

                case GateStatus.GateOpening:
                    if (item.CommandValue.Equals(CommandValue.Open.ToString()) ||
                        item.CommandValue.Equals(CommandValue.ForceOpen.ToString()))
                    {
                        return(Accepted(new CommandItem()
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.GateOpening.ToString()
                        }));
                    }
                    else if (item.CommandValue.Equals(CommandValue.Close.ToString()))
                    {
                        return(Accepted(new CommandItem
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.GateOpening.ToString()
                        }));
                    }
                    else if (item.CommandValue.Equals(CommandValue.ForceClose.ToString()))
                    {
                        _iDataSingleton.GetSystemStatus().IsGateMoving = true;
                        await _openHab.PostData(_iDataSingleton.GatePathValueChange(), "ON", true);

                        Thread.Sleep(500);
                        await _openHab.PostData(_iDataSingleton.GatePathValueChange(), "ON", true);

                        return(Accepted(new CommandItem
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.GateClosing.ToString()
                        }));
                    }
                    break;

                case GateStatus.GateClosed:
                    if (item.CommandValue.Equals(CommandValue.Open.ToString()) ||
                        item.CommandValue.Equals(CommandValue.ForceOpen.ToString()))
                    {
                        _iDataSingleton.GetSystemStatus().IsGateMoving = true;
                        await _openHab.PostData(_iDataSingleton.GatePathValueChange(), "ON", true);

                        return(Accepted(new CommandItem()
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.GateOpening.ToString()
                        }));
                    }
                    else if (item.CommandValue.Equals(CommandValue.Close.ToString()) ||
                             item.CommandValue.Equals(CommandValue.ForceClose.ToString()))
                    {
                        return(Accepted(new CommandItem
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.AlreadyClosed.ToString()
                        }));
                    }
                    break;

                case GateStatus.GateClosing:
                    if (item.CommandValue.Equals(CommandValue.Close.ToString()) ||
                        item.CommandValue.Equals(CommandValue.ForceClose.ToString()))
                    {
                        return(Accepted(new CommandItem()
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.GateClosing.ToString()
                        }));
                    }
                    else if (item.CommandValue.Equals(CommandValue.Open.ToString()))
                    {
                        return(Accepted(new CommandItem
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.GateClosing.ToString()
                        }));
                    }
                    else if (item.CommandValue.Equals(CommandValue.ForceOpen.ToString()))
                    {
                        _iDataSingleton.GetSystemStatus().IsGateMoving = true;
                        await _openHab.PostData(_iDataSingleton.GatePathValueChange(), "ON", true);

                        Thread.Sleep(500);
                        await _openHab.PostData(_iDataSingleton.GatePathValueChange(), "ON", true);

                        return(Accepted(new CommandItem
                        {
                            Id = item.Id,
                            Command = item.Command,
                            CommandValue = CommandValueAnswer.GateOpening.ToString()
                        }));
                    }
                    break;

                default:
                    return(NotFound());
                }
                break;
            }
            return(NotFound());
        }
        public async Task <ActionResult <AnswerModel> > GetGateStatus([FromBody] AuthModel auth)
        {
            ActionResult <AnswerModel> result = await CheckUser(auth);

            if (result != null)
            {
                return(result);
            }

            return(Accepted(new AnswerModel()
            {
                Data = _iDataSingleton.GetSystemStatus().GateStatus.ToString()
            }));
        }
 public SystemStatus GetOpenHabStatus()
 {
     return(_dataSingleton.GetSystemStatus());
 }