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.
 
 

187 lines
7.7 KiB

  1. using BPA.KitChen.GroupMeal.Application.BaseDto;
  2. using BPA.KitChen.GroupMeal.Application.Service.Device.Dtos;
  3. using BPA.KitChen.GroupMeal.Core.Entity;
  4. using BPA.KitChen.GroupMeal.Core.Enum;
  5. using BPA.KitChen.GroupMeal.SqlSugar;
  6. using BPA.Message;
  7. using Furion.DependencyInjection;
  8. using Furion.FriendlyException;
  9. using Mapster;
  10. using MQTTnet;
  11. using MQTTnet.Client;
  12. using Newtonsoft.Json;
  13. using SqlSugar;
  14. namespace BPA.KitChen.GroupMeal.Application.Service.Device.Services
  15. {
  16. public class DevicePushRecodeService: IDevicePushRecodeService, ITransient
  17. {
  18. private readonly SqlSugarScope _db;
  19. private readonly IMqttClient _mqttClient;
  20. RequestParmsHeadlen requestParmsHeadlen = new();
  21. public DevicePushRecodeService( IMqttClient mqttClient)
  22. {
  23. _db = SqlSugarDb.Db;
  24. _mqttClient =mqttClient;
  25. }
  26. /// <summary>
  27. /// 分页
  28. /// </summary>
  29. /// <param name="input"></param>
  30. /// <returns></returns>
  31. public async Task<PageUtil> Page(DevicePushRecodeDtoPageInput input)
  32. {
  33. RefAsync<int> total = 0;
  34. var res = await _db.Queryable<BPA_DevicePushRecode>().Where(x=>x.Type== input.Type)
  35. .WhereIF(!string.IsNullOrWhiteSpace(input.DeviceName),x=>x.DeviceName.Contains(input.DeviceName))
  36. .WhereIF(!string.IsNullOrWhiteSpace(input.Status), x => x.Status == (CommonStatus)Convert.ToInt32(input.Status))
  37. .Select(t => new
  38. {
  39. CreateAt = t.CreateAt,
  40. CreateBy = t.CreateBy,
  41. Id = t.Id,
  42. DeviceId = t.DeviceId,
  43. DeviceName= t.DeviceName,
  44. Type =t.Type,
  45. Topic=t.Topic,
  46. DataResore=t.DataResore,
  47. Status=t.Status,
  48. Description=t.Description,
  49. }).OrderBy(x => x.CreateAt, OrderByType.Desc).ToPageListAsync(input.Current, input.PageSize, total);
  50. PageUtil util = new PageUtil()
  51. {
  52. Total = total,
  53. Data = res
  54. };
  55. return util;
  56. }
  57. /// <summary>
  58. /// 添加
  59. /// </summary>
  60. /// <param name="input"></param>
  61. /// <returns></returns>
  62. public async Task<bool> Add(DevicePushRecodeDtoInput input)
  63. {
  64. var DataResore =await requestParmsHeadlen.GetParm(input.Type, input.Data.ToString(), input.DeviceId);
  65. var data = input.Adapt<BPA_DevicePushRecode>();
  66. string topstr = "";
  67. string Topic = GetTopic(input.Type, input.DeviceAutoKey.ToString(), input.ProductVersion, input.ProductKey, out topstr);
  68. if (string.IsNullOrEmpty(Topic)) throw Oops.Oh("请配置相关topic");
  69. data.Topic= Topic;
  70. data.Status = CommonStatus.ENABLE;
  71. data.DataResore = JsonConvert.SerializeObject(DataResore);
  72. data.DeviceAutoKey= input.DeviceAutoKey;
  73. data.Description = "成功";
  74. if (input.TopicsData.Count > 0)
  75. {
  76. var chaeck = input.TopicsData.Any(x => x.Topics.Trim() == topstr.Trim());
  77. if (!chaeck)
  78. {
  79. data.Status = CommonStatus.DISABLE;
  80. data.Description = "下发数据失败,该产品还未配置对应的topic,请联系管理员进行配置";
  81. }
  82. }
  83. else
  84. {
  85. data.Status = CommonStatus.DISABLE;
  86. data.Description = "下发数据失败,该产品还未配置对应的topic,请联系管理员进行配置";
  87. }
  88. if (data.Status == CommonStatus.ENABLE)
  89. {
  90. var respush = await Push(Topic, new PushData() { Data = data.DataResore, DeviceId = input.DeviceAutoKey });
  91. if (respush != "success")
  92. {
  93. data.Description = respush;
  94. data.Status = CommonStatus.ENABLE;
  95. }
  96. }
  97. // return true;
  98. var res = await _db.Insertable(data).CallEntityMethod(t => t.Create()).ExecuteCommandAsync();
  99. return res>0;
  100. }
  101. public async Task<bool> Update(string id)
  102. {
  103. var data = _db.Queryable<BPA_DevicePushRecode>().Where(x => x.Id == id).First();
  104. data.Status = CommonStatus.ENABLE;
  105. var respush = await Push(data.Topic, new PushData() { Data = data.DataResore, DeviceId = data.DeviceAutoKey });
  106. if (respush!= "success")
  107. {
  108. data.Description = respush;
  109. data.Status = CommonStatus.DISABLE;
  110. }
  111. var res = await _db.Updateable(data).ExecuteCommandAsync();
  112. return res > 0;
  113. }
  114. /// <summary>
  115. /// 删除
  116. /// </summary>
  117. /// <param name="input"></param>
  118. /// <returns></returns>
  119. public async Task<bool> Delete(List<string> input)
  120. {
  121. try
  122. {
  123. // 查询数据库中是否存在未删除的活动信息
  124. var resEntitites = _db.Queryable<BPA_DevicePushRecode>().In(input).ToList();
  125. var res = await _db.Deleteable(resEntitites).ExecuteCommandAsync();
  126. return res > 0;
  127. }
  128. catch (Exception)
  129. {
  130. throw Oops.Oh("删除失败");
  131. }
  132. }
  133. private async Task<string> Push(string Topic,PushData data)
  134. {
  135. try
  136. {
  137. // Topic = TOPIC.GetInstance.GetBusinessTopic(x, storeInfo.FirstOrDefault(a => a.Id == item.OrgId).AutoKey) + "/" + item.AutoKey;
  138. BPAPackage bPAPackage = new BPAPackage
  139. {
  140. MessageId = MessageID.TMC_PUSH_INGREDIENTS,
  141. ClientId = data.DeviceId,
  142. //ClientType = Procuct
  143. MessageVersion = 0x30,
  144. Timestamp = DateTime.Now,
  145. Message = data
  146. };
  147. //每次下发暂停200毫秒 by 王刚 2022-06-08 测试提出修改
  148. // Thread.Sleep(200);
  149. //string aa = bPAPackage.Serialize(false);
  150. var applictionmessage = new MqttApplicationMessageBuilder().WithTopic(Topic).WithPayload(bPAPackage.Serialize(false)).WithAtLeastOnceQoS().Build();
  151. await _mqttClient.PublishAsync(applictionmessage);
  152. return "success";
  153. }
  154. catch (Exception e)
  155. {
  156. return "下发错误,错误信息:" + e.Message;
  157. }
  158. }
  159. private string GetTopic(int type,string deviceKey, string productVersion, string productKey,out string topstr )
  160. {
  161. string topic = "";
  162. topstr = "";
  163. switch (type)
  164. {
  165. case 1: //商品下发
  166. topic = $"/{productKey}/{productVersion}/{deviceKey}/defaul/goodspush";
  167. topstr = "/"+productKey+"/"+productVersion+"/${deviceKey}/defaul/goodspush";
  168. break;
  169. case 2://物料下发
  170. topic = $"/{productKey}/{productVersion}/{deviceKey}/defaul/batvhingpush";
  171. topstr = "/" + productKey + "/" + productVersion + "/${deviceKey}/defaul/batvhingpush";
  172. break;
  173. case 4:
  174. topic = $"/{productKey}/{productVersion}/{deviceKey}/defaul/chnologypush";
  175. topstr = "/" + productKey + "/" + productVersion + "/${deviceKey}/defaul/chnologypush";
  176. break;
  177. }
  178. return topic;
  179. }
  180. }
  181. }