Automatic updates example for 51Degrees device detection ‘Hash’ engine
shows how to build a pipeline using the 51Degrees device detection pipeline builder
and demonstrates how to configure the update polling interval.
The automatic data file updates
page explains the different options you can configure.
This example requires a subscription to 51Degrees Device Data, a subscription can be acquired from the 51Degrees pricing page.
This example requires a local data file. Free data files can be acquired by
pulling the submodules under this repository or from the
device-detection-data
GitHub repository.
using FiftyOne.Pipeline.Core.FlowElements;
using FiftyOne.Pipeline.Engines.Services;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
{
public class Program
{
public class Example : ExampleBase
{
private IPipeline pipeline;
private int updatePollingInterval = 30;
private int pollingIntervalRandomisation = 10;
private EventWaitHandle ewh;
private string dataFile = "51Degrees.hash";
public void Run(string originalDataFile, string licenseKey)
{
File.Copy(originalDataFile, dataFile, true);
ewh = new EventWaitHandle(false, EventResetMode.AutoReset);
FileInfo f = new FileInfo(dataFile);
Console.WriteLine($"Using data file at '{f.FullName}'");
var loggerFactory = new LoggerFactory();
var httpClient = new HttpClient();
var dataUpdateService = new DataUpdateService(loggerFactory.CreateLogger<DataUpdateService>(), httpClient);
dataUpdateService.CheckForUpdateComplete += LogPublishedDate;
dataUpdateService.CheckForUpdateStarted += LogPublishedDate;
pipeline = new DeviceDetectionPipelineBuilder(loggerFactory, httpClient, dataUpdateService)
.UseOnPremise(dataFile, licenseKey, true)
.SetAutoUpdate(true)
.SetDataFileSystemWatcher(true)
.SetDataUpdateOnStartUp(false)
.SetUpdatePollingInterval(30)
.SetUpdateRandomisationMax(10)
.Build();
var publishedDate = pipeline
.GetElement<DeviceDetectionHashEngine>()
.DataFiles
.Single()
.DataPublishedDateTime;
Console.WriteLine($"Initial data file published date: {publishedDate}");
Console.WriteLine($"The pipeline has now been set up to poll for updates every {updatePollingInterval} seconds, " +
$"a random ammount of time up to {pollingIntervalRandomisation} seconds will be added.");
ewh.WaitOne();
}
private void LogPublishedDate<T>(object sender, T e) where T : DataUpdateEventArgs
{
var publishedDate = pipeline
.GetElement<DeviceDetectionHashEngine>()
.DataFiles
.Single()
.DataPublishedDateTime;
if (e is DataUpdateCompleteArgs completeArgs)
{
Console.WriteLine($"Update completed. Status {completeArgs.Status}");
ewh.Set();
}
else
{
Console.WriteLine($"Update started for {e.DataFile.DataFilePath}");
}
Console.WriteLine($"Data file published date: {publishedDate}");
}
}
static void Main(string[] args)
{
var licenseKey = "!!Your license key!!";
if (licenseKey.StartsWith("!!"))
{
Console.WriteLine("You need a license key to run this example, " +
"you can obtain one by subscribing to a 51Degrees bundle: https://51degrees.com/pricing");
Console.ReadKey();
return;
}
#if NETCORE
var defaultDataFile = "..\\..\\..\\..\\..\\..\\..\\..\\..\\device-detection-cxx\\device-detection-data\\51Degrees-LiteV4.1.hash";
#else
var defaultDataFile = "..\\..\\..\\..\\..\\..\\..\\..\\device-detection-cxx\\device-detection-data\\51Degrees-LiteV4.1.hash";
#endif
var dataFile = args.Length > 0 ? args[0] : defaultDataFile;
new Example().Run(dataFile, licenseKey);
#if (DEBUG)
Console.WriteLine("Complete. Press key to exit.");
Console.ReadKey();
#endif
}
}
}