@Override public void run() { if (openSprinklerDevice != null) { if (openSprinklerDevice.isConnected()) { logger.debug("Refreshing state with the OpenSprinkler device."); try { if (openSprinklerDevice.isRainDetected()) { updateState(new ChannelUID(getThing().getUID(), SENSOR_RAIN), OnOffType.ON); } else { updateState(new ChannelUID(getThing().getUID(), SENSOR_RAIN), OnOffType.OFF); } for (int i = 0; i < openSprinklerDevice.getNumberOfStations(); i++) { ChannelUID channel = new ChannelUID(getThing().getUID(), Station.get(i).channelID()); State command = getStationState(i); updateState(channel, command); } updateStatus(ThingStatus.ONLINE); } catch (Exception exp) { updateStatus( ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Could not refresh current state from the OpenSprinkler."); logger.debug( "Could not refresh current state of the OpenSprinkler device. Exception received: {}", exp.toString()); } } else { updateStatus( ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Could not sync status with the OpenSprinkler."); } } }
@Override public void initialize() { openSprinklerConfig = getConfig().as(OpenSprinklerConfig.class); logger.debug( "Initializing OpenSprinkler with config (Hostname: {}, Port: {}, Password: {}, Refresh: {}).", openSprinklerConfig.hostname, openSprinklerConfig.port, openSprinklerConfig.password, openSprinklerConfig.refresh); if (openSprinklerConfig == null) { updateStatus( ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "Could not parse the config for the OpenSprinkler."); return; } try { openSprinklerDevice = OpenSprinklerApiFactory.getHttpApi( openSprinklerConfig.hostname, openSprinklerConfig.port, openSprinklerConfig.password); } catch (Exception exp) { updateStatus( ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Could not create a connection to the OpenSprinkler."); logger.debug( "Could not create API connection to the OpenSprinkler device. Exception received: {}", exp.toString()); return; } logger.debug("Successfully created API connection to the OpenSprinkler device."); try { openSprinklerDevice.openConnection(); } catch (Exception exp) { updateStatus( ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Could not open the connection to the OpenSprinkler."); logger.debug( "Could not open API connection to the OpenSprinkler device. Exception received: {}", exp.toString()); } if (openSprinklerDevice.isConnected()) { updateStatus(ThingStatus.ONLINE); logger.debug("OpenSprinkler connected."); } else { updateStatus( ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Could not initialize the connection to the OpenSprinkler."); return; } onUpdate(); }