This example shows how to use the 51Degrees Cloud service to lookup
the details of a device based on a given 'native model name'.
Native model name is a string of characters that are returned from
a query to the device's OS. For more details on this, see the example below.
Make sure to include the HardwareVendor, HardwareModel and HardwareName properties as they are used by this example.
Create a cloud request engine. This will make the HTTP calls to the 51Degrees cloud service. Add your resource key here.
Create the 'hardware-profile' cloud engine. This will expose the response from received by the cloud request engine in a more user-friendly format.
After creating a flowdata instance, add the native model name as evidence.
The result is an array containing the details of any devices that match the specified native model name. The code in this example iterates through this array, outputting the vendor and model of each matching device.
package fiftyone.devicedetection.examples.cloud;
import fiftyone.devicedetection.cloud.data.MultiDeviceDataCloud;
import fiftyone.devicedetection.cloud.flowelements.HardwareProfileCloudEngine;
import fiftyone.devicedetection.cloud.flowelements.HardwareProfileCloudEngineBuilder;
import fiftyone.devicedetection.examples.ExampleBase;
import fiftyone.devicedetection.examples.ProgramBase;
import fiftyone.devicedetection.shared.DeviceData;
import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngine;
import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngineBuilder;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.core.flowelements.PipelineBuilder;
import fiftyone.pipeline.engines.data.AspectPropertyValue;
import fiftyone.pipeline.engines.services.HttpClient;
import fiftyone.pipeline.engines.services.HttpClientDefault;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import java.util.List;
import static fiftyone.devicedetection.shared.Constants.EVIDENCE_QUERY_NATIVE_MODEL_KEY;
import static fiftyone.pipeline.util.StringManipulation.stringJoin;
public class NativeModelLookup extends ProgramBase {
public static void main(String[] args) throws Exception {
String resourceKey = args.length > 0 ? args[0] :
"!!YOUR_RESOURCE_KEY!!";
new NativeModelLookup.Example(true).run(resourceKey);
System.out.println("Complete. Press enter to exit.");
System.in.read();
}
public static class Example extends ExampleBase {
private static final String nativemodel1 = "SC-03L";
private static final String nativemodel2 = "iPhone11,8";
public Example(boolean printOutput) {
super(printOutput);
}
public void run(String resourceKey) throws Exception {
if (resourceKey.startsWith("!!")) {
println("You need to create a resource key at " +
"https://configure.51degrees.com and paste it into this example.");
println("Make sure to include the 'HardwareVendor', " +
"'HardwareName' and 'HardwareModel' properties as they " +
"are used by this example.");
}
else {
println("This example shows the details of devices " +
"associated with a given 'native model name'.");
println("The native model name can be retrieved by " +
"code running on the device (For example, a mobile app).");
println("For Android devices, see " +
"https://developer.android.com/reference/android/os/Build#MODEL");
println("For iOS devices, see " +
"https://gist.github.com/soapyigu/c99e1f45553070726f14c1bb0a54053b#file-machinename-swift");
println("----------------------------------------");
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
HttpClient httpClient = new HttpClientDefault();
try (CloudRequestEngine cloudEngine =
new CloudRequestEngineBuilder(loggerFactory, httpClient)
.setResourceKey(resourceKey)
.build();
HardwareProfileCloudEngine hardwareProfileEngine =
new HardwareProfileCloudEngineBuilder(loggerFactory)
.build();
Pipeline pipeline = new PipelineBuilder(loggerFactory)
.addFlowElement(cloudEngine)
.addFlowElement(hardwareProfileEngine)
.build()) {
analyseNativeModel(nativemodel1, pipeline);
analyseNativeModel(nativemodel2, pipeline);
}
}
}
void analyseNativeModel(String nativeModel, Pipeline pipeline) throws Exception {
try (FlowData data = pipeline.createFlowData()) {
data.addEvidence(EVIDENCE_QUERY_NATIVE_MODEL_KEY, nativeModel);
data.process();
MultiDeviceDataCloud result = data.get(MultiDeviceDataCloud.class);
printf("Which devices are associated with the " +
"native model name '%s'?\n", nativeModel);
for (DeviceData device : result.getProfiles()) {
AspectPropertyValue<String> vendor = device.getHardwareVendor();
AspectPropertyValue<List<String>> name = device.getHardwareName();
AspectPropertyValue<String> model = device.getHardwareModel();
if (vendor.hasValue() &&
model.hasValue() &&
name.hasValue()) {
println("\t" + vendor.getValue() +
" " + stringJoin(name.getValue(), ",") +
" (" + model.getValue() + ")");
} else {
println(vendor.getNoValueMessage());
}
}
}
}
}
}