public void SetPower(bool status) { YeelightControl control = new YeelightControl(); control.id = commandId++; control.method = "set_power"; [email protected](status ? "on" : "off"); [email protected]("smooth"); [email protected](2000); SendControl(control); this._Power = status; }
private YeelightResult SendControl(YeelightControl control) { var jsonText = JSON.Dump(control, EncodeOptions.NoTypeHints) + "\r\n"; byte[] data = System.Text.Encoding.ASCII.GetBytes(jsonText); stream.Write(data, 0, data.Length); stream.Flush(); byte[] recvData = new byte[1024]; StringBuilder myCompleteMessage = new StringBuilder(); int numberOfBytesRead = 0; do { numberOfBytesRead = stream.Read(recvData, 0, recvData.Length); myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(recvData, 0, numberOfBytesRead)); }while (stream.DataAvailable); // Print out the received message to the console. MessageBox.Show(myCompleteMessage.ToString()); return(new YeelightResult()); }