123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- using EVCB_OCPP.TaskScheduler.Models;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Transactions;
- namespace EVCB_OCPP.TaskScheduler.Services
- {
- public class CommonCustomerService : ICustomerService
- {
- private NLog.ILogger logger = NLog.LogManager.GetCurrentClassLogger();
- private Guid customerId = Guid.Empty;
- private string customerName = string.Empty;
- private string _partnerAPIRoot = string.Empty;
- private string _saltkey = string.Empty;
- private CancellationToken _ct;
- private DatabaseService _dbService = new DatabaseService();
- private ParallelOptions po = new ParallelOptions();
- private OuterHttpClient httpClient = new OuterHttpClient();
- private int ChargeRecordCallCounter = 0;
- public CommonCustomerService() { }
- public CommonCustomerService(Guid customerId)
- {
- this.customerId = customerId;
- customerName = _dbService.GetCustomerName(this.customerId);
- _dbService.GetCustomerName(this.customerId);
- var connectionInfo = _dbService.GetAPIConnectionInfo(customerId);
- _saltkey = connectionInfo.ApiKey;
- _partnerAPIRoot = connectionInfo.ApiUrl;
- }
- public List<Guid> GetCallPartnerCustomers()
- {
- return _dbService.GetCallParterAPICustomers();
- }
- async public Task ReportStartTransaction()
- {
- var items = _dbService.GetNeedReportSession(customerId, true, 1000);
- Stopwatch watch = new Stopwatch();
- watch.Start();
- List<Task> groupTasks = new List<Task>();
- int skipCount = 0;
- int count = items.Count / 5 <= 100 ? items.Count : items.Count / 5;
- while (skipCount < items.Count)
- {
- if (items.Count - skipCount < count)
- {
- count = items.Count - skipCount;
- }
- var templst = items.Skip(skipCount).Take(count).ToList();
- if (templst.Count > 0)
- {
- Task t = Task.Factory.StartNew(async () =>
- {
- await Assigned_StartTransactionCallbackTask(templst);
- }, TaskCreationOptions.AttachedToParent);
- groupTasks.Add(t);
- }
- skipCount += count;
- }
- while (ChargeRecordCallCounter != groupTasks.Count)
- {
- await Task.Delay(10);
- }
- watch.Stop();
- logger.Debug("ReportStartTransaction Task(" + items.Count() + ") : It takes " + watch.ElapsedMilliseconds / 1000 + " Seconds");
- }
- async private Task Assigned_StartTransactionCallbackTask(List<Models.Transaction> reportlst)
- {
- await Task.Factory.StartNew(async () =>
- {
- //處理主機傳送的有指令
- try
- {
- if (reportlst.Count > 0)
- {
- int completecounter = 0;
- Dictionary<int, TransactionResponse> sendBack = new Dictionary<int, TransactionResponse>();
- object responseLock = new object();
- po.CancellationToken = _ct;
- po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
- Parallel.ForEach(reportlst, po, async (r) =>
- {
- var request = new
- {
- ChargeBoxId = r.ChargeBoxId,
- ConnectorId = r.ConnectorId,
- SessionId = r.Id,
- MeterStart = r.MeterStart,
- IdTag = r.StartIdTag,
- StartTime = r.StartTime.ToString(DefaultSetting.UTC_DATETIMEFORMAT)
- };
- var response = await httpClient.Post(_partnerAPIRoot + "start_session", new Dictionary<string, string>()
- {
- { "PartnerId",customerId.ToString()}
- }, JsonConvert.SerializeObject(request, DefaultSetting.JSONSERIALIZER_FORMAT), _saltkey);
- lock (responseLock)
- {
- sendBack.Add(r.Id, new TransactionResponse()
- {
- StartTransactionReportedOn = DateTime.Now,
- ErrorMsg = response.Success ? null :
- (response.Exception == null ? response.Response : response.Exception.ToString())
- });
- completecounter++;
- }
- });
- while (completecounter != reportlst.Count)
- {
- await Task.Delay(10);
- }
- _dbService.ReportStartTx(sendBack);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Assigned_StartTransactionCallbackTask Exception: " + ex.GetBaseException().ToString());
- }
- ChargeRecordCallCounter++;
- }, TaskCreationOptions.AttachedToParent);
- }
- async public Task ReportStopTransaction()
- {
- var items = _dbService.GetNeedReportSession(customerId, false, 1000);
- Stopwatch watch = new Stopwatch();
- watch.Start();
- List<Task> groupTasks = new List<Task>();
- int skipCount = 0;
- int count = items.Count / 5 <= 100 ? items.Count : items.Count / 5;
- while (skipCount < items.Count)
- {
- if (items.Count - skipCount < count)
- {
- count = items.Count - skipCount;
- }
- var templst = items.Skip(skipCount).Take(count).ToList();
- if (templst.Count > 0)
- {
- Task t = Task.Factory.StartNew(async () =>
- {
- await Assigned_StopTransactionCallbackTask(templst);
- }, TaskCreationOptions.AttachedToParent);
- groupTasks.Add(t);
- }
- skipCount += count;
- }
- while (ChargeRecordCallCounter != groupTasks.Count)
- {
- await Task.Delay(10);
- }
- watch.Stop();
- Console.WriteLine("ReportStopTransaction Task(" + items.Count() + ") : It takes " + watch.ElapsedMilliseconds / 1000 + " Seconds");
- }
- async private Task Assigned_StopTransactionCallbackTask(List<Models.Transaction> reportlst)
- {
- await Task.Factory.StartNew(async () =>
- {
- //處理主機傳送的有指令
- try
- {
- if (reportlst.Count > 0)
- {
- int completecounter = 0;
- Dictionary<int, TransactionResponse> sendBack = new Dictionary<int, TransactionResponse>();
- object responseLock = new object();
- po.CancellationToken = _ct;
- po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
- Parallel.ForEach(reportlst, po, async (r) =>
- {
- var request = new
- {
- ChargeBoxId = r.ChargeBoxId,
- ConnectorId = r.ConnectorId,
- SessionId = r.Id,
- MeterStart = r.MeterStart,
- MeterStop = r.MeterStop,
- IdTag = r.StartIdTag,
- StartTime = r.StartTime.ToString(DefaultSetting.UTC_DATETIMEFORMAT),
- StopTime = r.StopTime.ToString(DefaultSetting.UTC_DATETIMEFORMAT),
- StopReason = r.StopReasonId < 1 ? "Unknown" : (r.StopReasonId > 12 ? "Unknown" : ((Reason)r.StopReasonId).ToString()),
- Receipt = r.Receipt,
- TotalCost = r.Cost,
- Fee = r.Fee
- };
- var response = await httpClient.Post(_partnerAPIRoot + "completed_session", new Dictionary<string, string>()
- {
- { "PartnerId",customerId.ToString()}
- }, JsonConvert.SerializeObject(request, DefaultSetting.JSONSERIALIZER_FORMAT), _saltkey);
- lock (responseLock)
- {
- sendBack.Add(r.Id, new TransactionResponse()
- {
- StopTransactionReportedOn = DateTime.Now,
- ErrorMsg = response.Success ? null :
- (response.Exception == null ? response.Response : response.Exception.ToString())
- });
- completecounter++;
- }
- });
- while (completecounter != reportlst.Count)
- {
- await Task.Delay(10);
- }
- _dbService.ReportStopTx(sendBack);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Assigned_StartTransactionCallbackTask Exception: " + ex.GetBaseException().ToString());
- }
- ChargeRecordCallCounter++;
- }, TaskCreationOptions.AttachedToParent);
- }
- async public Task MonitorRemoteCommand()
- {
- Stopwatch watch = new Stopwatch();
- watch.Start();
- _dbService.TurntoTimeoutMachineOperateCommands(60);
- await Task.Delay(10);
- watch.Stop();
- logger.Debug("ReportExecutionofRemoteCommand Task : It takes " + watch.ElapsedMilliseconds / 1000 + " Seconds");
- }
- async public Task ReportExecutionofRemoteCommand()
- {
- var items = _dbService.GetNeedReportExecution(customerId, 1000);
- Stopwatch watch = new Stopwatch();
- watch.Start();
- List<Task> groupTasks = new List<Task>();
- int skipCount = 0;
- int count = items.Count / 5 <= 100 ? items.Count : items.Count / 5;
- while (skipCount < items.Count)
- {
- if (items.Count - skipCount < count)
- {
- count = items.Count - skipCount;
- }
- var templst = items.Skip(skipCount).Take(count).ToList();
- if (templst.Count > 0)
- {
- Task t = Task.Factory.StartNew(async () =>
- {
- await Assigned_ReportExecutionofRemoteCommandTask(templst);
- }, TaskCreationOptions.AttachedToParent);
- groupTasks.Add(t);
- }
- skipCount += count;
- }
- while (ChargeRecordCallCounter != groupTasks.Count)
- {
- await Task.Delay(10);
- }
- watch.Stop();
- logger.Debug("ReportExecutionofRemoteCommand Task(" + items.Count() + ") : It takes " + watch.ElapsedMilliseconds / 1000 + " Seconds");
- }
- async private Task Assigned_ReportExecutionofRemoteCommandTask(List<MachineOperateRecord> reportlst)
- {
- await Task.Factory.StartNew(async () =>
- {
- //處理主機傳送的有指令
- try
- {
- if (reportlst.Count > 0)
- {
- int completecounter = 0;
- Dictionary<int, BasicResponse> sendBack = new Dictionary<int, BasicResponse>();
- object responseLock = new object();
- po.CancellationToken = _ct;
- po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
- Parallel.ForEach(reportlst, po, async (r) =>
- {
- var request = new
- {
- ChargeBoxId = r.ChargeBoxId,
- SerialNo = r.SerialNo,
- CommandType = r.ActionConverttoCommandType(),
- Result = r.GetExecution().Result,
- Message = r.GetExecution().Detail,
- };
- var response = await httpClient.Post(_partnerAPIRoot + "commands/results", new Dictionary<string, string>()
- {
- { "PartnerId",customerId.ToString()}
- }, JsonConvert.SerializeObject(request, DefaultSetting.JSONSERIALIZER_FORMAT), _saltkey);
- lock (responseLock)
- {
- sendBack.Add(r.Id, new BasicResponse()
- {
- ReportedOn = DateTime.Now,
- ErrorMsg = response.Success ? null :
- (response.Exception == null ? response.Response : response.Exception.ToString())
- });
- completecounter++;
- }
- });
- while (completecounter != reportlst.Count)
- {
- await Task.Delay(10);
- }
- _dbService.ReportExecution(sendBack);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Assigned_ReportExecutionofRemoteCommandTask Exception: " + ex.GetBaseException().ToString());
- }
- ChargeRecordCallCounter++;
- }, TaskCreationOptions.AttachedToParent);
- }
- }
- }
|