我想保留服务器中的一些历史数据。因此,文档说明您必须先向Orion发送订阅,然后Orion会将通知发送给Cygnus。
我这样订阅:
Entity payload = Entity.json("{\r\n" +
" \"entities\": [{\r\n" +
" \"type\": \"Usuario\",\r\n" +
" \"isPattern\": \"true\",\r\n" +
" \"id\": \"Usuario*\"\r\n" +
" }],\r\n" +
" \"attributes\": [],\r\n" +
" \"reference\": \"http://192.168.10.3:5050/notify\",\r\n" +
" \"duration\": \"P4Y\",\r\n" +
" \"notifyConditions\": [{\r\n" +
" \"type\": \"ONCHANGE\",\r\n" +
" \"condValues\": [\r\n" +
" \"speed\"\r\n" +
" ]\r\n" +
" }],\r\n" +
" \"throttling\": \"PT0.001S\"\r\n" +
"}");
Response response = client.target("http://192.168.10.3:1026/v1/subscribeContext")
.request(MediaType.APPLICATION_JSON_TYPE)
.post(payload);
以及实体的创建:
Entity payload = Entity.json("{ \"type\": \"Usuario\", \"id\": \"Usuario22\", \"temperature\": { \"value\": \"80.0\" }, \"location\": { \"value\": \""+latitud+", "+altitud+"\", \"type\": \"geo:point\", \"metadata\": { \"crs\": { \"value\": \"WGS84\" } } }}");
Response response = client.target("http://192.168.10.3:1026/v2/entities")
.request(MediaType.APPLICATION_JSON_TYPE)
.post(payload);
然后,天鹅座日志告诉我:
HTTPBadRequestException: 'fiware-servicepath' header value does not match the number of notified context responses [...]
你们当中有人知道为什么会这样吗?标头的创建应由Orion完成,否则,应使用Cygnus的配置...
谢谢。
更新:
我更改了服务器的http客户端,以使其更易于合并标头。
我的订阅:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://192.168.10.3:1026/v1/subscribeContext");
String json = "{\r\n" +
" \"entities\": [{\r\n" +
" \"type\": \"cargador\",\r\n" +
" \"isPattern\": \"true\",\r\n" +
" \"id\": \"cargador*\"\r\n" +
" }],\r\n" +
" \"attributes\": [\"speed\"],\r\n" +
" \"reference\": \"http://192.168.10.3:5050/notify\",\r\n" +
" \"duration\": \"P4Y\",\r\n" +
" \"notifyConditions\": [{\r\n" +
" \"type\": \"ONCHANGE\",\r\n" +
" \"condValues\": [\r\n" +
" \"speed\"\r\n" +
" ]\r\n" +
" }],\r\n" +
" \"throttling\": \"PT0.001S\"\r\n" +
"}";
StringEntity entity;
try {
entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("fiware-servicepath", "/");
CloseableHttpResponse response;
response = client.execute(httpPost);
System.out.println(response.getStatusLine());
client.close();
} catch (UnsupportedEncodingException e1) {
我的更新上下文:
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build()).build();
HttpPost httpPost = new HttpPost("http://192.168.10.3:1026/v1/updateContext");
String json = "{\r\n" +
" \"contextElements\": \r\n" +
"\r\n" +
" \r\n" +
"\r\n" +
" [\r\n" +
" {\r\n" +
" \"type\": \"cargador\",\r\n" +
" \"isPattern\": \"false\",\r\n" +
" \"id\": \"cargador48\",\r\n" +
" \"attributes\": [\r\n" +
" {\r\n" +
" \"name\": \"speed\",\r\n" +
" \"type\": \"float\",\r\n" +
" \"value\": \"798\"\r\n" +
" }\r\n" +
" ]\r\n" +
" }\r\n" +
" ],\r\n" +
" \"updateAction\": \"APPEND\"\r\n" +
" }";
StringEntity entity;
try {
entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("fiware-servicepath", "/");
CloseableHttpResponse response;
response = client.execute(httpPost);
System.out.println(response.getStatusLine());
client.close();
我生成的跟踪如下:
再次感谢您。
答案 0 :(得分:0)
您收到的错误消息似乎表明您正在尝试使用较新的NGSI v2格式发送对Cygnus的订阅,但是当前的Cygnus版本仅接受旧NGSI v1格式的通知-attrsFormat=legacy
因此,在设置订阅时需要提供此属性
请参阅this question,以获取有关如何修复它的更多详细信息。