using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace TempHumAutoApp
{
    public partial class Form1 : Form
    {
       private   SerialPort Comm = new SerialPort();
       string strnewdata = "";
       bool Fan_on;
       bool IsAuto;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            comboPortName.Items.AddRange(ports);
            if(comboPortName.Items.Count>0)
            {
                comboPortName.SelectedIndex = 0;
            }
            Comm.DataReceived += Comm_DataReceived;
            Fan_on = false;
            IsAuto = false;
        }

        void Comm_DataReceived(object sender,SerialDataReceivedEventArgs e)
        {
            this.BeginInvoke(new Action(() => {
                string serialdata = Comm.ReadExisting();
                strnewdata += serialdata;
                if(strnewdata.LastIndexOf("0101")>0)
                {
                    int tempindex = strnewdata.LastIndexOf("0101");
                    if(strnewdata.Substring(tempindex).Length>=6)
                    {
                        txtTemp.Text = strnewdata.Substring(tempindex + 4, 2);
                    }
                }
                if (strnewdata.LastIndexOf("0102") > 0)
                {
                    int humindex = strnewdata.LastIndexOf("0102");
                    if (strnewdata.Substring(humindex).Length >= 6)
                    {
                        txtHum.Text = strnewdata.Substring(humindex + 4, 2);
                    }
                }

            }), null);
        }

        private void buttonOpenCloseCom_Click(object sender, EventArgs e)
        {
            if (Comm.IsOpen)
            {
                Comm.Close();
                txtTemp.Text = "";
                txtHum.Text = "";
                buttonOpenCloseCom.Text = "打开串口";
            }
            else
            {
                Comm.PortName = comboPortName.Text;
                Comm.BaudRate = 9600;
                try
                {
                    Comm.Open();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                buttonOpenCloseCom.Text = "关闭串口";
            }
        }

        private void btnFanon_Click(object sender, EventArgs e)
        {
             if(Comm.IsOpen&&IsAuto==false)
             {
                if(!Fan_on)
                {
                    Comm.Write("267");
                    System.Threading.Thread.Sleep(500);
                    Fan_on = true;
                    btnFanon.Enabled = false;
                    btnFanoff.Enabled = true;
                }
             }
        }

        private void btnFanoff_Click(object sender, EventArgs e)
        {
           if(Comm.IsOpen&&IsAuto==false)
           {
              if(Fan_on)
              {
                  Comm.Write("267");
                  System.Threading.Thread.Sleep(500);
                  Fan_on = false;
                  btnFanon.Enabled = true;
                  btnFanoff.Enabled = false;
              }
           }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
             if(IsAuto==true&&Comm.IsOpen)
             {
                 if (cbktemphum.SelectedIndex == 0)
                 {
                     int temp = Convert.ToInt32(txtTemp.Text);
                     int settemp = (int)this.numericUpDown1.Value;
                     if (temp > settemp)
                     {
                         if (!Fan_on)
                         {
                             Comm.Write("267");
                             System.Threading.Thread.Sleep(500);
                             Fan_on = true;
                             this.pictureBox1.Image = TempHumAutoApp.Resource1.backward;
                         }
                     }
                     else
                     {
                         if (Fan_on)
                         {
                             Comm.Write("267");
                             System.Threading.Thread.Sleep(500);
                             Fan_on = false;
                             this.pictureBox1.Image = TempHumAutoApp.Resource1.stop;
                         }
                     }
                 }
                 else
                 {
                     if (cbktemphum.SelectedIndex == 1)
                     {
                         int hum = Convert.ToInt32(txtHum.Text);
                         int sethum = (int)this.numericUpDown1.Value;
                         if (hum > sethum)
                         {
                             if (!Fan_on)
                             {
                                 Comm.Write("267");
                                 System.Threading.Thread.Sleep(500);
                                 Fan_on = true;
                                 this.pictureBox1.Image = TempHumAutoApp.Resource1.backward;
                             }
                         }
                         else
                         {
                             if (Fan_on)
                             {
                                 Comm.Write("267");
                                 System.Threading.Thread.Sleep(500);
                                 Fan_on = false;
                                 this.pictureBox1.Image = TempHumAutoApp.Resource1.stop;
                             }
                         }
                     }
                 }


             }
        }

        private void cbkAutomode_CheckedChanged(object sender, EventArgs e)
        {
            if (cbkAutomode.Checked)
            {
                IsAuto = true;
                timer1.Enabled = true;
                btnFanoff.Enabled = false;
                btnFanon.Enabled = false;
            }
            else
            {
                IsAuto = false;
                timer1.Enabled = false;
                if (!Fan_on)
                {
                    btnFanon.Enabled = true;
                    btnFanoff.Enabled = false;
                }
                else
                { 
                   if(Fan_on)
                   {
                       btnFanon.Enabled = false;
                       btnFanoff.Enabled = true;
                   }
                }

            }
        }

       
    }
}
