📄 onewireacdriver.java
字号:
// VT: FIXME: Make this configurable if ( false ) { socket.close(); socket = null; in = null; out = null; throw new IOException("Required devices not present:" + list); } else { complain(LOG_WARNING, CH_OWAC, "Required devices not present:" + list); } } } catch ( NoSuchElementException nseex ) { throw new IOException("Malformed input: '" + header + "'"); } complain(LOG_INFO, CH_OWAC, "Connected to " + remoteHost + ":" + remotePort); // Restore the hardware state if it is available if ( mode != null ) { complain(LOG_NOTICE, CH_OWAC, "Restoring mode: " + mode); setMode(mode.intValue()); } if ( speed != null ) { complain(LOG_NOTICE, CH_OWAC, "Restoring fan speed: " + speed); setFanSpeed(speed.doubleValue()); } if ( stage != null ) { complain(LOG_NOTICE, CH_OWAC, "Restoring stage: " + stage); setStage(stage.intValue()); } } finally { notifyAll(); } } protected void execute() throws Throwable { // Keep the socket connection alive while ( isEnabled() ) { heartbeat(); Thread.sleep(10000); } } protected synchronized void shutdown(Throwable cause) throws Throwable { complain(LOG_INFO, CH_OWAC, "Shutting down the A/C...", new Exception("Call stack trace")); // Shut down the A/C setStage(0); setFanSpeed(0); setMode(0); // Close the socket socket.close(); complain(LOG_INFO, CH_OWAC, "A/C shut down"); } /** * Try to send the command, give up only if the service is stopped. * * @param command Command to send. */ private synchronized void send(String command) throws IOException { complain(LOG_DEBUG, CH_OWAC, "send: '" + command + "'"); long retryDelay = initialRetryTimeout; while ( isEnabled() ) { try { if ( in == null || out == null ) { throw new IOException("Socket is not available"); } out.println(command); out.flush(); String response = in.readLine(); if ( response == null ) { socket = null; in = null; out = null; throw new IOException("Connection lost"); } complain(LOG_DEBUG, CH_OWAC, "Response: '" + response + "'"); if ( !"OK".equals(response) ) { throw new IOException("Error sending command '" + command + "', check daemon logs (response: '" + response + "')"); } // We've fulfilled the task return; } catch ( IOException ioex ) { complain(LOG_WARNING, CH_OWAC, "send() failed: " + ioex.getMessage()); try { // Try to be graceful complain(LOG_INFO, CH_OWAC, "Sleeping " + retryDelay + " before retrying"); Thread.sleep(retryDelay); retryDelay *= 2; retryDelay = (retryDelay > maxRetryTimeout) ? maxRetryTimeout : retryDelay; } catch ( InterruptedException iex ) { // VT: FIXME: Can I just ignore it here? } if ( socket == null ) { // This means that the connection was lost try { connect(); } catch ( IOException ioex2 ) { // Can't do anything except complain now complain(LOG_WARNING, CH_OWAC, "Trying to reestablish the connection: " + ioex2.getMessage()); } catch ( Throwable t ) { // VT: Since connect() started throwing Throwable // after implementing SSL support, this must be a // problem with SSL. Damn. Can't do anything about // it... complain(LOG_WARNING, CH_OWAC, "Probably SSL problem, trying to reestablish the connection: " + t.getMessage()); } } else { // Do nothing and hope it will work next time. This is // potentially a dangerous situation because if the // target is unavailable for a long time, we're going to // get in trouble - the system will run amok. } } } } /** * Set the unit mode. * * @param mode Operating mode. -1 is cooling, 0 is off, 1 is heating. * Actually, the switch is only between cooling and heating, but * different units may have different rules (the contact is energized * only for cooling, or only for heating). * * @exception IOException if there was a problem talking to the hardware. * * @exception IllegalArgumentException if the stage is neither -1, 0 nor * 1. */ public void setMode(int mode) throws IOException { // This is required for translation int value = 0; switch ( mode ) { case -1: case 0: // Need to de-energize for cooling value = 0; break; case 1: // Need to energize for heating value = 1; break; default: throw new IllegalArgumentException("Invalid mode: " + mode); } this.mode = new Integer(mode); // THIS IS STUPID: Address and channel have to be parsed once at startup StringTokenizer st = new StringTokenizer(modeSwitchAddress, ":"); String address = st.nextToken(); String channel = st.nextToken(); send(address + " write " + channel + " " + value); } /** * Set the unit stage. * * This method will work with single stage unit only. * * @param stage A/C stage. For single stage units, 0 is off, 1 is on. * For multistage units, it is 0, 1, 2 and so on. * * @exception IOException if there was a problem talking to the hardware. * * @exception IllegalArgumentException if the stage is neither 0 nor 1. */ public void setStage(int stage) throws IOException { switch ( stage ) { case 0: case 1: break; default: throw new IllegalArgumentException("Invalid stage: " + stage); } this.stage = new Integer(stage); // THIS IS STUPID: Address and channel have to be parsed once at startup StringTokenizer st = new StringTokenizer(stageSwitchAddress, ":"); String address = st.nextToken(); String channel = st.nextToken(); send(address + " write " + channel + " " + stage); } /** * Set the fan speed. * * This method will work with single speed fan only. * * @param speed Fan speed. For single speed fan, 0 is off, 1 is on. For * variable speed or multispeed fans, 0 is off, 1 is full, anything in * between defines the actual speed. Since both variable speed and * multispeed fans have some preset values, it is up to the * implementation to define the rounding rules. * * @exception IOException if there was a problem talking to the hardware. * * @exception IllegalArgumentException if the speed is neither 0 nor 1. */ public void setFanSpeed(double speed) throws IOException { if ( speed != 0 && speed != 1 ) { throw new IllegalArgumentException("Unsupported speed: " + speed + " (only 0 and 1 are valid)"); } this.speed = new Double(speed); // THIS IS STUPID: Address and channel have to be parsed once at startup StringTokenizer st = new StringTokenizer(fanSwitchAddress, ":"); String address = st.nextToken(); String channel = st.nextToken(); send(address + " write " + channel + " " + (int)speed); } private void heartbeat() throws IOException { send("heartbeat"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -