예제 #1
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}"));
        }
예제 #2
0
        protected void EnqueueCommand(SPWF04SxCommand cmd)
        {
            lock (this.pendingCommands) {
                this.pendingCommands.Enqueue(cmd);

                this.ReadyNextCommand();
            }
        }
예제 #3
0
 private void ReadyNextCommand()
 {
     lock (this.pendingCommands) {
         if (this.pendingCommands.Count != 0)
         {
             var cmd = (SPWF04SxCommand)this.pendingCommands.Dequeue();
             cmd.SetPayloadBuffer(this.readPayloadBuffer);
             this.activeCommand = cmd;
         }
         else
         {
             this.activeCommand = null;
         }
     }
 }
예제 #4
0
        protected void FinishCommand(SPWF04SxCommand cmd)
        {
            if (this.activeCommand != cmd)
            {
                throw new ArgumentException();
            }

            lock (this.pendingCommands) {
                cmd.Reset();

                this.commandPool.Release(cmd);

                this.ReadyNextCommand();
            }
        }
예제 #5
0
        public int ReadHttpResponse(byte[] buffer, int offset, int count)
        {
            if (this.activeHttpCommand == null)
            {
                throw new InvalidOperationException();
            }

            var len = this.activeHttpCommand.ReadBuffer(buffer, offset, count);

            if (len == 0)
            {
                this.FinishCommand(this.activeHttpCommand);

                this.activeHttpCommand = null;
            }

            return(len);
        }
예제 #6
0
        public void TurnOff()
        {
            if (!this.running)
            {
                return;
            }

            this.reset.SetDriveMode(GpioPinDriveMode.Output);
            this.reset.Write(GpioPinValue.Low);

            this.running = false;
            this.worker.Join();
            this.worker = null;

            this.pendingCommands.Clear();
            this.readPayloadBuffer.Reset();

            this.netifSockets.Clear();
            this.nextSocketId      = 0;
            this.activeCommand     = null;
            this.activeHttpCommand = null;

            this.commandPool.ResetAll();
        }