All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.agrosense.spi.agent.AgentQuery Maven / Gradle / Ivy

/**
 * Copyright (C) 2008-2013 LimeTri. All rights reserved.
 *
 * AgroSense is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it
 * is applied to this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * AgroSense. If not, see .
 */

package eu.agrosense.spi.agent;

import eu.agrosense.api.agent.AgentConstants;
import eu.agrosense.api.farm.FarmConstants;
import eu.agrosense.shared.model.Farm;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.opendolphin.core.PresentationModel;
import org.opendolphin.core.client.ClientAttribute;
import org.opendolphin.core.client.ClientDolphin;
import org.opendolphin.core.client.comm.OnFinishedHandler;
import org.opendolphin.core.client.comm.OnFinishedHandlerAdapter;

/**
 * Support class to query for agents
 * 
 * @since 14.02-beta
 * @author johan
 */
public class AgentQuery {

    private final ClientDolphin client;
    private String farmId;

    public AgentQuery(ClientDolphin client) {
        this.client = client;
    }

    /**
     * Restrict query output to farm.
     * Use as fluent api
     * @param farmId
     * @return 
     */
    public AgentQuery forFarm(String farmId) {
        this.farmId = farmId;
        return this;
    }

    /**
     * Asynchronous call to retrieve all agents
     * 

* To filter on a farm please use {@link AgentQuery#forFarm(java.lang.String) } in addition to this method *

*

{@link OnFinishedHandler#onFinishedData(java.util.List) } of the provided handler will be called with * the data result. For columns see {@link AgentConstants#CMD_PULL_AGENT_LIST}

*

If not on the gui thread, the synchronous version {@link AgentQuery#agents() can also be used as alternative

* * @since 14.01-beta * @see AgentConstants#CMD_PULL_AGENT_LIST * @param handler */ public void agents(OnFinishedHandler handler) { checkFarm(); client.send(AgentConstants.CMD_PULL_AGENT_LIST, handler); } /** * Synchronized call to retrieve all agents. *

* To filter on a farm please use {@link AgentQuery#forFarm(java.lang.String) } in addition to this method *

*

Do not use this method on a gui thread, in that case use {@link AgentQuery#agents(org.opendolphin.core.client.comm.OnFinishedHandler) }

* * @since 14.01-beta * @see AgentConstants#CMD_PULL_AGENT_LIST * @return * @throws InterruptedException */ public List agents() throws InterruptedException { checkFarm(); final List result = new ArrayList<>(); final CountDownLatch latch = new CountDownLatch(1); client.send(AgentConstants.CMD_PULL_AGENT_LIST, new OnFinishedHandlerAdapter() { @Override public void onFinishedData(List data) { result.addAll(data); latch.countDown(); } }); latch.await(10, TimeUnit.SECONDS); return result; } private void checkFarm() { // clear current farm id if not required for this query if (this.farmId == null) { PresentationModel currentFarm = client.getAt(FarmConstants.ID_CURRENT_FARM); if (currentFarm != null) { currentFarm.getAt(Farm.PROP_URI).setValue(null); } } else { PresentationModel currentFarm = client.getAt(FarmConstants.ID_CURRENT_FARM); if (currentFarm != null) { currentFarm.getAt(Farm.PROP_URI).setValue(this.farmId); } else { client.presentationModel(FarmConstants.ID_CURRENT_FARM, new ClientAttribute(Farm.PROP_URI, this.farmId)); } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy