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 rentihongwaiAutoApp
{
    public partial class Form1 : Form
    {
       private SerialPort Comm = new SerialPort();
       string strnewdata = "";
       bool IsAuto;
       bool Led_on;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            this.comboPortName.Items.AddRange(ports);
            this.comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;
            Comm.DataReceived += Comm_DataReceived;
            IsAuto = false;
            Led_on = false;

        }
        void Comm_DataReceived(object sender,SerialDataReceivedEventArgs e)
        {
            this.BeginInvoke(new Action(() =>
                {
                    string serialdata = Comm.ReadExisting();
                    strnewdata += serialdata;
                    if(strnewdata.LastIndexOf("555555")>=0)
                    {
                        txtPerson.Text = "当前无人";
                        HongwaiBaoJing.BackColor = Color.GreenYellow;
                        strnewdata = "";
                    }
                    if(strnewdata.LastIndexOf("666666")>=0)
                    {
                        txtPerson.Text = "当前有人";
                        HongwaiBaoJing.BackColor = Color.Red;
                        strnewdata = "";
                    }
                }), null);
        }


        private void buttonOpenCloseCom_Click(object sender, EventArgs e)
        {
            if (Comm.IsOpen)
            {
                Comm.Close();
            }
            else
            {
                Comm.PortName = this.comboPortName.Text;
                Comm.BaudRate = 9600;
                try
                {
                    Comm.Open();
                }
                catch(Exception ex)
                {
                    Comm = new SerialPort();
                    MessageBox.Show(ex.Message);
                }
            }

            buttonOpenCloseCom.Text = Comm.IsOpen ? "关闭串口" : "打开串口";
        }

        private void btnRelayon_Click(object sender, EventArgs e)
        {
              if(IsAuto==false && Comm.IsOpen)
              {
                 if(!Led_on)
                 {
                     Comm.Write("287");
                     System.Threading.Thread.Sleep(500);
                     Led_on = true;
                     btnRelayoff.Enabled = true;
                     btnRelayon.Enabled = false;
                 }
              }
        }

        private void btnRelayoff_Click(object sender, EventArgs e)
        {
                if(IsAuto==false && Comm.IsOpen)
                {
                    if(Led_on)
                    {
                        Comm.Write("287");
                        System.Threading.Thread.Sleep(500);
                        Led_on = false;
                        btnRelayon.Enabled = true;
                        btnRelayoff.Enabled = false;

                    }
                }
        }

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

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
              if(IsAuto==true && Comm.IsOpen)
              {
                  if (txtPerson.Text == "当前有人")
                  {
                      if (!Led_on)
                      {
                          Comm.Write("287");
                          System.Threading.Thread.Sleep(500);
                          Led_on = true;
                          pictureBox1.Image = rentihongwaiAutoApp.Resource1.main_lamp;
                      }
                  }
                  else
                  { 
                         if(txtPerson.Text=="当前无人")
                          {
                             if(Led_on)
                             {
                                 Comm.Write("287");
                                 System.Threading.Thread.Sleep(500);
                                 Led_on = false;
                                 pictureBox1.Image = rentihongwaiAutoApp.Resource1.room_lamp_off;
                             }

                          }
                  }

              }
        }
    }
}
