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. Free data files can be acquired by
pulling the submodules under this repository or from the
device-detection-data
GitHub repository.
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
{
public void Run(string dataFile, string licenseKey)
{
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;
}
#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
}
}
}