DeviceService.RemoteMaintainPatch.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using WingInterfaceLibrary.DTO.LiveRoom;
  5. using WingInterfaceLibrary.Enum;
  6. using WingInterfaceLibrary.Interface;
  7. using WingInterfaceLibrary.Internal.Request;
  8. using WingInterfaceLibrary.Notifications;
  9. using WingInterfaceLibrary.Notifications.Live;
  10. using WingInterfaceLibrary.Notifications.Remote;
  11. using WingInterfaceLibrary.Request;
  12. using WingInterfaceLibrary.Request.Remote;
  13. using WingServerCommon.Interfaces.Cache;
  14. using WingServerCommon.Service;
  15. using WingInterfaceLibrary.Request.Device;
  16. using WingInterfaceLibrary.Result;
  17. using WingInterfaceLibrary.DTO.Device;
  18. using WingServerCommon.Mapper;
  19. using System.Collections.Concurrent;
  20. using System;
  21. using System.Linq;
  22. using WingServerCommon.Log;
  23. namespace WingDeviceService.Service
  24. {
  25. /// <summary>
  26. /// 设备更新包和打印机驱动管理
  27. /// </summary>
  28. public partial class DeviceService : JsonRpcService, IDeviceService
  29. {
  30. /// <summary>
  31. ///缓存设备打印机驱动列表信息 fixed 0012169
  32. /// </summary>
  33. /// <typeparam name="string"></typeparam>
  34. /// <typeparam name="CacheControllingParameter"></typeparam>
  35. /// <returns></returns>
  36. private ConcurrentDictionary<string, IList<DevicePrinterParameterDTO>> _cacheDevicePrinterDict = new ConcurrentDictionary<string, IList<DevicePrinterParameterDTO>>();
  37. /// <summary>
  38. /// 增加更新包
  39. /// </summary>
  40. /// <param name="request"></param>
  41. /// <returns></returns>
  42. public async Task<bool> AddDevicePatchAsync(AddDevicePatchRequest request)
  43. {
  44. var fileUploadInfoList = request.FileUploadInfoList;
  45. var userCode = await _authenticationService.GetTokenAsync(request);
  46. //更新包名称不为空
  47. if (string.IsNullOrWhiteSpace(request.Name))
  48. {
  49. ThrowCustomerException(CustomerRpcCode.NameEmpty, "Required parameter:DeviceName cannot be empty");
  50. }
  51. if (fileUploadInfoList == null && fileUploadInfoList.Count <= 0)
  52. {
  53. ThrowCustomerException(CustomerRpcCode.FileTokenIsEmpty, "fileUploadInfoList cannot be empty");
  54. }
  55. //更新包名称不为空
  56. if (string.IsNullOrWhiteSpace(request.DeviceType))
  57. {
  58. ThrowCustomerException(CustomerRpcCode.DeviceTypeRequired, "Device type cannot be empty");
  59. }
  60. var patch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(
  61. new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
  62. {
  63. Name = request.Name
  64. });
  65. if (patch != null)
  66. {
  67. ThrowCustomerException(CustomerRpcCode.PatchNameExistError, "Patch name is exist");
  68. }
  69. return await _deviceInfoDBServiceProxy.AddDevicePatchDBAsync(
  70. new WingInterfaceLibrary.Request.DBRequest.AddDevicePatchDBRequest()
  71. {
  72. Name = request.Name,
  73. Description = request.Description,
  74. DeviceType = request.DeviceType,
  75. SoftwareVersion = request.SoftwareVersion,
  76. OsVersion = request.OsVersion,
  77. DeviceFileInfoList = request.FileUploadInfoList,
  78. FileSize = request.FileSize,
  79. FileName = request.FileName
  80. });
  81. }
  82. /// <summary>
  83. /// 查询更新包
  84. /// </summary>
  85. /// <param name="request"></param>
  86. /// <returns></returns>
  87. public async Task<PageResult<DevicePatchDTO>> FindDevicePatchPagesAsync(FindDevicePatchPageRequest request)
  88. {
  89. var userCode = await _authenticationService.GetTokenAsync(request);
  90. var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchPageDBAsync(
  91. new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchPageDBRequest()
  92. {
  93. Keyword = request.Keyword,
  94. PageIndex = request.PageIndex,
  95. PageSize = request.PageSize,
  96. });
  97. return patchList;
  98. }
  99. /// <summary>
  100. /// 删除更新包
  101. /// </summary>
  102. /// <param name="request"></param>
  103. /// <returns></returns>
  104. public async Task<bool> DeletePatchByCodeAsync(DeleteDevicePatchByCodeRequest request)
  105. {
  106. var userCode = await _authenticationService.GetTokenAsync(request);
  107. //更新包名称不为空
  108. if (string.IsNullOrWhiteSpace(request.Code))
  109. {
  110. ThrowCustomerException(CustomerRpcCode.PatchCodeIsEmpty, "Patch code cannot be empty");
  111. }
  112. return await _deviceInfoDBServiceProxy.DeleteDevicePatchDBbyCodeAsync(
  113. new WingInterfaceLibrary.Request.DBRequest.DeleteDevicePatchDBbyCodeRequest()
  114. {
  115. Code = request.Code
  116. });
  117. }
  118. /// <summary>
  119. /// 修改更新包
  120. /// </summary>
  121. /// <param name="request"></param>
  122. /// <returns></returns>
  123. public async Task<bool> UpdateDevicePatchAsync(UpdateDevicePatchRequest request)
  124. {
  125. var userCode = await _authenticationService.GetTokenAsync(request);
  126. //更新包名称不为空
  127. if (string.IsNullOrWhiteSpace(request.Name))
  128. {
  129. ThrowCustomerException(CustomerRpcCode.NameEmpty, "Name cannot be empty");
  130. }
  131. //更新包编码不为空
  132. if (string.IsNullOrWhiteSpace(request.Code))
  133. {
  134. ThrowCustomerException(CustomerRpcCode.PatchCodeIsEmpty, "Patch code is empty");
  135. }
  136. var myPatch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(
  137. new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
  138. {
  139. Code = request.Code
  140. });
  141. if (myPatch == null)
  142. {
  143. ThrowCustomerException(CustomerRpcCode.PatchNoFoundError, "Patch no found");
  144. }
  145. var otherPatch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(
  146. new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
  147. {
  148. Name = request.Name
  149. });
  150. if (otherPatch != null)
  151. {
  152. if (myPatch.Code != otherPatch.Code)
  153. {
  154. ThrowCustomerException(CustomerRpcCode.PatchNameExistError, "Patch name is exist");
  155. }
  156. }
  157. return await _deviceInfoDBServiceProxy.UpdateDevicePatchDBAsync(
  158. new WingInterfaceLibrary.Request.DBRequest.UpdateDevicePatchDBRequest()
  159. {
  160. Code = request.Code,
  161. Name = request.Name,
  162. Description = request.Description,
  163. SoftwareVersion = request.SoftwareVersion,
  164. OsVersion = request.OsVersion
  165. });
  166. }
  167. /// <summary>
  168. ///推送设备补丁包
  169. /// </summary>
  170. /// <param name="request"></param>
  171. /// <returns></returns>
  172. public async Task<bool> PushDevicePatchAsync(PushDevicePatchRequest request)
  173. {
  174. var token = await _authenticationService.GetTokenAsync(request);
  175. var deviceCode = request.DeviceCode;
  176. if (string.IsNullOrWhiteSpace(deviceCode))
  177. {
  178. ThrowCustomerException(CustomerRpcCode.DeviceCodeRequired, "Device code cannot be empty");
  179. }
  180. DevicePatchDTO patch = null;
  181. if (request.PushEnum == PushDevicePatchEnum.Start)
  182. {
  183. if (string.IsNullOrWhiteSpace(request.PatchCode))
  184. {
  185. ThrowCustomerException(CustomerRpcCode.PatchCodeIsEmpty, "Patch code cannot be empty");
  186. }
  187. patch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
  188. {
  189. Code = request.PatchCode
  190. });
  191. if (patch == null)
  192. {
  193. ThrowCustomerException(CustomerRpcCode.PatchNoFoundError, "Patch no found");
  194. }
  195. }
  196. var notification = new PushDevicePatchToDeviceNotification()//设备通知
  197. {
  198. PatchName = patch?.Name ?? string.Empty,
  199. PatchCode = patch?.Code ?? string.Empty,
  200. UserCode = token.ClientId,
  201. DeviceCode = deviceCode,
  202. };
  203. if (request.PushEnum == PushDevicePatchEnum.Start)
  204. {
  205. notification.DeviceFileInfoList = patch?.DeviceFileInfoList?.ToList() ?? new List<UploadDeviceFileInfoDTO>();
  206. notification.FileSize = patch?.FileSize ?? 0;
  207. notification.FileName = patch?.FileName ?? string.Empty;
  208. notification.DeviceType = patch?.DeviceType ?? string.Empty;
  209. }
  210. notification.PushEnum = request.PushEnum;
  211. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  212. notificationRequest.ClientIds = new List<string>() { deviceCode };
  213. notificationRequest.Message = notification;
  214. notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
  215. notificationRequest.NotificationType = notification.NotificationType;
  216. notificationRequest.TransactionType = TransactionTypeEnum.AfterSales;
  217. notificationRequest.RelevanceCode = deviceCode;
  218. notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
  219. var deviceTokenInfo = CacheMaintenance.Instance.Get<ITokensManager>().Where(x => x.ClientId == deviceCode)?.FirstOrDefault();
  220. if (deviceTokenInfo != null && deviceTokenInfo.IsOldPlatform && deviceTokenInfo.IsOnline)
  221. {
  222. await _deviceForwardService.PushDevicePatchAsync(notification);
  223. }
  224. var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
  225. return res;
  226. }
  227. /// <summary>
  228. ///根据设备编码分页查询设备
  229. /// </summary>
  230. /// <param name="request"></param>
  231. /// <returns></returns>
  232. public async Task<PageResult<DevicePatchDTO>> FindPushDevicePatchPagesAsync(FindPushDevicePatchPageRequest request)
  233. {
  234. var token = await _authenticationService.GetTokenAsync(request);
  235. //更新包名称不为空
  236. if (string.IsNullOrWhiteSpace(request.DeviceCode))
  237. {
  238. ThrowCustomerException(CustomerRpcCode.DeviceCodeRequired, "Device code cannot be empty");
  239. }
  240. var softwareVersion = string.Empty;
  241. var osVersion = string.Empty;
  242. var device = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
  243. if (device == null)
  244. {
  245. ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Device no found");
  246. }
  247. softwareVersion = device.DeviceSoftwareVersion;
  248. osVersion = device.SystemVersion;
  249. var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchPageDBAsync(
  250. new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchPageDBRequest()
  251. {
  252. Code = request.DeviceCode,
  253. Keyword = request.Keyword,
  254. PageIndex = request.PageIndex,
  255. PageSize = request.PageSize,
  256. SoftwareVersion = softwareVersion,
  257. OsVersion = osVersion,
  258. FindOnlyDeviceTypes = request.FindOnlyDeviceTypes
  259. });
  260. return patchList;
  261. }
  262. /// <summary>
  263. ///上传补丁包下载进度
  264. /// </summary>
  265. /// <param name="request"></param>
  266. /// <docFirstDirectory>设备端API</docFirstDirectory>
  267. /// <docSecondDirectory>设备信息接口</docSecondDirectory>
  268. /// <returns></returns>
  269. public async Task<bool> UploadDeviceDownloadPatchProgressToUserAsync(UploadDeviceDownloadPatchProgressToUserRequest request)
  270. {
  271. string deviceCode = await GetClientIdByTokenAsync(request.Token);
  272. if (string.IsNullOrEmpty(deviceCode))
  273. {
  274. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  275. }
  276. Logger.WriteLineInfo($"DeviceService UploadDeviceDownloadPatchProgressToUserAsync deviceCode: {deviceCode} PatchCode:{request.PatchCode} State:{request.RemoteDeviceState} ");
  277. var patchName = Guid.NewGuid().ToString("N");
  278. if (!string.IsNullOrWhiteSpace(request.PatchCode))
  279. {
  280. var patch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
  281. {
  282. Code = request.PatchCode
  283. });
  284. if (patch == null)
  285. {
  286. ThrowCustomerException(CustomerRpcCode.PatchNoFoundError, "Patch no found");
  287. }
  288. patchName = patch.Name;
  289. }
  290. var notification = new DeviceDownloadPatchProgressToUserNotification()//客户端通知
  291. {
  292. Progress = request.Progress,
  293. PatchCode = request.PatchCode,
  294. DeviceCode = deviceCode,
  295. RemoteDeviceState = request.RemoteDeviceState,
  296. PatchName = patchName,
  297. };
  298. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  299. notificationRequest.ClientIds = new List<string>() { request.UserCode };
  300. notificationRequest.Message = notification;
  301. notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
  302. notificationRequest.NotificationType = notification.NotificationType;
  303. notificationRequest.TransactionType = TransactionTypeEnum.AfterSales;
  304. notificationRequest.RelevanceCode = deviceCode;
  305. notificationRequest.ReceiverType = ApplicantTypeEnum.Client;
  306. var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
  307. return res;
  308. }
  309. /// <summary>
  310. /// 增加设备更新包版本号
  311. /// </summary>
  312. /// <param name="request"></param>
  313. /// <returns></returns>
  314. public async Task<bool> AddDevicePatchVersionAsync(AddDevicePatchVersionRequest request)
  315. {
  316. var token = await _authenticationService.GetTokenAsync(request);
  317. //更新包名称不为空
  318. if (string.IsNullOrWhiteSpace(request.Version))
  319. {
  320. ThrowCustomerException(CustomerRpcCode.VersionInvalid, "Version invalid");
  321. }
  322. if (string.IsNullOrWhiteSpace(request.DeviceType))
  323. {
  324. ThrowCustomerException(CustomerRpcCode.DeviceTypeEmpty, "Device type is empty");
  325. }
  326. var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchVersionListDBAsync(
  327. new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchVersionListDBRequest()
  328. {
  329. VersionTypeEnum = request.VersionTypeEnum,
  330. DeviceType = request.DeviceType
  331. });
  332. if (patchList != null && patchList.FirstOrDefault(v => v.Version == request.Version) != null)
  333. {
  334. //已存在
  335. ThrowCustomerException(CustomerRpcCode.VersionIsExistError, "Version is exist");
  336. }
  337. return await _deviceInfoDBServiceProxy.AddDevicePatchVersionDBAsync(
  338. new WingInterfaceLibrary.Request.DBRequest.AddDevicePatchVersionDBRequest()
  339. {
  340. Version = request.Version,
  341. VersionTypeEnum = request.VersionTypeEnum,
  342. DeviceType = request.DeviceType
  343. });
  344. }
  345. /// <summary>
  346. /// 查询设备更新包版本号
  347. /// </summary>
  348. /// <param name="request"></param>
  349. /// <returns></returns>
  350. public async Task<List<DevicePatchVersionDTO>> FindDevicePatchVersionListAsync(FindDevicePatchListRequest request)
  351. {
  352. var token = await _authenticationService.GetTokenAsync(request);
  353. if (string.IsNullOrWhiteSpace(request.DeviceType))
  354. {
  355. ThrowCustomerException(CustomerRpcCode.DeviceTypeEmpty, "Device type is empty");
  356. }
  357. return await _deviceInfoDBServiceProxy.FindDevicePatchVersionListDBAsync(
  358. new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchVersionListDBRequest()
  359. {
  360. VersionTypeEnum = request.VersionTypeEnum,
  361. DeviceType = request.DeviceType
  362. });
  363. }
  364. /// <summary>
  365. /// 删除设备更新包版本号
  366. /// </summary>
  367. /// <param name="request"></param>
  368. /// <returns></returns>
  369. public async Task<bool> DeleteDevicePatchVersionAsync(DeleteDevicePatchVersionRequest request)
  370. {
  371. var token = await _authenticationService.GetTokenAsync(request);
  372. //更新包编码不为空
  373. if (string.IsNullOrWhiteSpace(request.Code))
  374. {
  375. ThrowCustomerException(CustomerRpcCode.CodeIsEmpty, "Code is empty");
  376. }
  377. return await _deviceInfoDBServiceProxy.DeleteDevicePatchVersionDBAsync(
  378. new WingInterfaceLibrary.Request.DBRequest.DeleteDevicePatchVersionDBRequest()
  379. {
  380. Code = request.Code
  381. });
  382. }
  383. /// <summary>
  384. /// 增加打印机系统版本号
  385. /// </summary>
  386. /// <param name="request"></param>
  387. /// <returns></returns>
  388. public async Task<bool> AddDevicePrinterVersionAsync(AddDevicePrinterVersionRequest request)
  389. {
  390. var token = await _authenticationService.GetTokenAsync(request);
  391. var deviceType = "Printer";
  392. //更新包名称不为空
  393. if (string.IsNullOrWhiteSpace(request.Version))
  394. {
  395. ThrowCustomerException(CustomerRpcCode.VersionInvalid, "Version invalid");
  396. }
  397. var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchVersionListDBAsync(
  398. new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchVersionListDBRequest()
  399. {
  400. VersionTypeEnum = request.VersionTypeEnum,
  401. DeviceType = deviceType
  402. });
  403. if (patchList != null && patchList.FirstOrDefault(v => v.Version == request.Version) != null)
  404. {
  405. //已存在
  406. ThrowCustomerException(CustomerRpcCode.VersionIsExistError, "Version is exist");
  407. }
  408. return await _deviceInfoDBServiceProxy.AddDevicePatchVersionDBAsync(
  409. new WingInterfaceLibrary.Request.DBRequest.AddDevicePatchVersionDBRequest()
  410. {
  411. Version = request.Version,
  412. VersionTypeEnum = request.VersionTypeEnum,
  413. DeviceType = deviceType
  414. });
  415. }
  416. /// <summary>
  417. /// 查询打印机系统版本号
  418. /// </summary>
  419. /// <param name="request"></param>
  420. /// <returns></returns>
  421. public async Task<List<DevicePrinterVersionDTO>> FindDevicePrinterVersionListAsync(FindDevicePrinterVersionListRequest request)
  422. {
  423. var token = await _authenticationService.GetTokenAsync(request);
  424. var deviceType = "Printer";
  425. var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchVersionListDBAsync(
  426. new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchVersionListDBRequest()
  427. {
  428. VersionTypeEnum = request.VersionTypeEnum,
  429. DeviceType = deviceType
  430. });
  431. var result = new List<DevicePrinterVersionDTO>();
  432. foreach (var item in patchList)
  433. {
  434. result.Add(item.MappingTo<DevicePrinterVersionDTO>());
  435. }
  436. return result;
  437. }
  438. /// <summary>
  439. /// 删除打印机系统版本号
  440. /// </summary>
  441. /// <param name="request"></param>
  442. /// <returns></returns>
  443. public async Task<bool> DeleteDevicePrinterVersionAsync(DeleteDevicePrinterVersionRequest request)
  444. {
  445. var token = await _authenticationService.GetTokenAsync(request);
  446. //更新包编码不为空
  447. if (string.IsNullOrWhiteSpace(request.Code))
  448. {
  449. ThrowCustomerException(CustomerRpcCode.CodeIsEmpty, "Code is empty");
  450. }
  451. return await _deviceInfoDBServiceProxy.DeleteDevicePatchVersionDBAsync(
  452. new WingInterfaceLibrary.Request.DBRequest.DeleteDevicePatchVersionDBRequest()
  453. {
  454. Code = request.Code
  455. });
  456. }
  457. /// <summary>
  458. /// 增加打印机更新包
  459. /// </summary>
  460. /// <param name="request"></param>
  461. /// <returns></returns>
  462. public async Task<bool> AddDevicePrinterAsync(AddDevicePrinterRequest request)
  463. {
  464. var token = await _authenticationService.GetTokenAsync(request);
  465. //更新包名称不为空
  466. if (string.IsNullOrWhiteSpace(request.Name))
  467. {
  468. ThrowCustomerException(CustomerRpcCode.NameEmpty, "Name is empty");
  469. }
  470. if (request.FileUploadInfoList == null && request.FileUploadInfoList.Count <= 0)
  471. {
  472. ThrowCustomerException(CustomerRpcCode.FileTokenIsEmpty, "fileUploadInfoList cannot be empty");
  473. }
  474. return await _deviceInfoDBServiceProxy.AddDevicePrinterDBAsync(
  475. new WingInterfaceLibrary.Request.DBRequest.AddDevicePrinterDBRequest()
  476. {
  477. Name = request.Name,
  478. Description = request.Description,
  479. OsVersion = request.OsVersion,
  480. FileUploadInfoList = request.FileUploadInfoList,
  481. FileSize = request.FileSize,
  482. PrinterBrands = request.PrinterBrands,
  483. PrinterModels = request.PrinterModels,
  484. });
  485. }
  486. /// <summary>
  487. /// 查询打印机更新包
  488. /// </summary>
  489. /// <param name="request"></param>
  490. /// <returns></returns>
  491. public async Task<PageResult<DevicePrinterDTO>> FindDevicePrinterPagesAsync(FindDevicePrinterPageRequest request)
  492. {
  493. var token = await _authenticationService.GetTokenAsync(request);
  494. var printerList = await _deviceInfoDBServiceProxy.FindDevicePrinterPageDBAsync(
  495. new WingInterfaceLibrary.Request.DBRequest.FindDevicePrinterPageDBRequest()
  496. {
  497. Keyword = request.Keyword,
  498. PageIndex = request.PageIndex,
  499. PageSize = request.PageSize,
  500. });
  501. return printerList;
  502. }
  503. /// <summary>
  504. /// 查询打印机驱动是否存在
  505. /// </summary>
  506. /// <param name="request"></param>
  507. /// <returns></returns>
  508. public async Task<bool> ExistsDevicePrinterAsync(ExistsDevicePrinterRequest request)
  509. {
  510. return await _deviceInfoDBServiceProxy.ExistsDevicePrinterDBAsync(
  511. new WingInterfaceLibrary.Request.DBRequest.ExistsDevicePrinterDBRequest
  512. {
  513. Name = request.Name
  514. }
  515. );
  516. }
  517. /// <summary>
  518. /// 删除打印机更新包
  519. /// </summary>
  520. /// <param name="request"></param>
  521. /// <returns></returns>
  522. public async Task<bool> DeletePrinterByCodeAsync(DeleteDevicePrinterByCodeRequest request)
  523. {
  524. var token = await _authenticationService.GetTokenAsync(request);
  525. //更新包编码不为空
  526. if (string.IsNullOrWhiteSpace(request.Code))
  527. {
  528. ThrowCustomerException(CustomerRpcCode.CodeIsEmpty, "Code is empty");
  529. }
  530. return await _deviceInfoDBServiceProxy.DeleteDevicePrinterDBAsync(
  531. new WingInterfaceLibrary.Request.DBRequest.DeleteDevicePrinterDBRequest()
  532. {
  533. Code = request.Code
  534. });
  535. }
  536. /// <summary>
  537. /// 修改打印机更新包
  538. /// </summary>
  539. /// <param name="request"></param>
  540. /// <returns></returns>
  541. public async Task<bool> UpdateDevicePrinterAsync(UpdateDevicePrinterRequest request)
  542. {
  543. var userCode = await _authenticationService.GetTokenAsync(request);
  544. //更新包名称不为空
  545. if (string.IsNullOrWhiteSpace(request.Name))
  546. {
  547. ThrowCustomerException(CustomerRpcCode.NameEmpty, "Name cannot be empty");
  548. }
  549. //更新包编码不为空
  550. if (string.IsNullOrWhiteSpace(request.Code))
  551. {
  552. ThrowCustomerException(CustomerRpcCode.CodeIsEmpty, "Code is empty");
  553. }
  554. var printer = await _deviceInfoDBServiceProxy.GetDevicePrinterDBAsync(
  555. new WingInterfaceLibrary.Request.DBRequest.GetDevicePrinterDBRequest()
  556. {
  557. Code = request.Code
  558. });
  559. //打印机驱动已存在
  560. if (printer != null)
  561. {
  562. ThrowCustomerException(CustomerRpcCode.DevicePrinterExistError, "Device printer is exist");
  563. }
  564. return await _deviceInfoDBServiceProxy.UpdateDevicePrinterDBAsync(
  565. new WingInterfaceLibrary.Request.DBRequest.UpdateDevicePrinterDBRequest()
  566. {
  567. Code = request.Code,
  568. Name = request.Name,
  569. Description = request.Description,
  570. OsVersion = request.OsVersion
  571. });
  572. }
  573. /// <summary>
  574. /// 客户端设置设备打印机
  575. /// </summary>
  576. /// <param name="request"></param>
  577. /// <returns></returns>
  578. public async Task<bool> SetDevicePrinterAsync(SetDevicePrinterRequest request)
  579. {
  580. Logger.WriteLineInfo($"DeviceService SetDevicePrinterAsync request: {Newtonsoft.Json.JsonConvert.SerializeObject(request)} ");
  581. var token = await _authenticationService.GetTokenAsync(request);
  582. var userCode = token?.ClientId ?? string.Empty;
  583. if (string.IsNullOrWhiteSpace(request.DeviceCode))
  584. {
  585. ThrowCustomerException(CustomerRpcCode.DeviceCodeRequired, "Device code cannot be empty");
  586. }
  587. var setPrinterEnum = request.SetPrinterEnum;
  588. DevicePrinterDTO devicePrinter = null;
  589. var device = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
  590. if (device == null)
  591. {
  592. ThrowCustomerException(CustomerRpcCode.DeviceNotFind, "device is not found");
  593. }
  594. if (setPrinterEnum == DevicePrinterEnum.InstallPrinterDrive)//安装云打印机驱动
  595. {
  596. var devicePrinterPatch = request.Parameters?.FirstOrDefault(c => c.SetPrinterEnum == DevicePrinterParameterEnum.DevicePrinterPatchCode);
  597. if (devicePrinterPatch == null)
  598. {
  599. ThrowCustomerException(CustomerRpcCode.DevicePrinterPatchNoFound, "Printer patch no found");
  600. }
  601. var patchCode = devicePrinterPatch.ParameterValue;
  602. var printer = await _deviceInfoDBServiceProxy.GetDevicePrinterDBAsync(
  603. new WingInterfaceLibrary.Request.DBRequest.GetDevicePrinterDBRequest()
  604. {
  605. Code = patchCode
  606. });
  607. var printerNameParameter = request.Parameters?.FirstOrDefault(c => c.SetPrinterEnum == DevicePrinterParameterEnum.PrinterName);
  608. if (printerNameParameter == null)
  609. {
  610. ThrowCustomerException(CustomerRpcCode.NameEmpty, "Printer name is empty");
  611. }
  612. devicePrinter = printer;
  613. //增加打印机驱动品牌模型名称
  614. var supportedPrinterModelName =
  615. devicePrinter.PrinterModels.FirstOrDefault(m => m.Replace(" ", string.Empty).ToLower().Contains(printerNameParameter.ParameterValue.Replace(" ", string.Empty).ToLower()));
  616. devicePrinter.DriveModelName = supportedPrinterModelName;
  617. }
  618. var notification = new DevicePrinterRequestNotification()//设备收到打印机通知
  619. {
  620. SetPrinterEnum = request.SetPrinterEnum,
  621. Parameters = request.Parameters?.ToList(),
  622. DevicePrinter = devicePrinter,
  623. DeviceCode = request.DeviceCode,
  624. OperatorCode = userCode,
  625. };
  626. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  627. notificationRequest.ClientIds = new List<string>() { request.DeviceCode };
  628. notificationRequest.Message = notification;
  629. notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
  630. notificationRequest.NotificationType = notification.NotificationType;
  631. notificationRequest.TransactionType = TransactionTypeEnum.AfterSales;
  632. notificationRequest.RelevanceCode = request.DeviceCode;
  633. notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
  634. var deviceTokenInfo = CacheMaintenance.Instance.Get<ITokensManager>().Where(x => x.ClientId == request.DeviceCode)?.FirstOrDefault();
  635. if (deviceTokenInfo != null && deviceTokenInfo.IsOldPlatform && deviceTokenInfo.IsOnline)
  636. {
  637. return await _deviceForwardService.SetDevicePrinterAsync(notification);
  638. }
  639. else
  640. {
  641. var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
  642. Logger.WriteLineInfo($"DeviceService SetDevicePrinterAsync Result: {res} ");
  643. return res;
  644. }
  645. }
  646. /// <summary>
  647. /// 设备上传打印机信息
  648. /// </summary>
  649. /// <param name="request"></param>
  650. /// <docFirstDirectory>设备端API</docFirstDirectory>
  651. /// <docSecondDirectory>设备信息接口</docSecondDirectory>
  652. /// <returns></returns>
  653. public async Task<bool> UploadDevicePrinterAsync(UploadDevicePrinterRequest request)
  654. {
  655. var userCode = request.UserCode;
  656. string deviceCode = await GetClientIdByTokenAsync(request.Token);
  657. if (string.IsNullOrEmpty(deviceCode))
  658. {
  659. ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
  660. }
  661. if (string.IsNullOrEmpty(userCode))
  662. {
  663. ThrowCustomerException(CustomerRpcCode.UsercodeIsEmpty, "User code is empty");
  664. }
  665. if (request.SetPrinterEnum == DevicePrinterEnum.InstallPrinterProgress)
  666. {
  667. _cacheDevicePrinterDict.TryGetValue(deviceCode, out var printerParameters);
  668. if (printerParameters != null && printerParameters.Any())
  669. {
  670. var installPrinterProgress = request.DevicePrinterList.FirstOrDefault()?.InstallPrinterProgress ?? 0;
  671. foreach (var item in printerParameters)
  672. {
  673. item.InstallPrinterProgress = installPrinterProgress;
  674. }
  675. request.DevicePrinterList = printerParameters;
  676. }
  677. }
  678. _cacheDevicePrinterDict.AddOrUpdate(deviceCode, (k) => request.DevicePrinterList, (k, v) =>
  679. {
  680. return request.DevicePrinterList;
  681. });
  682. var notification = new DevicePrinterResultNotification()//客户端收到打印机通知
  683. {
  684. SetPrinterEnum = request.SetPrinterEnum,
  685. DevicePrinterList = null,
  686. DeviceCode = deviceCode,
  687. RemoteDeviceState = request.RemoteDeviceState
  688. };
  689. BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
  690. notificationRequest.ClientIds = new List<string>() { userCode };
  691. notificationRequest.Message = notification;
  692. notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
  693. notificationRequest.NotificationType = notification.NotificationType;
  694. notificationRequest.TransactionType = TransactionTypeEnum.AfterSales;
  695. notificationRequest.RelevanceCode = deviceCode;
  696. notificationRequest.ReceiverType = ApplicantTypeEnum.Client;
  697. var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
  698. return res;
  699. }
  700. /// <summary>
  701. /// 获取设备上传的打印机列表信息
  702. /// </summary>
  703. /// <param name="request"></param>
  704. /// <returns></returns>
  705. public async Task<IList<DevicePrinterParameterDTO>> GetDevicePrintersForUploadAsync(GetDevicePrintersForUploadRequest request)
  706. {
  707. Logger.WriteLineInfo($"DeviceService GetDevicePrintersForUploadAsync request: {Newtonsoft.Json.JsonConvert.SerializeObject(request)} ");
  708. var token = await _authenticationService.GetTokenAsync(request);
  709. if (string.IsNullOrWhiteSpace(request.DeviceCode))
  710. {
  711. ThrowCustomerException(CustomerRpcCode.DeviceCodeRequired, "Device code cannot be empty");
  712. }
  713. _cacheDevicePrinterDict.TryGetValue(request.DeviceCode, out var printerParameters);
  714. Logger.WriteLineInfo($"DeviceService GetDevicePrintersForUploadAsync response: {Newtonsoft.Json.JsonConvert.SerializeObject(printerParameters)} ");
  715. return printerParameters?.ToList() ?? new List<DevicePrinterParameterDTO>();
  716. }
  717. /// <summary>
  718. /// 客户端获取补丁包设备类型集合
  719. /// </summary>
  720. /// <param name="request"></param>
  721. /// <returns>Us,sonopost,FISSDK</returns>
  722. public async Task<IList<string>> GetDevicePatchTypeListAsync(TokenRequest request)
  723. {
  724. var token = await _authenticationService.GetTokenAsync(request);
  725. var enums = Enum.GetValues(typeof(DevicePatchTypeEnum));
  726. var list = new List<string>();
  727. foreach (var item in enums)
  728. {
  729. list.Add(item.ToString());
  730. }
  731. return list;
  732. }
  733. }
  734. }