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
}
}
}
}
}
}