use of org.eclipse.smarthome.core.library.types.StringType in project smarthome by eclipse.
the class ItemUIRegistryImpl method getLabel.
@Override
public String getLabel(Widget w) {
String label = getLabelFromWidget(w);
String itemName = w.getItem();
if (StringUtils.isBlank(itemName)) {
return transform(label, null);
}
String labelMappedOption = null;
State state = null;
StateDescription stateDescription = null;
String formatPattern = getFormatPattern(label);
// (i.e. it contains at least a %)
try {
final Item item = getItem(itemName);
// There is a known issue in the implementation of the method getStateDescription() of class Item
// in the following case:
// - the item provider returns as expected a state description without pattern but with for
// example a min value because a min value is set in the item definition but no label with
// pattern is set.
// - the channel state description provider returns as expected a state description with a pattern
// In this case, the result is no display of value by UIs because no pattern is set in the
// returned StateDescription. What is expected is the display of a value using the pattern
// provided by the channel state description provider.
stateDescription = item.getStateDescription();
if (formatPattern == null && stateDescription != null && stateDescription.getPattern() != null) {
label = label + " [" + stateDescription.getPattern() + "]";
}
String updatedPattern = getFormatPattern(label);
if (updatedPattern != null) {
formatPattern = updatedPattern;
// a number is requested, PercentType must not be converted to DecimalType:
if (formatPattern.contains("%d") && !(item.getState() instanceof PercentType)) {
state = item.getStateAs(DecimalType.class);
} else {
state = item.getState();
}
}
} catch (ItemNotFoundException e) {
logger.error("Cannot retrieve item for widget {}", w.eClass().getInstanceTypeName());
}
if (formatPattern != null) {
if (formatPattern.isEmpty()) {
label = label.substring(0, label.indexOf("[")).trim();
} else {
if (state == null || state instanceof UnDefType) {
formatPattern = formatUndefined(formatPattern);
} else if (state instanceof Type) {
// if the channel contains options, we build a label with the mapped option value
if (stateDescription != null && stateDescription.getOptions() != null) {
for (StateOption option : stateDescription.getOptions()) {
if (option.getValue().equals(state.toString()) && option.getLabel() != null) {
State stateOption = new StringType(option.getLabel());
try {
String formatPatternOption = stateOption.format(formatPattern);
labelMappedOption = label.trim();
labelMappedOption = labelMappedOption.substring(0, labelMappedOption.indexOf("[") + 1) + formatPatternOption + "]";
} catch (IllegalArgumentException e) {
logger.debug("Mapping option value '{}' for item {} using format '{}' failed ({}); mapping is ignored", stateOption, itemName, formatPattern, e.getMessage());
labelMappedOption = null;
}
break;
}
}
}
if (state instanceof QuantityType) {
QuantityType<?> quantityState = (QuantityType<?>) state;
// sanity convert current state to the item state description unit in case it was updated in the
// meantime. The item state is still in the "original" unit while the state description will
// display the new unit:
Unit<?> patternUnit = UnitUtils.parseUnit(formatPattern);
if (patternUnit != null && !quantityState.getUnit().equals(patternUnit)) {
quantityState = quantityState.toUnit(patternUnit);
}
// The widget may define its own unit in the widget label. Convert to this unit:
quantityState = convertStateToWidgetUnit(quantityState, w);
state = quantityState;
}
// This also handles IllegalFormatConversionException, which is a subclass of IllegalArgument.
try {
formatPattern = fillFormatPattern(formatPattern, state);
} catch (IllegalArgumentException e) {
logger.warn("Exception while formatting value '{}' of item {} with format '{}': {}", state, itemName, formatPattern, e.getMessage());
formatPattern = new String("Err");
}
}
label = label.trim();
label = label.substring(0, label.indexOf("[") + 1) + formatPattern + "]";
}
}
return transform(label, labelMappedOption);
}
use of org.eclipse.smarthome.core.library.types.StringType in project smarthome by eclipse.
the class ZonePlayerHandler method playPlayList.
public void playPlayList(Command command) {
if (command != null && command instanceof StringType) {
String playlist = command.toString();
List<SonosEntry> playlists = getPlayLists();
SonosEntry theEntry = null;
// search for the appropriate play list based on its name (title)
for (SonosEntry somePlaylist : playlists) {
if (somePlaylist.getTitle().equals(playlist)) {
theEntry = somePlaylist;
break;
}
}
// set the URI of the group coordinator
if (theEntry != null) {
try {
ZonePlayerHandler coordinator = getCoordinatorHandler();
coordinator.addURIToQueue(theEntry);
coordinator.setCurrentURI(QUEUE_URI + coordinator.getUDN() + "#0", "");
String firstTrackNumberEnqueued = stateMap.get("FirstTrackNumberEnqueued");
if (firstTrackNumberEnqueued != null) {
coordinator.seek("TRACK_NR", firstTrackNumberEnqueued);
}
coordinator.play();
} catch (IllegalStateException e) {
logger.debug("Cannot play playlist ({})", e.getMessage());
}
} else {
logger.debug("Playlist '{}' not found", playlist);
}
}
}
use of org.eclipse.smarthome.core.library.types.StringType in project smarthome by eclipse.
the class ZonePlayerHandler method updateChannel.
protected void updateChannel(String channelId) {
if (!isLinked(channelId)) {
return;
}
String url;
State newState = UnDefType.UNDEF;
switch(channelId) {
case STATE:
if (stateMap.get("TransportState") != null) {
newState = new StringType(stateMap.get("TransportState"));
}
break;
case CONTROL:
if (stateMap.get("TransportState") != null) {
if (stateMap.get("TransportState").equals(STATE_PLAYING)) {
newState = PlayPauseType.PLAY;
} else if (stateMap.get("TransportState").equals(STATE_STOPPED)) {
newState = PlayPauseType.PAUSE;
} else if (stateMap.get("TransportState").equals(STATE_PAUSED_PLAYBACK)) {
newState = PlayPauseType.PAUSE;
}
}
break;
case STOP:
if (stateMap.get("TransportState") != null) {
if (stateMap.get("TransportState").equals(STATE_STOPPED)) {
newState = OnOffType.ON;
} else {
newState = OnOffType.OFF;
}
}
break;
case SHUFFLE:
if (stateMap.get("CurrentPlayMode") != null) {
newState = isShuffleActive() ? OnOffType.ON : OnOffType.OFF;
}
break;
case REPEAT:
if (stateMap.get("CurrentPlayMode") != null) {
newState = new StringType(getRepeatMode());
}
break;
case LED:
if (stateMap.get("CurrentLEDState") != null) {
newState = stateMap.get("CurrentLEDState").equals("On") ? OnOffType.ON : OnOffType.OFF;
}
break;
case ZONENAME:
if (stateMap.get("CurrentZoneName") != null) {
newState = new StringType(stateMap.get("CurrentZoneName"));
}
break;
case ZONEGROUPID:
if (stateMap.get("LocalGroupUUID") != null) {
newState = new StringType(stateMap.get("LocalGroupUUID"));
}
break;
case COORDINATOR:
newState = new StringType(getCoordinator());
break;
case LOCALCOORDINATOR:
if (stateMap.get("GroupCoordinatorIsLocal") != null) {
newState = stateMap.get("GroupCoordinatorIsLocal").equals("true") ? OnOffType.ON : OnOffType.OFF;
}
break;
case VOLUME:
if (stateMap.get("VolumeMaster") != null) {
newState = new PercentType(stateMap.get("VolumeMaster"));
}
break;
case MUTE:
if (stateMap.get("MuteMaster") != null) {
newState = stateMap.get("MuteMaster").equals("1") ? OnOffType.ON : OnOffType.OFF;
}
break;
case LINEIN:
if (stateMap.get("LineInConnected") != null) {
newState = stateMap.get("LineInConnected").equals("true") ? OnOffType.ON : OnOffType.OFF;
} else if (stateMap.get("TOSLinkConnected") != null) {
newState = stateMap.get("TOSLinkConnected").equals("true") ? OnOffType.ON : OnOffType.OFF;
}
break;
case ALARMRUNNING:
if (stateMap.get("AlarmRunning") != null) {
newState = stateMap.get("AlarmRunning").equals("1") ? OnOffType.ON : OnOffType.OFF;
}
break;
case ALARMPROPERTIES:
if (stateMap.get("RunningAlarmProperties") != null) {
newState = new StringType(stateMap.get("RunningAlarmProperties"));
}
break;
case CURRENTTRACK:
if (stateMap.get("CurrentURIFormatted") != null) {
newState = new StringType(stateMap.get("CurrentURIFormatted"));
}
break;
case CURRENTTITLE:
if (stateMap.get("CurrentTitle") != null) {
newState = new StringType(stateMap.get("CurrentTitle"));
}
break;
case CURRENTARTIST:
if (stateMap.get("CurrentArtist") != null) {
newState = new StringType(stateMap.get("CurrentArtist"));
}
break;
case CURRENTALBUM:
if (stateMap.get("CurrentAlbum") != null) {
newState = new StringType(stateMap.get("CurrentAlbum"));
}
break;
case CURRENTALBUMART:
newState = null;
updateAlbumArtChannel(false);
break;
case CURRENTALBUMARTURL:
url = getAlbumArtUrl();
if (url != null) {
newState = new StringType(url);
}
break;
case CURRENTTRANSPORTURI:
if (stateMap.get("CurrentURI") != null) {
newState = new StringType(stateMap.get("CurrentURI"));
}
break;
case CURRENTTRACKURI:
if (stateMap.get("CurrentTrackURI") != null) {
newState = new StringType(stateMap.get("CurrentTrackURI"));
}
break;
case TUNEINSTATIONID:
if (stateMap.get("CurrentTuneInStationId") != null) {
newState = new StringType(stateMap.get("CurrentTuneInStationId"));
}
break;
default:
newState = null;
break;
}
if (newState != null) {
updateState(channelId, newState);
}
}
use of org.eclipse.smarthome.core.library.types.StringType in project smarthome by eclipse.
the class ZonePlayerHandler method playTuneinStation.
public void playTuneinStation(Command command) {
if (command instanceof StringType) {
String stationId = command.toString();
List<SonosMusicService> allServices = getAvailableMusicServices();
SonosMusicService tuneinService = null;
// search for the TuneIn music service based on its name
if (allServices != null) {
for (SonosMusicService service : allServices) {
if (service.getName().equals("TuneIn")) {
tuneinService = service;
break;
}
}
}
// set the URI of the group coordinator
if (tuneinService != null) {
try {
ZonePlayerHandler coordinator = getCoordinatorHandler();
SonosEntry entry = new SonosEntry("", "TuneIn station", "", "", "", "", "object.item.audioItem.audioBroadcast", String.format(TUNEIN_URI, stationId, tuneinService.getId()));
entry.setDesc("SA_RINCON" + tuneinService.getType().toString() + "_");
coordinator.setCurrentURI(entry);
coordinator.play();
} catch (IllegalStateException e) {
logger.debug("Cannot play TuneIn station {} ({})", stationId, e.getMessage());
}
} else {
logger.debug("TuneIn service not found");
}
}
}
use of org.eclipse.smarthome.core.library.types.StringType in project smarthome by eclipse.
the class NtpHandler method refreshTimeDate.
private synchronized void refreshTimeDate() {
if (timeZone != null && locale != null) {
long networkTimeInMillis;
if (refreshNtpCount <= 0) {
networkTimeInMillis = getTime(hostname);
timeOffset = networkTimeInMillis - System.currentTimeMillis();
logger.debug("{} delta system time: {}", getThing().getUID(), timeOffset);
refreshNtpCount = refreshNtp.intValue();
} else {
networkTimeInMillis = System.currentTimeMillis() + timeOffset;
refreshNtpCount--;
}
ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(networkTimeInMillis), timeZone.toZoneId());
updateState(dateTimeChannelUID, new DateTimeType(zoned));
updateState(stringChannelUID, new StringType(dateTimeFormat.format(zoned)));
} else {
logger.debug("Not refreshing, since we do not seem to be initialized yet");
}
}
Aggregations