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. The free 'Lite' data file can be acquired by
pulling the git submodules under this repository (run `git submodule update --recursive`)
or from the device-detection-data
GitHub repository.
The Lite data file is only used for illustration, and has limited accuracy and capabilities.
Find out about the more capable data files that are available on our
pricing page
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(updatePollingInterval)
.SetUpdateRandomisationMax(pollingIntervalRandomisation)
.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 amount 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;
}
var filename = "51Degrees-LiteV4.1.hash";
var dataFile = args.Length > 0 ? args[0] : ExampleUtils.FindFile(filename);
new Example().Run(dataFile, licenseKey);
#if (DEBUG)
Console.WriteLine("Complete. Press key to exit.");
Console.ReadKey();
#endif
}
}
}