예제 #1
0
        private static void TestHttp(string host, string url, int port, SPWF04SxConnectionSecurityType security, bool get)
        {
            var buffer = new byte[512];
            var start  = DateTime.UtcNow;
            var code   = get ? wifi.SendHttpGet(host, url, port, security) : wifi.SendHttpPost(host, url, port, security);

            Debug.WriteLine($"HTTP {code}");

            var total = 0;

            while (wifi.ReadHttpResponse(buffer, 0, buffer.Length) is var read && read > 0)
            {
                total += read;

                try {
                    Debugger.Log(0, "", Encoding.UTF8.GetString(buffer, 0, read));
                }
                catch {
                    Debugger.Log(0, "", Encoding.UTF8.GetString(buffer, 0, read - 1));
                }

                Thread.Sleep(100);
            }

            Debug.WriteLine($"\r\nRead: {total:N0} in {(DateTime.UtcNow - start).TotalMilliseconds:N0}ms");
        }
예제 #2
0
        public int SendHttpPost(string host, string path, int port, SPWF04SxConnectionSecurityType connectionSecurity)
        {
            if (this.activeHttpCommand != null)
            {
                throw new InvalidOperationException();
            }

            this.activeHttpCommand = this.GetCommand()
                                     .AddParameter(host)
                                     .AddParameter(path)
                                     .AddParameter(port.ToString())
                                     .AddParameter(connectionSecurity == SPWF04SxConnectionSecurityType.None ? "0" : "2")
                                     .AddParameter(null)
                                     .AddParameter(null)
                                     .AddParameter(null)
                                     .AddParameter(null)
                                     .Finalize(SPWF04SxCommandIds.HTTPPOST);

            this.EnqueueCommand(this.activeHttpCommand);

            var result = this.activeHttpCommand.ReadString();

            if (connectionSecurity == SPWF04SxConnectionSecurityType.Tls && result == string.Empty)
            {
                result = this.activeHttpCommand.ReadString();

                if (result.IndexOf("Loading:") == 0)
                {
                    result = this.activeHttpCommand.ReadString();
                }
            }

            return(result.Split(':') is var parts && parts[0] == "Http Server Status Code" ? int.Parse(parts[1]) : throw new Exception($"Request failed: {result}"));
        }
예제 #3
0
        private static void TestHttp(string host, string url, int port, SPWF04SxConnectionSecurityType security, bool get)
        {
            var    buffer   = new byte[512];
            string resps    = "";
            var    reqjason = $@"{{
                                'message': 'Device Ok: 60',
                                'ioT_Device': 'TINYCLR1',
                                'iotDate': '{DateTime.Now:yyyy-MM-ddTHH:mm:ss}',
                                'sensor1': 59.215686274509807,
                                'sensor2': 22.805429864253391,
                                'sens_power': 0
                            }}";
            var    start    = DateTime.UtcNow;
            int    code     = 0;
            var    total    = 0;

            if (get)
            {
                code = wifi.SendHttpGet(host, url, port, security);
                while (wifi.ReadHttpResponse(buffer, 0, buffer.Length) is var read && read > 0)
                {
                    total += read;

                    try {
                        //Debugger.Log(0, "", Encoding.UTF8.GetString(buffer, 0, read));
                        resps = Encoding.UTF8.GetString(buffer, 0, read);
                    }
                    catch {
                        //Debugger.Log(0, "", Encoding.UTF8.GetString(buffer, 0, read - 1));
                        resps = Encoding.UTF8.GetString(buffer, 0, read - 1);
                    }
                }
            }
            else
            {
                using (HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + host + ":" + port + url))
                {
                    var jbuf = Encoding.UTF8.GetBytes(reqjason);
                    httpWebRequest.AllowWriteStreamBuffering = false;
                    httpWebRequest.Method        = "POST";
                    httpWebRequest.ContentType   = "application/json";
                    httpWebRequest.ContentLength = jbuf.Length;

                    Stream sreq = httpWebRequest.GetRequestStream();
                    sreq.Write(jbuf, 0, jbuf.Length);

                    //code = wifi.SendHttpPost(host, url, port, security);
                    //sreq.Flush();
                    var    resp = httpWebRequest.GetResponse();
                    Stream s    = resp.GetResponseStream();
                    using (StreamReader srd = new StreamReader(s))
                    {
                        resps = srd.ReadToEnd();
                    }
                    //resps = Encoding.UTF8.GetString(buffer);
                    //s.Flush();
                    //s.Close();
                    //sreq.Close();
                    //resp.Close();
                };
            }
            Debug.WriteLine($"HTTP {code} - {resps}");
            Debug.WriteLine($"\r\nRead: {total:N0} in {(DateTime.UtcNow - start).TotalMilliseconds:N0}ms");
            Thread.Sleep(1000);
        }
예제 #4
0
        private static void TestSocket(string host, string url, int port, SPWF04SxConnectionType connectionType, SPWF04SxConnectionSecurityType connectionSecurity, string commonName = null)
        {
            var buffer = new byte[512];
            var id     = wifi.OpenSocket(host, port, connectionType, connectionSecurity, commonName);

            var cont = true;

            while (cont)
            {
                var start = DateTime.UtcNow;

                wifi.WriteSocket(id, Encoding.UTF8.GetBytes($"GET {url} HTTP/1.1\r\nHost: {host}\r\n\r\n"));

                Thread.Sleep(100);

                var total = 0;
                var first = true;
                while ((wifi.QuerySocket(id) is var avail && avail > 0) || first || total < 120)
                {
                    if (avail > 0)
                    {
                        first = false;

                        var read = wifi.ReadSocket(id, buffer, 0, Math.Min(avail, buffer.Length));

                        total += read;

                        Debugger.Log(0, "", Encoding.UTF8.GetString(buffer, 0, read));
                    }

                    Thread.Sleep(100);
                }

                Debug.WriteLine($"\r\nRead: {total:N0} in {(DateTime.UtcNow - start).TotalMilliseconds:N0}ms");

                WaitForButton();
            }

            wifi.CloseSocket(id);
        }
예제 #5
0
        public int OpenSocket(string host, int port, SPWF04SxConnectionType connectionType, SPWF04SxConnectionSecurityType connectionSecurity, string commonName = null)
        {
            var cmd = this.GetCommand()
                      .AddParameter(host)
                      .AddParameter(port.ToString())
                      .AddParameter(null)
                      .AddParameter(commonName ?? (connectionType == SPWF04SxConnectionType.Tcp ? (connectionSecurity == SPWF04SxConnectionSecurityType.Tls ? "s" : "t") : "u"))
                      .Finalize(SPWF04SxCommandIds.SOCKON);

            this.EnqueueCommand(cmd);

            var a = cmd.ReadString();
            var b = cmd.ReadString();

            if (connectionSecurity == SPWF04SxConnectionSecurityType.Tls && b.IndexOf("Loading:") == 0)
            {
                a = cmd.ReadString();
                b = cmd.ReadString();
            }

            this.FinishCommand(cmd);

            return(a.Split(':') is var result && result[0] == "On" ? int.Parse(result[2]) : throw new Exception("Request failed"));
        }