예제 #1
0
        public Tuple <string, byte[]> Invoke(string connectionId, string url, string method, byte[] inputData,
                                             ConnectionSession session)
        {
            if (!_sessions.ContainsKey(connectionId))
            {
                _sessions.TryAdd(connectionId, new HapSession());
            }


            _logger.LogDebug($"Working on request {url}");
            var queryString = new NameValueCollection();

            if (url.Contains("?"))
            {
                var parts = url.Split('?');

                queryString = HttpUtility.ParseQueryString(parts[1]);
                url         = parts[0];
            }

            try
            {
                if (inputData.Length > 0)
                {
                    var parts = TlvParser.Parse(inputData);

                    if (method == "POST")
                    {
                        var state = parts.GetTypeAsInt(Constants.State);

                        if (url.EndsWith("pair-setup"))
                        {
                            _logger.LogDebug($"Working on pair-setup request");
                            if (_pairController != null && state == 1)
                            {
                                return(ReturnError(state, ErrorCodes.Busy));
                            }

                            if (state == 1 && _pairController == null)
                            {
                                _pairController =
                                    new PairSetupController(_logger, _pairCode);
                            }

                            if (_pairController == null)
                            {
                                _logger.LogError($"Something is wrong here, closing tcp connection");
                                throw new ArgumentException($"Something is wrong here, closing tcp connection");
                            }

                            var data = _pairController.Post(parts, session);

                            var raw = TlvParser.Serialize(data.TlvData);

                            if (!data.Ok)
                            {
                                _pairController = null;
                                _sessions.TryRemove(connectionId, out var _);
                            }
                            else if (data.State == 5)
                            {
                                PairingCompleted?.Invoke(this, new PairSetupCompleteEventArgs(data.Ltsk, data.Ltpk));
                                _pairController = null;
                            }

                            return(new Tuple <string, byte[]>(PairSetupReturn.ContentType, raw));
                        }

                        if (url.EndsWith("pair-verify"))
                        {
                            _logger.LogDebug($"Working on pair-verify request");
                            if (string.IsNullOrEmpty(HapControllerServer.HapControllerLtsk))
                            {
                                return(ReturnError(state, ErrorCodes.Busy));
                            }

                            var verify = new PairVerifyController(_logger);
                            var data   = verify.Post(parts, _sessions[connectionId]);

                            if (data.HapSession != null)
                            {
                                _sessions[connectionId] = data.HapSession;
                            }

                            var raw = TlvParser.Serialize(data.TlvData);
                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("pairings"))
                        {
                            _logger.LogDebug($"Working on pairings");
                            var pair = new PairingController(_logger);
                            var data = pair.Post(parts);

                            var raw = TlvParser.Serialize(data.TlvData);
                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("identify"))
                        {
                            var identify = new IdentifyController();
                            var data     = identify.Post(inputData);

                            return(new Tuple <string, byte[]>(data.ContentType, new byte[0]));
                        }
                    }
                    else if (method == "PUT")
                    {
                        if (url.EndsWith("characteristics"))
                        {
                            _logger.LogDebug($"Working on characteristics");
                            var c    = new CharacteristicsController(_logger);
                            var data = c.Put(inputData, _sessions[connectionId], _homeKitServer);

                            // response with no data if the call was successful - errors need to be implemented
                            return(new Tuple <string, byte[]>(data.ContentType, new byte[0]));
                        }
                    }
                }
                else
                {
                    if (method == "GET")
                    {
                        if (url.EndsWith("accessories"))
                        {
                            _logger.LogDebug($"Working on accessories");

                            var accessoryController = new AccesoriesController(_logger);
                            var accessories         = accessoryController.Get(_homeKitServer, queryString);
                            var strValue            = JsonConvert.SerializeObject(accessories, JsonSettings);

                            _logger.LogDebug($"Sending {strValue}");

                            return(new Tuple <string, byte[]>("application/hap+json", Encoding.UTF8.GetBytes(strValue)));
                        }

                        if (url.EndsWith("characteristics"))
                        {
                            _logger.LogDebug($"Working on characteristics");

                            var ids      = queryString["id"].Split(",");
                            var c        = new CharacteristicsController(_logger);
                            var data     = c.Get(ids, _homeKitServer);
                            var strValue = JsonConvert.SerializeObject(data, JsonSettings);

                            _logger.LogDebug($"Sending {strValue}");

                            return(new Tuple <string, byte[]>(data.ContentType, Encoding.UTF8.GetBytes(strValue)));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"{e}: Error occured while processing request");
                _pairController = null;
            }

            throw new InvalidOperationException();
        }
예제 #2
0
        public Tuple <string, byte[]> Invoke(string connectionId, string url, string method, byte[] inputData)
        {
            if (!_sessions.ContainsKey(connectionId))
            {
                _sessions.Add(connectionId, new HapSession());
            }

            var queryString = new NameValueCollection();

            if (url.Contains("?"))
            {
                var parts = url.Split('?');

                queryString = HttpUtility.ParseQueryString(parts[1]);
                url         = parts[0];
            }

            try
            {
                if (inputData.Length > 0)
                {
                    var parts = TlvParser.Parse(inputData);

                    if (method == "POST")
                    {
                        var state = parts.GetTypeAsInt(Constants.State);

                        if (url.EndsWith("pair-setup"))
                        {
                            if (_pairController != null && state == 1)
                            {
                                return(ReturnError(state, ErrorCodes.Busy));
                            }

                            if (state == 1 && _pairController == null)
                            {
                                _pairController =
                                    new PairSetupController(_logger, _pairCode); //Todo: change code to param
                            }

                            var data = _pairController.Post(parts);

                            var raw = TlvParser.Serialise(data.TlvData);

                            if (!data.Ok)
                            {
                                _pairController = null;
                                _sessions.Remove(connectionId);
                            }
                            else if (data.State == 5)
                            {
                                PairingCompleted?.Invoke(this, new PairSetupCompleteEventArgs(data.Ltsk, data.Ltpk));
                                _pairController = null;
                            }

                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("pair-verify"))
                        {
                            if (string.IsNullOrEmpty(HapControllerServer.HapControllerLtsk))
                            {
                                return(ReturnError(state, ErrorCodes.Busy));
                            }

                            var verify = new PairVerifyController(_logger);
                            var data   = verify.Post(parts, _sessions[connectionId]);

                            if (data.HapSession != null)
                            {
                                _sessions[connectionId] = data.HapSession;
                            }

                            var raw = TlvParser.Serialise(data.TlvData);
                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("pairings"))
                        {
                            var pair = new PairingController(_logger);
                            var data = pair.Post(parts);

                            var raw = TlvParser.Serialise(data.TlvData);
                            return(new Tuple <string, byte[]>(data.ContentType, raw));
                        }

                        if (url.EndsWith("identify"))
                        {
                            var identify = new IdentifyController();
                            var data     = identify.Post(inputData);

                            return(new Tuple <string, byte[]>(data.ContentType, new byte[0]));
                        }
                    }
                    else if (method == "PUT")
                    {
                        if (url.EndsWith("characteristics"))
                        {
                            var c    = new CharacteristicsController(_logger);
                            var data = c.Put(inputData, _sessions[connectionId], _homeKitServer);

                            if (data.Characteristics.Count == 0)
                            {
                                return(new Tuple <string, byte[]>(data.ContentType, new byte[0]));
                            }
                            return(new Tuple <string, byte[]>(data.ContentType, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))));
                        }
                    }
                }
                else
                {
                    if (method == "GET")
                    {
                        if (url.EndsWith("accessories"))
                        {
                            var accessoryController = new AccesoriesController();
                            var accessories         = accessoryController.Get(_homeKitServer, queryString);
                            var strValue            = JsonConvert.SerializeObject(accessories, JsonSettings);

                            return(new Tuple <string, byte[]>("application/hap+json", Encoding.UTF8.GetBytes(strValue)));
                        }

                        if (url.EndsWith("characteristics"))
                        {
                            var ids  = queryString["id"].Split(",");
                            var c    = new CharacteristicsController(_logger);
                            var data = c.Get(ids, _homeKitServer);

                            return(new Tuple <string, byte[]>(data.ContentType, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data, JsonSettings))));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error occured while processing request");
                _pairController = null;
            }

            throw new InvalidOperationException();
        }