• Code-C#:定时任务


    ylbtech-Code-C#:定时任务
    1.返回顶部
    1、
    using System;
    using System.IO;
    using System.Timers;
    using System.Web.Mvc;
    using System.Web.Routing;
    using DS.Common;
    using DS.Api.Libraries.Mvc;
    using Dapper.Contrib.Extensions;
    using Dapper;
    using NLog;
    using DS.Service.IoT;
    using DS.Base.Customers;
    using com.jaspersystems.api;
    using com.jaspersystems.api.Terminal;
    using DS.Base.IoT;
    using DS.Service.Customers;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace DS.Api {
        public class MvcApplication : System.Web.HttpApplication {
            protected void Application_Start()
            {
                log4net.Config.XmlConfigurator.Configure();
    
                AreaRegistration.RegisterAllAreas();
                RouteConfig.RegisterRoutes(RouteTable.Routes);
    
                GlobalFilters.Filters.Add(new HandleApiError());
    
                MvcHandler.DisableMvcResponseHeader = true;
    
                Timed_Execution();  //物联网数据同步任务
            }
    
            protected void Application_BeginRequest(object sender, EventArgs e) {
                if (Context.Request.FilePath == "/") Context.RewritePath("index.html");
            }
            private void Timed_Execution()
            {
                Timer objTimer = new Timer();
                objTimer.Interval = 1000 * 60 * 60;
                objTimer.Enabled = true;
                objTimer.Elapsed += ObjTimer_Elapsed;
            }
    
            private void ObjTimer_Elapsed(object sender, ElapsedEventArgs e)
            {
                //var logger = LogManager.GetCurrentClassLogger();
                
                #region 获取轮询账户
                List<Customer> dalsCustomer = null;
                DbConnectionManipulator.RunMainDatabase(conn =>
                {
                    dalsCustomer = conn.Query<Customer>("select a.* from Customer a WHERE IoT_Status=@IoT_Status and a.IsDeleted = 0", new { IoT_Status="0" }).ToList();
                });
                #endregion
    
                IoTTerminalService dalIoT = new IoTTerminalService();
                //首次同步,先删除,后同步
                if(dalsCustomer.Count>0)
                {
                    foreach(Customer dalCustomer in dalsCustomer)
                    {
    
                        #region 判断同步方式
                        bool flagSync = true;   //首次同步|增量同步
                        if (dalCustomer.IsSync==false)
                        {
                            flagSync = true;    //首次同步
                        }
                        else if(dalCustomer.IsSync==true&&dalCustomer.IsAsyn==false&&dalCustomer.IsComplete==false)
                        {
    
                            flagSync = true;    //首次同步
                        }
                        else
                        {
                            flagSync = false;   //增量同步
                        }
                        #endregion
    
                        // 配置物联网账户
                        int accountId = dalCustomer.Id;  //客户id
                        IoTSettingInfo dalEnter = new IoTSettingInfo() { username = dalCustomer.IoT_username, password = dalCustomer.IoT_password, licenseKey = dalCustomer.IoT_licenseKey };
    
                        if(flagSync)
                        {
                            #region 首次同步
                            DbConnectionManipulator.RunMainDatabaseInTransaction((conn, trans) => {
                                conn.Execute("delete IoTTerminal where accountId = @accountId", new { accountId = accountId }, trans);
                                conn.Execute("update Customer set IsSync=1,IsComplete=0 where Id = @Id", new { Id = accountId }, trans);
                            });
    
                            int totalPage = TerminalOpt.GetTotalPage(dalEnter); //总页数
                            for (int i = 1; i <= totalPage; i++)
                            {
                                string[] iccids = TerminalOpt.GetIccids(dalEnter, i);
                                TerminalType[] terminalTypes = TerminalOpt.Search(dalEnter, iccids);
    
                                DbConnectionManipulator.RunMainDatabase(conn =>
                                {
                                    foreach (TerminalType item in terminalTypes)
                                    {
                                        var dal = new IoTTerminal();
                                        #region
                                        dal.AccountId = accountId;
                                        dal.Iccid = item.iccid;
                                        dal.Msisdn = item.msisdn;
                                        dal.Imei = item.imei;
                                        dal.Status = item.status;
                                        dal.RatePlan = item.ratePlan;
                                        dal.Suspended = item.suspended;
                                        dal.OverageLimitReached = item.overageLimitReached;
                                        dal.MonthToDateUsage = item.monthToDateDataUsage;
                                        dal.Custom10 = item.custom10;
                                        #endregion
                                        conn.Insert(dal);
                                    }
                                });
                            }
    
                            DbConnectionManipulator.RunMainDatabaseInTransaction((conn, trans) => {
                                conn.Execute("update Customer set IsSync=1,IsComplete=1,UpdateDate=getdate() where Id = @Id", new { Id = accountId }, trans);
                            });
                            #endregion
                        }
                        else
                        {
                            #region 增量同步
                            DbConnectionManipulator.RunMainDatabaseInTransaction((conn, trans) => {
                                conn.Execute("update Customer set IsAsyn=1,IsComplete=0 where Id = @Id", new { Id = accountId }, trans);
                            });
                            DateTime since = new DateTime(dalCustomer.UpdateDate.Year,dalCustomer.UpdateDate.Month,dalCustomer.UpdateDate.Day); //上次更新时间
                            int totalPage = TerminalOpt.GetTotalPage(dalEnter, since); //总页数
                            for (int i = 1; i <= totalPage; i++)
                            {
                                string[] iccids = TerminalOpt.GetIccids(dalEnter, i, since);
                                TerminalType[] terminalTypes = TerminalOpt.Search(dalEnter, iccids);
    
                                DbConnectionManipulator.RunMainDatabase(conn =>
                                {
                                    foreach (TerminalType item in terminalTypes)
                                    {
                                        //var dal = new IoTTerminal();
                                        var dal = conn.QueryFirstOrDefault<IoTTerminal>("select * from [IoTTerminal] where iccid = @iccid", new { iccid = item.iccid });
                                        #region//dal.AccountId = accountId;
                                        dal.Iccid = item.iccid;
                                        dal.Msisdn = item.msisdn;
                                        dal.Imei = item.imei;
                                        dal.Status = item.status;
                                        dal.RatePlan = item.ratePlan;
                                        dal.Suspended = item.suspended;
                                        dal.OverageLimitReached = item.overageLimitReached;
                                        dal.MonthToDateUsage = item.monthToDateDataUsage;
                                        dal.Custom10 = item.custom10;
                                        #endregion
                                        conn.Update(dal);
                                    }
                                });
                            }
    
                            DbConnectionManipulator.RunMainDatabaseInTransaction((conn, trans) => {
                                conn.Execute("update Customer set IsComplete=1,UpdateDate=getdate() where Id = @Id", new { Id = accountId }, trans);
                            });
                            #endregion
                        }
                        
                    }
                }
            }
        }
    }
    2、
    2.返回顶部
     
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
     
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Vue学习笔记(十三) 响应式原理
    CSS学习笔记(十一) CSS3新特性
    JavaScript学习笔记(十三) ES6新特性
    Node.js学习笔记(六) express模块
    Node.js学习笔记(五) http模块
    Node.js学习笔记(四) fs、os、path模块
    Node.js学习笔记(三) 模块系统
    Node.js学习笔记(二) 包管理器
    Node.js学习笔记(一) 安装配置
    用Visual Studio创建集成了gtest的命令行工程
  • 原文地址:https://www.cnblogs.com/storebook/p/9389631.html
Copyright © 2020-2023  润新知