/// <summary> /// Create api model /// </summary> /// <param name="model"></param> /// <returns></returns> public static GatewayModulesApiModel ToApiModel( this GatewayModulesModel model) { if (model == null) { return(null); } return(new GatewayModulesApiModel { Publisher = model.Publisher.ToApiModel(), Supervisor = model.Supervisor.ToApiModel(), Discoverer = model.Discoverer.ToApiModel() }); }
/// <inheritdoc/> public async Task <GatewayInfoModel> GetGatewayAsync(string gatewayId, bool onlyServerState, CancellationToken ct) { if (string.IsNullOrEmpty(gatewayId)) { throw new ArgumentException(nameof(gatewayId)); } var deviceId = gatewayId; var device = await _iothub.GetAsync(deviceId, null, ct); var registration = device.ToEntityRegistration() as GatewayRegistration; if (registration == null) { throw new ResourceNotFoundException( $"{gatewayId} is not a gateway registration."); } var modules = await _iothub.QueryAllDeviceTwinsAsync( $"SELECT * FROM devices.modules WHERE deviceId = '{device.Id}'", ct); var gatewayModules = new GatewayModulesModel(); foreach (var module in modules) { var entity = module.ToEntityRegistration(onlyServerState); switch (entity) { case SupervisorRegistration sr: gatewayModules.Supervisor = sr.ToServiceModel(); break; case PublisherRegistration pr: gatewayModules.Publisher = pr.ToServiceModel(); break; case DiscovererRegistration dr: gatewayModules.Discoverer = dr.ToServiceModel(); break; default: // might add module to dictionary in the future break; } } return(new GatewayInfoModel { Gateway = registration.ToServiceModel(), Modules = gatewayModules }); }