use of org.openhab.binding.sonance.SonanceBindingProvider in project openhab1-addons by openhab.
the class SonanceBinding method execute.
/*
* {@inheritDoc}
*/
@Override
protected void execute() {
if (!bindingsExist()) {
logger.debug("There is no existing Sonance binding configuration => refresh cycle aborted!");
return;
}
logger.info("Refreshing all items");
List<String> offlineEndPoints = new ArrayList<String>();
for (SonanceBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
String group = provider.getGroup(itemName);
String ip = provider.getIP(itemName);
int port = provider.getPort(itemName);
String key = ip + ":" + port;
if (!offlineEndPoints.contains(key)) {
try {
if (!socketCache.containsKey(key)) {
socketCache.put(key, new Socket(ip, port));
outputStreamCache.put(key, new DataOutputStream(socketCache.get(key).getOutputStream()));
bufferedReaderCache.put(key, new BufferedReader(new InputStreamReader(socketCache.get(key).getInputStream())));
logger.debug("New socket created ({}:{})", ip, port);
}
if (provider.isMute(itemName)) {
sendMuteCommand(itemName, SonanceConsts.MUTE_QUERY + group, outputStreamCache.get(key), bufferedReaderCache.get(key));
} else if (provider.isVolume(itemName)) {
sendVolumeCommand(itemName, SonanceConsts.VOLUME_QUERY + group, outputStreamCache.get(key), bufferedReaderCache.get(key));
} else if (provider.isPower(itemName)) {
sendPowerCommand(itemName, SonanceConsts.POWER_QUERY, outputStreamCache.get(key), bufferedReaderCache.get(key));
}
} catch (UnknownHostException e) {
logger.error("UnknownHostException occured when connecting to amplifier {}:{}.", ip, port);
} catch (IOException e) {
logger.debug("Amplifier ({},{}) is offline, status can't be updated at this moment.", ip, port);
try {
socketCache.get(key).close();
} catch (Exception ex) {
}
socketCache.remove(key);
outputStreamCache.remove(key);
bufferedReaderCache.remove(key);
// Stop trying to fetch other
offlineEndPoints.add(key);
// values from this end
// point until next execute
// cycle
}
}
}
}
}
use of org.openhab.binding.sonance.SonanceBindingProvider in project openhab1-addons by openhab.
the class SonanceBinding method internalReceiveCommand.
/*
* {@inheritDoc}
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
logger.debug("Command received ({}, {})", itemName, command);
SonanceBindingProvider provider = findFirstMatchingBindingProvider(itemName);
String group = provider.getGroup(itemName);
String ip = provider.getIP(itemName);
int port = provider.getPort(itemName);
Socket s = null;
try {
s = new Socket(ip, port);
DataOutputStream outToServer = new DataOutputStream(s.getOutputStream());
BufferedReader i = new BufferedReader(new InputStreamReader(s.getInputStream()));
if (provider.isMute(itemName)) {
if (command.equals(OnOffType.OFF)) {
sendMuteCommand(itemName, SonanceConsts.MUTE_ON + group, outToServer, i);
} else if (command.equals(OnOffType.ON)) {
sendMuteCommand(itemName, SonanceConsts.MUTE_OFF + group, outToServer, i);
} else {
logger.error("I don't know what to do with the command \"{}\"", command);
}
} else if (provider.isPower(itemName)) {
if (command.equals(OnOffType.OFF)) {
sendPowerCommand(itemName, SonanceConsts.POWER_OFF, outToServer, i);
} else if (command.equals(OnOffType.ON)) {
sendPowerCommand(itemName, SonanceConsts.POWER_ON, outToServer, i);
} else {
logger.error("I don't know what to do with the command \"{}\"", command);
}
} else if (provider.isVolume(itemName)) {
if (command.equals(UpDownType.UP)) {
sendVolumeCommand(itemName, SonanceConsts.VOLUME_UP + group, outToServer, i);
} else if (command.equals(UpDownType.DOWN)) {
sendVolumeCommand(itemName, SonanceConsts.VOLUME_DOWN + group, outToServer, i);
} else {
try {
Double d = Double.parseDouble(command.toString());
setVolumeCommand(itemName, group, d.intValue(), outToServer, i, ip + ":" + port);
} catch (NumberFormatException nfe) {
logger.error("I don't know what to do with the volume command \"{}\" ({})", command, nfe.getMessage());
}
}
}
s.close();
} catch (IOException e) {
logger.debug("IO Exception when sending command. Exception: {}", e.getMessage());
} finally {
closeSilently(s);
}
}
use of org.openhab.binding.sonance.SonanceBindingProvider in project openhab1-addons by openhab.
the class SonanceBinding method internalReceiveUpdate.
/*
* {@inheritDoc}
*/
@Override
protected void internalReceiveUpdate(String itemName, State newState) {
logger.debug("Update received ({},{})", itemName, newState);
SonanceBindingProvider provider = findFirstMatchingBindingProvider(itemName);
String group = provider.getGroup(itemName);
String ip = provider.getIP(itemName);
int port = provider.getPort(itemName);
ip = null;
// cleanup
group = null;
Socket s = null;
try {
s = new Socket(ip, port);
DataOutputStream outToServer = new DataOutputStream(s.getOutputStream());
BufferedReader i = new BufferedReader(new InputStreamReader(s.getInputStream()));
if (provider.isMute(itemName)) {
if (newState.equals(OnOffType.OFF)) {
sendMuteCommand(itemName, SonanceConsts.MUTE_ON + group, outToServer, i);
} else if (newState.equals(OnOffType.ON)) {
sendMuteCommand(itemName, SonanceConsts.MUTE_OFF + group, outToServer, i);
} else {
logger.error("I don't know what to do with this new state \"{}\"", newState);
}
}
if (provider.isPower(itemName)) {
if (newState.equals(OnOffType.OFF)) {
sendPowerCommand(itemName, SonanceConsts.POWER_OFF, outToServer, i);
} else if (newState.equals(OnOffType.ON)) {
sendPowerCommand(itemName, SonanceConsts.POWER_ON, outToServer, i);
} else {
logger.error("I don't know what to do with this new state \"{}\"", newState);
}
} else if (provider.isVolume(itemName)) {
if (newState.equals(IncreaseDecreaseType.INCREASE)) {
sendVolumeCommand(itemName, SonanceConsts.VOLUME_UP + group, outToServer, i);
} else if (newState.equals(IncreaseDecreaseType.DECREASE)) {
sendVolumeCommand(itemName, SonanceConsts.VOLUME_DOWN + group, outToServer, i);
} else {
logger.error("I don't know what to do with this new state \"{}\"", newState);
}
s.close();
}
} catch (IOException e) {
logger.error("IO Exception when received internal command. Message: {}", e.getMessage());
} finally {
closeSilently(s);
}
}
Aggregations