import java.util.ArrayList; import java.util.List; import ch.ntb.usb.Device; import ch.ntb.usb.USB; import ch.ntb.usb.USBException; public class X10Interface extends AbstractDeviceInfo { private static Device dev; private static List houseCodes; private static List deviceCodes; private static List functionCodes; private static List commandQueue; public X10Interface() { super(); // TODO Auto-generated constructor stub houseCodes = new ArrayList(); houseCodes.add("M"); houseCodes.add("E"); houseCodes.add("C"); houseCodes.add("K"); houseCodes.add("O"); houseCodes.add("G"); houseCodes.add("A"); houseCodes.add("I"); houseCodes.add("N"); houseCodes.add("F"); houseCodes.add("D"); houseCodes.add("L"); houseCodes.add("P"); houseCodes.add("H"); houseCodes.add("B"); houseCodes.add("J"); deviceCodes = new ArrayList(); deviceCodes.add(13); deviceCodes.add(5); deviceCodes.add(3); deviceCodes.add(11); deviceCodes.add(15); deviceCodes.add(7); deviceCodes.add(1); deviceCodes.add(9); deviceCodes.add(14); deviceCodes.add(6); deviceCodes.add(4); deviceCodes.add(12); deviceCodes.add(16); deviceCodes.add(8); deviceCodes.add(2); deviceCodes.add(10); functionCodes = new ArrayList(); functionCodes.add("All Units Off"); functionCodes.add("All Lights On"); functionCodes.add("On"); functionCodes.add("Off"); functionCodes.add("Dim"); functionCodes.add("Bright"); functionCodes.add("All Lights Off"); functionCodes.add("Extended Code"); functionCodes.add("Hail Request"); functionCodes.add("Hail Acknowledge"); functionCodes.add("Preset Dim (1)"); functionCodes.add("Preset Dim (2)"); functionCodes.add("Extended Data Transfer"); functionCodes.add("Status On"); functionCodes.add("Status Off"); functionCodes.add("Status Request"); commandQueue = new ArrayList(); } private static int parseInt(String s) { if (s == "") return 0; if (s.indexOf('x') > 0) { // is hex number if (s.length() <= 2) { // exception for "0x" return 0; } return Integer.parseInt( s.substring(s.indexOf('x') + 1, s.length()), 16); } // is decimal number return Integer.parseInt(s); } @Override public void initValues() { // TODO Auto-generated method stub setIdVendor((short) 0xbc7); setIdProduct((short) 0x1); setTimeout(500); setConfiguration(1); setInterface(0); setAltinterface(-1); setOutEPBulk(0x02); setInEPBulk(0x81); setOutEPInt(0x02); setInEPInt(0x81); setSleepTimeout(500); setMaxDataSize(USB.FULLSPEED_MAX_BULK_PACKET_SIZE); setMode(TransferMode.Bulk); } public static byte[] parseByteArray(String s) { final int HEX_WIDTH = 5; StringBuffer sb = new StringBuffer(); int stringIndex = 0, spaceIndex = 0; String ss; while (stringIndex + 3 < s.length()) { ss = s.substring(spaceIndex, spaceIndex + 4); spaceIndex = s.indexOf(' ', stringIndex) + 1; sb.append((char) parseInt(ss)); stringIndex += HEX_WIDTH; } return sb.toString().getBytes(); } public void openUsbDevice() { dev = USB.getDevice(getIdVendor(), getIdProduct()); try { dev.open(getConfiguration(), getInterface(), getAltinterface()); } catch (USBException e) { e.printStackTrace(); } } public void write(byte[] data) { try { dev.writeBulk(getOutEPInt(), data, data.length, 2000, false); } catch (USBException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public StringBuffer read() { StringBuffer sb = new StringBuffer(); byte[] data1 = new byte[dev.getMaxPacketSize()]; int lenRead1 = 0; try { lenRead1 = dev.readBulk(getInEPInt(), data1, dev.getMaxPacketSize(), getTimeout(), false); for (int i = 0; i < lenRead1; i++) { sb.append("0x" + String.format("%1$02X", data1[i]) + " "); } } catch (USBException e) { // e.printStackTrace(); } return sb; } public static void processData(StringBuffer data) { String[] dataArray = data.toString().split(" "); if (dataArray[0].equals("0x5D")) { System.out.print("RF "); } else if (dataArray[0].equals("0x5A")) { System.out.print("PL "); if (dataArray[2].equals("0x00")) { // set device System.out.print(houseCodes.get(Integer.decode("0x" + dataArray[3].substring(2, 3)))); System.out.print(deviceCodes.get(Integer.decode("0x" + dataArray[3].substring(3, 4)))); System.out.print(" "); } if (dataArray[2].equals("0x01")) { // send command System.out.print(houseCodes.get(Integer.decode("0x" + dataArray[3].substring(2, 3)))); System.out.print(functionCodes.get(Integer.decode("0x" + dataArray[3].substring(3, 4)))); System.out.print(" "); } } // if(!dataArray[0].equals("0x55")) { System.out.println("--" + data + "--"); // } } public static String translateData(StringBuffer data) { StringBuffer sb= new StringBuffer(); String[] dataArray = data.toString().split(" "); if (dataArray[0].equals("0x5D")) { sb.append("RFC "); } else if (dataArray[0].equals("0x5A")) { sb.append("PLC "); if (dataArray[2].equals("0x00")) { // set device sb.append(houseCodes.get(Integer.decode("0x" + dataArray[3].substring(2, 3)))); sb.append(deviceCodes.get(Integer.decode("0x" + dataArray[3].substring(3, 4)))); sb.append(" "); } if (dataArray[2].equals("0x01")) { // send command sb.append(houseCodes.get(Integer.decode("0x" + dataArray[3].substring(2, 3)))); sb.append(functionCodes.get(Integer.decode("0x" + dataArray[3].substring(3, 4)))); sb.append(" "); } } else if (dataArray[0].equals("0x55")) { sb.append("PLC "); } sb.append(data); return sb.toString(); } public String sendCommand(String houseCode, Integer deviceCode, String command) { StringBuffer returnCommands = new StringBuffer(); StringBuffer hexCommand; // First part of command setting the device hexCommand = new StringBuffer("0x04 "); int houseCodeNum = houseCodes.indexOf(houseCode) * 16; int deviceCodeNum = deviceCodes.indexOf(deviceCode); if (houseCodeNum == 0 & deviceCodeNum == 0) { hexCommand.append("0x00\n"); } else { hexCommand.append(toHexString(houseCodeNum + deviceCodeNum) + "\n"); } commandQueue.add(hexCommand.toString()); returnCommands.append("Send PLC " + hexCommand); // Second part sending the command hexCommand = new StringBuffer("0x06 "); int functionCodeNum = functionCodes.indexOf(command); if (houseCodeNum == 0 & functionCodeNum == 0) { hexCommand.append("0x00\n"); } else { hexCommand.append(toHexString(houseCodeNum + functionCodeNum) + "\n"); } commandQueue.add(hexCommand.toString()); returnCommands.append("Send PLC " + hexCommand); return returnCommands.toString(); } public String statusCommand() { StringBuffer returnCommands = new StringBuffer(); StringBuffer hexCommand; // Send status request to cm15a the device hexCommand = new StringBuffer("0x8b"); commandQueue.add(hexCommand.toString()); returnCommands.append("Send PLC " + hexCommand); return returnCommands.toString(); } private static String toHexString(int value) { return "0x" + Integer.toHexString(value); } public List getCommandQueue() { return commandQueue; } public void popCommandQueue() { commandQueue.remove(0); } public static List getHouseCodes() { return houseCodes; } public static List getDeviceCodes() { return deviceCodes; } public static List getFunctionCodes() { return functionCodes; } }