use of com.nabalive.data.core.model.TmpData in project NabAlive by jcheype.
the class RecordController method init.
@PostConstruct
void init() {
restHandler.post(new Route("/vl/record.jsp") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
String mac = checkNotNull(request.getParam("sn")).toLowerCase();
if (!connectionManager.containsKey(mac))
throw new HttpException(HttpResponseStatus.NOT_FOUND, "sn is not connected");
Nabaztag nabaztag = checkNotNull(nabaztagDAO.findOne("macAddress", mac));
ChannelBuffer content = request.request.getContent();
logger.debug("record orig size: {}", content.readableBytes());
ChannelBufferInputStream inputStream = new ChannelBufferInputStream(content);
TmpData sound = new TmpData();
sound.setData(ByteStreams.toByteArray(inputStream));
tmpDataDAO.save(sound, WriteConcern.SAFE);
String host = request.request.getHeader("Host");
String url = "http://" + host + "/record/" + sound.getId().toString();
logger.debug("sound url: {}", url);
final String command = "ST " + url + "\nMW\n";
Query<Nabaztag> query = nabaztagDAO.createQuery();
query.filter("subscribe.objectId", nabaztag.getId().toString());
List<Nabaztag> nabaztags = nabaztagDAO.find(query).asList();
logger.debug("sending to {} subscribers", nabaztags.size());
for (Nabaztag nab : nabaztags) {
if (connectionManager.containsKey(nab.getMacAddress())) {
final Nabaztag nabTmp = nab;
Runnable runnable = new Runnable() {
@Override
public void run() {
logger.debug("sending to {}", nabTmp.getMacAddress());
logger.debug("command {}", command);
messageService.sendMessage(nabTmp.getMacAddress(), command);
}
};
ses.schedule(runnable, 500, TimeUnit.MILLISECONDS);
}
}
response.write("ok");
}
}).get(new Route("/record/:recordId") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
ObjectId recordId = new ObjectId(checkNotNull(map.get("recordId")));
TmpData sound = checkNotNull(tmpDataDAO.get(recordId));
response.write(sound.getData());
}
});
}
Aggregations