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

📄 unitserverproxy.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
字号:
package net.sf.dz.view.tcp.server;import java.io.PrintWriter;import java.util.Iterator;import java.util.Map;import java.util.StringTokenizer;import java.util.TreeMap;import org.freehold.jukebox.logger.LogChannel;import org.freehold.jukebox.logger.Logger;import org.freehold.jukebox.scheduler.Scheduler;import net.sf.dz.device.actuator.AC;import net.sf.dz.device.actuator.Damper;import net.sf.dz.device.model.DamperController;import net.sf.dz.device.model.Unit;import net.sf.dz.device.model.Thermostat;import net.sf.dz.event.ACListener;import net.sf.dz.event.DamperListener;import net.sf.dz.scheduler.Schedule;/** * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2001-2002 * @version $Id: UnitServerProxy.java,v 1.14 2004/02/03 20:47:26 vtt Exp $ */public class UnitServerProxy extends AbstractServerProxy implements Unit, ACListener, DamperListener  {    public static final LogChannel CH_USP = new LogChannel("Unit/proxy");    private Unit unit;        /**     * Map of thermostats that are connected to this unit.     *     * The key is the thermostat proxy, the value is the thermostat.     */    private Map tsMap = new TreeMap();        public UnitServerProxy(Logger logger, Unit unit) {            super(logger);            if ( unit == null ) {                    throw new IllegalArgumentException("Unit can't be null");        }                this.unit = unit;                for ( Iterator i = unit.iterator(); i.hasNext(); ) {                    Thermostat ts = (Thermostat)i.next();            ThermostatServerProxy tsProxy = new ThermostatServerProxy(logger, this, ts);            tsMap.put(tsProxy, ts);                        // VT: The reason *we* serve as a listener instead of the            // thermostat proxy is because we have to implement the            // ThermostatListener anyway (as Unit).                        ts.addListener(this);                        Damper d = unit.getDamperController().getDamper(ts);                        d.addListener(this);                        Schedule s = unit.getSchedule(ts);                        if ( s == null ) {                            complain(LOG_WARNING, CH_USP, "Unable to get the schedule for " + ts.getName());                            } else {                            s.addListener(tsProxy);            }        }                unit.getAC().addListener(this);    }        public void controlSignalChanged(Thermostat source, Object signal) {            if ( signal instanceof Double ) {                    send("status:unit:" + unit.getName() + ":ac:signal:" + source.getName() + ":" + signal);                } else {                    // VT: FIXME: Make sure the receiving end understands this                    send("status:unit:" + unit.getName() + ":ac:signal:" + source.getName() + ":E " + signal);        }    }        public void enabledStateChanged(Thermostat source, boolean enabled) {            send("status:unit:" + unit.getName() + ":zone:" + source.getName() + ":on:" + enabled);    }    public void votingStateChanged(Thermostat source, boolean voting) {            send("status:unit:" + unit.getName() + ":zone:" + source.getName() + ":voting:" + voting);    }        public void holdStateChanged(Thermostat source, boolean hold) {            send("status:unit:" + unit.getName() + ":zone:" + source.getName() + ":hold:" + hold);    }    public void throttleChanged(Damper d, double throttle) {            // VT: FIXME: So far, no notion of the actual position is there, so        // we'll just double the requested position.                send("status:unit:" + unit.getName() + ":zone:"             + unit.getDamperController().getThermostat(d).getName()             + ":damper:status:"             + throttle             + "/"             + throttle);    }        public AC getAC() {            throw new Error("Not Implemented");    }    public DamperController getDamperController() {            throw new Error("Not Implemented");    }    public Iterator iterator() {            throw new Error("Not Implemented");    }        public int getZoneCount() {            return unit.getZoneCount();    }    public String getName() {            return unit.getName();    }        public void renderReflection(PrintWriter pw) {            pw.println("    <unit name=\"" + getName() + "\">");                for ( Iterator i = tsMap.keySet().iterator(); i.hasNext(); ) {                    ((ThermostatServerProxy)i.next()).renderReflection(pw);        }                pw.println("    </unit>");    }        public void dumpState(PrintWriter pw) {            // Only the HVAC mode can be determined at this point                pw.println("status:unit:" + unit.getName() + ":ac:mode:" + unit.getAC().getMode());                // Dump the zone state                for ( Iterator i = tsMap.keySet().iterator(); i.hasNext(); ) {                    ((ThermostatServerProxy)i.next()).dumpState(pw);        }            }        public void listen(PrintWriter pw) {            super.listen(pw);        for ( Iterator i = tsMap.keySet().iterator(); i.hasNext(); ) {                    ((ThermostatServerProxy)i.next()).listen(pw);        }    }        public void parse(StringTokenizer st) throws Throwable {            String target = st.nextToken();                if ( target.equals("zone") ) {                    String zoneName = st.nextToken();                        for ( Iterator i = tsMap.keySet().iterator(); i.hasNext(); ) {                            ThermostatServerProxy proxy = (ThermostatServerProxy)i.next();                                if ( proxy.getName().equals(zoneName) ) {                                    proxy.parse(st);                    return;                }            }                        throw new IllegalArgumentException("Non-existent zone '" + zoneName + "'");        } else if ( target.equals("ac") ) {                    String op = st.nextToken();                        if ( op.equals("mode") ) {                            int mode = Integer.parseInt(st.nextToken());                                unit.getAC().setMode(mode);                            } else {                            throw new IllegalArgumentException("Unknown AC operation: '" + op + "'");                        }        } else {                    throw new IllegalArgumentException("Unknown target '" + target + "'");        }        }    public void modeChanged(AC source, int mode) {            send("status:unit:" + unit.getName() + ":ac:mode:" + mode);    }        public void stageChanged(AC source, int stage) {            send("status:unit:" + unit.getName() + ":ac:stage:" + stage);    }        public void demandChanged(AC source, double demand) {            send("status:unit:" + unit.getName() + ":ac:demand:" + demand);    }    public void fanSpeedChanged(AC source, double speed) {            send("status:unit:" + unit.getName() + ":ac:fan:" + speed);    }        public Schedule getSchedule(Thermostat ts) {            return unit.getSchedule(ts);    }}

⌨️ 快捷键说明

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