This example shows how to use the 51Degrees Cloud service to look up the details of a device based on a given 'TAC'. More background information on TACs can be found through various online sources such as Wikipedia.
Unlike other examples, use of this example requires a license key which can be purchased from our pricing page. Once this is done, a resource key with the properties required by this example can be created here.
package fiftyone.devicedetection.examples.console;
import fiftyone.devicedetection.cloud.data.MultiDeviceDataCloud;
import fiftyone.devicedetection.shared.Constants;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.devicedetection.shared.testhelpers.KeyUtils;
import fiftyone.pipeline.core.configuration.PipelineOptions;
import fiftyone.pipeline.core.configuration.PipelineOptionsFactory;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.engines.data.AspectPropertyValue;
import fiftyone.pipeline.engines.fiftyone.flowelements.FiftyOnePipelineBuilder;
import java.io.File;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import static fiftyone.common.testhelpers.LogbackHelper.configureLogback;
import static fiftyone.devicedetection.examples.shared.PropertyHelper.asString;
import static fiftyone.devicedetection.examples.shared.KeyHelper.*;
import static fiftyone.pipeline.util.FileFinder.getFilePath;
public class TacCloud {
static final String TAC_1 = "35925406";
static final String TAC_2 = "86386802";
public static final String TAC_EXAMPLE_RESOURCE_KEY_NAME = "SuperResourceKey";
public static void main (String[] args) throws Exception {
configureLogback(getFilePath("logback.xml"));
String resourceKey = args.length > 0 ? args[0] : null;
run(resourceKey, System.out);
}
public static void run(String resourceKey, OutputStream os) throws Exception {
File optionsFile = getFilePath("tacCloud.xml");
PipelineOptions pipelineOptions = PipelineOptionsFactory.getOptionsFromFile(optionsFile);
String fileResourceKey = pipelineOptions.find("CloudRequestEngine", "ResourceKey");
if (KeyUtils.isInvalidKey(fileResourceKey)) {
getOrSetSuperResourceKey(resourceKey, TAC_EXAMPLE_RESOURCE_KEY_NAME);
}
try (PrintWriter writer = new PrintWriter(os)) {
writer.println("This example shows the details of devices " +
"associated with a given 'Type Allocation Code' or 'TAC'.");
writer.println("More background information on TACs can be " +
"found through various online sources such as Wikipedia: " +
"https://en.wikipedia.org/wiki/Type_Allocation_Code");
writer.println("----------------------------------------");
try (Pipeline pipeline = new FiftyOnePipelineBuilder()
.buildFromConfiguration(pipelineOptions)) {
analyseTac(TAC_1, pipeline, writer);
analyseTac(TAC_2, pipeline, writer);
}
}
}
static void analyseTac(String tac, Pipeline pipeline, PrintWriter output) throws Exception {
try (FlowData data = pipeline.createFlowData()) {
data.addEvidence(Constants.EVIDENCE_QUERY_TAC_KEY, tac);
data.process();
MultiDeviceDataCloud result = data.get(MultiDeviceDataCloud.class);
output.format("Which devices are associated with the TAC '%s'?%n", tac);
for (DeviceData device : result.getProfiles()) {
AspectPropertyValue<String> vendor = device.getHardwareVendor();
AspectPropertyValue<List<String>> name = device.getHardwareName();
AspectPropertyValue<String> model = device.getHardwareModel();
output.format("\t%s; %s; %s%n", asString(vendor), asString(name), asString(model));
}
}
}
}