Automatic updates example for 51Degrees device detection ‘Hash’ engine
shows how to build a pipeline using the 51Degrees device detection pipeline builder
and demonstrates the update on start up functionality.
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.Engines.Services;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
{
public class Program
{
public class Example : ExampleBase
{
private string dataFile = "51Degrees.hash";
public void Run(string originalDataFile, string licenseKey)
{
File.Copy(originalDataFile, dataFile, true);
FileInfo f = new FileInfo(dataFile);
Console.WriteLine($"Using data file at '{f.FullName}'");
DateTime initialPublishedDate = DateTime.MinValue;
using (var loggerFactory = new LoggerFactory()) {
var dataUpdateService = new DataUpdateService(loggerFactory.CreateLogger<DataUpdateService>(), new HttpClient());
using (var temporaryDeviceDetectionEngine = new DeviceDetectionHashEngineBuilder(loggerFactory, dataUpdateService)
.Build(dataFile, false))
{
initialPublishedDate = temporaryDeviceDetectionEngine.DataFiles.Single().DataPublishedDateTime;
}
}
Console.WriteLine($"Data file published date: {initialPublishedDate}");
Console.Write($"Creating pipeline and updating device data");
CancellationTokenSource cancellationSource = new CancellationTokenSource();
Task.Run(() => { OutputUntilCancelled(".", 1000, cancellationSource.Token); });
var pipeline = new DeviceDetectionPipelineBuilder()
.UseOnPremise(dataFile, licenseKey, true)
.SetAutoUpdate(true)
.SetDataFileSystemWatcher(true)
.SetDataUpdateOnStartUp(true)
.Build();
cancellationSource.Cancel();
Console.WriteLine();
var updatedPublishedDate = pipeline
.GetElement<DeviceDetectionHashEngine>()
.DataFiles
.Single()
.DataPublishedDateTime;
if (DateTime.Equals(initialPublishedDate, updatedPublishedDate))
{
Console.WriteLine("There was no update available at this time.");
}
Console.WriteLine($"Data file published date: {updatedPublishedDate}");
}
}
private static void OutputUntilCancelled(string text, int intervalMs, CancellationToken token)
{
while (token.IsCancellationRequested == false)
{
Console.Write(text);
Task.Delay(intervalMs).Wait();
}
}
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
}
}
}