You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

180 lines
6.3 KiB

  1. using BPA.KitChen.GroupMeal.Application.BaseDto;
  2. using BPA.KitChen.GroupMeal.Application.Service.OneCard.Gate;
  3. using BPA.KitChen.GroupMeal.Core.Entity;
  4. using BPA.KitChen.GroupMeal.SqlSugar;
  5. using BPA.Message;
  6. using BPA.Message.API请求;
  7. using BPA.Message.IOT;
  8. using BPA.SAAS.KitChenManage.Application.Device.Dtos;
  9. using Furion.DependencyInjection;
  10. using Furion.DynamicApiController;
  11. using Furion.FriendlyException;
  12. using Furion.LinqBuilder;
  13. using Mapster;
  14. using Microsoft.AspNetCore.Mvc;
  15. using Microsoft.Extensions.Hosting;
  16. using MQTTnet;
  17. using MQTTnet.Client;
  18. using Newtonsoft.Json;
  19. using SqlSugar;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Net.Http.Json;
  24. using System.Text;
  25. using System.Threading.Tasks;
  26. namespace BPA.KitChen.GroupMeal.Application.Service.Device.Services
  27. {
  28. [ApiDescriptionSettings("设备下发", Tag = "设备下发", SplitCamelCase = false)]
  29. public class DevicePushRecodeService: IDynamicApiController, ITransient, IDevicePushRecodeService
  30. {
  31. private readonly SqlSugarScope _db;
  32. private readonly IMqttClient _mqttClient;
  33. public DevicePushRecodeService(ISqlSugarClient db, IMqttClient mqttClient)
  34. {
  35. _db = SqlSugarDb.Db;
  36. _mqttClient=mqttClient;
  37. }
  38. /// <summary>
  39. /// 分页
  40. /// </summary>
  41. /// <param name="input"></param>
  42. /// <returns></returns>
  43. [HttpPost("/api/devicepushrecode/page")]
  44. public async Task<PageUtil> Page(DevicePushRecodeDtoPageInput input)
  45. {
  46. RefAsync<int> total = 0;
  47. var res = await _db.Queryable<BPA_DevicePushRecode>()
  48. .WhereIF(!string.IsNullOrEmpty(input.DeviceName),x=>x.DeviceName.Contains(input.DeviceName))
  49. .Where(x=>x.Type== input.Type)
  50. .Select(t => new
  51. {
  52. CreateAt = t.CreateAt,
  53. CreateBy = t.CreateBy,
  54. Id = t.Id,
  55. DeviceId = t.DeviceId,
  56. DeviceName= t.DeviceName,
  57. Type =t.Type,
  58. Topic=t.Topic,
  59. DataResore=t.DataResore,
  60. }).OrderBy(x => x.CreateAt, OrderByType.Desc).ToPageListAsync(input.Current, input.PageSize, total);
  61. PageUtil util = new PageUtil()
  62. {
  63. Total = total,
  64. Data = res
  65. };
  66. return util;
  67. }
  68. /// <summary>
  69. /// 添加
  70. /// </summary>
  71. /// <param name="input"></param>
  72. /// <returns></returns>
  73. [HttpPost("/api/devicepushrecode/add")]
  74. public async Task<bool> Add(DevicePushRecodeDtoInput input)
  75. {
  76. var data = input.Adapt<BPA_DevicePushRecode>();
  77. string Topic = GetTopic(input.Type, input.DeviceAutoKey.ToString());
  78. if (string.IsNullOrEmpty(Topic)) throw Oops.Oh("请配置相关topic");
  79. data.Topic= Topic;
  80. data.DataResore = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<dynamic>(input.Data.ToString()));
  81. var res = await Push(Topic, new PushData() { Data = input.Data, DeviceId = input.DeviceAutoKey });
  82. if (res)
  83. {
  84. await _db.Insertable(data).CallEntityMethod(t => t.Create()).ExecuteCommandAsync();
  85. }
  86. return res;
  87. }
  88. /// <summary>
  89. /// 删除
  90. /// </summary>
  91. /// <param name="input"></param>
  92. /// <returns></returns>
  93. [HttpPost("/api/devicepushrecode/delete")]
  94. public async Task<bool> Delete(List<string> input)
  95. {
  96. try
  97. {
  98. // 查询数据库中是否存在未删除的活动信息
  99. var resEntitites = _db.Queryable<BPA_DevicePushRecode>().In(input).ToList();
  100. var res = await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  101. return res > 0;
  102. }
  103. catch (Exception)
  104. {
  105. throw Oops.Oh("删除失败");
  106. }
  107. }
  108. private async Task<bool> Push(string Topic, PushData data)
  109. {
  110. try
  111. {
  112. // Topic = TOPIC.GetInstance.GetBusinessTopic(x, storeInfo.FirstOrDefault(a => a.Id == item.OrgId).AutoKey) + "/" + item.AutoKey;
  113. BPAPackage bPAPackage = new BPAPackage
  114. {
  115. MessageId = MessageID.TMC_PUSH_INGREDIENTS,
  116. ClientId = data.DeviceId,
  117. //ClientType = Procuct
  118. MessageVersion = 0x30,
  119. Timestamp = DateTime.Now,
  120. Message = data
  121. };
  122. //每次下发暂停200毫秒 by 王刚 2022-06-08 测试提出修改
  123. // Thread.Sleep(200);
  124. string aa = bPAPackage.Serialize(false);
  125. var thisData = new
  126. {
  127. MessageId = MessageID.TMC_PUSH_INGREDIENTS,
  128. ClientId = data.DeviceId,
  129. //ClientType = Procuct
  130. MessageVersion = 0x30,
  131. Timestamp = DateTime.Now,
  132. Message = data.Data.ToString()
  133. };
  134. var applictionmessage = new MqttApplicationMessageBuilder()
  135. .WithTopic(Topic)
  136. .WithPayload(JsonConvert.SerializeObject(thisData))
  137. .WithAtLeastOnceQoS().Build();
  138. await _mqttClient.PublishAsync(applictionmessage);
  139. return true;
  140. }
  141. catch (Exception e)
  142. {
  143. throw Oops.Oh("下发错误,错误信息:"+e.Message);
  144. }
  145. }
  146. private string GetTopic(int type,string deviceKey)
  147. {
  148. string topic = "";
  149. switch (type)
  150. {
  151. case 1: //商品下发
  152. topic = $"/da4bfff042c656210/${deviceKey}/use/goodspush";
  153. break;
  154. case 2://物料下发
  155. topic = $"/da4bfff042c656210/${deviceKey}/use/batvhingpush";
  156. break;
  157. case 4:
  158. topic = $"/da4bfff042c656210/${deviceKey}/use/chnologypush";
  159. break;
  160. }
  161. return topic;
  162. }
  163. }
  164. }