Example #1
0
        public TelemetryPublisherFirestore(TelemetryPublisherBuilderOptions opts) : base(opts)
        {
            var options = ConnectionStringParser.Parse(opts.ConnectionString);

            if (!options.TryGetValue("ProjectId", out var firestoreProjectId))
            {
                Logger?.LogCritical(
                    $"Can't start {nameof(TelemetryPublisherFirestore)}! Malformed connection string! " +
                    $"Missing ProjectId!");
            }

            if (!options.TryGetValue("CollectionName", out var firestoreCollection))
            {
                Logger?.LogCritical(
                    $"Can't start {nameof(TelemetryPublisherFirestore)}! Malformed connection string! " +
                    $"Missing CollectionName!");
            }

            _timeout = int.Parse(options.GetValueOrDefault("Timeout", "10000"));

            var requestUrl = $"https://firestore.googleapis.com/v1/projects/{firestoreProjectId}/" +
                             $"databases/(default)/documents/{firestoreCollection}/";

            _httpClient             = new HttpClient();
            _httpClient.BaseAddress = new Uri(requestUrl);
            Logger?.LogInformation($"Initialized {nameof(TelemetryPublisherFirestore)}");
        }
Example #2
0
 /// <summary>
 /// Constructor for <see cref="TelemetryPublisher"/>.
 /// </summary>
 /// <param name="opts">TelemetryPublisher options, see: <see cref="TelemetryPublisherBuilderOptions"/></param>
 protected TelemetryPublisher(TelemetryPublisherBuilderOptions opts)
 {
     ConnectionString     = opts.ConnectionString;
     TelemetrySource      = opts.TelemetrySource;
     Logger               = opts.Logger;
     RegisteredTelemeters = new List <ITelemeter>(5);
 }
 private static TelemetryPublisher SpawnPublisher(string type, TelemetryPublisherBuilderOptions opts)
 {
     return(type switch
     {
         TelemetryPublisherType.Azure => (TelemetryPublisher) new TelemetryPublisherAzure(opts),
         TelemetryPublisherType.Disk => new TelemetryPublisherDisk(opts),
         TelemetryPublisherType.Firestore => new TelemetryPublisherFirestore(opts),
         _ => throw new ArgumentException($"Invalid TelemetryPublisher type: {type}.")
     });
        /// <summary>
        /// Creates an instance of <see cref="TelemetryPublisher"/>.
        /// </summary>
        /// <param name="type">The type of the publisher. See <see cref="TelemetryPublisherType"/> </param>
        /// <param name="connectionString">The device connection string for the selected publisher.</param>
        /// <returns>A <see cref="TelemetryPublisher"/> instance.</returns>
        public static TelemetryPublisher CreateFromConnectionString(string type, string connectionString)
        {
            Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString);
            var opts = new TelemetryPublisherBuilderOptions()
            {
                ConnectionString = connectionString, TelemetrySource = "TelemetryPublisherAzure"
            };

            return(SpawnPublisher(type, opts));
        }
        /// <summary>
        /// Creates an instance of <see cref="TelemetryPublisher"/>. See <see cref="TelemetryPublisherType"/>
        /// </summary>
        /// <param name="type">The type of the publisher. <see cref="TelemetryPublisherType"/> </param>
        /// <param name="connectionString">Device connection string for Microsoft Azure IoT hub device.</param>
        /// <param name="telemetrySource">String that is used to identify the source of the telemetry data.</param>
        /// <param name="logger">An <see cref="ILogger"/> logger instance. </param>
        /// <returns>A <see cref="TelemetryPublisher"/> instance.</returns>
        public static TelemetryPublisher Create(string type, string connectionString,
                                                string telemetrySource, ILogger logger)
        {
            Guard.ArgumentNotNullOrWhiteSpace(nameof(connectionString), connectionString);
            Guard.ArgumentNotNullOrWhiteSpace(nameof(telemetrySource), telemetrySource);
            Guard.ArgumentNotNull(nameof(logger), logger);
            var opts = new TelemetryPublisherBuilderOptions()
            {
                ConnectionString = connectionString, TelemetrySource = telemetrySource, Logger = logger
            };

            return(SpawnPublisher(type, opts));
        }
Example #6
0
        /// <summary>
        /// Constructs an instance of <see cref="TelemetryPublisherDisk"/>.
        /// <remarks>
        ///    The connection string must contain the following options:
        ///     Filename (optional) - The path of the filename in which to log telemetry data.
        ///     FileExtension (optional) - The extension of the filename. Default csv
        ///     Separator (optional) - The separator of the messages. Default ,
        ///     BufferSize (optional) - The buffer size for the async writer. Default: 4096
        /// </remarks>
        /// </summary>
        /// <param name="opts"></param>
        public TelemetryPublisherDisk(TelemetryPublisherBuilderOptions opts) : base(opts)
        {
            var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString);
            var fileName      = connectionStringParams.GetValueOrDefault("FileName", "telemetry");
            var fileExtension = connectionStringParams.GetValueOrDefault("FileExtension", "csv");
            var bufferSize    = connectionStringParams.GetValueOrDefault("BufferSize", "4096");

            _separator = connectionStringParams.GetValueOrDefault("Separator", ",");

            _fileStream = new FileStream(NormalizeFilename(fileName, fileExtension), FileMode.Append, FileAccess.Write,
                                         FileShare.Read, int.Parse(bufferSize), true);
            Logger?.LogDebug("Initialized the TelemetryPublisherDisk!");
        }
        public TelemetryPublisherAzure(TelemetryPublisherBuilderOptions opts) : base(opts)
        {
            try
            {
                DeviceClient = DeviceClient.CreateFromConnectionString(ConnectionString, TransportType.Mqtt);
            }
            catch (FormatException)
            {
                Logger?.LogCritical("Can't start telemetry service! Malformed connection string!");
                throw;
            }

            Logger?.LogDebug("Initialized the AzureTelemetryPublisher!");
        }