use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class MpdBinding method internalReceiveCommand.
/**
* @{inheritDoc}
*/
@Override
public void internalReceiveCommand(String itemName, Command command) {
MpdBindingProvider provider;
String matchingPlayerCommand;
// nothing by default
Object params = new Object();
if (command instanceof PercentType) {
// we have received volume adjustment request
matchingPlayerCommand = "PERCENT";
params = command;
} else if (command instanceof DecimalType) {
// we have received play song id request
matchingPlayerCommand = "NUMBER";
params = command;
} else {
matchingPlayerCommand = command.toString();
}
provider = findFirstMatchingBindingProvider(itemName, matchingPlayerCommand);
if (provider == null) {
logger.warn("Cannot find matching binding provider [itemName={}, command={}]", itemName, command);
return;
}
String playerCommand = provider.getPlayerCommand(itemName, matchingPlayerCommand);
if (StringUtils.isNotBlank(playerCommand)) {
String playerCommandParam = provider.getPlayerCommandParam(itemName, matchingPlayerCommand);
if (playerCommandParam != null) {
params = playerCommandParam;
}
executePlayerCommand(playerCommand, params);
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class TACmiBinding method execute.
/**
* @{inheritDoc
*/
@Override
protected void execute() {
logger.trace("execute() method is called!");
try {
clientSocket.setBroadcast(true);
clientSocket.setSoTimeout(120000);
byte[] receiveData = new byte[14];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
byte[] data = receivePacket.getData();
Message message;
if (data[1] > 0) {
logger.debug("Processing analog message");
message = new AnalogMessage(data);
} else if (data[1] == 0) {
logger.debug("Processing digital message");
message = new DigitalMessage(data);
} else {
logger.debug("Invalid message received");
return;
}
logger.debug(message.toString());
for (TACmiBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
logger.debug("Processing item: " + itemName);
int portNumber = provider.getPortNumber(itemName);
if (provider.getCanNode(itemName) == message.canNode && provider.getPortType(itemName).equals(message.getType().toString().toLowerCase())) {
if (message.hasPortnumber(portNumber)) {
if (message.getType() == MessageType.A) {
AnalogValue value = ((AnalogMessage) message).getAnalogValue(portNumber);
if (value.value != null) {
logger.debug("Updating item: " + itemName + " with value: " + value.value);
eventPublisher.postUpdate(itemName, new DecimalType(value.value));
}
} else {
OnOffType state = ((DigitalMessage) message).getPortStateAsOnOffType(portNumber);
logger.debug("Updating item {} with state {}", itemName, state);
eventPublisher.postUpdate(itemName, state);
}
} else {
logger.debug("Portnumber {} not included in message", portNumber);
}
} else {
logger.debug("CAN Node does not match");
}
}
}
} catch (SocketTimeoutException te) {
logger.info("Receive timeout on CoE socket, retrying ...");
} catch (Exception e) {
logger.error("Error in execute: ", e);
}
logger.trace("TACmi execute() finished");
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class SonanceBinding method sendVolumeCommand.
/**
* Send volume commands to groups (music zones)
*
* @param itemName
* item name to send update to
* @param command
* Sonance IP code to execute
* @param outToServer
* date output stream we can write to
* @param i
* bufered reader where we can read from
* @throws IOException
* throws an exception when we can't reach to amplifier
*/
private void sendVolumeCommand(String itemName, String command, DataOutputStream outToServer, BufferedReader i) throws IOException {
// Response is always 50 characters
char[] cbuf = new char[50];
logger.debug("Sending volume command {}", command);
outToServer.write(hexStringToByteArray(command));
i.read(cbuf, 0, 50);
Matcher m = volumePattern.matcher(new String(cbuf));
if (m.find()) {
String volume = m.group(1);
eventPublisher.postUpdate(itemName, new DecimalType(volume));
logger.debug("Setting volume for item {} on {}", itemName, volume);
} else {
logger.error("Error sending regular volume command {}, received this: {}", command, new String(cbuf));
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class SonanceBinding method setVolumeCommand.
/**
* Sets the group to the specified target volume. Amplifier doesn't support
* direct volume commands, so a loop is needed
*
* @param itemName
* item to publish result to
* @param group
* target group
* @param targetVolume
* target volume
* @param outToServer
* data output stream where we can write to
* @param i
* buffered reader where we can read from
* @param endpoint
* ip:port
* @throws IOException
* throws an IOException when we can't reach the amplifier
*/
private void setVolumeCommand(String itemName, String group, int targetVolume, DataOutputStream outToServer, BufferedReader i, String endpoint) throws IOException {
// Response is always 50 characters
char[] cbuf = new char[50];
String question = String.format("%s%s%s", SonanceConsts.DIRECT_VOLUME_QUERY, Integer.toHexString(183 + targetVolume), group);
logger.trace("Sending this to amplifier: {}", question);
outToServer.write(hexStringToByteArray(question));
i.read(cbuf, 0, 50);
String result = new String(cbuf);
logger.trace("Received this as response : {}", result);
Matcher m = volumePattern.matcher(result);
if (m.find()) {
double currentVolume = Integer.parseInt(m.group(1));
eventPublisher.postUpdate(itemName, new DecimalType(currentVolume));
logger.debug("Updating {} with new volume {}", itemName, currentVolume);
} else {
logger.error("Error sending volume command, received this: {}", new String(cbuf));
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class SqueezeboxBinding method internalReceiveCommand.
/**
* @{inheritDoc}
*/
@Override
public void internalReceiveCommand(String itemName, Command command) {
if (squeezeServer == null) {
logger.warn("Squeeze Server not initialised or configured yet, ignoring command '{}' for item '{}'", command.toString(), itemName);
return;
}
logger.trace("internalReceiveCommand(itemname = {}, command = {})", itemName, command.toString());
for (SqueezeboxBindingProvider provider : providers) {
SqueezeboxBindingConfig bindingConfig = provider.getSqueezeboxBindingConfig(itemName);
String playerId = bindingConfig.getPlayerId();
try {
switch(bindingConfig.getCommandType()) {
case POWER:
if (command.equals(OnOffType.ON)) {
squeezeServer.powerOn(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.powerOff(playerId);
}
break;
case MUTE:
if (command.equals(OnOffType.ON)) {
squeezeServer.mute(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.unMute(playerId);
}
break;
case VOLUME:
if (command.equals(IncreaseDecreaseType.INCREASE)) {
squeezeServer.volumeUp(playerId);
} else if (command.equals(IncreaseDecreaseType.DECREASE)) {
squeezeServer.volumeDown(playerId);
} else if (command.equals(UpDownType.UP)) {
squeezeServer.volumeUp(playerId);
} else if (command.equals(UpDownType.DOWN)) {
squeezeServer.volumeDown(playerId);
} else if (command instanceof DecimalType) {
squeezeServer.setVolume(playerId, ((DecimalType) command).intValue());
}
break;
case PLAY:
if (command.equals(OnOffType.ON)) {
squeezeServer.play(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.stop(playerId);
}
break;
case PAUSE:
if (command.equals(OnOffType.ON)) {
squeezeServer.pause(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.unPause(playerId);
}
break;
case STOP:
if (command.equals(OnOffType.ON)) {
squeezeServer.stop(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.play(playerId);
}
break;
case NEXT:
if (command.equals(OnOffType.ON)) {
squeezeServer.next(playerId);
}
break;
case PREV:
if (command.equals(OnOffType.ON)) {
squeezeServer.prev(playerId);
}
break;
case HTTP:
if (command.equals(OnOffType.ON)) {
squeezeServer.playUrl(playerId, "http://" + bindingConfig.getExtra());
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.stop(playerId);
}
break;
case FILE:
if (command.equals(OnOffType.ON)) {
squeezeServer.playUrl(playerId, "file://" + bindingConfig.getExtra());
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.stop(playerId);
}
break;
case SYNC:
if (command.equals(OnOffType.ON)) {
squeezeServer.syncPlayer(playerId, bindingConfig.getExtra());
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.unSyncPlayer(bindingConfig.getExtra());
}
break;
case COMMAND:
if (command instanceof StringType) {
squeezeServer.playerCommand(playerId, command.toString());
} else {
squeezeServer.playerCommand(playerId, bindingConfig.getExtra());
}
break;
default:
logger.warn("Unsupported command type '{}'", bindingConfig.getCommandType());
}
} catch (Exception e) {
logger.warn("Error executing command type '{}'", bindingConfig.getCommandType(), e);
}
}
}
Aggregations