|
@@ -1,11 +1,15 @@
|
|
-using Fuel.Core;
|
|
|
|
|
|
+using DFS.Infrastructure;
|
|
|
|
+using Fuel.Application.MqttService;
|
|
|
|
+using Fuel.Core;
|
|
using Fuel.Core.Models;
|
|
using Fuel.Core.Models;
|
|
using Fuel.Core.Nozzle.Dto;
|
|
using Fuel.Core.Nozzle.Dto;
|
|
using FuelServer.Core.Entity;
|
|
using FuelServer.Core.Entity;
|
|
|
|
+using Newtonsoft.Json;
|
|
using Org.BouncyCastle.Ocsp;
|
|
using Org.BouncyCastle.Ocsp;
|
|
using System;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
|
|
+using System.Net;
|
|
using System.Text;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
using System.Transactions;
|
|
using System.Transactions;
|
|
@@ -17,10 +21,13 @@ namespace Fuel.Application.Service
|
|
{
|
|
{
|
|
private readonly EntityHelper _entityHelper;
|
|
private readonly EntityHelper _entityHelper;
|
|
public readonly IFreeSql _fsql;
|
|
public readonly IFreeSql _fsql;
|
|
- public NozzleService(EntityHelper entityHelper, IFreeSql fsql)
|
|
|
|
|
|
+ private readonly IMqttClientService _mqttService;
|
|
|
|
+
|
|
|
|
+ public NozzleService(EntityHelper entityHelper, IFreeSql fsql, IMqttClientService mqttService)
|
|
{
|
|
{
|
|
_entityHelper = entityHelper;
|
|
_entityHelper = entityHelper;
|
|
_fsql = fsql;
|
|
_fsql = fsql;
|
|
|
|
+ _mqttService = mqttService;
|
|
}
|
|
}
|
|
#region 油品
|
|
#region 油品
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -30,6 +37,7 @@ namespace Fuel.Application.Service
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
public async Task<ServiceResponse> UploadProduct(UploadProduct uploadProduct)
|
|
public async Task<ServiceResponse> UploadProduct(UploadProduct uploadProduct)
|
|
{
|
|
{
|
|
|
|
+
|
|
Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First();
|
|
var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First();
|
|
if (_product != null)
|
|
if (_product != null)
|
|
@@ -190,6 +198,9 @@ namespace Fuel.Application.Service
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
public async Task<ServiceResponse> UploadNozzle(UploadNozzle uploadNozzle)
|
|
public async Task<ServiceResponse> UploadNozzle(UploadNozzle uploadNozzle)
|
|
{
|
|
{
|
|
|
|
+ await _mqttService.SubscribeAsync("fromClound/12345678-9abc-def0-1234-56789abcdef0");
|
|
|
|
+ await Task.Delay(2000);
|
|
|
|
+ await _mqttService.PublishAsync("fromClound/12345678-9abc-def0-1234-56789abcdef0", "测试");
|
|
//RedisHelper.HSetAsync("Transaction", "11:22:33:44", "3232");
|
|
//RedisHelper.HSetAsync("Transaction", "11:22:33:44", "3232");
|
|
//RedisHelper.SetAsync("33:22:33:44", "qweqweqwe", 3600);
|
|
//RedisHelper.SetAsync("33:22:33:44", "qweqweqwe", 3600);
|
|
// var fsdds = RedisHelper.GetAsync("33:22:33:44");
|
|
// var fsdds = RedisHelper.GetAsync("33:22:33:44");
|
|
@@ -302,5 +313,102 @@ namespace Fuel.Application.Service
|
|
});
|
|
});
|
|
}
|
|
}
|
|
#endregion
|
|
#endregion
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 更新授权状态
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="nozzleAuthorization"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public async Task<ServiceResponse> UpdateNozzleAuthorization(NozzleAuthorization nozzleAuthorization)
|
|
|
|
+ {
|
|
|
|
+ Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
|
+ string key = guid + "_" + nozzleAuthorization.NozzleId;//授权结果的key
|
|
|
|
+ KeyValueCache.AddOrUpdate("authGun", key, nozzleAuthorization);//更新授权结果
|
|
|
|
+ return ServiceResponse.Ok();
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 向fcc发起油枪授权
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="nozzleAuthorization"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public async Task<ServiceResponse> NozzleAuthorizationAsync(int trxId)
|
|
|
|
+ {
|
|
|
|
+ Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
|
+ var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
|
|
|
|
+ if (trx == null)
|
|
|
|
+ {
|
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
|
|
|
|
+ }
|
|
|
|
+ string key = guid + "_" + trx.NozzleId;//授权结果的key
|
|
|
|
+ string jsonString = JsonConvert.SerializeObject(trx);
|
|
|
|
+ await _mqttService.SubscribeAsync("fromClound/" + guid);
|
|
|
|
+ await Task.Delay(2000);
|
|
|
|
+ var sendjson = new { type= 1,data = jsonString };
|
|
|
|
+ await _mqttService.PublishAsync("fromClound/" + guid, JsonConvert.SerializeObject(sendjson));
|
|
|
|
+ KeyValueCache.AddOrUpdate("authGun", key, AuthorizationStatus.WaitAuthorization);//添加字典,用于监听授权结果
|
|
|
|
+ bool changed = await KeyValueCache.MonitorDictionaryChanges("authGun", key, AuthorizationStatus.WaitAuthorization);
|
|
|
|
+ if (!changed)
|
|
|
|
+ {
|
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "授权失败");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var auth = KeyValueCache.GetValueOrDefault<NozzleAuthorization>("authGun", key);
|
|
|
|
+ if (auth == null || auth.OilMachineStatus != OilMachineStatus.Success)
|
|
|
|
+ {
|
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "授权失败");
|
|
|
|
+ }
|
|
|
|
+ trx.TransactionNumber = auth.TransactionNumber;
|
|
|
|
+ trx.authorizationStatus = AuthorizationStatus.Authorized;//将订单授权状态更改成已授权
|
|
|
|
+ _entityHelper.UpdateAsync(trx);
|
|
|
|
+ return ServiceResponse.Ok("授权成功");
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 向fcc发起取消油枪授权
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="nozzleAuthorization"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public async Task<ServiceResponse> CancelNozzleAuthorizationAsync(int trxId)
|
|
|
|
+ {
|
|
|
|
+ Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
|
+ var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
|
|
|
|
+ if (trx == null)
|
|
|
|
+ {
|
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
|
|
|
|
+ }
|
|
|
|
+ string key = guid + "_" + trx.NozzleId;//授权结果的key
|
|
|
|
+ string jsonString = JsonConvert.SerializeObject(trx);
|
|
|
|
+ await _mqttService.SubscribeAsync("fromClound/" + guid);
|
|
|
|
+ await Task.Delay(2000);
|
|
|
|
+ var sendjson = new { type = 2, data = jsonString };
|
|
|
|
+ await _mqttService.PublishAsync("fromClound/" + guid, JsonConvert.SerializeObject(sendjson));
|
|
|
|
+ KeyValueCache.AddOrUpdate("cancelAuth", key, AuthorizationStatus.WaitAuthorization);//添加字典,用于监听授权结果
|
|
|
|
+ bool changed = await KeyValueCache.MonitorDictionaryChanges("cancelAuth", key, AuthorizationStatus.WaitAuthorization);
|
|
|
|
+ if (!changed)
|
|
|
|
+ {
|
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "取消授权失败");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var auth = KeyValueCache.GetValueOrDefault<NozzleAuthorization>("cancelAuth", key);
|
|
|
|
+ if (auth == null || auth.OilMachineStatus != OilMachineStatus.Success)
|
|
|
|
+ {
|
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "取消授权失败");
|
|
|
|
+ }
|
|
|
|
+ trx.TransactionNumber = auth.TransactionNumber;
|
|
|
|
+ trx.authorizationStatus = AuthorizationStatus.Unauthorized;//将订单授权状态更改成已授权
|
|
|
|
+ _entityHelper.UpdateAsync(trx);
|
|
|
|
+ return ServiceResponse.Ok("取消授权成功");
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 更新取消授权状态
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="nozzleAuthorization"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public async Task<ServiceResponse> UpdateCancelNozzleAuthorization(NozzleAuthorization nozzleAuthorization)
|
|
|
|
+ {
|
|
|
|
+ Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
|
+ string key = guid + "_" + nozzleAuthorization.NozzleId;//授权结果的key
|
|
|
|
+ KeyValueCache.AddOrUpdate("cancelAuth", key, nozzleAuthorization);//更新授权结果
|
|
|
|
+ return ServiceResponse.Ok();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|