// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/config/AutoSIMEnvironmentConfig.java
// public class AutoSIMEnvironmentConfig {
// 	public String name;
// 	public int count;
// 	public String device;
// 	public int delayInMilliSeconds;
// 	public int threads;
// }
// 
// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/config/AutoSIMEnvironmentsConfig.java
// public class AutoSIMEnvironmentsConfig {
// 
// 	public List<AutoSIMEnvironmentConfig> environments = new ArrayList<AutoSIMEnvironmentConfig>();
// 	
// }
// 
// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/config/EnvironmentThread.java
// public class EnvironmentThread{
// 
// 	public String name;
// 	public int count;
// 	public String device;
// 	public int delayInMilliSeconds;
// 	public int threads;
// 	
// 	private List<IDevice> devices = new ArrayList<IDevice>();
// 	private IEnvironments environments = null;
// 	private boolean running = false;
// 	private Thread thread = null;
// 	
// 	public EnvironmentThread(AutoSIMEnvironmentConfig conf){
// 		this.name = conf.name;
// 		this.count = conf.count;
// 		this.device = conf.device;
// 		this.delayInMilliSeconds = conf.delayInMilliSeconds;
// 		this.threads = conf.threads;
// 	}
// 	
// 	
// 	public void setEnvironments(IEnvironments environments){
// 		this.environments = environments;
// 	}
// 	
// 
// 	public void setup() throws Exception {
// 		for (int i=0; i<count;i++){
// 			IDevice d = environments.getDevice(device);
// 			d.setup();
// 			devices.add(d);
// 		}		
// 	}
// 
// 	public void start() throws Exception {
// 		for (IDevice d: devices){
// 			d.start();
// 		}
// 		running = true;
// 		thread = new Thread(){
// 			public void run(){
// 				while (running){
// 					ExecutorService executor = Executors.newFixedThreadPool(threads);
// 					for (IDevice d: devices) {
// 						final IDevice device = d;
// 						Runnable worker = new Runnable(){
// 							public void run(){
// 								try {
// 									device.step();
// 								} catch (Exception e){
// 									System.out.println(e.getMessage());
// 								}
// 							}
// 						};
// 					    executor.execute(worker);
// 					}
// 					executor.shutdown();
// 					while (!executor.isTerminated()) {}
// 					
// 					try {
// 						Thread.currentThread().sleep(delayInMilliSeconds);
// 					}catch (Exception e){}
// 					
// 				}
// 			}
// 		};
// 		thread.start();
// 	}
// 
// 	public void stop() throws Exception {
// 		thread = null;
// 		running = false;
// 		for (IDevice d: devices){
// 			d.stop();
// 		}
// 	}
// 
// 	public void teardown() throws Exception {
// 		for (IDevice d: devices){
// 			d.teardown();
// 		}		
// 		devices.clear();
// 	}
// 	
// }
// 
// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/utils/GsonUtils.java
// public class GsonUtils {
// 
// 	private static GsonBuilder builder = null;
// 	
// 	static {
//         builder = new GsonBuilder();
//         builder.setPrettyPrinting().serializeNulls();
// 	}
// 	
// 	public static String objectToJson(Object obj) throws Exception {
//         Gson gson = builder.create();
//         return gson.toJson(obj);
// 	}
// 	
// 	public static Object jsonToObject(String path, Class clas) throws Exception {
//         Gson gson = builder.create();
// 		String json = new String(Files.readAllBytes(Paths.get(path)));//"./albums.json")));
//         return gson.fromJson(json, clas);
// 	}
// 	
// 	
// }


import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import org.omg.CORBA.Environment;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import com.automatski.autosim.environments.config.AutoSIMEnvironmentConfig;
import com.automatski.autosim.environments.config.AutoSIMEnvironmentsConfig;
import com.automatski.autosim.environments.config.EnvironmentThread;
import com.automatski.autosim.environments.utils.GsonUtils;
/*
 *  AutoSIM - Internet of Things Simulator
 *  Copyright (C) 2014, Aditya Yadav <aditya@automatski.com>
 *
 *  This program 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.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.automatski.autosim.environments;






/**
 * This class implements a simple bundle that uses the bundle
 * context to register an English language dictionary service
 * with the OSGi framework. The dictionary service interface is
 * defined in a separate class file and is implemented by an
 * inner class.
**/

//https://chamibuddhika.wordpress.com/2011/10/02/apache-thrift-quickstart-tutorial/

public class EnvironmentsServiceActivator implements BundleActivator
{
	
    /**
     * Implements BundleActivator.start(). Registers an
     * instance of a dictionary service using the bundle context;
     * attaches properties to the service that can be queried
     * when performing a service look-up.
     * @param context the framework context for the bundle.
    **/
    public void start(BundleContext context) throws Exception 
    {
    	Hashtable<String, String> props = new Hashtable<String, String>();
        props.put("Language", "English");

        // read the config
// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/config/AutoSIMEnvironmentConfig.java
// public class AutoSIMEnvironmentConfig {
// 	public String name;
// 	public int count;
// 	public String device;
// 	public int delayInMilliSeconds;
// 	public int threads;
// }
// 
// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/config/AutoSIMEnvironmentsConfig.java
// public class AutoSIMEnvironmentsConfig {
// 
// 	public List<AutoSIMEnvironmentConfig> environments = new ArrayList<AutoSIMEnvironmentConfig>();
// 	
// }
// 
// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/config/EnvironmentThread.java
// public class EnvironmentThread{
// 
// 	public String name;
// 	public int count;
// 	public String device;
// 	public int delayInMilliSeconds;
// 	public int threads;
// 	
// 	private List<IDevice> devices = new ArrayList<IDevice>();
// 	private IEnvironments environments = null;
// 	private boolean running = false;
// 	private Thread thread = null;
// 	
// 	public EnvironmentThread(AutoSIMEnvironmentConfig conf){
// 		this.name = conf.name;
// 		this.count = conf.count;
// 		this.device = conf.device;
// 		this.delayInMilliSeconds = conf.delayInMilliSeconds;
// 		this.threads = conf.threads;
// 	}
// 	
// 	
// 	public void setEnvironments(IEnvironments environments){
// 		this.environments = environments;
// 	}
// 	
// 
// 	public void setup() throws Exception {
// 		for (int i=0; i<count;i++){
// 			IDevice d = environments.getDevice(device);
// 			d.setup();
// 			devices.add(d);
// 		}		
// 	}
// 
// 	public void start() throws Exception {
// 		for (IDevice d: devices){
// 			d.start();
// 		}
// 		running = true;
// 		thread = new Thread(){
// 			public void run(){
// 				while (running){
// 					ExecutorService executor = Executors.newFixedThreadPool(threads);
// 					for (IDevice d: devices) {
// 						final IDevice device = d;
// 						Runnable worker = new Runnable(){
// 							public void run(){
// 								try {
// 									device.step();
// 								} catch (Exception e){
// 									System.out.println(e.getMessage());
// 								}
// 							}
// 						};
// 					    executor.execute(worker);
// 					}
// 					executor.shutdown();
// 					while (!executor.isTerminated()) {}
// 					
// 					try {
// 						Thread.currentThread().sleep(delayInMilliSeconds);
// 					}catch (Exception e){}
// 					
// 				}
// 			}
// 		};
// 		thread.start();
// 	}
// 
// 	public void stop() throws Exception {
// 		thread = null;
// 		running = false;
// 		for (IDevice d: devices){
// 			d.stop();
// 		}
// 	}
// 
// 	public void teardown() throws Exception {
// 		for (IDevice d: devices){
// 			d.teardown();
// 		}		
// 		devices.clear();
// 	}
// 	
// }
// 
// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/utils/GsonUtils.java
// public class GsonUtils {
// 
// 	private static GsonBuilder builder = null;
// 	
// 	static {
//         builder = new GsonBuilder();
//         builder.setPrettyPrinting().serializeNulls();
// 	}
// 	
// 	public static String objectToJson(Object obj) throws Exception {
//         Gson gson = builder.create();
//         return gson.toJson(obj);
// 	}
// 	
// 	public static Object jsonToObject(String path, Class clas) throws Exception {
//         Gson gson = builder.create();
// 		String json = new String(Files.readAllBytes(Paths.get(path)));//"./albums.json")));
//         return gson.fromJson(json, clas);
// 	}
// 	
// 	
// }


// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/EnvironmentsServiceActivator.java
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import org.omg.CORBA.Environment;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import com.automatski.autosim.environments.config.AutoSIMEnvironmentConfig;
import com.automatski.autosim.environments.config.AutoSIMEnvironmentsConfig;
import com.automatski.autosim.environments.config.EnvironmentThread;
import com.automatski.autosim.environments.utils.GsonUtils;

/*
 *  AutoSIM - Internet of Things Simulator
 *  Copyright (C) 2014, Aditya Yadav <aditya@automatski.com>
 *
 *  This program 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.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.automatski.autosim.environments;






/**
 * This class implements a simple bundle that uses the bundle
 * context to register an English language dictionary service
 * with the OSGi framework. The dictionary service interface is
 * defined in a separate class file and is implemented by an
 * inner class.
**/

//https://chamibuddhika.wordpress.com/2011/10/02/apache-thrift-quickstart-tutorial/

public class EnvironmentsServiceActivator implements BundleActivator
{
	
    /**
     * Implements BundleActivator.start(). Registers an
     * instance of a dictionary service using the bundle context;
     * attaches properties to the service that can be queried
     * when performing a service look-up.
     * @param context the framework context for the bundle.
    **/
    public void start(BundleContext context) throws Exception 
    {
    	Hashtable<String, String> props = new Hashtable<String, String>();
        props.put("Language", "English");

        // read the config
    	final AutoSIMEnvironmentsConfig config = (AutoSIMEnvironmentsConfig) GsonUtils.jsonToObject("./conf/autosim-environments.json", AutoSIMEnvironmentsConfig.class);