123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using WingInterfaceLibrary.DTO.LiveRoom;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Internal.Request;
- using WingInterfaceLibrary.Notifications;
- using WingInterfaceLibrary.Notifications.Live;
- using WingInterfaceLibrary.Notifications.Remote;
- using WingInterfaceLibrary.Request;
- using WingInterfaceLibrary.Request.Remote;
- using WingServerCommon.Interfaces.Cache;
- using WingServerCommon.Service;
- using WingInterfaceLibrary.Request.Device;
- using WingInterfaceLibrary.Result;
- using WingInterfaceLibrary.DTO.Device;
- using WingServerCommon.Mapper;
- using System.Collections.Concurrent;
- using System;
- using System.Linq;
- using WingServerCommon.Log;
- namespace WingDeviceService.Service
- {
- /// <summary>
- /// 设备更新包和打印机驱动管理
- /// </summary>
- public partial class DeviceService : JsonRpcService, IDeviceService
- {
- /// <summary>
- ///缓存设备打印机驱动列表信息 fixed 0012169
- /// </summary>
- /// <typeparam name="string"></typeparam>
- /// <typeparam name="CacheControllingParameter"></typeparam>
- /// <returns></returns>
- private ConcurrentDictionary<string, IList<DevicePrinterParameterDTO>> _cacheDevicePrinterDict = new ConcurrentDictionary<string, IList<DevicePrinterParameterDTO>>();
- /// <summary>
- /// 增加更新包
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> AddDevicePatchAsync(AddDevicePatchRequest request)
- {
- var fileUploadInfoList = request.FileUploadInfoList;
- var userCode = await _authenticationService.GetTokenAsync(request);
- //更新包名称不为空
- if (string.IsNullOrWhiteSpace(request.Name))
- {
- ThrowCustomerException(CustomerRpcCode.NameEmpty, "Required parameter:DeviceName cannot be empty");
- }
- if (fileUploadInfoList == null && fileUploadInfoList.Count <= 0)
- {
- ThrowCustomerException(CustomerRpcCode.FileTokenIsEmpty, "fileUploadInfoList cannot be empty");
- }
- //更新包名称不为空
- if (string.IsNullOrWhiteSpace(request.DeviceType))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceTypeRequired, "Device type cannot be empty");
- }
- var patch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(
- new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
- {
- Name = request.Name
- });
- if (patch != null)
- {
- ThrowCustomerException(CustomerRpcCode.PatchNameExistError, "Patch name is exist");
- }
- return await _deviceInfoDBServiceProxy.AddDevicePatchDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.AddDevicePatchDBRequest()
- {
- Name = request.Name,
- Description = request.Description,
- DeviceType = request.DeviceType,
- SoftwareVersion = request.SoftwareVersion,
- OsVersion = request.OsVersion,
- DeviceFileInfoList = request.FileUploadInfoList,
- FileSize = request.FileSize,
- FileName = request.FileName
- });
- }
- /// <summary>
- /// 查询更新包
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PageResult<DevicePatchDTO>> FindDevicePatchPagesAsync(FindDevicePatchPageRequest request)
- {
- var userCode = await _authenticationService.GetTokenAsync(request);
- var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchPageDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchPageDBRequest()
- {
- Keyword = request.Keyword,
- PageIndex = request.PageIndex,
- PageSize = request.PageSize,
- });
- return patchList;
- }
- /// <summary>
- /// 删除更新包
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> DeletePatchByCodeAsync(DeleteDevicePatchByCodeRequest request)
- {
- var userCode = await _authenticationService.GetTokenAsync(request);
- //更新包名称不为空
- if (string.IsNullOrWhiteSpace(request.Code))
- {
- ThrowCustomerException(CustomerRpcCode.PatchCodeIsEmpty, "Patch code cannot be empty");
- }
- return await _deviceInfoDBServiceProxy.DeleteDevicePatchDBbyCodeAsync(
- new WingInterfaceLibrary.Request.DBRequest.DeleteDevicePatchDBbyCodeRequest()
- {
- Code = request.Code
- });
- }
- /// <summary>
- /// 修改更新包
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateDevicePatchAsync(UpdateDevicePatchRequest request)
- {
- var userCode = await _authenticationService.GetTokenAsync(request);
- //更新包名称不为空
- if (string.IsNullOrWhiteSpace(request.Name))
- {
- ThrowCustomerException(CustomerRpcCode.NameEmpty, "Name cannot be empty");
- }
- //更新包编码不为空
- if (string.IsNullOrWhiteSpace(request.Code))
- {
- ThrowCustomerException(CustomerRpcCode.PatchCodeIsEmpty, "Patch code is empty");
- }
- var myPatch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(
- new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
- {
- Code = request.Code
- });
- if (myPatch == null)
- {
- ThrowCustomerException(CustomerRpcCode.PatchNoFoundError, "Patch no found");
- }
- var otherPatch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(
- new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
- {
- Name = request.Name
- });
- if (otherPatch != null)
- {
- if (myPatch.Code != otherPatch.Code)
- {
- ThrowCustomerException(CustomerRpcCode.PatchNameExistError, "Patch name is exist");
- }
- }
- return await _deviceInfoDBServiceProxy.UpdateDevicePatchDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.UpdateDevicePatchDBRequest()
- {
- Code = request.Code,
- Name = request.Name,
- Description = request.Description,
- SoftwareVersion = request.SoftwareVersion,
- OsVersion = request.OsVersion
- });
- }
- /// <summary>
- ///推送设备补丁包
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> PushDevicePatchAsync(PushDevicePatchRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- var deviceCode = request.DeviceCode;
- if (string.IsNullOrWhiteSpace(deviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeRequired, "Device code cannot be empty");
- }
- DevicePatchDTO patch = null;
- if (request.PushEnum == PushDevicePatchEnum.Start)
- {
- if (string.IsNullOrWhiteSpace(request.PatchCode))
- {
- ThrowCustomerException(CustomerRpcCode.PatchCodeIsEmpty, "Patch code cannot be empty");
- }
- patch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
- {
- Code = request.PatchCode
- });
- if (patch == null)
- {
- ThrowCustomerException(CustomerRpcCode.PatchNoFoundError, "Patch no found");
- }
- }
- var notification = new PushDevicePatchToDeviceNotification()//设备通知
- {
- PatchName = patch?.Name ?? string.Empty,
- PatchCode = patch?.Code ?? string.Empty,
- UserCode = token.ClientId,
- DeviceCode = deviceCode,
- };
- if (request.PushEnum == PushDevicePatchEnum.Start)
- {
- notification.DeviceFileInfoList = patch?.DeviceFileInfoList?.ToList() ?? new List<UploadDeviceFileInfoDTO>();
- notification.FileSize = patch?.FileSize ?? 0;
- notification.FileName = patch?.FileName ?? string.Empty;
- notification.DeviceType = patch?.DeviceType ?? string.Empty;
- }
- notification.PushEnum = request.PushEnum;
- BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
- notificationRequest.ClientIds = new List<string>() { deviceCode };
- notificationRequest.Message = notification;
- notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
- notificationRequest.NotificationType = notification.NotificationType;
- notificationRequest.TransactionType = TransactionTypeEnum.AfterSales;
- notificationRequest.RelevanceCode = deviceCode;
- notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
- var deviceTokenInfo = CacheMaintenance.Instance.Get<ITokensManager>().Where(x => x.ClientId == deviceCode)?.FirstOrDefault();
- if (deviceTokenInfo != null && deviceTokenInfo.IsOldPlatform && deviceTokenInfo.IsOnline)
- {
- await _deviceForwardService.PushDevicePatchAsync(notification);
- }
- var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
- return res;
- }
- /// <summary>
- ///根据设备编码分页查询设备
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PageResult<DevicePatchDTO>> FindPushDevicePatchPagesAsync(FindPushDevicePatchPageRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- //更新包名称不为空
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeRequired, "Device code cannot be empty");
- }
- var softwareVersion = string.Empty;
- var osVersion = string.Empty;
- var device = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
- if (device == null)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Device no found");
- }
- softwareVersion = device.DeviceSoftwareVersion;
- osVersion = device.SystemVersion;
- var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchPageDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchPageDBRequest()
- {
- Code = request.DeviceCode,
- Keyword = request.Keyword,
- PageIndex = request.PageIndex,
- PageSize = request.PageSize,
- SoftwareVersion = softwareVersion,
- OsVersion = osVersion,
- FindOnlyDeviceTypes = request.FindOnlyDeviceTypes
- });
- return patchList;
- }
- /// <summary>
- ///上传补丁包下载进度
- /// </summary>
- /// <param name="request"></param>
- /// <docFirstDirectory>设备端API</docFirstDirectory>
- /// <docSecondDirectory>设备信息接口</docSecondDirectory>
- /// <returns></returns>
- public async Task<bool> UploadDeviceDownloadPatchProgressToUserAsync(UploadDeviceDownloadPatchProgressToUserRequest request)
- {
- string deviceCode = await GetClientIdByTokenAsync(request.Token);
- if (string.IsNullOrEmpty(deviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
- }
- Logger.WriteLineInfo($"DeviceService UploadDeviceDownloadPatchProgressToUserAsync deviceCode: {deviceCode} PatchCode:{request.PatchCode} State:{request.RemoteDeviceState} ");
- var patchName = Guid.NewGuid().ToString("N");
- if (!string.IsNullOrWhiteSpace(request.PatchCode))
- {
- var patch = await _deviceInfoDBServiceProxy.GetDevicePatchDBbyNameAsync(new WingInterfaceLibrary.Request.DBRequest.GetDevicePatchDBbyNameRequest()
- {
- Code = request.PatchCode
- });
- if (patch == null)
- {
- ThrowCustomerException(CustomerRpcCode.PatchNoFoundError, "Patch no found");
- }
- patchName = patch.Name;
- }
- var notification = new DeviceDownloadPatchProgressToUserNotification()//客户端通知
- {
- Progress = request.Progress,
- PatchCode = request.PatchCode,
- DeviceCode = deviceCode,
- RemoteDeviceState = request.RemoteDeviceState,
- PatchName = patchName,
- };
- BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
- notificationRequest.ClientIds = new List<string>() { request.UserCode };
- notificationRequest.Message = notification;
- notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
- notificationRequest.NotificationType = notification.NotificationType;
- notificationRequest.TransactionType = TransactionTypeEnum.AfterSales;
- notificationRequest.RelevanceCode = deviceCode;
- notificationRequest.ReceiverType = ApplicantTypeEnum.Client;
- var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
- return res;
- }
- /// <summary>
- /// 增加设备更新包版本号
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> AddDevicePatchVersionAsync(AddDevicePatchVersionRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- //更新包名称不为空
- if (string.IsNullOrWhiteSpace(request.Version))
- {
- ThrowCustomerException(CustomerRpcCode.VersionInvalid, "Version invalid");
- }
- if (string.IsNullOrWhiteSpace(request.DeviceType))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceTypeEmpty, "Device type is empty");
- }
- var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchVersionListDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchVersionListDBRequest()
- {
- VersionTypeEnum = request.VersionTypeEnum,
- DeviceType = request.DeviceType
- });
- if (patchList != null && patchList.FirstOrDefault(v => v.Version == request.Version) != null)
- {
- //已存在
- ThrowCustomerException(CustomerRpcCode.VersionIsExistError, "Version is exist");
- }
- return await _deviceInfoDBServiceProxy.AddDevicePatchVersionDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.AddDevicePatchVersionDBRequest()
- {
- Version = request.Version,
- VersionTypeEnum = request.VersionTypeEnum,
- DeviceType = request.DeviceType
- });
- }
- /// <summary>
- /// 查询设备更新包版本号
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<List<DevicePatchVersionDTO>> FindDevicePatchVersionListAsync(FindDevicePatchListRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- if (string.IsNullOrWhiteSpace(request.DeviceType))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceTypeEmpty, "Device type is empty");
- }
- return await _deviceInfoDBServiceProxy.FindDevicePatchVersionListDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchVersionListDBRequest()
- {
- VersionTypeEnum = request.VersionTypeEnum,
- DeviceType = request.DeviceType
- });
- }
- /// <summary>
- /// 删除设备更新包版本号
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> DeleteDevicePatchVersionAsync(DeleteDevicePatchVersionRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- //更新包编码不为空
- if (string.IsNullOrWhiteSpace(request.Code))
- {
- ThrowCustomerException(CustomerRpcCode.CodeIsEmpty, "Code is empty");
- }
- return await _deviceInfoDBServiceProxy.DeleteDevicePatchVersionDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.DeleteDevicePatchVersionDBRequest()
- {
- Code = request.Code
- });
- }
- /// <summary>
- /// 增加打印机系统版本号
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> AddDevicePrinterVersionAsync(AddDevicePrinterVersionRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- var deviceType = "Printer";
- //更新包名称不为空
- if (string.IsNullOrWhiteSpace(request.Version))
- {
- ThrowCustomerException(CustomerRpcCode.VersionInvalid, "Version invalid");
- }
- var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchVersionListDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchVersionListDBRequest()
- {
- VersionTypeEnum = request.VersionTypeEnum,
- DeviceType = deviceType
- });
- if (patchList != null && patchList.FirstOrDefault(v => v.Version == request.Version) != null)
- {
- //已存在
- ThrowCustomerException(CustomerRpcCode.VersionIsExistError, "Version is exist");
- }
- return await _deviceInfoDBServiceProxy.AddDevicePatchVersionDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.AddDevicePatchVersionDBRequest()
- {
- Version = request.Version,
- VersionTypeEnum = request.VersionTypeEnum,
- DeviceType = deviceType
- });
- }
- /// <summary>
- /// 查询打印机系统版本号
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<List<DevicePrinterVersionDTO>> FindDevicePrinterVersionListAsync(FindDevicePrinterVersionListRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- var deviceType = "Printer";
- var patchList = await _deviceInfoDBServiceProxy.FindDevicePatchVersionListDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.FindDevicePatchVersionListDBRequest()
- {
- VersionTypeEnum = request.VersionTypeEnum,
- DeviceType = deviceType
- });
- var result = new List<DevicePrinterVersionDTO>();
- foreach (var item in patchList)
- {
- result.Add(item.MappingTo<DevicePrinterVersionDTO>());
- }
- return result;
- }
- /// <summary>
- /// 删除打印机系统版本号
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> DeleteDevicePrinterVersionAsync(DeleteDevicePrinterVersionRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- //更新包编码不为空
- if (string.IsNullOrWhiteSpace(request.Code))
- {
- ThrowCustomerException(CustomerRpcCode.CodeIsEmpty, "Code is empty");
- }
- return await _deviceInfoDBServiceProxy.DeleteDevicePatchVersionDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.DeleteDevicePatchVersionDBRequest()
- {
- Code = request.Code
- });
- }
- /// <summary>
- /// 增加打印机更新包
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> AddDevicePrinterAsync(AddDevicePrinterRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- //更新包名称不为空
- if (string.IsNullOrWhiteSpace(request.Name))
- {
- ThrowCustomerException(CustomerRpcCode.NameEmpty, "Name is empty");
- }
- if (request.FileUploadInfoList == null && request.FileUploadInfoList.Count <= 0)
- {
- ThrowCustomerException(CustomerRpcCode.FileTokenIsEmpty, "fileUploadInfoList cannot be empty");
- }
- return await _deviceInfoDBServiceProxy.AddDevicePrinterDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.AddDevicePrinterDBRequest()
- {
- Name = request.Name,
- Description = request.Description,
- OsVersion = request.OsVersion,
- FileUploadInfoList = request.FileUploadInfoList,
- FileSize = request.FileSize,
- PrinterBrands = request.PrinterBrands,
- PrinterModels = request.PrinterModels,
- });
- }
- /// <summary>
- /// 查询打印机更新包
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PageResult<DevicePrinterDTO>> FindDevicePrinterPagesAsync(FindDevicePrinterPageRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- var printerList = await _deviceInfoDBServiceProxy.FindDevicePrinterPageDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.FindDevicePrinterPageDBRequest()
- {
- Keyword = request.Keyword,
- PageIndex = request.PageIndex,
- PageSize = request.PageSize,
- });
- return printerList;
- }
- /// <summary>
- /// 查询打印机驱动是否存在
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> ExistsDevicePrinterAsync(ExistsDevicePrinterRequest request)
- {
- return await _deviceInfoDBServiceProxy.ExistsDevicePrinterDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.ExistsDevicePrinterDBRequest
- {
- Name = request.Name
- }
- );
- }
- /// <summary>
- /// 删除打印机更新包
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> DeletePrinterByCodeAsync(DeleteDevicePrinterByCodeRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- //更新包编码不为空
- if (string.IsNullOrWhiteSpace(request.Code))
- {
- ThrowCustomerException(CustomerRpcCode.CodeIsEmpty, "Code is empty");
- }
- return await _deviceInfoDBServiceProxy.DeleteDevicePrinterDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.DeleteDevicePrinterDBRequest()
- {
- Code = request.Code
- });
- }
- /// <summary>
- /// 修改打印机更新包
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateDevicePrinterAsync(UpdateDevicePrinterRequest request)
- {
- var userCode = await _authenticationService.GetTokenAsync(request);
- //更新包名称不为空
- if (string.IsNullOrWhiteSpace(request.Name))
- {
- ThrowCustomerException(CustomerRpcCode.NameEmpty, "Name cannot be empty");
- }
- //更新包编码不为空
- if (string.IsNullOrWhiteSpace(request.Code))
- {
- ThrowCustomerException(CustomerRpcCode.CodeIsEmpty, "Code is empty");
- }
- var printer = await _deviceInfoDBServiceProxy.GetDevicePrinterDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.GetDevicePrinterDBRequest()
- {
- Code = request.Code
- });
- //打印机驱动已存在
- if (printer != null)
- {
- ThrowCustomerException(CustomerRpcCode.DevicePrinterExistError, "Device printer is exist");
- }
- return await _deviceInfoDBServiceProxy.UpdateDevicePrinterDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.UpdateDevicePrinterDBRequest()
- {
- Code = request.Code,
- Name = request.Name,
- Description = request.Description,
- OsVersion = request.OsVersion
- });
- }
- /// <summary>
- /// 客户端设置设备打印机
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> SetDevicePrinterAsync(SetDevicePrinterRequest request)
- {
- Logger.WriteLineInfo($"DeviceService SetDevicePrinterAsync request: {Newtonsoft.Json.JsonConvert.SerializeObject(request)} ");
- var token = await _authenticationService.GetTokenAsync(request);
- var userCode = token?.ClientId ?? string.Empty;
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeRequired, "Device code cannot be empty");
- }
- var setPrinterEnum = request.SetPrinterEnum;
- DevicePrinterDTO devicePrinter = null;
- var device = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(request.DeviceCode);
- if (device == null)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotFind, "device is not found");
- }
- if (setPrinterEnum == DevicePrinterEnum.InstallPrinterDrive)//安装云打印机驱动
- {
- var devicePrinterPatch = request.Parameters?.FirstOrDefault(c => c.SetPrinterEnum == DevicePrinterParameterEnum.DevicePrinterPatchCode);
- if (devicePrinterPatch == null)
- {
- ThrowCustomerException(CustomerRpcCode.DevicePrinterPatchNoFound, "Printer patch no found");
- }
- var patchCode = devicePrinterPatch.ParameterValue;
- var printer = await _deviceInfoDBServiceProxy.GetDevicePrinterDBAsync(
- new WingInterfaceLibrary.Request.DBRequest.GetDevicePrinterDBRequest()
- {
- Code = patchCode
- });
- var printerNameParameter = request.Parameters?.FirstOrDefault(c => c.SetPrinterEnum == DevicePrinterParameterEnum.PrinterName);
- if (printerNameParameter == null)
- {
- ThrowCustomerException(CustomerRpcCode.NameEmpty, "Printer name is empty");
- }
- devicePrinter = printer;
- //增加打印机驱动品牌模型名称
- var supportedPrinterModelName =
- devicePrinter.PrinterModels.FirstOrDefault(m => m.Replace(" ", string.Empty).ToLower().Contains(printerNameParameter.ParameterValue.Replace(" ", string.Empty).ToLower()));
- devicePrinter.DriveModelName = supportedPrinterModelName;
- }
- var notification = new DevicePrinterRequestNotification()//设备收到打印机通知
- {
- SetPrinterEnum = request.SetPrinterEnum,
- Parameters = request.Parameters?.ToList(),
- DevicePrinter = devicePrinter,
- DeviceCode = request.DeviceCode,
- OperatorCode = userCode,
- };
- BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
- notificationRequest.ClientIds = new List<string>() { request.DeviceCode };
- notificationRequest.Message = notification;
- notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
- notificationRequest.NotificationType = notification.NotificationType;
- notificationRequest.TransactionType = TransactionTypeEnum.AfterSales;
- notificationRequest.RelevanceCode = request.DeviceCode;
- notificationRequest.ReceiverType = ApplicantTypeEnum.Device;
- var deviceTokenInfo = CacheMaintenance.Instance.Get<ITokensManager>().Where(x => x.ClientId == request.DeviceCode)?.FirstOrDefault();
- if (deviceTokenInfo != null && deviceTokenInfo.IsOldPlatform && deviceTokenInfo.IsOnline)
- {
- return await _deviceForwardService.SetDevicePrinterAsync(notification);
- }
- else
- {
- var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
- Logger.WriteLineInfo($"DeviceService SetDevicePrinterAsync Result: {res} ");
- return res;
- }
- }
- /// <summary>
- /// 设备上传打印机信息
- /// </summary>
- /// <param name="request"></param>
- /// <docFirstDirectory>设备端API</docFirstDirectory>
- /// <docSecondDirectory>设备信息接口</docSecondDirectory>
- /// <returns></returns>
- public async Task<bool> UploadDevicePrinterAsync(UploadDevicePrinterRequest request)
- {
- var userCode = request.UserCode;
- string deviceCode = await GetClientIdByTokenAsync(request.Token);
- if (string.IsNullOrEmpty(deviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.NoExistDevice, "Device does not exist");
- }
- if (string.IsNullOrEmpty(userCode))
- {
- ThrowCustomerException(CustomerRpcCode.UsercodeIsEmpty, "User code is empty");
- }
- if (request.SetPrinterEnum == DevicePrinterEnum.InstallPrinterProgress)
- {
- _cacheDevicePrinterDict.TryGetValue(deviceCode, out var printerParameters);
- if (printerParameters != null && printerParameters.Any())
- {
- var installPrinterProgress = request.DevicePrinterList.FirstOrDefault()?.InstallPrinterProgress ?? 0;
- foreach (var item in printerParameters)
- {
- item.InstallPrinterProgress = installPrinterProgress;
- }
- request.DevicePrinterList = printerParameters;
- }
- }
- _cacheDevicePrinterDict.AddOrUpdate(deviceCode, (k) => request.DevicePrinterList, (k, v) =>
- {
- return request.DevicePrinterList;
- });
- var notification = new DevicePrinterResultNotification()//客户端收到打印机通知
- {
- SetPrinterEnum = request.SetPrinterEnum,
- DevicePrinterList = null,
- DeviceCode = deviceCode,
- RemoteDeviceState = request.RemoteDeviceState
- };
- BroadcastNotificationRequest notificationRequest = new BroadcastNotificationRequest();
- notificationRequest.ClientIds = new List<string>() { userCode };
- notificationRequest.Message = notification;
- notificationRequest.JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(notification);
- notificationRequest.NotificationType = notification.NotificationType;
- notificationRequest.TransactionType = TransactionTypeEnum.AfterSales;
- notificationRequest.RelevanceCode = deviceCode;
- notificationRequest.ReceiverType = ApplicantTypeEnum.Client;
- var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
- return res;
- }
- /// <summary>
- /// 获取设备上传的打印机列表信息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<IList<DevicePrinterParameterDTO>> GetDevicePrintersForUploadAsync(GetDevicePrintersForUploadRequest request)
- {
- Logger.WriteLineInfo($"DeviceService GetDevicePrintersForUploadAsync request: {Newtonsoft.Json.JsonConvert.SerializeObject(request)} ");
- var token = await _authenticationService.GetTokenAsync(request);
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeRequired, "Device code cannot be empty");
- }
- _cacheDevicePrinterDict.TryGetValue(request.DeviceCode, out var printerParameters);
- Logger.WriteLineInfo($"DeviceService GetDevicePrintersForUploadAsync response: {Newtonsoft.Json.JsonConvert.SerializeObject(printerParameters)} ");
- return printerParameters?.ToList() ?? new List<DevicePrinterParameterDTO>();
- }
- /// <summary>
- /// 客户端获取补丁包设备类型集合
- /// </summary>
- /// <param name="request"></param>
- /// <returns>Us,sonopost,FISSDK</returns>
- public async Task<IList<string>> GetDevicePatchTypeListAsync(TokenRequest request)
- {
- var token = await _authenticationService.GetTokenAsync(request);
- var enums = Enum.GetValues(typeof(DevicePatchTypeEnum));
- var list = new List<string>();
- foreach (var item in enums)
- {
- list.Add(item.ToString());
- }
- return list;
- }
- }
- }
|