198 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			198 lines
		
	
	
		
			6.6 KiB
		
	
	
	
		
			C#
		
	
	
	
using GraphQL;
 | 
						|
using LY.App.Common.Redis;
 | 
						|
using LY.App.Device;
 | 
						|
using LY.App.Extensions.DI;
 | 
						|
using LY.App.Model;
 | 
						|
using Mapster;
 | 
						|
using Newtonsoft.Json;
 | 
						|
using SqlSugar;
 | 
						|
 | 
						|
namespace LY.App.Service
 | 
						|
{
 | 
						|
    [ServiceInjection(InjectionType.Transient)]
 | 
						|
    public class DeviceService
 | 
						|
    {
 | 
						|
        private readonly SqlSugarClient _db;
 | 
						|
        private readonly IConfiguration _config;
 | 
						|
        private readonly RedisService _redisService;
 | 
						|
        private readonly AlarmService _alarmService;
 | 
						|
        private readonly DeviceManager deviceManager = DeviceManager.Instance;
 | 
						|
        public DeviceService(SqlSugarClient db, IConfiguration config, RedisService redisService, AlarmService alarmService)
 | 
						|
        {
 | 
						|
            _db = db;
 | 
						|
            _config = config;
 | 
						|
            _redisService = redisService;
 | 
						|
            _alarmService = alarmService;
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 分页查询
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="pageNum"></param>
 | 
						|
        /// <param name="pageSize"></param>
 | 
						|
        /// <param name="key"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> List(int pageNum, int pageSize, string key)
 | 
						|
        {
 | 
						|
            RefAsync<int> total = 0;
 | 
						|
            var items = await _db.Queryable<DeviceEntity>()
 | 
						|
                .Where(s => s.IsDeleted == false)
 | 
						|
                .WhereIF(!string.IsNullOrEmpty(key), s => s.Name.Contains(key) || s.DeviceSN.Contains(key))
 | 
						|
                .OrderByDescending(s => s.Id)
 | 
						|
                .ToPageListAsync(pageNum, pageSize, total);
 | 
						|
            return new ApiResult()
 | 
						|
            {
 | 
						|
                data = new
 | 
						|
                {
 | 
						|
                    total = total.Value,
 | 
						|
                    items
 | 
						|
                }
 | 
						|
            };
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 新增设备
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="input"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> Add(AddDevice input)
 | 
						|
        {
 | 
						|
            var entity = input.Adapt<DeviceEntity>();
 | 
						|
            var id = await _db.Insertable(entity).ExecuteReturnSnowflakeIdAsync();
 | 
						|
            entity.Id = id;
 | 
						|
            await AddDevice2Manager(entity);
 | 
						|
            var key = RedisKeyList.DeviceInfo(entity.DeviceSN);
 | 
						|
            await _redisService.SetAsync(key, entity, TimeSpan.FromDays(10));
 | 
						|
            return new ApiResult()
 | 
						|
            {
 | 
						|
                code = 0,
 | 
						|
                msg = "success"
 | 
						|
            };
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 获取协议列表
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public Task<ApiResult> ProtocolTypeList()
 | 
						|
        {
 | 
						|
            var dict = new Dictionary<int, string>();
 | 
						|
            foreach (ProtocolType protocol in Enum.GetValues(typeof(ProtocolType)))
 | 
						|
            {
 | 
						|
                dict.Add((int)protocol, protocol.ToString());
 | 
						|
            }
 | 
						|
            return Task.FromResult(new ApiResult()
 | 
						|
            {
 | 
						|
                data = dict
 | 
						|
            });
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 更新设备信息
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="input"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> updata(UpdateDevice input)
 | 
						|
        {
 | 
						|
            var entity = input.Adapt<DeviceEntity>();
 | 
						|
            await _db.Updateable(entity).ExecuteCommandAsync();
 | 
						|
            await deviceManager.RemoveDevice(entity.Id);
 | 
						|
            await AddDevice2Manager(entity);
 | 
						|
            return new ApiResult()
 | 
						|
            {
 | 
						|
                code = 0,
 | 
						|
                msg = "success"
 | 
						|
            };
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 删除设备
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="Id"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<ApiResult> delete(long Id)
 | 
						|
        {
 | 
						|
            var entity = await _db.Queryable<DeviceEntity>().FirstAsync(s => s.Id == Id);
 | 
						|
            entity.IsDeleted = true;
 | 
						|
            await _db.Updateable(entity).ExecuteCommandAsync();
 | 
						|
            await deviceManager.RemoveDevice(entity.Id);
 | 
						|
            return new ApiResult()
 | 
						|
            {
 | 
						|
                code = 0,
 | 
						|
                msg = "success"
 | 
						|
            };
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 取单个设备详情
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="id"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<DeviceEntity> detail(long Id)
 | 
						|
        {
 | 
						|
            return await _db.Queryable<DeviceEntity>()
 | 
						|
                .FirstAsync(s => s.Id == Id);
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 获取首页数据
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<List<DeviceEntity>> All()
 | 
						|
        {
 | 
						|
            var result = await _db.Queryable<DeviceEntity>()
 | 
						|
               .Where(s => s.IsDeleted == false)
 | 
						|
               .ToListAsync();
 | 
						|
            return result;
 | 
						|
        }
 | 
						|
 | 
						|
        private Task AddDevice2Manager(DeviceEntity entity)
 | 
						|
        {
 | 
						|
            var device = new Device.Device()
 | 
						|
            {
 | 
						|
                Id = entity.Id,
 | 
						|
                Port = entity.Port,
 | 
						|
                GraphQlEndpoint = entity.GraphQlEndpoint,
 | 
						|
                Topic = entity.Topic,
 | 
						|
                IpAddress = entity.ip,
 | 
						|
                Protocol = entity.Protocol,
 | 
						|
                username = entity.account,
 | 
						|
                password = entity.password,
 | 
						|
                DataReceived = async (data) =>
 | 
						|
                {
 | 
						|
                    try
 | 
						|
                    {
 | 
						|
                        var input = JsonConvert.DeserializeObject<RevData>(data);
 | 
						|
                        //  Console.WriteLine($"rev data:{data}");
 | 
						|
                        await _alarmService.AddAlarm(input);
 | 
						|
                    }
 | 
						|
                    catch (Exception ex)
 | 
						|
                    {
 | 
						|
                        Console.WriteLine($"error:{ex.Message},data:{data}");
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            };
 | 
						|
            deviceManager.AddDevice(device);
 | 
						|
            return Task.CompletedTask;
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 初始化,加载所有设备
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task Init()
 | 
						|
        {
 | 
						|
            var devices = await _db.Queryable<DeviceEntity>().Where(s => s.IsDeleted == false).ToListAsync();
 | 
						|
            foreach (var item in devices)
 | 
						|
            {
 | 
						|
                var key = RedisKeyList.DeviceInfo(item.DeviceSN);
 | 
						|
                await _redisService.SetAsync(key, item, TimeSpan.FromDays(100));
 | 
						|
                await AddDevice2Manager(item);
 | 
						|
            }
 | 
						|
            // 加载阵地信息
 | 
						|
            var position = await _db.Queryable<PositionInfo>()
 | 
						|
                .Where(s => s.IsDeleted == false).ToListAsync();
 | 
						|
            foreach (var item in position)
 | 
						|
            {
 | 
						|
                var key = RedisKeyList.PositioinRegion(item.Id);
 | 
						|
                await _redisService.SetAsync(key, item.Region);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |