예제 #1
0
        private void SaveData(object obj)
        {
            while (true)
            {
                bool sleep = (bool)obj;
                try
                {
                    DeviceData[] recDatas = receivedDatas.ToArray();
                    if (recDatas.Length > 0)
                    {
                        submitTable.Clear();
                        foreach (DeviceData item in recDatas)
                        {
                            DataRow dr_bg = submitTable.NewRow();
                            dr_bg["mt_name"]      = "bg";
                            dr_bg["mt_value"]     = item.Value;
                            dr_bg["mt_isnormal"]  = TmoDataComm.CheckValueIsNormal("bg", item.Value) ? 1 : 0;
                            dr_bg["mt_time"]      = item.ValueTime;
                            dr_bg["mt_timestamp"] = TmoShare.DateTime2TimeStamp(item.ValueTime).ToString();
                            dr_bg["user_id"]      = item.SerialNumber;
                            dr_bg["dev_type"]     = item.DeviceType;
                            submitTable.Rows.Add(dr_bg);
                            AddListMsg(null, item.ToString());
                        }

                        bool addsuc = tmo_monitorManager.Instance.AddMonitorData(submitTable);
                        if (!addsuc)
                        {
                            AddListMsg(null, "保存无线设备数据到数据库失败");
                            LogHelper.Log.Error("保存无线设备数据", new Exception("保存无线设备数据到数据库失败"));
                        }
                        else
                        {
                            foreach (DeviceData item in recDatas)
                            {
                                receivedDatas.Remove(item);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    AddListMsg(null, "SaveData 保存无线设备数据异常");
                    LogHelper.Log.Error("保存无线设备数据异常", ex);
                }

                if (sleep)
                {
                    Thread.Sleep(10000); //10秒检测一次数据
                }
                else
                {
                    break;
                }
            }
        }