Exemplo n.º 1
0
        public async Task <DetectorModel> Create(AuthorizedDto <DetectorDto> dto)
        {
            return(await Execute(async() => {
                using (UnitOfWork db = new UnitOfWork())
                {
                    DetectorEntity detectorEntity = dto.Data.ToModel <DetectorModel>().ToEntity <DetectorEntity>();
                    DetectorPrefabEntity detectorPrefabEntity = await db.GetRepo <DetectorPrefabEntity>().Get(detectorEntity.detector_prefab_id);

                    detectorEntity.DetectorSettingsValues = null;

                    detectorEntity.DetectorFaultPrefabs = detectorEntity.DetectorFaultPrefabs.Select(
                        faultPrefab => detectorPrefabEntity.DetectorFaultPrefabs.FirstOrDefault(fp => fp.id == faultPrefab.id)
                        ).Where(fp => fp != null).ToList();

                    await db.GetRepo <DetectorEntity>().Create(detectorEntity);
                    await db.Save();

                    RoleEntity ownerRole = await RoleService.GetCompanyOwnerRole(detectorPrefabEntity.company_id, db);
                    ownerRole.DetectorPermissions.Add(detectorEntity);

                    RoleEntity creatorRole = await RoleService.GetCompanyWorkerRole(dto.Session.UserId, db);
                    creatorRole?.DetectorPermissions.Add(detectorEntity);

                    await db.Save();

                    return detectorEntity.ToModel <DetectorModel>();
                }
            }));
        }
Exemplo n.º 2
0
        public async Task <DetectorDataModel> GetData(AuthorizedDto <GetDetectorDataDto> dto)
        {
            return(await Execute(async() =>
            {
                using (UnitOfWork db = new UnitOfWork())
                {
                    DetectorEntity detector = await db.GetRepo <DetectorEntity>().FirstOrDefault(
                        d => d.id == dto.Data.DetectorId.Value
                        );

                    DetectorDataEntity[] datas = detector.DetectorDatas.Where(
                        data => data.timespan >= dto.Data.DateSince && data.timespan <= dto.Data.DateUntil
                        ).ToArray();

                    return new DetectorDataModel()
                    {
                        Detector = detector.ToModel <DetectorModel>(),
                        Data = ModelEntityMapper.Mapper.Map <ICollection <DetectorDataItemModel> >(datas)
                    };
                }
            }));
        }
Exemplo n.º 3
0
        public async Task <DetectorModel> SetupSettings(AuthorizedDto <DetectorDto> dto)
        {
            return(await Execute(async() => {
                using (UnitOfWork db = new UnitOfWork())
                {
                    IRepo <DetectorEntity> detectorRepo = db.GetRepo <DetectorEntity>();

                    DetectorEntity detectorEntity = await detectorRepo.FirstOrDefault(pi => pi.id == dto.Data.Id.Value);

                    DetectorInteractionEventEntity detectorEvent = new DetectorInteractionEventEntity()
                    {
                        account_id = dto.Session.UserId,
                        detector_id = detectorEntity.id,
                        timespan = DateTime.Now,
                        log = $"Settings setup for Detector #{detectorEntity.id} by {dto.Session.UserId}"
                    };

                    await db.GetRepo <DetectorInteractionEventEntity>().Create(detectorEvent);

                    foreach (DetectorSettingsValueDto settingsValueDto in dto.Data.SettingsValues)
                    {
                        if (settingsValueDto.Id.HasValue)
                        {
                            DetectorSettingsValueEntity settingsValueEntity = detectorEntity.DetectorSettingsValues.First(
                                setting => setting.id == settingsValueDto.Id.Value
                                );

                            DataType dataType = settingsValueEntity.detector_settings_prefab.DataType.name.FromName();
                            DataTypeService.CheckIsDataOfType(settingsValueDto.ValueBase64, dataType);

                            settingsValueEntity.option_data_value_base64 = settingsValueDto.ValueBase64;
                        }
                        else
                        {
                            DetectorSettingsPrefabEntity detectorSettingsPrefab = detectorEntity.DetectorPrefab.DetectorSettingsPrefabs.FirstOrDefault(
                                settingsPrefab => settingsPrefab.id == settingsValueDto.PrefabId.Value
                                );

                            if (detectorSettingsPrefab == null)
                            {
                                throw new NotFoundException("DetectorSettingsPrefab");
                            }

                            DataType dataType = detectorSettingsPrefab.DataType.name.FromName();
                            DataTypeService.CheckIsDataOfType(settingsValueDto.ValueBase64, dataType);

                            DetectorSettingsValueEntity settingWithSameSettingPrefab = detectorEntity.DetectorSettingsValues.FirstOrDefault(
                                setting => setting.detector_settings_prefab_id == settingsValueDto.PrefabId.Value
                                );

                            if (settingWithSameSettingPrefab != null)
                            {
                                settingWithSameSettingPrefab.option_data_value_base64 = settingsValueDto.ValueBase64;
                            }
                            else
                            {
                                DetectorSettingsValueEntity settingsValueEntity = new DetectorSettingsValueEntity()
                                {
                                    detector_id = detectorEntity.id,
                                    option_data_value_base64 = settingsValueDto.ValueBase64,
                                    detector_settings_prefab_id = settingsValueDto.PrefabId.Value
                                };

                                detectorEntity.DetectorSettingsValues.Add(settingsValueEntity);
                            }
                        }
                    }

                    await db.Save();
                    return detectorEntity.ToModel <DetectorModel>();
                }
            }));
        }