|
@@ -1,12 +1,18 @@
|
|
|
using System;
|
|
|
using System.Threading.Tasks;
|
|
|
using JsonRpcLite.Services;
|
|
|
+using WingDeviceService.Common;
|
|
|
+using WingInterfaceLibrary.DTO.Authentication;
|
|
|
using WingInterfaceLibrary.DTO.Device;
|
|
|
+using WingInterfaceLibrary.Enum;
|
|
|
using WingInterfaceLibrary.Interface;
|
|
|
+using WingInterfaceLibrary.Request;
|
|
|
using WingInterfaceLibrary.Request.Authentication;
|
|
|
using WingInterfaceLibrary.Request.Device;
|
|
|
using WingInterfaceLibrary.Request.Notification;
|
|
|
using WingInterfaceLibrary.Result.Device;
|
|
|
+using WingServerCommon.Log;
|
|
|
+using WingServerCommon.Mapper;
|
|
|
using WingServerCommon.Service;
|
|
|
|
|
|
namespace WingDeviceService.Service
|
|
@@ -16,11 +22,10 @@ namespace WingDeviceService.Service
|
|
|
/// </summary>
|
|
|
public partial class DeviceService : JsonRpcService, IConnectService
|
|
|
{
|
|
|
- private readonly object _createLocker = new object();
|
|
|
+ private readonly string _sourceUrl = "cloud.xinglinghui.com";
|
|
|
+ private readonly object _createShortCodeLocker = new object();
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 设备登录/注册
|
|
|
- /// </summary>
|
|
|
+ /// <summary>设备连接云服务</summary>
|
|
|
/// <param name="request">请求对象</param>
|
|
|
/// <returns>授权响应</returns>
|
|
|
/// <remarks>POST</remarks>
|
|
@@ -66,10 +71,7 @@ namespace WingDeviceService.Service
|
|
|
var deviceCode = deviceDto?.DeviceCode;
|
|
|
if (string.IsNullOrWhiteSpace(shortCode))
|
|
|
{
|
|
|
- lock (_createLocker)
|
|
|
- {
|
|
|
- shortCode = GenerateShortCode();
|
|
|
- }
|
|
|
+ shortCode = GenerateShortCode();
|
|
|
}
|
|
|
if (deviceDto == null)
|
|
|
{
|
|
@@ -112,8 +114,9 @@ namespace WingDeviceService.Service
|
|
|
{
|
|
|
Code = deviceCode,
|
|
|
IssueTime = now,
|
|
|
- SourceUrl = "cloud.xinglinghui.com",
|
|
|
+ SourceUrl = _sourceUrl,
|
|
|
});
|
|
|
+ await SetDeviceOnlineAsync(deviceCode, true, true);
|
|
|
//注册通知
|
|
|
var subscribeNotifyRequest = new SubscribeNotifyRequest()
|
|
|
{
|
|
@@ -137,25 +140,192 @@ namespace WingDeviceService.Service
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 生成唯一 shortCode
|
|
|
- /// </summary>
|
|
|
+ /// <summary>查询当前设备信息</summary>
|
|
|
+ /// <param name="request">请求对象</param>
|
|
|
+ /// <returns>设备信息</returns>
|
|
|
+ /// <remarks>POST</remarks>
|
|
|
+ public async Task<CacheDeviceDTO> GetDeviceByToken(TokenRequest request)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var deviceTokenDTO = await _authenticationServiceProxy.GetAuthToken(request);
|
|
|
+ if (deviceTokenDTO == null || string.IsNullOrWhiteSpace(deviceTokenDTO.Code))
|
|
|
+ {
|
|
|
+ throw new RpcException(2004, "Permission validation error", "Permission validation error");
|
|
|
+ }
|
|
|
+ var cacheDeviceDTO = await GetDevice(deviceTokenDTO.Code);
|
|
|
+ if (cacheDeviceDTO == null)
|
|
|
+ {
|
|
|
+ throw new RpcException(2004, "deviceInfo not find", "deviceInfo not find");
|
|
|
+ }
|
|
|
+ if (cacheDeviceDTO.LastLoginTime > deviceTokenDTO.IssueTime)
|
|
|
+ {
|
|
|
+ throw new RpcException(20041, "Permission validation error", "Permission validation error");
|
|
|
+ }
|
|
|
+ await SetDeviceOnlineAsync(deviceTokenDTO.Code, true);
|
|
|
+ return cacheDeviceDTO;
|
|
|
+ }
|
|
|
+ catch (RpcException)
|
|
|
+ {
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw new RpcException(1001, ex.Message, ex.InnerException?.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>设备与云服务断开连接</summary>
|
|
|
+ /// <param name="request">请求对象</param>
|
|
|
+ /// <returns>是否成功</returns>
|
|
|
+ /// <value>true</value>
|
|
|
+ public async Task<bool> DisConnect(TokenRequest request)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var deviceTokenDTO = await _authenticationServiceProxy.GetAuthToken(request);
|
|
|
+ if (deviceTokenDTO == null || string.IsNullOrWhiteSpace(deviceTokenDTO.Code))
|
|
|
+ {
|
|
|
+ Logger.WriteLineWarn($"DisConnect: invalid token");
|
|
|
+ }
|
|
|
+ var cacheDeviceDTO = await GetDevice(deviceTokenDTO.Code);
|
|
|
+ if (cacheDeviceDTO != null && cacheDeviceDTO.LastLoginTime <= deviceTokenDTO.IssueTime)
|
|
|
+ {
|
|
|
+ if (await UpdateLastLoginTime(cacheDeviceDTO))
|
|
|
+ {
|
|
|
+ _cacheDeviceManager.Remove(cacheDeviceDTO.DeviceCode);
|
|
|
+ Logger.WriteLineInfo($"DisConnect: device disconnect success.");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new RpcException(20041, "disconnect error", "disconnect error");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch (RpcException)
|
|
|
+ {
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw new RpcException(1001, ex.Message, ex.InnerException?.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>生成唯一 shortCode</summary>
|
|
|
/// <returns></returns>
|
|
|
private string GenerateShortCode()
|
|
|
{
|
|
|
- var uniqueId = new byte[6];
|
|
|
- var rand = new Random((int)(DateTime.Now.Ticks % 1000000));
|
|
|
- for (int i = 0; i < 6; i++)
|
|
|
+ try
|
|
|
{
|
|
|
- int randCode;
|
|
|
- do
|
|
|
+ lock (_createShortCodeLocker)
|
|
|
{
|
|
|
- randCode = rand.Next(50, 90);
|
|
|
- uniqueId[i] = Convert.ToByte(randCode);
|
|
|
- } while (randCode >= 58 && randCode <= 64 || randCode == 79 || randCode == 73);
|
|
|
+ var uniqueId = new byte[6];
|
|
|
+ var rand = new Random((int)(DateTime.Now.Ticks % 1000000));
|
|
|
+ for (int i = 0; i < 6; i++)
|
|
|
+ {
|
|
|
+ int randCode;
|
|
|
+ do
|
|
|
+ {
|
|
|
+ randCode = rand.Next(50, 90);
|
|
|
+ uniqueId[i] = Convert.ToByte(randCode);
|
|
|
+ } while (randCode >= 58 && randCode <= 64 || randCode == 79 || randCode == 73);
|
|
|
+ }
|
|
|
+
|
|
|
+ return System.Text.Encoding.ASCII.GetString(uniqueId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"GenerateShortCode error {ex}");
|
|
|
}
|
|
|
+ return string.Empty;
|
|
|
+ }
|
|
|
|
|
|
- return System.Text.Encoding.ASCII.GetString(uniqueId);
|
|
|
+ /// <summary>查询设备信息</summary>
|
|
|
+ /// <param name="deviceCode"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task<CacheDeviceDTO> GetDevice(string deviceCode)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var cacheDTO = _cacheDeviceManager.Get(deviceCode);
|
|
|
+ if (cacheDTO == null)
|
|
|
+ {
|
|
|
+ var deviceDTO = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
|
|
|
+ cacheDTO = deviceDTO.MappingTo<CacheDeviceDTO>();
|
|
|
+ _cacheDeviceManager.AddOrUpdate(cacheDTO.DeviceCode, cacheDTO);
|
|
|
+ }
|
|
|
+ return cacheDTO;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"GetDevice error {ex}");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>更新设备在线状态</summary>
|
|
|
+ /// <param name="deviceCode"></param>
|
|
|
+ /// <param name="isOnline"></param>
|
|
|
+ /// <param name="refresh"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task<CacheDeviceDTO> SetDeviceOnlineAsync(string deviceCode, bool isOnline, bool refresh = false)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var cacheDTO = _cacheDeviceManager.Get(deviceCode);
|
|
|
+ if (cacheDTO == null || refresh)
|
|
|
+ {
|
|
|
+ var deviceDTO = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
|
|
|
+ cacheDTO = deviceDTO.MappingTo<CacheDeviceDTO>();
|
|
|
+ cacheDTO.IsOnline = isOnline;
|
|
|
+ cacheDTO.SourceUrl = _sourceUrl;
|
|
|
+ _cacheDeviceManager.AddOrUpdate(cacheDTO.DeviceCode, cacheDTO);
|
|
|
+ }
|
|
|
+ else if (cacheDTO.IsOnline != isOnline)
|
|
|
+ {
|
|
|
+ cacheDTO.IsOnline = isOnline;
|
|
|
+ _cacheDeviceManager.AddOrUpdate(cacheDTO.DeviceCode, cacheDTO);
|
|
|
+ }
|
|
|
+ return cacheDTO;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"SetDeviceOnlineAsync error {ex}");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task<bool> UpdateLastLoginTime(DeviceInfoDTO deviceInfo)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var result = await _deviceInfoDBServiceProxy.UpdateLoginTimeAsync(new WingInterfaceLibrary.DB.Request.UpdateLoginTimeDBRequest
|
|
|
+ {
|
|
|
+ DeviceCode = deviceInfo.DeviceCode,
|
|
|
+ LastLoginTime = DateTime.UtcNow,
|
|
|
+ ShortCode = deviceInfo.ShortCode,
|
|
|
+ DeviceModel = deviceInfo.DeviceModel,
|
|
|
+ DeviceType = deviceInfo.DeviceType,
|
|
|
+ SoftwareVersion = deviceInfo.DeviceSoftwareVersion,
|
|
|
+ SystemVersion = deviceInfo.SystemVersion,
|
|
|
+ CPUModel = deviceInfo.CPUModel,
|
|
|
+ Description = deviceInfo.Description,
|
|
|
+ Name = deviceInfo.Name,
|
|
|
+ OrganizationCode = deviceInfo.OrganizationCode,
|
|
|
+ SystemLanguage = deviceInfo.SystemLanguage,
|
|
|
+ });
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"UpdateLastLoginTime err, {ex}");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|