示例#1
0
        public async Task <int> ConnectToInterface(TunnelRequest req)
        {
            try
            {
                _connId = new Random().Next(1, 255);
                await _knxConn.Connect();

                TunnelResponse res = new TunnelResponse();
                res.Type           = TunnelTypes.Connect;
                res.SequenceNumber = req.SequenceNumber;
                res.Group          = req.Group;
                res.ChannelId      = req.ChannelId;
                res.ConnId         = _connId;
                Group     = req.Group;
                ChannelId = req.ChannelId;
                _         = _conn.Send(res, false);
            }
            catch (Exception ex)
            {
                TunnelResponse res = new TunnelResponse();
                res.Group     = req.Group;
                res.ChannelId = req.ChannelId;
                res.ConnId    = 0;
                res.Data      = Encoding.UTF8.GetBytes(ex.Message);
                _             = _conn.Send(res, false);
            }

            return(_connId);
        }
示例#2
0
        public async Task Connect()
        {
            Debug.WriteLine(DateTime.Now);
            TunnelRequest req = new TunnelRequest(Encoding.UTF8.GetBytes(Hash));

            req.Type = TunnelTypes.Connect;
            IRemoteMessage resp = await _conn.Send(req);

            if (resp == null)
            {
                throw new Exception("Keine Antwort vom Remote Server");
            }

            TunnelResponse response = resp as TunnelResponse;

            if (response == null)
            {
                throw new Exception("Unerwartete Antwort vom Remote Server: " + response.ToString());
            }

            IsConnected = true;
            _connId     = response.ConnId;
            Debug.WriteLine(DateTime.Now);
            Debug.WriteLine("Verbunden");
        }
示例#3
0
        private async void _conn_OnRequest(IRemoteMessage message)
        {
            if (message is TunnelRequest)
            {
                TunnelRequest req = message as TunnelRequest;
                if (req.ConnId != 0 && req.ConnId != _connId)
                {
                    return;
                }

                switch (req.Type)
                {
                case TunnelTypes.Tunnel:
                    IMessage msg = (IMessage)JsonConvert.DeserializeObject(Encoding.UTF8.GetString(req.Data), new JsonSerializerSettings()
                    {
                        TypeNameHandling = TypeNameHandling.Auto
                    });
                    Debug.WriteLine("Tunnel: " + msg);
                    byte seq = await _knxConn.Send(msg);

                    TunnelResponse req2 = new TunnelResponse(new byte[] { seq });
                    req2.Type           = TunnelTypes.Response;
                    req2.ConnId         = req.ConnId;
                    req2.SequenceNumber = req.SequenceNumber;
                    _ = _conn.Send(req2, false);
                    break;
                }
            }
        }
示例#4
0
        private void OnTunnelActivity(IMessage message)
        {
            TunnelResponse req = new TunnelResponse();

            req.Type   = TunnelTypes.Tunnel;
            req.ConnId = _connId;
            req.Data   = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message, new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.All
            }));
            _ = _conn.Send(req, false);
        }
        public async Task <IRemoteMessage> Send(IRemoteMessage message, bool waitForResponse = true)
        {
            if (socket == null || socket.State != WebSocketState.Open)
            {
                return(null);
            }
            IRemoteMessage mesg = null;

            try
            {
                if (message.SequenceNumber == -1)
                {
                    message.SequenceNumber = SequenceNumber++;
                }
                if (message is TunnelRequest)
                {
                    TunnelRequest req = message as TunnelRequest;
                    if (req.ChannelId == 0)
                    {
                        req.ChannelId = Remotes[req.ConnId].ChannelId;
                    }
                    if (req.Group == "")
                    {
                        req.Group = Remotes[req.ConnId].Group;
                    }
                }
                if (message is TunnelResponse)
                {
                    TunnelResponse res = message as TunnelResponse;
                    if (res.ChannelId == 0)
                    {
                        res.ChannelId = Remotes[res.ConnId].ChannelId;
                    }
                    if (res.Group == "")
                    {
                        res.Group = Remotes[res.ConnId].Group;
                    }
                }
                await socket.SendAsync(message.GetBytes(), WebSocketMessageType.Binary, true, source.Token);

                mesg = await WaitForResponse(message.SequenceNumber);
            }catch (Exception ex)
            {
            }

            return(mesg);
        }