|
|
@@ -1,16 +1,12 @@ |
|
|
|
|
|
|
|
using BPA.KitChen.StoreManagement.Application.BaseDto; |
|
|
|
using BPA.KitChen.StoreManagement.Application.Service.Device.Dtos; |
|
|
|
using BPA.KitChen.StoreManagement.Application.BaseDto; |
|
|
|
using BPA.KitChen.StoreManagement.Core.Entity; |
|
|
|
using BPA.KitChen.StoreManagement.SqlSugar; |
|
|
|
using BPA.KitChen.StoreManagement.Core.Enum; |
|
|
|
using BPA.Message; |
|
|
|
using BPA.Message.IOT; |
|
|
|
using Furion.DependencyInjection; |
|
|
|
using Furion.DynamicApiController; |
|
|
|
using Furion.FriendlyException; |
|
|
|
using Furion.LinqBuilder; |
|
|
|
using Mapster; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.Extensions.Hosting; |
|
|
|
using MQTTnet; |
|
|
|
using MQTTnet.Client; |
|
|
@@ -23,42 +19,38 @@ using System.Net.Http.Json; |
|
|
|
using System.Text; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
namespace BPA.KitChen.StoreManagement.Application.Service.Device.Services |
|
|
|
namespace BPA.KitChen.StoreManagement.Application.Service.Device.Dtos |
|
|
|
{ |
|
|
|
|
|
|
|
[ApiDescriptionSettings("设备下发", Tag = "设备下发", SplitCamelCase = false)] |
|
|
|
public class DevicePushRecodeService: IDynamicApiController, ITransient, IDevicePushRecodeService |
|
|
|
public class DevicePushRecodeService: IDevicePushRecodeService, ITransient |
|
|
|
{ |
|
|
|
private readonly SqlSugarScope _db; |
|
|
|
private readonly ISqlSugarClient _db; |
|
|
|
private readonly IMqttClient _mqttClient; |
|
|
|
public DevicePushRecodeService(ISqlSugarClient db, IMqttClient mqttClient) |
|
|
|
{ |
|
|
|
_db = SqlSugarDb.Db; |
|
|
|
_db = db; |
|
|
|
_mqttClient=mqttClient; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 分页 |
|
|
|
/// </summary> |
|
|
|
/// <param name="input"></param> |
|
|
|
/// <returns></returns> |
|
|
|
[HttpPost("/api/devicepushrecode/page")] |
|
|
|
public async Task<PageUtil> Page(DevicePushRecodeDtoPageInput input) |
|
|
|
{ |
|
|
|
RefAsync<int> total = 0; |
|
|
|
var res = await _db.Queryable<BPA_DevicePushRecode>().Where(x=>x.Type== input.Type) |
|
|
|
.Select(t => new |
|
|
|
{ |
|
|
|
CreateAt = t.CreateAt, |
|
|
|
CreateBy = t.CreateBy, |
|
|
|
Id = t.Id, |
|
|
|
DeviceId = t.DeviceId, |
|
|
|
DeviceName= t.DeviceName, |
|
|
|
Type =t.Type, |
|
|
|
Topic=t.Topic, |
|
|
|
DataResore=t.DataResore, |
|
|
|
}).OrderBy(x => x.CreateAt, OrderByType.Desc).ToPageListAsync(input.Current, input.PageSize, total); |
|
|
|
var res = await _db.Queryable<BPA_DevicePushRecode>().Where(x => x.Type == input.Type) |
|
|
|
.Select(t => new |
|
|
|
{ |
|
|
|
CreateAt = t.CreateAt, |
|
|
|
CreateBy = t.CreateBy, |
|
|
|
Id = t.Id, |
|
|
|
DeviceId = t.DeviceId, |
|
|
|
DeviceName = t.DeviceName, |
|
|
|
Type = t.Type, |
|
|
|
Topic = t.Topic, |
|
|
|
DataResore = t.DataResore, |
|
|
|
t.Status |
|
|
|
}).OrderBy(x => x.CreateAt, OrderByType.Desc).ToPageListAsync(input.Current, input.PageSize, total); |
|
|
|
PageUtil util = new PageUtil() |
|
|
|
{ |
|
|
|
Total = total, |
|
|
@@ -67,14 +59,11 @@ namespace BPA.KitChen.StoreManagement.Application.Service.Device.Services |
|
|
|
}; |
|
|
|
return util; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 添加 |
|
|
|
/// </summary> |
|
|
|
/// <param name="input"></param> |
|
|
|
/// <returns></returns> |
|
|
|
[HttpPost("/api/devicepushrecode/add")] |
|
|
|
public async Task<bool> Add(DevicePushRecodeDtoInput input) |
|
|
|
{ |
|
|
|
var data = input.Adapt<BPA_DevicePushRecode>(); |
|
|
@@ -83,19 +72,16 @@ namespace BPA.KitChen.StoreManagement.Application.Service.Device.Services |
|
|
|
data.Topic= Topic; |
|
|
|
data.DataResore = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<dynamic>(input.Data.ToString())); |
|
|
|
var res=await Push(Topic, new PushData() { Data=input.Data, DeviceId= input.DeviceAutoKey }); |
|
|
|
if (res) |
|
|
|
{ |
|
|
|
await _db.Insertable(data).CallEntityMethod(t => t.Create()).ExecuteCommandAsync(); |
|
|
|
} |
|
|
|
data.Status = res ? CommonStatus.ENABLE : CommonStatus.DISABLE; |
|
|
|
await _db.Insertable(data).CallEntityMethod(t => t.Create()).ExecuteCommandAsync(); |
|
|
|
|
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 删除 |
|
|
|
/// </summary> |
|
|
|
/// <param name="input"></param> |
|
|
|
/// <returns></returns> |
|
|
|
[HttpPost("/api/devicepushrecode/delete")] |
|
|
|
public async Task<bool> Delete(List<string> input) |
|
|
|
{ |
|
|
|
try |
|
|
@@ -111,8 +97,7 @@ namespace BPA.KitChen.StoreManagement.Application.Service.Device.Services |
|
|
|
throw Oops.Oh("删除失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<bool> Push(string Topic, PushData data) |
|
|
|
private async Task<bool> Push(string Topic,PushData data) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
@@ -130,21 +115,8 @@ namespace BPA.KitChen.StoreManagement.Application.Service.Device.Services |
|
|
|
//每次下发暂停200毫秒 by 王刚 2022-06-08 测试提出修改 |
|
|
|
// Thread.Sleep(200); |
|
|
|
//string aa = bPAPackage.Serialize(false); |
|
|
|
var thisData = new |
|
|
|
{ |
|
|
|
MessageId = MessageID.TMC_PUSH_INGREDIENTS, |
|
|
|
ClientId = data.DeviceId, |
|
|
|
//ClientType = Procuct |
|
|
|
MessageVersion = 0x30, |
|
|
|
Timestamp = DateTime.Now, |
|
|
|
Message = data.Data.ToString() |
|
|
|
}; |
|
|
|
|
|
|
|
var applictionmessage = new MqttApplicationMessageBuilder() |
|
|
|
.WithTopic(Topic) |
|
|
|
.WithPayload(JsonConvert.SerializeObject(thisData)) |
|
|
|
.WithAtLeastOnceQoS().Build(); |
|
|
|
await _mqttClient.PublishAsync(applictionmessage); |
|
|
|
var applictionmessage = new MqttApplicationMessageBuilder().WithTopic(Topic).WithPayload(bPAPackage.Serialize(false)).WithAtLeastOnceQoS().Build(); |
|
|
|
await _mqttClient.PublishAsync(applictionmessage); |
|
|
|
return true; |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|