物联网工具——前后台分离 (16)

126 阅读1分钟

修改显示界面thsensor

说明:V2.0 版本待制作完毕,会同步发布到gitee仓库。
gitee地址:gitee.com/vectorli/te…

首先删除以下不用的内容

  • 删除mainform中的打开获取串口配置功能
  • 删除global中的全部串口配置,和串口操作变量

修改sysconf界面

image.png

确定按钮回调函数修改:

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //串口
                string comname = this.comboBox1.Text;
                //波特率
                int baudrate = int.Parse(this.comboBox2.Text);
                //入库
                JObject confinfo = new JObject();
                confinfo.Add("port", comname);
                confinfo.Add("baute", baudrate);
                confinfo.Add("devaddr", this.textBox1.Text);
                DB_SerAPI.SetComConfToDB(confinfo.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace + "\r\n" + ex.Message);
            }
        }

修改load函数:

        /// <summary>
        /// 加载函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sysconf_Load(object sender, EventArgs e)
        {
            //扫描串口
            string[] comlist = ComPort.V_ScanPort();
            if (comlist.Length < 1)
            {
                MessageBox.Show("没有扫描到串口,请检查硬件连接");
                return;
            }
            else
            {
                foreach (string name in comlist)
                {
                    this.comboBox1.Items.Add(name);
                }
            }
            //波特率初始化
            this.comboBox2.Items.Add("4800");
            this.comboBox2.Items.Add("9600");
            this.comboBox2.Items.Add("115200");
        }

修改实时数据界面RealDataWin

修改定时器

定时器由原来的从com口读取数据,转变为读取数据库数据,并判断如果有新数据产生,则刷新界面

        /// <summary>
        /// 定时器回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                //判断数据的个数
                int nowcount = DB_SerAPI.GetTodayDataCountFromDB();
                //刷新
                if (nowcount > this.DataBuffer.Rows.Count)
                {
                    //获取最新数据
                    DataRow lastdata = DB_SerAPI.GetLastDataFromDB();
                    //刷新列表
                    if (lastdata != null)
                    {
                        //MessageBox.Show("刷新");
                        DataRow row = DataBuffer.NewRow();
                        row["id"] = lastdata["id"].ToString();
                        row["intime"] = lastdata["intime"].ToString();
                        row["temp"] = lastdata["temp"].ToString();
                        row["humi"] = lastdata["humi"].ToString();
                        DataBuffer.Rows.Add(row);
                        if (!lineopend)
                            datagridewin.RefreshDisplay(DataBuffer);
                        else
                            datalinewin.RefreshDisplay(DataBuffer);
                    }
                }
            }
            catch (Exception ex)
            {
                MyLogger._.Error(ex.Message + "\r\n" + ex.StackTrace);
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
            }
        }

新增db函数

  • 新增获取当日数据条目
  • 新增获取当前最新数据
  • 新增获取当日数据
        #region 获取最新数据
        /// <summary>
        /// 返回最新数据
        /// </summary>
        /// <param name="mn"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DataRow GetLastDataFromDB()
        {
            string sqlcmd = $"select * from THData order by id desc limit 0,1";
            DataTable dt = MySqliteAPI.GetDataFromDB(sqlcmd);
            if (dt == null || dt.Rows.Count < 1)
                return null;
            else
                return dt.Rows[0];
        }
        #endregion

        #region 获取数据个数
        /// <summary>
        /// 获取当日数据个数
        /// </summary>
        /// <returns></returns>
        public static int GetTodayDataCountFromDB()
        {
            string begintime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            string endtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string sqlcmd = $"select count(*) from THData where intime between '{begintime}' and '{endtime}'";
            return MySqliteAPI.GetDataCountFromDB(sqlcmd);
        }
        #endregion

        #region 获取温湿度信息
        /// <summary>
        /// 获取所有的数据
        /// </summary>
        /// <returns></returns>
        public static DataTable GetTodayDataFromDB()
        {
            string begintime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            string endtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string sqlcmd = $"Select * from THData where intime between '{begintime}' and '{endtime}'";
            return MySqliteAPI.GetDataFromDB(sqlcmd);
        }

编译测试

image.png

image.png