我们知道在Vantiq平台中的每一个资源都是一个service。Everything is a service。这句话的含义就是,我们可以通过REST接口来访问Vantiq平台的所有的资源,这包括创建,修改,删除这些资源。这些资源包括type(数据表格),topic,procedure的调用。我们可以通过Vantiq平台提供的JAVA SDK或Nodejs SDK来访问这些资源,甚至我们可以使用Micro Edition来在微处理器中来访问Vantiq平台所提供的资源。我们甚至提供了iOS SDK工开发者来开发。
在今天的这篇文章中,来讲述如果使用Java语言来访问Vantiq的一些资源。虽然,这里的Vantiq提供了专门的Java SDK,我们可以直接使用Vantiq平台提供的REST接口来访问。
在Vantiq平台的type中插入一个记录
我们可以使用如下的代码来实现一个记录的插入:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("https://dev.vantiq.cn/api/v1/resources/custom/Person");
JSONObject json = new JSONObject();
json.put("id", "5");
json.put("name", "yyyyyyy");
json.put("number", "00000000000");
String body = json.toString();
System.out.println("body: " + body);
HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
post.setEntity(entity);
post.setHeader("Content-Type", "application/json; charset=UTF-8");
post.addHeader(new BasicHeader("Authorization", "Bearer Your token in your namespace"));
CloseableHttpResponse respnse = client.execute(post);
System.out.println(respnse.getStatusLine().getStatusCode());
在我们实现时,我们需要记得修个这里的如下行:
一旦修改完成,运行上面的代码,我们可以看到:
我们可以看到在Person这个type里有一个新的记录产生。
调用Vantiq平台的procedure接口
同样的方法,我们可以使用Vantiq平台提供的REST API即可来访问在Vantiq平台里定义的一个procedure,并把相应的参数传入到这个procedure里去。在如下的代码中,我们调用一个叫做“rest_procedure”的procedure。
class JsonObjectBuilder {
private JsonObject obj = new JsonObject();
public JsonObjectBuilder addProperty(String name, String value) {
this.obj.addProperty(name, value);
return this;
}
public JsonObjectBuilder addProperty(String name, Number value) {
this.obj.addProperty(name, value);
return this;
}
public JsonObject obj() {
return this.obj;
}
public String json() {
Gson gson = new Gson();
return gson.toJson(this.obj);
}
}
JsonObject msg3 = new JsonObjectBuilder().addProperty("machineId", "Machine2").obj();
msg3.addProperty("temperature", 333);
msg3.addProperty("timestamp", "2019-01-04T09:05:58.004Z");
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("https://dev.vantiq.cn/api/v1/resources/procedures/rest_procedure");
// /api/v1/resources/procedures/ConvertToCelcius
JSONObject json = new JSONObject();
json.put("msg", msg3);
String body = json.toString();
System.out.println("body: " + body);
HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
post.setEntity(entity);
post.setHeader("Content-Type", "application/json; charset=UTF-8");
post.addHeader(new BasicHeader("Authorization", "Bearer Your token your namespace"));
CloseableHttpResponse respnse = client.execute(post);
System.out.println(respnse.getStatusLine().getStatusCode());
System.out.println("response: " + respnse);
同样,我们需要对如下的一行代码进行修改:
运行我们的代码,我们可以看到:
当我们运行完我们的代码,我们可以看到我们的procedure被调用,并把相应的参数传入到Vantiq平台中去了。
向Vantiq平台的topic发送数据
很多的时候,我们可以利用Vantiq平台提供的REST API接口向Vantiq里的topic发布信息,并把相应的消息传入到平台中。在如下的代码中,它展示了想Vantiq平台中的/xiaoguo/publish_topic发送消息。
class JsonObjectBuilder {
private JsonObject obj = new JsonObject();
public JsonObjectBuilder addProperty(String name, String value) {
this.obj.addProperty(name, value);
return this;
}
public JsonObjectBuilder addProperty(String name, Number value) {
this.obj.addProperty(name, value);
return this;
}
public JsonObject obj() {
return this.obj;
}
public String json() {
Gson gson = new Gson();
return gson.toJson(this.obj);
}
}
JsonObject msg3 = new JsonObjectBuilder().addProperty("machineId", "Machine2").obj();
msg3.addProperty("temperature", 333);
msg3.addProperty("timestamp", "2019-01-04T09:05:58.004Z");
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("https://dev.vantiq.cn/api/v1/resources/topics//xiaoguo/publish_topic");
JSONObject json = new JSONObject();
json.put("msg", msg3);
String body = json.toString();
System.out.println("body: " + body);
HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
post.setEntity(entity);
post.setHeader("Content-Type", "application/json; charset=UTF-8");
post.addHeader(new BasicHeader("Authorization", "Bearer Your token in your namespace"));
CloseableHttpResponse respnse = client.execute(post);
System.out.println(respnse.getStatusLine().getStatusCode());
System.out.println("response: " + respnse);
同样的,我们需要修改如下的这一行代码:
当我们运行完后,我可以看到:
我们可以看出来,我们发布的消息已经发布到Vantiq平台了。
这个项目的源码可以在如下的地址获得: