コード例 #1
0
        /// <summary>
        /// 写历史记录
        /// </summary>
        /// <param name="point"></param>
        /// <param name="value"></param>
        static void WriteHistory(DevicePoint point, double value)
        {
            if (hisDB == null)
            {
                return;
            }
            try
            {
                lock (hisDB)
                {
                    var data = new SunRizServer.History();
                    data.PointId = point.id;
                    data.Address = point.Name;
                    data.Time    = DateTime.Now;
                    data.Value   = value;
                    hisDB.Insert(data);

                    if ((DateTime.Now - LastHisTime).TotalMinutes >= 2)
                    {
                        hisDB.CommitTransaction();
                        LastHisTime = DateTime.Now;
                        hisDB.BeginTransaction();
                    }
                }
            }
            catch (Exception ex)
            {
                using (Way.Lib.CLog log = new Way.Lib.CLog("HistoryAutoRec WriteHistory error"))
                {
                    log.Log(ex.ToString());
                }
                hisDB.Dispose();
                hisDB = null;
                SystemHelper.AddAlarm(new Alarm()
                {
                    Content = $"记录历史时,发生错误,错误信息:{ex.Message}"
                });
            }
        }
コード例 #2
0
        static void start()
        {
            try
            {
                if (hisDB != null)
                {
                    lock (hisDB)
                    {
                        hisDB.CommitTransaction();
                        hisDB.Dispose();
                    }
                    hisDB = null;
                }
                using (SysDB db = new SysDB())
                {
                    var sysSetting = db.SystemSetting.FirstOrDefault();
                    if (string.IsNullOrEmpty(sysSetting.HistoryPath))
                    {
                        return;
                    }
                    try
                    {
                        //目录不存在,创建目录
                        if (System.IO.Directory.Exists(sysSetting.HistoryPath) == false)
                        {
                            System.IO.Directory.CreateDirectory(sysSetting.HistoryPath);
                        }
                        HistoryDataPath = $"data source=\"{sysSetting.HistoryPath.Replace("\\", "/")}/history_data.db\"";
                        hisDB           = new DB.SunRiz(HistoryDataPath, Way.EntityDB.DatabaseType.Sqlite);
                        LastHisTime     = DateTime.Now;
                        hisDB.BeginTransaction();
                    }
                    catch
                    {
                        return;
                    }


                    var pointGroups = from m in db.DevicePoint
                                      where m.ValueRelativeChange == true || m.ValueAbsoluteChange == true || m.ValueOnTimeChange == true || m.IsAlarm == true
                                      group m by m.DeviceId into g
                                      select g;
                    foreach (var pointArr in pointGroups)
                    {
                        var deviceId = pointArr.Key.GetValueOrDefault();
                        var device   = db.Device.AsTracking().FirstOrDefault(m => m.id == deviceId);
                        var driver   = db.CommunicationDriver.AsTracking().FirstOrDefault(m => m.id == device.DriverID);

                        MyDriverClient client = new MyDriverClient(driver.Address, driver.Port.Value);
                        client.Points = (from m in pointArr
                                         select new MyDevicePoint(m)).ToArray();
                        AllClients.Add(client);
                        string[] pointAddrs = new string[client.Points.Length];
                        for (int i = 0; i < client.Points.Length; i++)
                        {
                            pointAddrs[i] = client.Points[i].DevicePoint.Address;
                            if (client.Points[i].DevicePoint.ValueOnTimeChange == true)
                            {
                                client.SaveOnTimeInfos.Add(new SaveOnTimeInfo()
                                {
                                    PointObj = client.Points[i],
                                    PointId  = client.Points[i].DevicePoint.id.Value,
                                    Interval = client.Points[i].DevicePoint.ValueOnTimeChangeSetting.GetValueOrDefault(),
                                });
                            }
                        }
                        watchClient(client, device, pointAddrs);
                        //启动定时保存的线程
                        saveValueOnTime_Thread(client, device);
                    }
                }
            }
            catch (Exception ex)
            {
                using (Way.Lib.CLog log = new Way.Lib.CLog("HistoryAutoRec error "))
                {
                    log.Log(ex.ToString());
                }
            }
        }