public async Task <Queries.DeviceModel> Handle(CreateDeviceCommand request, CancellationToken cancellationToken)
        {
            var deviceTypeInfo = await _deviceTypeRepository.FindAsync(request.DeviceTypeCode, request.ModelCode);

            if (deviceTypeInfo == null)
            {
                throw new DeviceDomainException("不支持的设备类型");
            }



            if (await _deviceRepository.ExistsByEquipNumAsync(request.EquipNum))
            {
                throw new DeviceDomainException($"编号为{request.EquipNum}的设备已存在!");
            }

            var entity = CreateDevice(request, deviceTypeInfo);

            _deviceRepository.Add(entity);
            var result = await _deviceRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            if (!result)
            {
                return(null);
            }
            return(_mapper.Map <TerminalDevice>(entity));
        }
Beispiel #2
0
        public async Task Handle(DeviceAlarmStatusChangeDomainEvent notification, CancellationToken cancellationToken)
        {
            if (notification.Device is Terminal)
            {
                var    terminal     = notification.Device as Terminal;//设备详情
                var    arr          = notification.TargetCode.Split('_');
                string codeNum      = arr[arr.Length - 1];
                var    alarmSetting = terminal.DeviceAlarms.FirstOrDefault(where => where.AlarmCode == notification.AlarmCode);//警报状态及配置
                if (nameof(Domain.AggregatesModel.DeviceAggregate.Camera).Equals(arr[0], StringComparison.InvariantCultureIgnoreCase))
                {
                    var camera = terminal.Loads.FirstOrDefault(where => where.EquipNum.EndsWith(notification.TargetCode));
                    var status = int.Parse(alarmSetting.Status);
                    camera.SetConnetion(status == 0 ? 1 : 0);
                }
                else if (nameof(Domain.AggregatesModel.DeviceAggregate.Sensor).Equals(arr[0], StringComparison.InvariantCultureIgnoreCase))
                {
                    terminal.SetSensorAlarmStatus(notification.TargetCode, alarmSetting.Status);
                }
                else if (nameof(Domain.AggregatesModel.DeviceAggregate.Part).Equals(arr[0], StringComparison.InvariantCultureIgnoreCase))
                {
                    terminal.SetPartAlarmStatus(notification.TargetCode, alarmSetting.Status);
                }
                var    items   = terminal.ModelCode.Split('-');
                var    version = items[items.Length - 1];
                string alarmId = Guid.NewGuid().ToString();
                var    region  = await _regionRepository.GetAsync(terminal.RegionId);

                var type = await _deviceTypeRepository.FindAsync(terminal.DeviceTypeCode, version);

                var alarmInfo = type.Alarms.FirstOrDefault(where => where.AlarmCode == alarmSetting.AlarmCode);

                if (alarmSetting.NormalValue.Contains(alarmSetting.Status))
                {
                    await _deviceIntegrationEventService.AddAndSaveEventAsync(new TerminalClearAlarmIntegrationEvent(terminal.Id, terminal.DeviceName, terminal.DeviceTypeCode, terminal.ModelCode, terminal.EquipNum, alarmSetting.TargetCode, alarmSetting.AlarmCode, terminal.Connection == 0 ? 1 : 0, GetAlarmDescription(alarmSetting, alarmInfo, region, terminal)));//解除警报
                }
                else
                {
                    var tentant = await _userRepository.GetAccountAsync(terminal.TentantId);

                    var sensor = terminal.Sensors.FirstOrDefault(where => where.SensorCode == alarmSetting.TargetCode);
                    await _deviceIntegrationEventService.AddAndSaveEventAsync(new TerminalRaiseAlarmIntegrationEvent
                                                                              (terminal.Id, terminal.DeviceName, terminal.DeviceTypeCode, terminal.ModelCode, terminal.EquipNum, terminal.RegionId, region?.RegionCode, region?.RegionName, terminal.TentantId, tentant?.Name, terminal.ParentId, terminal.Location.Latitude, terminal.Location.Longitude,
                                                                               new Alarm(alarmId, alarmSetting.AlarmCode, alarmInfo.AlarmName, alarmInfo.GroupName, alarmInfo.AlarmType, (int)alarmInfo.AlarmFrom, alarmSetting.TargetCode ?? alarmInfo.TargetCode, alarmInfo.AlarmLevel, alarmSetting.Status, GetAlarmDescription(alarmSetting, alarmInfo, region, terminal), alarmSetting.RepairTime ?? alarmInfo.RepairTime, DateTime.UtcNow, alarmInfo.IsStatistics, sensor?.RealValue, sensor?.UpperValue, sensor?.LowerValue)
                                                                              ));//发生警报
                }
            }
            //todo:补全内容

            //TODO:推送警报状态给前端页面(在其他服务中订阅并执行)

            //TODO:发送短信、邮件等其他操作(在其他服务中订阅并执行)

            //TODO:触发分布式消息(在其他服务中订阅并执行)
        }