📄 coreconfigurationreader.java
字号:
package net.sf.dz.setup.core;import java.io.IOException;import java.io.File;import java.net.URL;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.NoSuchElementException;import java.util.Set;import java.util.TreeSet;import org.freehold.jukebox.conf.Configuration;import org.freehold.jukebox.conf.ConfigurationFactory;import org.freehold.jukebox.logger.LogAware;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.logger.Logger;import org.freehold.servomaster.device.model.Servo;import org.freehold.servomaster.device.model.ServoController;import net.sf.dz.util.wizard.ConfigurationReader;class CoreConfigurationReader extends LogAware implements ConfigurationReader { public static final LogChannel CH_CCR = new LogChannel("CoreCfReader"); public static final String CF_UNIT = "dz.unit"; private Set removeKeys = new TreeSet(); public CoreConfigurationReader(Logger logger) { super(logger); removeKeys.add("connector."); removeKeys.add("schedule."); removeKeys.add("unit."); removeKeys.add("zone."); } public void read(File cfFileName, Map context, String contextKey) throws IOException { URL cfURL = new URL("file", "", cfFileName.toString()); Configuration cf = (new ConfigurationFactory()).getConfiguration(cfURL); if ( cf == null ) { throw new IOException("Null configuration read"); } // We can afford to clear the context before actually putting the // configuration into it clear(context); context.put(contextKey, cf); render(context, cf); } /** * Clear all the data that was collected by the wizard pages, but not * the data that was gathered through discovery. * * <p> * * Wizard page data consists of information about the zones, units, * schedules and connectors. * * <p> * * Discovered data consists of information about sensors, switches, USB * devices and ports. * * @param context Context to clear. */ public void clear(Map context) { for ( Iterator i = context.keySet().iterator(); i.hasNext(); ) { String key = i.next().toString(); for ( Iterator i2 = removeKeys.iterator(); i2.hasNext(); ) { String removeKey = i2.next().toString(); if ( key.startsWith(removeKey) ) { i.remove(); break; } } } // Unfortunately, the above didn't take care of some things - like // releasing the device assignments. Have to do it separately. for ( Iterator i = getContextValues(context, "sensor.temp").iterator(); i.hasNext(); ) { SensorDescriptor sd = (SensorDescriptor)i.next(); sd.zone = null; } for ( Iterator i = getContextValues(context, "servo.").iterator(); i.hasNext(); ) { ServoDescriptor sd = (ServoDescriptor)i.next(); sd.zone = null; } for ( Iterator i = getContextValues(context, "unit.").iterator(); i.hasNext(); ) { SwitchDescriptor sd = (SwitchDescriptor)i.next(); sd.unit = null; } // Also, it is possible that some empty values are still left in the // context. for ( Iterator i = context.keySet().iterator(); i.hasNext(); ) { String key = i.next().toString(); if ( key.indexOf("NOT_CONFIGURED") != -1 ) { // This is a leftover i.remove(); } } complain(LOG_DEBUG, CH_CCR, "Discovery data: " + context); // And some cheating. // // The schedule persistence directory is lost, with no way to // restore it. Let's reset it to a default value. File scheduleDir = new File(System.getProperty("dz.conf.dir"), "schedule"); context.put("schedule.dir", scheduleDir.toString()); } /** * Populate the context with the values rendered out of the * configuration. * * @param context Context to populate. * * @param cf Configuration to populate from. */ private void render(Map context, Configuration cf) { List unitList = cf.getList(CF_UNIT); for ( Iterator i = unitList.iterator(); i.hasNext(); ) { renderUnit(context, cf, i.next().toString()); } renderSchedule(context, cf); } private void renderUnit(Map context, Configuration cf, String unitName) { complain(LOG_INFO, CH_CCR, "renderUnit(" + unitName + ")"); final String unitPrefix = CF_UNIT + "." + unitName; UnitDescriptor ud = new UnitDescriptor(unitName); renderUnitProperties(context, cf, ud); List zoneList = cf.getList(unitPrefix + ".zone"); for ( Iterator i = zoneList.iterator(); i.hasNext(); ) { renderZone(context, cf, ud, i.next().toString()); } context.put("unit." + unitName, ud); } private void renderUnitProperties(Map context, Configuration cf, UnitDescriptor ud) { // VT: FIXME: undefined at this point (ouch!) are: // // type // emergency // multistage // variable speed // energize // driver - actually, it's defined, but indirectly //ud.propertyMap.put("...", ...); String switchPrefix = CF_UNIT + "." + ud.name + ".driver.switch"; String modeAddress = cf.getString(switchPrefix + ".mode.address", "NOT_CONFIGURED"); String stageAddress = cf.getString(switchPrefix + ".stage.address", "NOT_CONFIGURED"); String fanAddress = cf.getString(switchPrefix + ".fan.address", "NOT_CONFIGURED"); ud.propertyMap.put("switch.mode.address", modeAddress); ud.propertyMap.put("switch.stage.address", stageAddress); ud.propertyMap.put("switch.fan.address", fanAddress); SwitchDescriptor md = new SwitchDescriptor(modeAddress); SwitchDescriptor sd = new SwitchDescriptor(stageAddress); SwitchDescriptor fd = new SwitchDescriptor(fanAddress); context.put("switch." + modeAddress, md); context.put("switch." + stageAddress, sd); context.put("switch." + fanAddress, fd); } private void renderZone(Map context, Configuration cf, UnitDescriptor ud, String zoneName) { complain(LOG_INFO, CH_CCR, "renderZone(" + zoneName + ")"); String zonePrefix = CF_UNIT + "." + ud.name + ".zone." + zoneName; ZoneDescriptor zd = new ZoneDescriptor(zoneName); zd.unit = ud; ud.zoneSet.add(zd); renderZoneSensor(context, cf, zd); renderZoneDamper(context, cf, zd); renderZoneSchedule(context, cf, zd); context.put("zone." + zoneName, zd); } private void renderZoneSensor(Map context, Configuration cf, ZoneDescriptor zd) { String sensorKey = CF_UNIT + "." + zd.unit.name + ".zone." + zd.name + ".thermostat.temp_sensor.address"; String sensorAddress = cf.getString(sensorKey, "NOT_CONFIGURED"); if ( sensorAddress != null && !"NOT_CONFIGURED".equals(sensorAddress) ) { SensorDescriptor sd = new SensorDescriptor(sensorAddress); zd.sensor = sd;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -