/** Parses configuration creating Modbus slave instances defined in cfg file {@inheritDoc} */ @Override protected void internalReceiveCommand(String itemName, Command command) { for (ModbusBindingProvider provider : providers) { if (provider.providesBindingFor(itemName)) { ModbusBindingConfig config = provider.getConfig(itemName); ModbusSlave slave = modbusSlaves.get(config.slaveName); slave.executeCommand(command, config.readRegister, config.writeRegister); } } }
/** updates all slaves from the modbusSlaves */ @Override protected void execute() { Collection<ModbusSlave> slaves = new HashSet<ModbusSlave>(); synchronized (slaves) { slaves.addAll(modbusSlaves.values()); } for (ModbusSlave slave : slaves) { slave.update(this); } }
@Override public void updated(Dictionary<String, ?> config) throws ConfigurationException { // remove all known items if configuration changed clearSlaves(); if (config != null) { Enumeration<String> keys = config.keys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); // the config-key enumeration contains additional keys that we // don't want to process here ... if ("service.pid".equals(key)) { continue; } Matcher matcher = EXTRACT_MODBUS_CONFIG_PATTERN.matcher(key); if (!matcher.matches()) { if ("poll".equals(key)) { if (StringUtils.isNotBlank((String) config.get(key))) { pollInterval = Integer.valueOf((String) config.get(key)); } } else if ("writemultipleregisters".equals(key)) { ModbusSlave.setWriteMultipleRegisters(Boolean.valueOf(config.get(key).toString())); } else { logger.debug( "given modbus-slave-config-key '{}' does not follow the expected pattern or 'serial.<slaveId>.<{}>'", key, VALID_COFIG_KEYS); } continue; } matcher.reset(); matcher.find(); String slave = matcher.group(2); ModbusSlave modbusSlave = modbusSlaves.get(slave); if (modbusSlave == null) { if (matcher.group(1).equals(TCP_PREFIX)) { modbusSlave = new ModbusTcpSlave(slave); } else if (matcher.group(1).equals(UDP_PREFIX)) { modbusSlave = new ModbusUdpSlave(slave); } else if (matcher.group(1).equals(SERIAL_PREFIX)) { modbusSlave = new ModbusSerialSlave(slave); } else { throw new ConfigurationException( slave, "the given slave type '" + slave + "' is unknown"); } logger.debug("modbusSlave '{}' instanciated", slave); modbusSlaves.put(slave, modbusSlave); } String configKey = matcher.group(3); String value = (String) config.get(key); if ("connection".equals(configKey)) { String[] chunks = value.split(":"); if (modbusSlave instanceof ModbusIPSlave) { // expecting: // <devicePort>:<port> ((ModbusIPSlave) modbusSlave).setHost(chunks[0]); if (chunks.length == 2) { ((ModbusIPSlave) modbusSlave).setPort(Integer.valueOf(chunks[1])); } } else if (modbusSlave instanceof ModbusSerialSlave) { // expecting: // <devicePort>[:<baudRate>:<dataBits>:<parity>:<stopBits>:<encoding>] ((ModbusSerialSlave) modbusSlave).setPort(chunks[0]); if (chunks.length >= 2) { ((ModbusSerialSlave) modbusSlave).setBaud(Integer.valueOf(chunks[1])); } if (chunks.length >= 3) { ((ModbusSerialSlave) modbusSlave).setDatabits(Integer.valueOf(chunks[2])); } if (chunks.length >= 4) { ((ModbusSerialSlave) modbusSlave).setParity(chunks[3]); } if (chunks.length >= 5) { ((ModbusSerialSlave) modbusSlave).setStopbits(Double.valueOf(chunks[4])); } if (chunks.length == 6) { ((ModbusSerialSlave) modbusSlave).setEncoding(chunks[5]); } } } else if ("start".equals(configKey)) { modbusSlave.setStart(Integer.valueOf(value)); } else if ("length".equals(configKey)) { modbusSlave.setLength(Integer.valueOf(value)); } else if ("id".equals(configKey)) { modbusSlave.setId(Integer.valueOf(value)); } else if ("type".equals(configKey)) { if (ArrayUtils.contains(ModbusBindingProvider.SLAVE_DATA_TYPES, value)) { modbusSlave.setType(value); } else { throw new ConfigurationException( configKey, "the given slave type '" + value + "' is invalid"); } } else if ("valuetype".equals(configKey)) { if (ArrayUtils.contains(ModbusBindingProvider.VALUE_TYPES, value)) { modbusSlave.setValueType(value); } else { throw new ConfigurationException( configKey, "the given value type '" + value + "' is invalid"); } } else if ("rawdatamultiplier".equals(configKey)) { modbusSlave.setRawDataMultiplier(Double.valueOf(value.toString())); } else { throw new ConfigurationException( configKey, "the given configKey '" + configKey + "' is unknown"); } } logger.debug("config looked good, proceeding with slave-connections"); // connect instances to modbus slaves for (ModbusSlave slave : modbusSlaves.values()) { slave.connect(); } setProperlyConfigured(true); } }
private void clearSlaves() { for (ModbusSlave slave : modbusSlaves.values()) { slave.resetConnection(); } modbusSlaves.clear(); }