Search in sources :

Example 91 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class DefaultChartProvider method addItem.

boolean addItem(Chart chart, QueryablePersistenceService service, Date timeBegin, Date timeEnd, Item item, int seriesCounter, ChartTheme chartTheme, int dpi) {
    Color color = chartTheme.getLineColor(seriesCounter);
    // Get the item label
    String label = null;
    if (itemUIRegistry != null) {
        // Get the item label
        label = itemUIRegistry.getLabel(item.getName());
        if (label != null && label.contains("[") && label.contains("]")) {
            label = label.substring(0, label.indexOf('['));
        }
    }
    if (label == null) {
        label = item.getName();
    }
    Iterable<HistoricItem> result;
    FilterCriteria filter;
    // Generate data collections
    List<Date> xData = new ArrayList<Date>();
    List<Number> yData = new ArrayList<Number>();
    // Declare state here so it will hold the last value at the end of the process
    State state = null;
    // First, get the value at the start time.
    // This is necessary for values that don't change often otherwise data will start
    // after the start of the graph (or not at all if there's no change during the graph period)
    filter = new FilterCriteria();
    filter.setEndDate(ZonedDateTime.ofInstant(timeBegin.toInstant(), timeZoneProvider.getTimeZone()));
    filter.setItemName(item.getName());
    filter.setPageSize(1);
    filter.setOrdering(Ordering.DESCENDING);
    result = service.query(filter);
    if (result.iterator().hasNext()) {
        HistoricItem historicItem = result.iterator().next();
        state = historicItem.getState();
        xData.add(timeBegin);
        yData.add(convertData(state));
    }
    // Now, get all the data between the start and end time
    filter.setBeginDate(ZonedDateTime.ofInstant(timeBegin.toInstant(), timeZoneProvider.getTimeZone()));
    filter.setEndDate(ZonedDateTime.ofInstant(timeEnd.toInstant(), timeZoneProvider.getTimeZone()));
    filter.setPageSize(Integer.MAX_VALUE);
    filter.setOrdering(Ordering.ASCENDING);
    // Get the data from the persistence store
    result = service.query(filter);
    Iterator<HistoricItem> it = result.iterator();
    // Iterate through the data
    while (it.hasNext()) {
        HistoricItem historicItem = it.next();
        // to avoid diagonal lines
        if (state instanceof OnOffType || state instanceof OpenClosedType) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(historicItem.getTimestamp());
            cal.add(Calendar.MILLISECOND, -1);
            xData.add(cal.getTime());
            yData.add(convertData(state));
        }
        state = historicItem.getState();
        xData.add(historicItem.getTimestamp());
        yData.add(convertData(state));
    }
    // Lastly, add the final state at the endtime
    if (state != null) {
        xData.add(timeEnd);
        yData.add(convertData(state));
    }
    // The chart engine will throw an exception if there's no data
    if (xData.size() == 0) {
        return false;
    }
    // If there's only 1 data point, plot it again!
    if (xData.size() == 1) {
        xData.add(xData.iterator().next());
        yData.add(yData.iterator().next());
    }
    Series series = chart.addSeries(label, xData, yData);
    float lineWidth = (float) chartTheme.getLineWidth(dpi);
    series.setLineStyle(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
    series.setMarker(SeriesMarker.NONE);
    series.setLineColor(color);
    // We use this to decide whether to put the legend in the top or bottom corner.
    if (yData.iterator().next().floatValue() > ((series.getYMax() - series.getYMin()) / 2 + series.getYMin())) {
        legendPosition++;
    } else {
        legendPosition--;
    }
    return true;
}
Also used : BasicStroke(java.awt.BasicStroke) Color(java.awt.Color) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) FilterCriteria(org.eclipse.smarthome.core.persistence.FilterCriteria) Date(java.util.Date) Series(org.knowm.xchart.Series) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) State(org.eclipse.smarthome.core.types.State) OpenClosedType(org.eclipse.smarthome.core.library.types.OpenClosedType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Example 92 with State

use of org.eclipse.smarthome.core.types.State 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);
    }
}
Also used : StringType(org.eclipse.smarthome.core.library.types.StringType) SonosZonePlayerState(org.eclipse.smarthome.binding.sonos.internal.SonosZonePlayerState) State(org.eclipse.smarthome.core.types.State) PercentType(org.eclipse.smarthome.core.library.types.PercentType)

Example 93 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class CommunicationManager method receiveUpdate.

private void receiveUpdate(ItemStateEvent updateEvent) {
    final String itemName = updateEvent.getItemName();
    final State newState = updateEvent.getItemState();
    handleEvent(itemName, newState, updateEvent.getSource(), s -> acceptedStateTypeMap.get(s), (profile, thing, convertedState) -> {
        // 
        safeCaller.create(profile, Profile.class).withAsync().withIdentifier(// 
        thing).withTimeout(// 
        THINGHANDLER_EVENT_TIMEOUT).build().onStateUpdateFromItem(convertedState);
    });
}
Also used : State(org.eclipse.smarthome.core.types.State) StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Profile(org.eclipse.smarthome.core.thing.profiles.Profile)

Example 94 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class ProfileCallbackImpl method sendUpdate.

@Override
public void sendUpdate(State state) {
    Item item = itemProvider.apply(link.getItemName());
    State acceptedState = itemStateConverter.convertToAcceptedState(state, item);
    eventPublisher.post(ItemEventFactory.createStateEvent(link.getItemName(), acceptedState, link.getLinkedUID().toString()));
}
Also used : Item(org.eclipse.smarthome.core.items.Item) State(org.eclipse.smarthome.core.types.State)

Example 95 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class RuleTriggerManager method internalGetRules.

private Iterable<Rule> internalGetRules(TriggerTypes triggerType, Item item, Type oldType, Type newType) {
    List<Rule> result = new ArrayList<>();
    switch(triggerType) {
        case STARTUP:
            return systemStartupTriggeredRules;
        case SHUTDOWN:
            return systemShutdownTriggeredRules;
        case TIMER:
            return timerEventTriggeredRules;
        case UPDATE:
            if (newType instanceof State) {
                List<Class<? extends State>> acceptedDataTypes = item.getAcceptedDataTypes();
                final State state = (State) newType;
                internalGetUpdateRules(item.getName(), false, acceptedDataTypes, state, result);
                for (String groupName : item.getGroupNames()) {
                    internalGetUpdateRules(groupName, true, acceptedDataTypes, state, result);
                }
            }
            break;
        case CHANGE:
            if (newType instanceof State && oldType instanceof State) {
                List<Class<? extends State>> acceptedDataTypes = item.getAcceptedDataTypes();
                final State newState = (State) newType;
                final State oldState = (State) oldType;
                internalGetChangeRules(item.getName(), false, acceptedDataTypes, newState, oldState, result);
                for (String groupName : item.getGroupNames()) {
                    internalGetChangeRules(groupName, true, acceptedDataTypes, newState, oldState, result);
                }
            }
            break;
        case COMMAND:
            if (newType instanceof Command) {
                List<Class<? extends Command>> acceptedCommandTypes = item.getAcceptedCommandTypes();
                final Command command = (Command) newType;
                internalGetCommandRules(item.getName(), false, acceptedCommandTypes, command, result);
                for (String groupName : item.getGroupNames()) {
                    internalGetCommandRules(groupName, true, acceptedCommandTypes, command, result);
                }
            }
            break;
        default:
            break;
    }
    return result;
}
Also used : Command(org.eclipse.smarthome.core.types.Command) State(org.eclipse.smarthome.core.types.State) ArrayList(java.util.ArrayList) Rule(org.eclipse.smarthome.model.rule.rules.Rule)

Aggregations

State (org.eclipse.smarthome.core.types.State)130 Test (org.junit.Test)59 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)23 PercentType (org.eclipse.smarthome.core.library.types.PercentType)22 Item (org.eclipse.smarthome.core.items.Item)21 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)19 Temperature (javax.measure.quantity.Temperature)18 StringType (org.eclipse.smarthome.core.library.types.StringType)18 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)17 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)15 HSBType (org.eclipse.smarthome.core.library.types.HSBType)15 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)15 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)13 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)12 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)11 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)10 RawType (org.eclipse.smarthome.core.library.types.RawType)10 Pressure (javax.measure.quantity.Pressure)9 GroupItem (org.eclipse.smarthome.core.items.GroupItem)8