private async Task ValidateTriggerParametersAsync(TriggerParameters parameters) { if (parameters.Action == ModifyAction.Add) { var data = await GetNotificationTriggerDataAsync(parameters.ObjectId).ConfigureAwait(false); if (!data.SupportedTypes.Contains(parameters.Type)) { throw new InvalidTriggerTypeException(parameters.ObjectId, parameters.Type, data.SupportedTypes.ToList()); } } var channel = RequestParser.GetTriggerChannel(parameters); if (channel == null) { return; } var sensor = await GetSensorsAsync(Property.Id, parameters.ObjectId).ConfigureAwait(false); if (sensor.Count > 0) //Validate this sensor has this channel { if (channel.channel is StandardTriggerChannel) { throw new InvalidOperationException($"Channel '{channel}' is not a valid value for sensor with ID {parameters.ObjectId}. Triggers assigned directly to sensors must refer to a specific Channel or Channel ID."); } bool anyResponse = false; if (channel.channel is Channel) { anyResponse = (await GetChannelsInternalAsync(parameters.ObjectId, n => n == ((Channel)channel.channel).Name, i => i == ((Channel)channel.channel).Id).ConfigureAwait(false)).Any(); } else { anyResponse = (await GetChannelPropertiesAsync(parameters.ObjectId, Convert.ToInt32(((ISerializable)channel).GetSerializedFormat())).ConfigureAwait(false)).Descendants().Any(); } if (!anyResponse) { throw new InvalidOperationException($"Channel {(channel.channel is int ? "ID " : "")}'{channel}' is not a valid channel for sensor with ID {parameters.ObjectId}. Channel could not be found."); } } else //It's a container. Only enum values are permitted. { if (!(channel.channel is StandardTriggerChannel)) { var prefix = channel.channel is int?$"Channel ID '{channel}'" : $"Channel '{channel.channel}' of type '{channel.channel.GetType().Name}'"; throw new InvalidOperationException($"{prefix} is not a valid channel for Device, Group or Probe with ID {parameters.ObjectId}. Channel must be one of 'Primary', 'Total', 'TrafficIn' or 'TrafficOut' of type '{nameof(StandardTriggerChannel)}'."); //todo: make this dynamically get all names in the enum } } }
//###################################### // ValidateTriggerParameters //###################################### private void ValidateTriggerParameters(TriggerParameters parameters) { if (parameters.Action == ModifyAction.Add) { var data = GetNotificationTriggerData(parameters.ObjectId); if (!data.SupportedTypes.Contains(parameters.Type)) { throw new InvalidTriggerTypeException(parameters.ObjectId, parameters.Type, data.SupportedTypes.ToList()); } } var channel = RequestParser.GetTriggerChannel(parameters); if (channel == null) { return; } var sensor = GetSensors(Property.Id, parameters.ObjectId); if (sensor.Count > 0) //Validate this sensor has this channel { if (channel.channel is StandardTriggerChannel) { throw new InvalidOperationException($"Channel '{channel}' is not a valid value for sensor with ID {parameters.ObjectId}. Triggers assigned directly to sensors must refer to a specific Channel or Channel ID."); } bool anyResponse = false; if (channel.channel is Channel) { anyResponse = (GetChannels(parameters.ObjectId, ((Channel)channel.channel).Name)).Any(); } else { anyResponse = (GetChannelProperties(parameters.ObjectId, Convert.ToInt32(((IFormattable)channel).GetSerializedFormat()))).Descendants().Any(); } if (!anyResponse) { throw new InvalidOperationException($"Channel '{channel}' is not a valid value for sensor ID {parameters.ObjectId}. Channel could not be found."); } } else //It's a container. Only enum values are permitted. { if (!(channel.channel is StandardTriggerChannel)) { throw new InvalidOperationException($"Channel '{channel}' is not a valid value for Device, Group or Probe with ID {parameters.ObjectId}. Channel must be one of 'Primary', 'Total', 'TrafficIn' or 'TrafficOut'"); //todo: make this dynamically get all names in the enum } } }