⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitmodel.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
字号:
package net.sf.dz.device.model.impl;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import java.util.TreeMap;import java.util.TreeSet;import org.freehold.jukebox.conf.Configurable;import org.freehold.jukebox.logger.LogAware;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.sem.EventSemaphore;import org.freehold.jukebox.sem.SemaphoreGroup;import org.freehold.jukebox.service.PassiveService;import org.freehold.jukebox.service.RunnableService;import org.freehold.jukebox.service.StoppableService;import net.sf.dz.device.actuator.AC;import net.sf.dz.device.actuator.ACDriver;import net.sf.dz.device.actuator.Damper;import net.sf.dz.device.model.DamperController;import net.sf.dz.device.model.Thermostat;import net.sf.dz.device.model.Unit;import net.sf.dz.device.model.ZoneController;import net.sf.dz.scheduler.Schedule;import net.sf.dz.util.ObjectFactory;public class UnitModel extends PassiveService implements Unit {        /**     * Log channel.     *     * Properly initialized in {@link #configure configure()} as soon as the     * unit name is determined.     */    private LogChannel CH_UNIT = new LogChannel("Unit");        /**     * The unit name.     */    private String name;        /**     * The HVAC controller.     */    private AC acController;        /**     * The zone controller.     */    private ZoneController zoneController;        /**     * The damper controller.     */    private DamperController damperController;        /**     * Thermostat to damper mapping.     *     * The key is the thermostat, the value is the damper in the room that     * this thermostat controls.     *     * VT: FIXME: This will have to go into the damper controller     */    private Map tstat2damper = new TreeMap();        /**     * Thermostat to schedule mapping.     *     * The key is the thermostat, the value is the scheduler that is     * associated with it.     */    private Map tstat2schedule = new TreeMap();        /**     * Thermostats that belong to this unit.     */    private Set tsSet = new TreeSet();        /**     * The zone controller.     */    private ZoneController controller;        /**     * Listener set.     */    private Set listenerSet = new HashSet();        /**     * Create the instance.     *     * The instance is useless until {@link #configure configured}.     */    public UnitModel() {        }        protected void configure() throws Throwable {            // Figure out the name                // The configuration will look like <dz><unit name="XXX">, the        // configuration root will look like "dz.unit.XXX" - it will include        // the name                for ( StringTokenizer st = new StringTokenizer(getConfigurationRoot(), "."); st.hasMoreTokens(); ) {                    name = st.nextToken();        }                if ( name == null ) {                    throw new IllegalArgumentException("Missing configuration value: " + getConfigurationRoot() + ".name");        }                // Create the proper log channel                CH_UNIT = new LogChannel("Unit/" + name);                // Instantiate and configure the AC controller                String acClassName = getConfiguration().getString(getConfigurationRoot() + ".class");                try {                    acController = (AC)ObjectFactory.instantiate(acClassName, AC.class);                } catch ( Throwable t ) {                    complain(LOG_CRIT, CH_UNIT, "Unable to instantiate the AC Controller, see below");                        throw t;        }                if ( acController instanceof LogAware ) {                    ((LogAware)acController).setLogger(getLogger());        }                acController.configure(getConfigurationRoot(), getConfiguration());                if ( acController instanceof RunnableService ) {                    // VT: FIXME: Why the hell am I starting it here instead of            // startup()???                    complain(LOG_DEBUG, CH_UNIT, "Starting AC Controller...");                    if ( !((RunnableService)acController).start().waitFor() ) {                            throw new IllegalStateException("Failed to start AC Controller, check the logs");            }                        complain(LOG_INFO, CH_UNIT, "AC Controller started");        }                // VT: FIXME: implement the class selection for the damper        // controller                damperController = new net.sf.dz.device.model.impl.SimpleDamperController();        ((LogAware)damperController).setLogger(getLogger());                // Instantiate and configure the zone controller                String zcClassName = getConfiguration().getString(getConfigurationRoot() + ".zone_controller.class");                try {                    zoneController = (ZoneController)ObjectFactory.instantiate(zcClassName, ZoneController.class);                } catch ( Throwable t ) {                    complain(LOG_CRIT, CH_UNIT, "Unable to instantiate the zone controller, see below");                        throw t;        }                if ( zoneController instanceof LogAware ) {                    ((LogAware)zoneController).setLogger(getLogger());        }                zoneController.attach(this);        zoneController.configure(getConfigurationRoot() + ".zone_controller", getConfiguration());                // Instantiate and configure the zones                String zoneSetRoot = getConfigurationRoot() + ".zone";        List zoneNameList = getConfiguration().getList(zoneSetRoot);                complain(LOG_INFO, CH_UNIT, "Zones: " + zoneNameList);                SemaphoreGroup schedule = new SemaphoreGroup();                for ( Iterator i = zoneNameList.iterator(); i.hasNext(); ) {                    String zoneName = i.next().toString();                        try {                            EventSemaphore start = createZone(zoneSetRoot, zoneName);                                if ( start != null ) {                                    schedule.add(start);                }                            } catch ( Throwable t ) {                            complain(LOG_WARNING, CH_UNIT, "Unable to create zone '" + zoneName + "', cause:", t);            }        }                if ( !schedule.waitForAll(true, false) ) {                    complain(LOG_WARNING, CH_UNIT, "Some schedules have failed to start, check the logs");        }    }        private EventSemaphore createZone(String zoneSetRoot, String zoneName) throws Throwable {            String zoneRoot = zoneSetRoot + "." + zoneName;                // VT: FIXME: Need to decide: what to do if the zone itself can't be        // created. There are multiple points of failure - can't create a        // thermostat, can't create a damper. Currently, if it blows up, it        // just does, and no recovery is attempted - I wonder whether it is        // right.                Thermostat ts = createThermostat(zoneSetRoot, zoneName);                damperController.put(ts, createDamper(zoneRoot));                tsSet.add(ts);        ts.addListener(this);                // Create the schedule                try {                    Schedule s = new Schedule(ts);                        s.setLogger(getLogger());            s.configure(zoneRoot + ".schedule", getConfiguration());                        tstat2schedule.put(ts, s);                        return s.start();                    } catch ( Throwable t ) {                    complain(LOG_WARNING, CH_UNIT, "Unable to create schedule for " + ts.getName() + ", cause:", t);        }                return null;    }        private Thermostat createThermostat(String zoneSetRoot, String zoneName) throws Throwable {            String tsRoot = zoneSetRoot + "." + zoneName + ".thermostat";        String tsName = zoneName;                // VT: NOTE: We know what class to create now since this is the        // core, not the facade                ThermostatModel ts = new ThermostatModel();                ts.setLogger(getLogger());        ts.attach(this);        ts.configure(tsRoot, getConfiguration());                return (Thermostat)ts;    }        private Damper createDamper(String zoneRoot) throws Throwable {            Set damperSet = new HashSet();        String droot = zoneRoot + ".damper";        String damperClassName = getConfiguration().getString(droot + ".class");        Damper d = (Damper)ObjectFactory.instantiate(damperClassName, Damper.class);                if ( d instanceof LogAware ) {                    ((LogAware)d).setLogger(getLogger());        }                if ( d instanceof Configurable ) {                    ((Configurable)d).configure(droot, getConfiguration());        }        return d;    }        public Iterator iterator() {            // VT: FIXME: Should I return an immutable iterator, maybe?            return tsSet.iterator();    }        public int getZoneCount() {            return tsSet.size();    }        protected void startup() throws Throwable {            // VT: FIXME: Start the AC                for ( Iterator i = iterator(); i.hasNext(); ) {                    Thermostat ts = (Thermostat)i.next();                        if ( ts instanceof RunnableService ) {                            ((RunnableService)ts).start().waitFor();            }        }        complain(LOG_INFO, CH_UNIT, "Started");    }    protected void shutdown(Throwable cause) throws Throwable {            for ( Iterator i = iterator(); i.hasNext(); ) {                    Thermostat ts = (Thermostat)i.next();                        if ( ts instanceof StoppableService ) {                            ((StoppableService)ts).stop().waitFor();            }                        i.remove();        }                if ( acController instanceof StoppableService ) {                        complain(LOG_DEBUG, CH_UNIT, "Stopping the AC Controller...");                    ((StoppableService)acController).stop().waitFor();            complain(LOG_INFO, CH_UNIT, "AC Controller stopped");        }    }        public void controlSignalChanged(Thermostat source, Object signal) {            zoneController.controlSignalChanged(source, signal);    }        public void enabledStateChanged(Thermostat source, boolean enabled) {            zoneController.enabledStateChanged(source, enabled);    }        public void votingStateChanged(Thermostat source, boolean voting) {            zoneController.votingStateChanged(source, voting);    }    public void holdStateChanged(Thermostat source, boolean hold) {            zoneController.holdStateChanged(source, hold);    }        public AC getAC() {            if ( acController == null ) {                    throw new IllegalStateException("Not Initialized");        }            return acController;    }        public DamperController getDamperController() {            if ( damperController == null ) {                    throw new IllegalStateException("Not Initialized");        }            return damperController;    }        public String getName() {            if ( name == null ) {                    throw new IllegalStateException("Not Initialized");        }            return name;    }        public Schedule getSchedule(Thermostat ts) {            return (Schedule)tstat2schedule.get(ts);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -