1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import com.jpeterson.x10.Gateway; 40 import com.jpeterson.x10.GatewayException; 41 import com.jpeterson.x10.SerialGateway; 42 import com.jpeterson.x10.Transmitter; 43 import com.jpeterson.x10.event.AddressEvent; 44 import com.jpeterson.x10.event.OffEvent; 45 import com.jpeterson.x10.event.OnEvent; 46 import com.jpeterson.x10.event.X10Event; 47 import com.jpeterson.x10.module.CM11A; 48 import com.jpeterson.x10.module.CM17A; 49 import net.sourceforge.cruisecontrol.CruiseControlException; 50 import net.sourceforge.cruisecontrol.Publisher; 51 import net.sourceforge.cruisecontrol.util.ValidationHelper; 52 import net.sourceforge.cruisecontrol.util.XMLLogHelper; 53 import org.apache.log4j.Logger; 54 import org.jdom.Element; 55 56 import java.io.IOException ; 57 58 129 public class X10Publisher implements Publisher { 130 private static final Logger LOG = Logger.getLogger(X10Publisher.class); 131 132 private String houseCode; 133 private String deviceCode; 134 private String port; 135 private boolean onWhenBroken = true; 136 private String interfaceModel; 137 138 public void publish(Element cruisecontrolLog) 139 throws CruiseControlException { 140 141 XMLLogHelper logHelper = new XMLLogHelper(cruisecontrolLog); 142 handleBuild(!logHelper.isBuildSuccessful()); 143 } 144 145 public void handleBuild(boolean isBroken) throws CruiseControlException { 146 if ((isBroken && onWhenBroken) || (!isBroken && !onWhenBroken)) { 147 turnOn(); 148 } else { 149 turnOff(); 150 } 151 } 152 153 public void turnOn() throws CruiseControlException { 154 final char houseCodeChar = houseCode.charAt(0); 155 final int deviceCodeInt = Integer.valueOf(deviceCode).intValue(); 156 157 X10Event[] events = new X10Event[ 2 ]; 158 events[ 0 ] = new AddressEvent(this, houseCodeChar, deviceCodeInt); 159 events[ 1 ] = new OnEvent(this, houseCodeChar); 160 161 send(events); 162 } 163 164 public void turnOff() throws CruiseControlException { 165 final char houseCodeChar = houseCode.charAt(0); 166 final int deviceCodeInt = Integer.valueOf(deviceCode).intValue(); 167 168 X10Event[] events = new X10Event[ 2 ]; 169 events[ 0 ] = new AddressEvent(this, houseCodeChar, deviceCodeInt); 170 events[ 1 ] = new OffEvent(this, houseCodeChar); 171 172 send(events); 173 174 } 175 176 private void send(X10Event[] events) throws CruiseControlException { 177 LOG.info("Sending X10 events..."); 178 Transmitter transmitter = getTransmitter(); 179 if (port != null) { 180 ((SerialGateway) transmitter).setPortName(port); 181 } 182 try { 183 ((Gateway) transmitter).allocate(); 184 } catch (Exception e) { 185 throw new CruiseControlException("Trouble allocating the x10 gateway.", e); 186 } 187 188 for (int j = 0; j < events.length; j++) { 189 LOG.debug("Transmitting: " + events[ j ]); 190 try { 191 transmitter.transmit(events[ j ]); 192 } catch (IOException e) { 193 throw new CruiseControlException("Trouble transmitting event " + events[ j ], e); 194 } 195 } 196 197 if (transmitter instanceof Gateway) { 198 Gateway gateway = (Gateway) transmitter; 199 200 try { 201 LOG.debug("Wait for empty queue..."); 202 gateway.waitGatewayState(Transmitter.QUEUE_EMPTY); 203 LOG.debug("Done"); 204 } catch (InterruptedException e) { 205 } 206 207 LOG.debug("Deallocating..."); 208 try { 209 gateway.deallocate(); 210 } catch (GatewayException e) { 211 LOG.warn("Error deallocation gateway: " + e.getMessage(), e); 212 } 213 LOG.debug("Done"); 214 } 215 LOG.debug("Done sending X10 events..."); 216 } 217 218 protected Transmitter getTransmitter() throws CruiseControlException { 219 if (interfaceModel != null && interfaceModel.equalsIgnoreCase("CM17A")) { 220 return new CM17A(); 221 } else if (interfaceModel == null || interfaceModel.equals("") || interfaceModel.equalsIgnoreCase("CM11A")) { 222 return new CM11A(); 223 } else { 224 throw new CruiseControlException("Unknown interface model specified [" + interfaceModel + "]."); 225 } 226 } 227 228 public void validate() throws CruiseControlException { 229 ValidationHelper.assertFalse(houseCode == null || deviceCode == null, 230 "Both houseCode and deviceCode are required fields."); 231 232 ValidationHelper.assertTrue(isLegalHouseCode(houseCode), 233 "The house code must be a single alphabetic " 234 + "letter between A and P, inclusive. You specified [" 235 + houseCode 236 + "]."); 237 238 ValidationHelper.assertTrue(isLegalDeviceCode(deviceCode), 239 "The device code must be an integer between" 240 + " 1 and 16, inclusive. You specified [" 241 + deviceCode + "]"); 242 243 ValidationHelper.assertTrue(isLegalInterfaceModel(interfaceModel), 244 "The interface model must is not a legal value. You specified [" 245 + deviceCode + "]"); 246 } 247 248 private static boolean isLegalInterfaceModel(String model) { 249 return model == null 250 || "".equals(model) 251 || "cm11a".equalsIgnoreCase(model) 252 || "cm17a".equalsIgnoreCase(model); 253 } 254 255 private static boolean isLegalDeviceCode(String deviceCode) { 256 return "1".equals(deviceCode) 257 || "2".equals(deviceCode) 258 || "3".equals(deviceCode) 259 || "4".equals(deviceCode) 260 || "5".equals(deviceCode) 261 || "6".equals(deviceCode) 262 || "7".equals(deviceCode) 263 || "8".equals(deviceCode) 264 || "9".equals(deviceCode) 265 || "10".equals(deviceCode) 266 || "11".equals(deviceCode) 267 || "12".equals(deviceCode) 268 || "13".equals(deviceCode) 269 || "14".equals(deviceCode) 270 || "15".equals(deviceCode) 271 || "16".equals(deviceCode); 272 } 273 274 private static boolean isLegalHouseCode(String houseCode) { 275 return "A".equalsIgnoreCase(houseCode) 276 || "B".equalsIgnoreCase(houseCode) 277 || "C".equalsIgnoreCase(houseCode) 278 || "D".equalsIgnoreCase(houseCode) 279 || "E".equalsIgnoreCase(houseCode) 280 || "F".equalsIgnoreCase(houseCode) 281 || "G".equalsIgnoreCase(houseCode) 282 || "H".equalsIgnoreCase(houseCode) 283 || "I".equalsIgnoreCase(houseCode) 284 || "J".equalsIgnoreCase(houseCode) 285 || "K".equalsIgnoreCase(houseCode) 286 || "L".equalsIgnoreCase(houseCode) 287 || "M".equalsIgnoreCase(houseCode) 288 || "N".equalsIgnoreCase(houseCode) 289 || "O".equalsIgnoreCase(houseCode) 290 || "P".equalsIgnoreCase(houseCode); 291 } 292 293 public void setHouseCode(String code) { 294 this.houseCode = code; 295 } 296 297 public void setDeviceCode(String code) { 298 this.deviceCode = code; 299 } 300 301 305 public void setPort(String portName) { 306 this.port = portName; 307 } 308 309 public void setOnWhenBroken(boolean shouldTurnOn) { 310 this.onWhenBroken = shouldTurnOn; 311 } 312 313 public void setInterfaceModel(String model) { 314 this.interfaceModel = model; 315 } 316 } 317 | Popular Tags |