-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExamples.java
More file actions
93 lines (72 loc) · 3.25 KB
/
Examples.java
File metadata and controls
93 lines (72 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* (c) Copyright 2012 EVRYTHNG Ltd London / Zurich
* www.evrythng.net
*
*/
package net.evrythng.thng.api.examples;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Calendar;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.evrythng.thng.api.model.Thng;
import net.evrythng.thng.api.model.Property;
import net.evrythng.thng.api.model.ThngCollection;
import net.evrythng.thng.api.wrapper.ThngAPIWrapper;
import org.apache.http.client.ClientProtocolException;
/**
* This exemplifies how the wrapper/API can be used.
* @author <href="http://www.guinard.org>domguinard</a>
*/
public class Examples {
private static String TOKEN = "YOUR-KEY-HERE!";
private Examples() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
// Intialize the wrapper (and provinding the API key)
ThngAPIWrapper wrapper = new ThngAPIWrapper(TOKEN);
// Create a Thng
Thng myThng = wrapper.createThng(new Thng("My-TV." +
Calendar.getInstance().getTimeInMillis(), "This is my TV.",
true, 10.88, 50.56));
// A Thng is uniquely identified by its auto-generated ID:
System.out.println(myThng.getId());
// Get this Thng again given its ID:
myThng = wrapper.getThng(myThng.getId());
System.out.println(myThng.getDescription());
// Add some properties to our Thng
Property<String> kwh = new Property<String>("KWh", "64");
wrapper.createProperties(myThng,
new Property<String>("Address", "Gasometerstrasse 9"),
kwh);
wrapper.getProperties(myThng);
// ... update them...
kwh.setValue("65");
wrapper.updateProperty(myThng, kwh);
// ... or get them!
kwh = wrapper.getProperty(myThng, kwh.getKey());
System.out.println(kwh.getKey() + ":" + kwh.getValue());
// Add the thng to a collection
ThngCollection homeCollection = wrapper.createCollection(new ThngCollection("Home" + Calendar.getInstance().getTimeInMillis(), "The things in my home."));
wrapper.addThngsToCollection(homeCollection, myThng);
// Search for our thngs
Collection<Thng> results = wrapper.search("tv");
System.out.println(results.toArray()[0].toString());
} catch (InstantiationException ex) {
Logger.getLogger(Examples.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Examples.class.getName()).log(Level.SEVERE, null, ex);
} catch (URISyntaxException ex) {
Logger.getLogger(Examples.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClientProtocolException ex) {
Logger.getLogger(Examples.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Examples.class.getName()).log(Level.SEVERE, null, ex);
}
}
}