平安校园
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CameraInfoService.cs 8.5 KiB

4ヶ月前
2ヶ月前
4ヶ月前
1ヶ月前
4ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
4ヶ月前
2ヶ月前
4ヶ月前
2ヶ月前
4ヶ月前
2ヶ月前
4ヶ月前
1ヶ月前
3ヶ月前
4ヶ月前
1ヶ月前
4ヶ月前
1ヶ月前
4ヶ月前
1ヶ月前
4ヶ月前
3ヶ月前
4ヶ月前
1ヶ月前
3ヶ月前
4ヶ月前
1ヶ月前
4ヶ月前
1ヶ月前
4ヶ月前
1ヶ月前
4ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using MoYu.RemoteRequest.Extensions;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using SafeCampus.Application.Manager.DeepelephManager;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. namespace SafeCampus.Application.Services.Business.CameraInfoService;
  9. public class CameraInfoService : DbRepository<CameraInfo>, ICameraInfoService
  10. {
  11. private readonly IDeepelephManager _deepelephManager;
  12. private readonly ISimpleCacheService _simpleCacheService;
  13. public CameraInfoService(IDeepelephManager deepelephManager, ISimpleCacheService simpleCacheService)
  14. {
  15. _deepelephManager = deepelephManager;
  16. _simpleCacheService = simpleCacheService;
  17. }
  18. public async Task<bool> DataSync()
  19. {
  20. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  21. var list = await Context.Queryable<CameraInfo>().ToListAsync();
  22. var str = await $"{appSettings.SXAPIURL}/device/console/v1/sensor/page_query/brief"
  23. .SetBody(new
  24. {
  25. token = _deepelephManager.GetToken(),
  26. tenantCode = appSettings.TenantCode,
  27. poiId = appSettings.PoiId,
  28. pageNo = 1,
  29. pageSize = 1000,
  30. })
  31. .SetContentType("application/json")
  32. .PostAsAsync<string>();
  33. var model = JsonConvert.DeserializeObject<JObject>(str);
  34. if (!(bool)model["success"]) throw Oops.Oh(model["message"].ToString());
  35. if (model["data"] != null)
  36. {
  37. foreach (var item in model["data"]["items"])
  38. {
  39. var old_model = list.FirstOrDefault(x => x.SensorId == item["sensorId"].ToString());
  40. if (old_model != null)
  41. {
  42. old_model.DeviceStatus = item["deviceStatus"].ToString() == "online";
  43. old_model.DirectUrlIp = item["directUrlIp"].ToString();
  44. old_model.FieldId = item["fieldId"].ToString();
  45. old_model.FieldName = item["fieldName"].ToString();
  46. old_model.LastTime = DateTime.Now;
  47. old_model.ResHeight = (int)item["resHeight"];
  48. old_model.ResWidth = (int)item["resWidth"];
  49. old_model.SensorName = item["sensorName"].ToString();
  50. old_model.SnapshotUrl = item["snapshotUrl"].ToString();
  51. var signImg = Path.Combine(Directory.GetCurrentDirectory(), "Files", App.Configuration["AppInfo:CameraImg"], old_model.SensorId + ".jpg");
  52. var steam = await old_model.SnapshotUrl.GetAsByteArrayAsync();
  53. using (MemoryStream ms = new MemoryStream(steam))
  54. {
  55. using (Bitmap bmp = new Bitmap(ms))
  56. {
  57. using (Graphics g = Graphics.FromImage(bmp))
  58. {
  59. }
  60. bmp.Save(signImg, ImageFormat.Jpeg);
  61. }
  62. }
  63. old_model.SnapshotUrl = $"/Files/{App.Configuration["AppInfo:CameraImg"]}/{old_model.SensorId}.jpg";
  64. await UpdateAsync(old_model);
  65. }
  66. else
  67. {
  68. var cameraModel = new CameraInfo
  69. {
  70. DeviceStatus = item["deviceStatus"].ToString() == "online",
  71. DirectUrlIp = item["directUrlIp"].ToString(),
  72. FieldId = item["fieldId"].ToString(),
  73. FieldName = item["fieldName"].ToString(),
  74. LastTime = DateTime.Now,
  75. ResHeight = (int)item["resHeight"],
  76. ResWidth = (int)item["resWidth"],
  77. SensorId = item["sensorId"].ToString(),
  78. SensorName = item["sensorName"].ToString(),
  79. SnapshotUrl = item["snapshotUrl"].ToString(),
  80. };
  81. var signImg = Path.Combine(Directory.GetCurrentDirectory(), "Files", App.Configuration["AppInfo:CameraImg"], cameraModel.SensorId + ".jpg");
  82. var steam = await cameraModel.SnapshotUrl.GetAsByteArrayAsync();
  83. using (MemoryStream ms = new MemoryStream(steam))
  84. {
  85. using (Bitmap bmp = new Bitmap(ms))
  86. {
  87. using (Graphics g = Graphics.FromImage(bmp))
  88. {
  89. }
  90. bmp.Save(signImg, ImageFormat.Jpeg);
  91. }
  92. }
  93. cameraModel.SnapshotUrl = $"/Files/{App.Configuration["AppInfo:CameraImg"]}/{cameraModel.SensorId}.jpg";
  94. await InsertAsync(cameraModel);
  95. }
  96. }
  97. return true;
  98. }
  99. return false;
  100. }
  101. private List<long> GetGroupChildren(List<CameraGroup> node)
  102. {
  103. var list = new List<long>();
  104. if (node == null) return list;
  105. foreach (var cameraGroup in node)
  106. {
  107. list.Add(cameraGroup.Id);
  108. list.AddRange(GetGroupChildren(cameraGroup.Children));
  109. }
  110. return list;
  111. }
  112. public async Task<SqlSugarPagedList<CameraInfoDto>> GetPageList(CameraSearch search)
  113. {
  114. var groupids = new List<long>();
  115. if (search.GroupId.HasValue && search.GroupId.Value != -1)
  116. {
  117. var group = await Context.Queryable<CameraGroup>()
  118. .ToTreeAsync(x => x.Children, x => x.ParentId, search.GroupId);
  119. groupids = GetGroupChildren(group);
  120. groupids.Add(search.GroupId.Value);
  121. }
  122. var query = Context.Queryable<CameraInfo>()
  123. .Includes(x => x.SysUserItem)
  124. .Includes(x => x.CameraGroupItem)
  125. .WhereIF(search.DeviceStatus.HasValue, x => x.DeviceStatus == search.DeviceStatus)
  126. .WhereIF(!string.IsNullOrEmpty(search.SensorId), x => x.SensorId == search.SensorId)
  127. .WhereIF(!string.IsNullOrEmpty(search.SensorName), x => x.SensorName == search.SensorName)
  128. .WhereIF(search.GroupId.HasValue, x => (search.GroupId.Value == -1 ? x.GroupId == null : groupids.Contains(x.GroupId.Value)));
  129. var list = await query.OrderBy(x => x.LastTime)
  130. .ToPagedListAsyncMapster<CameraInfo, CameraInfoDto>(search.PageNum, search.PageSize);
  131. return list;
  132. }
  133. public async Task<bool> BatchSetGroup(SetGroupInput input)
  134. {
  135. if (input.Id == -1)
  136. {
  137. var result = await Context.Updateable<CameraInfo>()
  138. .SetColumns(x => x.GroupId == null)
  139. .Where(x => input.Ids.Contains(x.Id))
  140. .ExecuteCommandAsync();
  141. return result == input.Ids.Count;
  142. }
  143. else
  144. {
  145. var result = await Context.Updateable<CameraInfo>()
  146. .SetColumns(x => x.GroupId == input.Id)
  147. .Where(x => input.Ids.Contains(x.Id))
  148. .ExecuteCommandAsync();
  149. return result == input.Ids.Count;
  150. }
  151. }
  152. public async Task<bool> BatchSetPushPersonByGroup(SetPushPersonGroupInput input)
  153. {
  154. var result = await Context.Updateable<CameraInfo>()
  155. .SetColumns(x => x.PushUserId == input.UserId)
  156. .Where(x => x.GroupId == input.GroupId)
  157. .ExecuteCommandAsync();
  158. await Context.Updateable<CameraGroup>().SetColumns(x => x.PushUserId == input.UserId)
  159. .Where(x => x.Id == input.GroupId).ExecuteCommandAsync();
  160. return result > 0;
  161. }
  162. public async Task<bool> BatchSetPushPerson(SetPushPersonInput input)
  163. {
  164. var result = await Context.Updateable<CameraInfo>()
  165. .SetColumns(x => x.PushUserId == input.UserId)
  166. .Where(x => input.Ids.Contains(x.Id))
  167. .ExecuteCommandAsync();
  168. return result == input.Ids.Count;
  169. }
  170. public async Task<bool> BatchSetPushPersonByWarn(SetPushPersonWarnInput input)
  171. {
  172. var warn = _simpleCacheService.Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup);
  173. var warnGroupInfo = warn.FirstOrDefault(x => x.Code == input.WarnCode);
  174. if (warnGroupInfo != null)
  175. {
  176. var ids = warnGroupInfo.CameraId;
  177. var result = await Context.Updateable<CameraInfo>()
  178. .SetColumns(x => x.PushUserId == input.UserId)
  179. .Where(x => ids.Contains(x.SensorId))
  180. .ExecuteCommandAsync();
  181. return result > 0;
  182. }
  183. throw Oops.Oh("分组不存在");
  184. }
  185. }