Exemplo n.º 1
0
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Timer_Ex           tm       = sender as Timer_Ex;
            DataTypePoint      dtp      = DataTypes[tm.Name];
            DateTime           CurrTime = DateTime.Now;//当前时间
            ExecSubscriptClass esc      = new ExecSubscriptClass(DataTypes[tm.Name]);

            esc.OnCancelSubScript += FinishedSubscript;
            esc.OnCancelSubScript += CancelSubscript;
            esc.OnUpdateData      += OnUpdateData;
            if (!tm.SubsciptStatus)                                              //未订阅状态,开始订阅,设置下次启动时间到停止时间
            {
                DateTime EndT = CurrTime.Date.Add(dtp.ReceiveEndTime.TimeOfDay); //当日的结束时间
                if (CurrTime > EndT)                                             //当前时间已经过了结束时间,不订阅,唤醒时间设置到下个周期的开始时间
                {
                    tm.Interval = CurrTime.AddDays(1).Add(dtp.ReceiveStartTime.TimeOfDay).Subtract(CurrTime).TotalMilliseconds;
                    Log("错过取消订阅时间,不订阅", string.Format("下次唤醒订阅时间{0}", CurrTime.AddDays(1).Add(dtp.ReceiveStartTime.TimeOfDay).ToLongTimeString()));
                    return;
                }
                esc.Subscript(CurrTime > CurrTime.Date.Add(dtp.ReceiveStartTime.TimeOfDay));
                tm.SubsciptStatus = true;
                tm.Interval       = EndT.Subtract(CurrTime).TotalMilliseconds;//当日结束时唤醒
                Log("已订阅,下次唤醒取消订阅时间", EndT.ToLongTimeString());
            }
            else//订阅状态,停止订阅,设置下次启动时间到停止时间
            {
                DateTime BegT = DateTime.Now.Date.AddDays(1).Add(dtp.ReceiveStartTime.TimeOfDay);//当日的结束时间
                esc.CancelSubscript();
                tm.SubsciptStatus = false;
                tm.Interval       = BegT.Subtract(CurrTime).TotalMilliseconds;
                Log("下次唤醒订阅时间", BegT.ToLongTimeString());
            }
        }
Exemplo n.º 2
0
        public SubscriptData()
        {
            InitializeComponent();
            this.ServiceName = "接收订阅数据服务";
            DataTypes        = new Dictionary <string, DataTypePoint>();
            int index = 0;

            AllHy = new Dictionary <string, ExpectList>();
            if (GlobalClass.TypeDataPoints.Where(p => p.Value.SubScriptModel == 1).Count() == 0)
            {
                return;                                                                                //如果订阅模式为1的数量为0,啥都不干
            }
            foreach (string key in GlobalClass.TypeDataPoints.Keys)
            {
                DataTypePoint dtp = GlobalClass.TypeDataPoints[key];
                if (dtp.SubScriptModel == 1)//订阅模式
                {
                    DataTypes.Add(key, dtp);
                    Timer_Ex timer = new Timer_Ex(index, key)
                    {
                        AutoReset = true,
                        Interval  = dtp.ReceiveSeconds * 1000
                    };
                    timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
                    timer.Enabled  = false;
                    if (Tm_ForSubScripts == null)
                    {
                        Tm_ForSubScripts = new List <Timer_Ex>();
                    }
                    Tm_ForSubScripts.Add(timer);
                    index++;
                }
            }
            try
            {
                SaveTimer.Interval = DataTypes.Where(p => p.Value.SaveInterVal > 0).First().Value.SaveInterVal;//第一个设置了SaveInterVal的对象的值,没有会报错
            }
            catch
            {
                SaveTimer.Interval = 5 * 60 * 1000;//默认5分钟保存一次
            }
            SaveTimer.Enabled   = false;
            SaveTimer.AutoReset = true;
            SaveTimer.Elapsed  += SaveTimer_Elapsed;
            //.First<DataTypePoint>().SaveInterVal;
        }