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

com.github.jy2.orchestrator.OrchestratorClient Maven / Gradle / Ivy

There is a newer version: 0.0.39
Show newest version
package com.github.jy2.orchestrator;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import com.github.jy2.Publisher;
import com.github.jy2.commandline.picocli.Main;
import com.github.jy2.orchestrator.utils.Holder;
import com.inovatica.orchestrator.json.OrcherstratorStatus;
import com.inovatica.orchestrator.json.OrchestratorStatusItem;

import go.jyroscope.ros.diagnostic_msgs.KeyValue;

public class OrchestratorClient {

	public static ArrayList getItemStatuses(String address) {

		String topic = address + "/status";

		OrcherstratorStatus status;
		Holder holder = new Holder(null);
		CountDownLatch latch = new CountDownLatch(1);
		com.github.jy2.Subscriber statusSubscriber = Main.di.createSubscriber(topic,
				OrcherstratorStatus.class);
		Object listenerId = statusSubscriber.addMessageListener(s -> {
			holder.value = s;
			latch.countDown();
		}, 1);
		try {
			latch.await(10, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
			return new ArrayList();
		} finally {
			statusSubscriber.shutdown();
		}
		status = holder.value;

		return status.items;
	}

	public static ArrayList getItemList(String address) {
		ArrayList list = getItemStatuses(address);
		ArrayList list2 = new ArrayList();
		for (OrchestratorStatusItem i : list) {
			list2.add(i.name);
		}
		return list2;
	}

	public static void startItem(String address, String itemName) {
		Publisher commandPublisher = Main.di.createPublisher(address + "/command", KeyValue.class);
		// give time for the listeners to connect
		sleep(500);
		commandPublisher.publish(new KeyValue("start", itemName));
	}

	public static void stopItem(String address, String itemName) {
		Publisher commandPublisher = Main.di.createPublisher(address + "/command", KeyValue.class);
		// give time for the listeners to connect
		sleep(500);
		commandPublisher.publish(new KeyValue("stop", itemName));
	}

	public static void killItem(String address, String itemName) {
		Publisher commandPublisher = Main.di.createPublisher(address + "/command", KeyValue.class);
		// give time for the listeners to connect
		sleep(500);
		commandPublisher.publish(new KeyValue("kill", itemName));
	}

	public static void scan(String address) {
		Publisher commandPublisher = Main.di.createPublisher(address + "/command", KeyValue.class);
		// give time for the listeners to connect
		sleep(500);
		commandPublisher.publish(new KeyValue("scan", ""));
	}

	private static void sleep(int ms) {
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy