<commit_before>package org.myrobotlab.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.ServiceType;
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.Logging;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.service.interfaces.DeviceControl;
import org.myrobotlab.service.interfaces.DeviceController;
import org.myrobotlab.service.interfaces.I2CControl;
import org.myrobotlab.service.interfaces.I2CController;
import org.myrobotlab.service.interfaces.ServiceInterface;
import org.slf4j.Logger;

/**
 * 
 * I2CMux - This is the MyRobotLab Service that can be used if you have several i2c devices that
 * share the same address. Create one I2CMux for each of the i2c buses that you want to use.
 * It can be used with tca9548a and possibly other devices.
 * 
 * 
 * @author <NAME>
 *  
 * More Info : https://www.adafruit.com/product/2717
 * 
 */
public class I2cMux extends Service implements I2CControl, I2CController {

	private static final long serialVersionUID = 1L;

	public final static Logger log = LoggerFactory.getLogger(I2cMux.class.getCanonicalName());

	transient I2CController controller;

	public ArrayList<String> controllers = new ArrayList<String>();
	public String controllerName;
	
	public List<String> deviceAddressList = Arrays.asList(
			 "0x70","0x71","0x72","0x73","0x74","0x75","0x76","0x77");
			
	public String deviceAddress = "0x70";
	
	public List<String> deviceBusList = Arrays.asList(
			"0","1","2","3","4","5","6","7","8");	
	public String deviceBus = "1";
	
	private boolean isAttached = false;

	public static void main(String[] args) {
		LoggingFactory.getInstance().configure();
		LoggingFactory.getInstance().setLevel(Level.DEBUG);
		try {
			I2cMux i2cMux = (I2cMux) Runtime.start("i2cMux", "I2CMux");
			Runtime.start("gui", "GUIService");

		} catch (Exception e) {
			Logging.logError(e);
		}
	}

	public I2cMux(String n) {
		super(n);
		
		subscribe(Runtime.getInstance().getName(), "registered", this.getName(), "onRegistered");
	}

	public void onRegistered(ServiceInterface s) {
		refreshControllers();
		broadcastState();
	}

   public void refreshControllers() {
  	 
  	controllers = Runtime.getServiceNamesFromInterface(I2CController.class);
		controllers.remove(this.getName());
		
		broadcastState();
	}
	
 	public void setDeviceBus(String deviceBus){
		this.deviceBus = deviceBus;
		broadcastState();
  }
	
	public void setDeviceAddress(String deviceAddress){
		this.deviceAddress = deviceAddress;
		broadcastState();
  }

	@Override
	public void createI2cDevice(I2CControl control, int busAddress, int deviceAddress) {
		// TODO 
		// Create a devicelist to be able to pass reads back if needed. 
		// Currently only a pass thru / return structure
		// controller.createI2cDevice((I2CControl) this, busAddress, deviceAddress);
	}

	@Override
	public void releaseI2cDevice(I2CControl control, int busAddress, int deviceAddress) {
			// controller.releaseI2cDevice(this, busAddress, deviceAddress);
	}
	
	/**
	 * This methods sets the i2c Controller that will be used to communicate with
	 * the i2c device
	 */
	// @Override
	public boolean setController(String controllerName, String deviceBus, String deviceAddress) {
		this.controllerName = controllerName;
		return setController((I2CController) Runtime.getService(controllerName), deviceBus, deviceAddress);
	}

	public boolean setController(String controllerName) {
		this.controllerName = controllerName;
		return setController((I2CController) Runtime.getService(controllerName), this.deviceBus, this.deviceAddress);
	}
	
	public boolean setController(I2CController controller) {
		return setController(controller, this.deviceBus, this.deviceAddress);
	}
	/**
	 * This methods sets the i2c Controller that will be used to communicate with
	 * the i2c device
	 */
	public boolean setController(I2CController controller, String deviceBus, String deviceAddress) {
		if (controller == null) {
			error("setting null as controller");
			return false;
		}
		controllerName = controller.getName();
		this.controller = controller;
		this.deviceBus = deviceBus;
		this.deviceAddress = deviceAddress;
		isAttached = true;

		log.info(String.format("%s setController %s", getName(), controllerName));
		
		broadcastState();
		return true;
	}

	public void unsetController() {
		controller = null;
		controllerName = null;
		this.deviceBus = null;
		this.deviceAddress = null;
		isAttached = false;
		broadcastState();
	}

	public I2CController getController() {
		return controller;
	}

	public String getControllerName() {

		String controlerName = null;

		if (controller != null) {
			controlerName = controller.getName();
		}

		return controlerName;
	}

	public boolean isAttached() {
		return isAttached;
	}<commit_msg>I2cMux debug 5<commit_after>package org.myrobotlab.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.ServiceType;
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.Logging;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.service.interfaces.DeviceControl;
import org.myrobotlab.service.interfaces.DeviceController;
import org.myrobotlab.service.interfaces.I2CControl;
import org.myrobotlab.service.interfaces.I2CController;
import org.myrobotlab.service.interfaces.ServiceInterface;
import org.slf4j.Logger;

/**
 * 
 * I2CMux - This is the MyRobotLab Service that can be used if you have several i2c devices that
 * share the same address. Create one I2CMux for each of the i2c buses that you want to use.
 * It can be used with tca9548a and possibly other devices.
 * 
 * 
 * @author <NAME>
 *  
 * More Info : https://www.adafruit.com/product/2717
 * 
 */
public class I2cMux extends Service implements I2CControl, I2CController {

	private static final long serialVersionUID = 1L;

	public final static Logger log = LoggerFactory.getLogger(I2cMux.class.getCanonicalName());

	transient I2CController controller;

	public ArrayList<String> controllers = new ArrayList<String>();
	public String controllerName;
	
	public List<String> deviceAddressList = Arrays.asList(
			 "0x70","0x71","0x72","0x73","0x74","0x75","0x76","0x77");
			
	public String deviceAddress = "0x70";
	
	public List<String> deviceBusList = Arrays.asList(
			"0","1","2","3","4","5","6","7","8");	
	public String deviceBus = "1";
	
	private boolean isAttached = false;

	public static void main(String[] args) {
		LoggingFactory.getInstance().configure();
		LoggingFactory.getInstance().setLevel(Level.DEBUG);
		try {
			I2cMux i2cMux = (I2cMux) Runtime.start("i2cMux", "I2CMux");
			Runtime.start("gui", "GUIService");

		} catch (Exception e) {
			Logging.logError(e);
		}
	}

	public I2cMux(String n) {
		super(n);
		
		subscribe(Runtime.getInstance().getName(), "registered", this.getName(), "onRegistered");
	}

	public void onRegistered(ServiceInterface s) {
		refreshControllers();
		broadcastState();
	}

   public void refreshControllers() {
  	 
  	controllers = Runtime.getServiceNamesFromInterface(I2CController.class);
		controllers.remove(this.getName());
		
		broadcastState();
	}
	
 	public void setDeviceBus(String deviceBus){
		this.deviceBus = deviceBus;
		broadcastState();
  }
	
	public void setDeviceAddress(String deviceAddress){
		this.deviceAddress = deviceAddress;
		broadcastState();
  }

	@Override
	public void createI2cDevice(I2CControl control, int busAddress, int deviceAddress) {
		// TODO 
		// Create a devicelist to be able to pass reads back if needed. 
		// Currently only a pass thru / return structure
		// controller.createI2cDevice((I2CControl) this, busAddress, deviceAddress);
	}

	@Override
	public void releaseI2cDevice(I2CControl control, int busAddress, int deviceAddress) {
			// controller.releaseI2cDevice(this, busAddress, deviceAddress);
	}
	
	/**
	 * This methods sets the i2c Controller that will be used to communicate with
	 * the i2c device
	 */
	// @Override
	public boolean setController(String controllerName, String deviceBus, String deviceAddress) {
		this.controllerName = controllerName;
		return setController((I2CController) Runtime.getService(controllerName), deviceBus, deviceAddress);
	}

	public boolean setController(String controllerName) {
		this.controllerName = controllerName;
		return setController((I2CController) Runtime.getService(controllerName), this.deviceBus, this.deviceAddress);
	}
	
	public boolean setController(I2CController controller) {
		return setController(controller, this.deviceBus, this.deviceAddress);
	}
	/**
	 * This methods sets the i2c Controller that will be used to communicate with
	 * the i2c device
	 */
	public boolean setController(I2CController controller, String deviceBus, String deviceAddress) {
		if (controller == null) {
			error("setting null as controller");
			return false;
		}
		controllerName = controller.getName();
		this.controller = controller;
		this.deviceBus = deviceBus;
		this.deviceAddress = deviceAddress;
		isAttached = true;

		log.info(String.format("%s setController %s", getName(), controllerName));
		
		createDevice();
		broadcastState();
		return true;
	}
	
	/**
	 * This method creates the i2c device
	 */
	boolean createDevice() {
		if (controller != null) {
				controller.releaseI2cDevice(this, Integer.parseInt(deviceBus), Integer.decode(deviceAddress));
				controller.createI2cDevice(this, Integer.parseInt(deviceBus), Integer.decode(deviceAddress));
		}

		log.info(String.format("Creating device on bus: %s address %s", deviceBus, deviceAddress));
		return true;
	}
	
	public void unsetController() {
		controller = null;
		controllerName = null;
		this.deviceBus = null;
		this.deviceAddress = null;
		isAttached = false;
		broadcastState();
	}

	public I2CController getController() {
		return controller;
	}

	public String getControllerName() {

		String controlerName = null;

		if (controller != null) {
			controlerName = controller.getName();
		}

		return controlerName;
	}

	public boolean isAttached() {
		return isAttached;
	}
{'fraction_non_alphanumeric': 0.10828707761223098, 'fraction_numerical': 0.016262537734930374, 'mean_word_length': 4.779403489026449, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4611608', 'n_tokens_mistral': 3404, 'n_tokens_neox': 3123, 'n_words': 777}