Esempio n. 1
0
        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");
        }
Esempio n. 2
0
        /// <summary>
        /// Loads configuration from the appsettings.json file and sets up configured OMF endpoints
        /// </summary>
        public static void LoadConfiguration()
        {
            Settings = JsonConvert.DeserializeObject <AppSettings>(File.ReadAllText(Directory.GetCurrentDirectory() + "\\appsettings.json"));

            OmfServices = new OmfServices();

            if (Settings.SendToOcs)
            {
                OmfServices.ConfigureOcsOmfIngress(Settings.OcsUri, Settings.OcsTenantId, Settings.OcsNamespaceId, Settings.OcsClientId, Settings.OcsClientSecret);
            }

            if (Settings.SendToEds)
            {
                OmfServices.ConfigureEdsOmfIngress(Settings.EdsPort);
            }

            if (Settings.SendToPi)
            {
                OmfServices.ConfigurePiOmfIngress(Settings.PiWebApiUri, Settings.Username, Settings.Password, Settings.ValidateEndpointCertificate);
            }

            // Send OMF Type Message
            OmfServices.SendOmfType(typeof(BartStationEtd));

            // Send OMF Container Message
            var data   = BartApi.GetRealTimeEstimates(Settings.BartApiKey, Settings.BartApiOrig, Settings.BartApiDest);
            var typeId = ClrToOmfTypeConverter.Convert(typeof(BartStationEtd)).Id;

            OmfServices.SendOmfContainersForData(data, typeId);
        }