public async Task SetLightSwitchStatus(bool on)
 {
     using (var s = new Sockets.Plugin.TcpSocketClient())
     {
         await s.ConnectAsync(NetduinoIp, Port);
         byte[] data = new byte[2];
         data[0] = WriteLightSwitchState;
         data[1] = (byte)(on ? ByteTrue : ByteFalse);
         s.WriteStream.Write(data, 0, 2);
     }
 }
 public async Task<bool> GetLightSwitchStatus()
 {
     using (var s = new Sockets.Plugin.TcpSocketClient())
     {
         await s.ConnectAsync(NetduinoIp, Port);
         byte[] data = new byte[2];
         data[0] = ReadLightSwitchState;
         data[1] = ReadLightSwitchState;
         s.WriteStream.Write(data, 0, 2);
         s.ReadStream.Read(data, 0, 1);
         return data[0] == ByteTrue;
     }
 }
示例#3
0
        public async Task <ClientResponse> Request(ClientRequest request)
        {
            IPEndPoint dns = request.Dns;

#if (!PORTABLE)
            using (TcpClient tcp = new TcpClient()) {
#else
            using (Sockets.Plugin.TcpSocketClient tcp = new Sockets.Plugin.TcpSocketClient())
            {
#endif
                await tcp.ConnectAsync(dns.Address.ToString(), dns.Port);

#if (!PORTABLE)
                Stream readStream  = tcp.GetStream();
                Stream writeStream = tcp.GetStream();
#else
                Stream readStream  = tcp.ReadStream;
                Stream writeStream = tcp.WriteStream;
#endif
                byte[] buffer = request.ToArray();
                byte[] length = BitConverter.GetBytes((ushort)buffer.Length);

                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(length);
                }

                await writeStream.WriteAsync(length, 0, length.Length);

                await writeStream.WriteAsync(buffer, 0, buffer.Length);

                buffer = new byte[2];
                await Read(readStream, buffer);

                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(buffer);
                }

                buffer = new byte[BitConverter.ToUInt16(buffer, 0)];
                await Read(readStream, buffer);

                Response response = Response.FromArray(buffer);

                return(new ClientResponse(request, response, buffer));
            }
        }
        private async Task TestRda()
        {
            var socket = new Sockets.Plugin.TcpSocketClient();

            await socket.ConnectAsync("echo.websocket.org", 80, false);

            // Send HS
            var handshake = "GET / HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: p2z/MFplfpRzjsVywqRQTg==\r\nHost: echo.websocket.org\r\nOrigin: http://echo.websocket.org/\r\n\r\n";
            var bytes = UTF8Encoding.UTF8.GetBytes(handshake);

            await socket.WriteStream.FlushAsync();
            await socket.WriteStream.WriteAsync(bytes, 0, bytes.Length);

            // Read HS, Never Ending
            var b = socket.ReadStream.ReadByte();

            Debug.WriteLine("TestRda");
        }
        private async Task TestRda()
        {
            var socket = new Sockets.Plugin.TcpSocketClient();

            await socket.ConnectAsync("echo.websocket.org", 80, false);

            // Send HS
            var handshake = "GET / HTTP/1.1\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: p2z/MFplfpRzjsVywqRQTg==\r\nHost: echo.websocket.org\r\nOrigin: http://echo.websocket.org/\r\n\r\n";
            var bytes     = UTF8Encoding.UTF8.GetBytes(handshake);

            await socket.WriteStream.FlushAsync();

            await socket.WriteStream.WriteAsync(bytes, 0, bytes.Length);

            // Read HS, Never Ending
            var b = socket.ReadStream.ReadByte();

            Debug.WriteLine("TestRda");
        }
示例#6
0
        /// <summary>
        /// Genericized function to send commands to a Particle device in listening mode
        /// </summary>
        /// <param name="setupCommand">The SetupCommand to use</param>
        /// <param name="data">Any extra data to send with the command</param>
        /// <returns></returns>
        private static async Task <string> SendSoftAPCommandAsync(SetupCommand setupCommand, string data = null)
        {
            string hostname = "192.168.0.1";

            string command;

            switch (setupCommand)
            {
            case SetupCommand.Version:
                command = "version";
                break;

            case SetupCommand.DeviceId:
                command = "device-id";
                break;

            case SetupCommand.ScanAP:
                command = "scan-ap";
                break;

            case SetupCommand.PublicKey:
                command = "public-key";
                break;

            case SetupCommand.ConfigureAP:
                command = "configure-ap";
                break;

            case SetupCommand.ConnectAP:
                command = "connect-ap";
                break;

            case SetupCommand.Set:
                command = "set";
                break;

            default:
                return(null);
            }

            int dataLength = 0;

            if (data != null)
            {
                dataLength = data.Length;
            }

            using (Sockets.Plugin.TcpSocketClient socket = new Sockets.Plugin.TcpSocketClient())
            {
                try
                {
                    var cancellationTokenSource = new CancellationTokenSource();
                    cancellationTokenSource.CancelAfter(5000);

                    await socket.ConnectAsync(hostname, 5609, false, cancellationTokenSource.Token);

                    var dataToSend = $"{command}\n{dataLength}\n\n";
                    if (data != null)
                    {
                        dataToSend += data;
                    }

                    var bytes = Encoding.UTF8.GetBytes(dataToSend);
                    await socket.WriteStream.WriteAsync(bytes, 0, bytes.Length);

                    await socket.WriteStream.FlushAsync();

                    var buffer = new byte[2048];

                    var cancellationTokenSourceRead = new CancellationTokenSource();
                    cancellationTokenSourceRead.CancelAfter(10000);
                    int count = await socket.ReadStream.ReadAsync(buffer, 0, buffer.Length, cancellationTokenSourceRead.Token);

                    var receivedData = "";
                    if (count > 0)
                    {
                        receivedData = Encoding.UTF8.GetString(buffer, 0, count);
                    }

                    if (string.IsNullOrWhiteSpace(receivedData))
                    {
                        return(null);
                    }

                    return(receivedData);
                }
                catch (Exception exception)
                {
                    throw exception;
                }

                return(null);
            }
        }