public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
        {
            _log = log;
            LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            LoadConfiguration();

            if (string.IsNullOrEmpty(Settings.OpenWeatherKey))
            {
                LogInformation("No OpenWeather API Key provided, function will generate random data");
            }

            // Set up OMF Ingress Service
            _omfIngressService = ConfigureOcsOmf(Settings.OcsUri, Settings.OcsTenantId, Settings.OcsNamespaceId, Settings.OcsClientId, Settings.OcsClientSecret);

            // Send OMF Type message
            SendOmfMessage(_omfIngressService, OmfMessageCreator.CreateTypeMessage(typeof(CurrentWeather)));

            // Prepare OMF containers
            var typeId     = ClrToOmfTypeConverter.Convert(typeof(CurrentWeather)).Id;
            var containers = new List <OmfContainer>();
            var data       = new List <OmfDataContainer>();

            var queries = Settings.OpenWeatherQueries.Split('|');

            foreach (var query in queries)
            {
                if (!string.IsNullOrEmpty(Settings.OpenWeatherKey))
                {
                    // Get Current Weather Data
                    var response = JsonConvert.DeserializeObject <JObject>(HttpGet($"{Settings.OpenWeatherUri}?q={query}&appid={Settings.OpenWeatherKey}"));

                    // Parse data into OMF messages
                    var value    = new CurrentWeather(response);
                    var streamId = $"OpenWeather_Current_{value.Name}";
                    containers.Add(new OmfContainer(streamId, typeId));
                    var omfValue = (OmfObjectValue)ClrToOmfValueConverter.Convert(value);
                    data.Add(new OmfDataContainer(streamId, new List <OmfObjectValue>()
                    {
                        omfValue
                    }));
                }
                else
                {
                    // No key provided, generate random data
                    containers.Add(new OmfContainer(query, typeId));
                    var value    = new CurrentWeather(query);
                    var omfValue = (OmfObjectValue)ClrToOmfValueConverter.Convert(value);
                    data.Add(new OmfDataContainer(query, new List <OmfObjectValue>()
                    {
                        omfValue
                    }));
                }
            }

            SendOmfMessage(_omfIngressService, new OmfContainerMessage(containers));
            LogInformation($"Sent {containers.Count} containers");
            SendOmfMessage(_omfIngressService, new OmfDataMessage(data));
            LogInformation($"Sent {data.Count} data messages");
        }
Beispiel #2
0
        public Device(string address, string tenantId, string namespaceId, string clientId, string clientSecret)
        {
            // Create the AuthenticationHandler and IngressSerice to use to send data
            AuthenticationHandler deviceAuthenticationHandler = new AuthenticationHandler(new Uri(address), clientId, clientSecret);

            OmfIngressService deviceBaseOmfIngressService = new OmfIngressService(new Uri(address), null, HttpCompressionMethod.None, deviceAuthenticationHandler);

            _deviceOmfIngressService = deviceBaseOmfIngressService.GetOmfIngressService(tenantId, namespaceId);
        }
        public OmfIngressClient(string address, string tenantId, string namespaceId, string clientId, string clientSecret)
        {
            // Get Ingress Services to communicate with server and handle ingress management
            AuthenticationHandler authenticationHandler = new AuthenticationHandler(new Uri(address), clientId, clientSecret);

            _tenantId    = tenantId;
            _namespaceId = namespaceId;
            OmfIngressService baseOmfIngressService = new OmfIngressService(new Uri(address), null, HttpCompressionMethod.None, authenticationHandler);

            _omfIngressService = baseOmfIngressService.GetOmfIngressService(tenantId, namespaceId);
        }
        /// <summary>
        /// Sends a message to the OCS OMF endpoint
        /// </summary>
        private static object SendOmfMessage(IOmfIngressService service, OmfMessage omfMessage)
        {
            var serializedOmfMessage = OmfMessageSerializer.Serialize(omfMessage);

            return(service.SendOmfMessageAsync(serializedOmfMessage).Result);
        }