Search in sources :

Example 1 with Nabaztag

use of com.nabalive.data.core.model.Nabaztag in project NabAlive by jcheype.

the class ApplicationScheduler method clock.

@Scheduled(cron = "0 0 * * * *")
public void clock() {
    logger.debug("clock trigger");
    // clock
    Application application = checkNotNull(applicationManager.getApplication(CLOCK_APIKEY));
    Query<Nabaztag> query = nabaztagDAO.createQuery().filter("applicationConfigList.applicationStoreApikey", CLOCK_APIKEY);
    Iterator<Nabaztag> iterator = nabaztagDAO.find(query).iterator();
    while (iterator.hasNext()) {
        Nabaztag nabaztag = iterator.next();
        Status status = connectionManager.get(nabaztag.getMacAddress());
        if (status != null && !status.isAsleep()) {
            try {
                application.onStartup(nabaztag, findConfig(CLOCK_APIKEY, nabaztag.getApplicationConfigList()));
            } catch (Exception e) {
                logger.debug("cannot send message", e);
            }
        }
    }
}
Also used : Status(com.nabalive.server.jabber.Status) Nabaztag(com.nabalive.data.core.model.Nabaztag) Application(com.nabalive.application.core.Application) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 2 with Nabaztag

use of com.nabalive.data.core.model.Nabaztag in project NabAlive by jcheype.

the class AppTest method testInserNabaztag.

@Test
public void testInserNabaztag() {
    Nabaztag n1 = new Nabaztag();
    Nabaztag n2 = new Nabaztag();
    n1.setName("n1");
    n2.setName("n2");
    n1.setMacAddress(UUID.randomUUID().toString());
    n2.setMacAddress(UUID.randomUUID().toString());
    nabaztagDAO.save(n1);
    nabaztagDAO.save(n2);
}
Also used : Nabaztag(com.nabalive.data.core.model.Nabaztag) Test(org.junit.Test)

Example 3 with Nabaztag

use of com.nabalive.data.core.model.Nabaztag in project NabAlive by jcheype.

the class SleepTest method sleepTest.

@Test
public void sleepTest() {
    ConnectionWelcome welcome = new ConnectionWelcome();
    Nabaztag nabaztag = new Nabaztag();
    nabaztag.getSleep().add("14:06-7");
    nabaztag.getSleep().add("08:50-5");
    nabaztag.getSleep().add("10:17-3");
    nabaztag.getWakeup().add("14:03-6");
    nabaztag.getWakeup().add("11:45-6");
    nabaztag.getWakeup().add("10:32-2");
    boolean b = welcome.checkSleep(nabaztag);
    System.out.println("b:" + b);
}
Also used : Nabaztag(com.nabalive.data.core.model.Nabaztag) Test(org.junit.Test)

Example 4 with Nabaztag

use of com.nabalive.data.core.model.Nabaztag 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());
        }
    });
}
Also used : ObjectId(org.bson.types.ObjectId) Request(com.nabalive.framework.web.Request) HttpException(com.nabalive.framework.web.exception.HttpException) ExecutionException(java.util.concurrent.ExecutionException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Response(com.nabalive.framework.web.Response) Nabaztag(com.nabalive.data.core.model.Nabaztag) TmpData(com.nabalive.data.core.model.TmpData) HttpException(com.nabalive.framework.web.exception.HttpException) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) Map(java.util.Map) Route(com.nabalive.framework.web.Route) PostConstruct(javax.annotation.PostConstruct)

Example 5 with Nabaztag

use of com.nabalive.data.core.model.Nabaztag in project NabAlive by jcheype.

the class ApplicationScheduler method taichi.

@Scheduled(fixedDelay = 400000)
public void taichi() {
    logger.debug("taichi trigger");
    // taichi
    Application application = checkNotNull(applicationManager.getApplication(TAICHI_APIKEY));
    Query<Nabaztag> query = nabaztagDAO.createQuery().filter("applicationConfigList.applicationStoreApikey", TAICHI_APIKEY);
    Iterator<Nabaztag> iterator = nabaztagDAO.find(query).iterator();
    while (iterator.hasNext()) {
        Nabaztag nabaztag = iterator.next();
        Status status = connectionManager.get(nabaztag.getMacAddress());
        if (status != null && status.isIdle()) {
            if (rand.nextInt(4) == 0) {
                try {
                    application.onStartup(nabaztag, findConfig(TAICHI_APIKEY, nabaztag.getApplicationConfigList()));
                } catch (Exception e) {
                    logger.debug("cannot send message", e);
                }
            }
        }
    }
}
Also used : Status(com.nabalive.server.jabber.Status) Nabaztag(com.nabalive.data.core.model.Nabaztag) Application(com.nabalive.application.core.Application) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

Nabaztag (com.nabalive.data.core.model.Nabaztag)8 Scheduled (org.springframework.scheduling.annotation.Scheduled)4 Application (com.nabalive.application.core.Application)3 Status (com.nabalive.server.jabber.Status)3 SleepPacket (com.nabalive.server.jabber.packet.SleepPacket)2 Test (org.junit.Test)2 TmpData (com.nabalive.data.core.model.TmpData)1 Request (com.nabalive.framework.web.Request)1 Response (com.nabalive.framework.web.Response)1 Route (com.nabalive.framework.web.Route)1 HttpException (com.nabalive.framework.web.exception.HttpException)1 PingPacket (com.nabalive.server.jabber.packet.PingPacket)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 PostConstruct (javax.annotation.PostConstruct)1 ObjectId (org.bson.types.ObjectId)1 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)1 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)1