gt: 		String eslType = getParam("eslType", filter);
context: /**********************************************************************************
   *
   *           S5TECH(c) NETWORK APPLICATION DOCUMENTATION AND LICENSE
   *                        Version 1.6, September 2014
   *                          http://www.s5tech.com/
   *
   * Permission to copy, modify, and distribute this software and its documentation,
   * with or without modification, for any purpose and without fee or royalty is
   * hereby granted.
   * 
   * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE
   * NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
   * TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT
   * THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
   * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
   * 
   * COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR
   * CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION.
   * 
   * The name and trademarks of  copyright holders may NOT be used in advertising or
   * publicity pertaining to the software without specific, written prior permission.
   * Title to copyright in this software and any associated documentation will at
   * all times remain with copyright holders.
   * 
   * FOR INFORMATION ABOUT OBTAINING, INSTALLING AND RUNNING THIS SOFTWARE WRITE AN
   * EMAIL TO assist@s5tech.com
   * 
   * S5Tech Development Team 2015-01-15
   * S5Tech S.P.A, Via Caboto 10, 20100 Legnano - Italy
   * 		
   *********************************************************************************/
   
  package com.s5tech.net.services;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileReader;
  import java.io.InputStream;
  import java.lang.management.ManagementFactory;
  import java.lang.management.OperatingSystemMXBean;
  import java.lang.management.RuntimeMXBean;
  import java.lang.management.ThreadInfo;
  import java.lang.management.ThreadMXBean;
  import java.net.InetAddress;
  import java.net.URL;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Arrays;
  import java.util.Comparator;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Properties;
  import java.util.Set;
  
  import org.castor.core.util.Base64Encoder;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import com.s5tech.data.JdbcConnectionFactory;
  import com.s5tech.net.desktop.S5TechDesktopApp;
  import com.s5tech.net.entity.EslDataStore;
  import com.s5tech.net.entity.EslDeviceInfo;
  import com.s5tech.net.entity.EslEntityManager;
  import com.s5tech.net.entity.EslPriceData;
  import com.s5tech.net.entity.IEslDataStore;
  import com.s5tech.net.entity.IEslEntityManager;
  import com.s5tech.net.eslnet.EslFirmwareUpdater;
  import com.s5tech.net.eslnet.IEslNetwork;
  import com.s5tech.net.firmware.FirmwareLibrary;
  import com.s5tech.net.services.logging.ApplicationServicesListener;
  import com.s5tech.net.type.EUI64Address;
  import com.s5tech.net.type.HubInfo;
  import com.s5tech.net.util.ISystemKeys;
  import com.s5tech.net.util.Tools;
  
  public class RemoteControlServices {
  	
  	private static final Logger log = LoggerFactory.getLogger(ISystemKeys.APPLICATION);
  
  	private String EOL = "\n";
  	private DateFormat timef = null;
  	private ApplicationServicesListener networkManager = null;	
  	private IEslEntityManager entityManager = null;
  	
  	public static RemoteControlServices instance_ = null;
  		
  	private RemoteControlServices()
  	{
  		log.info("creating network services facade ...");
  		networkManager = ApplicationServicesListener.instance();
  		entityManager = EslEntityManager.instance();
  		if (timef == null) timef = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  		log.trace("network services initializer started");
  	}
  	
  	public static synchronized RemoteControlServices getInstance()
  	{
  		if (instance_ == null) {
  			instance_ = new RemoteControlServices();
  		}
  		return instance_;
  	}
  	
  	
  	public String getEOL() {
  		return EOL;
  	}
  
  	public void setEOL(String eol) {
  		EOL = eol;
  	}
  
  	public String getVersion()
  	{
  		String type = null;
  		String title = null;
  		String version = null;
  		
  		try {
  			if (S5TechDesktopApp.class.getPackage() != null &&
  					S5TechDesktopApp.class.getPackage().getImplementationTitle() != null) {
  				type = "R";
  				title = S5TechDesktopApp.class.getPackage().getImplementationTitle();
  				version = S5TechDesktopApp.class.getPackage().getImplementationVersion();		
  			}	
  		}
  		catch (Throwable ignored) {}
  		
  		if (title != null && version != null) 
  			return "result=" + title + " " + type + " " + version; 
  		
  		for (int i = 0; i < 3; i ++) 
  		{
  			String basedir = "./";
  			if (i > 0) {
  				String resName = "/" + getClass().getName().replace('.', '/') + ".class";
  				URL url = getClass().getResource(resName);
  				basedir = url.getFile();
  				basedir = basedir.substring(0, basedir.length() - resName.length());
  				if (i == 1) {
  					basedir = basedir + "/";
  				}
  				else if (i == 2) {
  					basedir = basedir + "/../";
  				}				
  			}
  			try {
  				File rc = null;
  				float max = 0;
  				for (File f : new File(basedir).listFiles()) {
  					if (f.getName().indexOf("build.number.") >= 0) {
  						float c = Float.parseFloat(
  								f.getName().substring(
  								f.getName().indexOf("build.number.") + 13));
  						if (c > max) {
  							max = c;
  							rc = f;
  						}
  					}
  				}			
  				if (rc != null){					
  					type = "B";
  					title = "Network Application";
  					Properties p = new Properties();					
  					InputStream in = new FileInputStream(rc);
  					p.load(in);
  					version = p.getProperty("build.number");
  					while (version.length() < 4) { version = "0" + version; }
  					version = String.valueOf(max).trim() + "." + version;
  					in.close();					
  				}	
  			}
  			catch (Throwable ignored) { ignored.printStackTrace(); }
  			
  			if (title != null && version != null) 
  				return "result=" + title + " " + type + " " + version; 
  			
  			try {
  				File f = new File(basedir + "VERSION");
  				if (f.exists()){
  					type = "V";
  					title = "Network Application";
  					BufferedReader in = new BufferedReader(new FileReader(f));
  					version = in.readLine().trim();
  					in.close();
  				}	
  			}
  			catch (Throwable ignored) {}
  			
  			if (title != null && version != null) 
  				return "result=" + title + " " + version + "-" + type; 
  			
  		}
  		
  		return "result=Network Application Unversioned";
  			
  	}
  	
  	public String getCoordinators(String params)
  	{
  		boolean hasQueues = (getParam("queues", params) != null && getParam("queues", params).equalsIgnoreCase("true"));		
  		boolean channelsList = (getParam("channelslist", params) != null && getParam("channelslist", params).equalsIgnoreCase("true"));		
  		SimpleDateFormat dateformat = (getParam("dateformat", params) != null ? new SimpleDateFormat(getParam("dateformat", params).replace('_', ' ')) : null);					
  		
  		StringBuffer buf = new StringBuffer("port url;channel;esls;mac address;online;online count;offline count;last online time;last offline time;");		
  		if (hasQueues) buf.append("msg;lowmsg;bcast;status;");		
  		if (channelsList) buf.append("channelslist");		
  		buf.append(EOL);
  		
  		try 
  		{	
  			Set<String> ports = networkManager.getAll().keySet();
  			if (ports == null || ports.size() == 0) return null;
  			for (String p : ports)
  	    	{
  	    		IEslNetwork net =  networkManager.getAll().get(p);
  	    		buf.append(p).append(';')
  	    				.append(net.getActiveChannel()).append(';')
  	    				.append(net.getAssociatedEsls()).append(';')
  	    				.append(net.getCoordinatorMac()).append(';')
  	    				.append(net.isOnline()).append(';')
  	    				.append(net.getOnlineCount()).append(';')
  			    		.append(net.getOfflineCount()).append(';')
  	    				.append((dateformat != null ? (net.getOnlineTime() <= 0 ? 
  	    						"never" : 
  	    							dateformat.format(new Date(net.getOnlineTime()))) : net.getOnlineTime())
  	    						).append(';')
  			    		.append((dateformat != null ? (net.getOfflineTime() <= 0 ? 
  			    				"never" : 
  			    					dateformat.format(new Date(net.getOfflineTime()))) : net.getOfflineTime())
  			    				).append(';');
  	    		if (hasQueues) {
  	    			buf.append(net.getOutboundSize()).append(';')
  	    					.append(net.getOutboundLowPrioritySize()).append(';')
  	    					.append(net.getOutboundBroadcastSize()).append(';')
  	    					.append(net.getPendingStatusSize()).append(';');
  	    		}
  	    		if (channelsList) {
  	    			buf.append(net.getChannelList()).append(';');    					
  	    		}
  	    		buf.append(EOL);
  	    	}			
  		}
  		catch (Throwable t) {
  			log.error("cannot get coordinators info - reason {}", t);			
  		}
  		return buf.toString();
  	}
  	
  	public String getCoordinatorsQueues()
  	{
  		StringBuffer buf = new StringBuffer(
  				"port url;channel;mac address;online;esl;esl low;bcast;outgoing;status").append(EOL);				
  		try 
  		{			
  			Set<String> ports = networkManager.getAll().keySet();
  			if (ports == null || ports.size() == 0) return null;			
  			for (String p : ports)
  	    	{
  	    		IEslNetwork net =  networkManager.getAll().get(p);    		
  	    		buf.append(p).append(';')
  	    				.append(net.getActiveChannel()).append(';')
  	    				.append(net.getCoordinatorMac()).append(';')
  	    				.append(net.isOnline()).append(';')
  	    				.append(net.getOutboundSize()).append(';')
  	    				.append(net.getOutboundLowPrioritySize()).append(';')
  	    				.append(net.getOutboundBroadcastSize()).append(';')
  	    				.append(net.getOutQueueSize()).append(';')
  	    				.append(net.getPendingStatusSize())
  	    				.append(EOL);
  	    	}			
  		}
  		catch (Throwable t) {
  			log.error("cannot get coordinators info - reason {}", t);			
  		}
  		return buf.toString();
  	}
  	
  	public String getHubs()
  	{
  		StringBuffer b = new StringBuffer("mac address;ip adddress;ports;version;socket").append(EOL);		
  		try 
  		{
  			for (HubInfo hub : ApplicationServicesListener.instance().getHubsList()) {
  				b.append(hub.getMac()).append(";")
  						.append(hub.getIpAddress()).append(";")
  						.append(hub.getPorts()).append(";")
  						.append(hub.getVersion()).append(";")
  						.append(hub.getProtocol())
  						.append(EOL);
  			}			
  		}
  		catch (Throwable t) {
  			log.error("cannot get hub list - reason {}", t);			
  		}
  		return b.toString();
  	}
  	
  	public String setCoordinatorChannel(String params)
  	{	
  		String mac = getParam("mac", params);
  		String port = getParam("port", params);
  		String channel = getParam("channel", params);		
  		String allowedchannels = getParam("allowedchannels", params);
  		
  		if (log.isTraceEnabled()) {
  			log.trace("set coordinator channels invoked > {}", params);
  		}
  		
  		if (allowedchannels == null) {
  			log.error("allowed channels list not specified, exit from set coordinator channels ");
  			return "result=fail";
  		}
  				
  		int fail = 0;
  		int success = 0;
  		
  		for (String p : networkManager.getAll().keySet())
      	{    		
  			try 
  			{
  				IEslNetwork net =  networkManager.getAll().get(p);
  	    		if (port != null) {
  	    			if (!port.equalsIgnoreCase(p)) continue;
  	    		}
  	    		if (mac != null) {
  	    			if (net.getCoordinatorMac() == null 
  	    					|| net.getCoordinatorMac().toString().indexOf(mac) < 0) continue;
  	    		}
  	    		if (channel != null) {
  	    			int n = Integer.parseInt(getParam("channel", params));
  	    			if (net.getActiveChannel() == null 
  	    					|| net.getActiveChannel().getValue() != n) continue;
  	    		}
  	    		if (log.isTraceEnabled()) {
  	    			log.trace("controller setting allowed channels {} to coordinator {} ...", 
  	    					allowedchannels, p);
  	    		}
  	    		
  	    		net.setChannelList(allowedchannels, true);
  	    		success ++;
  	    		
  			}
  			catch (Throwable t) {
  				
  				log.error("setting coordinator channel - ",  t);
  				fail ++;
  			}
      	}
  		
      	return new StringBuffer("success=")
      			.append(success).append(EOL)
      			.append("fail=").append(fail).append(EOL)
      			.toString();
      		
  	}
  	
  	public String setCoordinatorReset(String params)
  	{
  		String mac = getParam("mac", params);
  		String port = getParam("port", params);
  		String channel = getParam("channel", params);
  				
  		int fail = 0;
  		int success = 0;
  		
  		for (String p : networkManager.getAll().keySet())
      	{    	
  			try 
  			{
  	    		IEslNetwork net =  networkManager.getAll().get(p);
  	    		if (port != null) {
  	    			if (!port.equalsIgnoreCase(p)) continue;
  	    		}
  	    		if (mac != null) {
  	    			if (net.getCoordinatorMac() == null 
  	    					|| net.getCoordinatorMac().toString().indexOf(mac) < 0) continue;
  	    		}
  	    		if (channel != null) {
  	    			int n = Integer.parseInt(getParam("channel", params));
  	    			if (net.getActiveChannel() == null 
  	    					|| net.getActiveChannel().getValue() != n) continue;
  	    		}    		
  	    		log.warn("!!! resetting coordinator {} !!!...", p);
  	    		net.reset();
  	    		success ++;
  			}
  			catch (Throwable t) {
  				log.error(" - ", t);
  				fail ++;
  			}    	
      	}
  		
  		return new StringBuffer("success=")
  				.append(success).append(EOL)
  				.append("fail=").append(fail).append(EOL)
  				.toString();
  
  	}
  	
  	public String setCoordinatorTime(String filter)
  	{
  		String mac = getParam("mac", filter);
  		String port = getParam("port", filter);
  		String channel = getParam("channel", filter);
  				
  		int fail = 0;
  		int success = 0;
  		
  		for (String p : networkManager.getAll().keySet())
      	{   
  			try {
  			
  	    		IEslNetwork net =  networkManager.getAll().get(p);
  	
  	    		if (port != null) {
  	    			if (!port.equalsIgnoreCase(p)) continue;
  	    		}
  	
  	    		if (mac != null) {
  	    			if (net.getCoordinatorMac() == null 
  	    					|| net.getCoordinatorMac().toString().indexOf(mac) < 0) continue;
  	    		}
  	
  	    		if (channel != null) {
  	    			int n = Integer.parseInt(getParam("channel", filter));
  	    			if (net.getActiveChannel() == null 
  	    					|| net.getActiveChannel().getValue() != n) continue;
  	    		}
  	    		
  	    		if (log.isTraceEnabled())
  	    			log.trace("controller setting time to coordinator {} ...", p);
  	    		
  	    		net.setTime();	    		
  	    		success ++;
  	    		
  			}
  			catch (Throwable t) {
  				
  				log.error("setting coordinator time - ", t);				
  				fail ++;
  				
  			}
      	
      	}
  		
  		return new StringBuffer("success=")
  				.append(success).append(EOL)
  				.append("fail=").append(fail).append(EOL)
  				.toString();
  	}
  	
  	
  	public String getEsls (String filter)
  	{
  		String mac = getParam("mac", filter);
  		String coordinatorMac= getParam("coordinatorMac", filter);
  