logger.debug("received json: {}", request.content);
Map<String, String> nabMap = mapper.readValue(request.content, Map.class);
String mac = CharMatcher.JAVA_LETTER_OR_DIGIT.retainFrom(checkNotNull(nabMap.get("mac")).toLowerCase());
if (!connectionManager.containsKey(mac)) {
throw new HttpException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Nabaztag not Connected");
}
Nabaztag nabaztag = new Nabaztag();
nabaztag.setMacAddress(mac);
nabaztag.setName(nabMap.get("name"));
nabaztag.setApikey(UUID.randomUUID().toString());
nabaztag.setOwner(token.getUserId());
try {
nabaztagDAO.save(nabaztag);
} catch (MongoException.DuplicateKey e) {
ImmutableMap<String, String> error = (new ImmutableMap.Builder<String, String>()).put("error", "Adresse mac déjà enregistrée").build();
response.writeJSON(error);
return;
}
messageService.sendMessage(mac, "ST " + OPERATIONNEL_URL + "\nMW\n");
response.writeJSON(nabaztag);
}
})
.post(new Route("/nabaztags") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
Token token = TokenUtil.decode(checkNotNull(request.getParamOrHeader("token")), Token.class);
String mac = CharMatcher.JAVA_LETTER_OR_DIGIT.retainFrom(checkNotNull(request.getParam("mac")).toLowerCase());
String name = request.getParam("name");
if (!connectionManager.containsKey(mac)) {
throw new HttpException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Nabaztag not Connected");
}
Nabaztag nabaztag = new Nabaztag();
nabaztag.setMacAddress(mac);
nabaztag.setName(name);