Search in sources :

Example 1 with Channel

use of org.openmuc.framework.dataaccess.Channel in project OpenMUC by isc-konstanz.

the class ChannelResourceServlet method doGetHistory.

private void doGetHistory(ToJson json, String channelId, String fromParameter, String untilParameter, HttpServletResponse response) {
    long fromTimeStamp = 0;
    long untilTimeStamp = 0;
    List<String> channelIds = dataAccess.getAllIds();
    List<Record> records = null;
    if (channelIds.contains(channelId)) {
        Channel channel = dataAccess.getChannel(channelId);
        try {
            fromTimeStamp = Long.parseLong(fromParameter);
            untilTimeStamp = Long.parseLong(untilParameter);
        } catch (NumberFormatException ex) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_BAD_REQUEST, logger, "From/To value is not a long number.");
        }
        try {
            records = channel.getLoggedRecords(fromTimeStamp, untilTimeStamp);
        } catch (DataLoggerNotAvailableException e) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, e.getMessage());
        } catch (IOException e) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, e.getMessage());
        }
        json.addRecordList(records, channel.getValueType());
    }
}
Also used : Channel(org.openmuc.framework.dataaccess.Channel) DataLoggerNotAvailableException(org.openmuc.framework.dataaccess.DataLoggerNotAvailableException) Record(org.openmuc.framework.data.Record) IOException(java.io.IOException)

Example 2 with Channel

use of org.openmuc.framework.dataaccess.Channel in project OpenMUC by isc-konstanz.

the class ToJson method addRecordList.

public void addRecordList(List<Channel> channels) throws ClassCastException {
    JsonArray jsa = new JsonArray();
    for (Channel channel : channels) {
        JsonObject jso = new JsonObject();
        jso.addProperty(Const.ID, channel.getId());
        jso.addProperty(Const.VALUE_TYPE, channel.getValueType().toString());
        jso.add(Const.RECORD, getRecordAsJsonElement(channel.getLatestRecord(), channel.getValueType()));
        jsa.add(jso);
    }
    jsonObject.add(Const.RECORDS, jsa);
}
Also used : JsonArray(com.google.gson.JsonArray) Channel(org.openmuc.framework.dataaccess.Channel) RestChannel(org.openmuc.framework.lib.rest.objects.RestChannel) JsonObject(com.google.gson.JsonObject)

Example 3 with Channel

use of org.openmuc.framework.dataaccess.Channel in project OpenMUC by isc-konstanz.

the class ToJson method addChannelStateList.

public void addChannelStateList(List<Channel> channelList) {
    JsonArray jsa = new JsonArray();
    for (Channel channel : channelList) {
        JsonObject jso = new JsonObject();
        jso.addProperty(Const.ID, channel.getId());
        jso.addProperty(Const.STATE, channel.getChannelState().name());
        jsa.add(jso);
    }
    jsonObject.add(Const.STATES, jsa);
}
Also used : JsonArray(com.google.gson.JsonArray) Channel(org.openmuc.framework.dataaccess.Channel) RestChannel(org.openmuc.framework.lib.rest.objects.RestChannel) JsonObject(com.google.gson.JsonObject)

Example 4 with Channel

use of org.openmuc.framework.dataaccess.Channel in project OpenMUC by isc-konstanz.

the class MqttDriverConnection method startListening.

@Override
public void startListening(List<ChannelRecordContainer> containers, RecordsReceivedListener listener) throws UnsupportedOperationException, ConnectionException {
    List<String> topics = new ArrayList<>();
    for (ChannelRecordContainer container : containers) {
        String topic = Stream.of(container.getChannelAddress().split(";")).findFirst().orElse(ChannelConfig.ADDRESS_DEFAULT);
        topics.add(topic);
    }
    if (topics.isEmpty()) {
        return;
    }
    mqttReader.listen(topics, (topic, message) -> {
        if (!topics.contains(topic)) {
            logger.warn("Unable to find received topic \"{}\" in subscribed list", topic);
            return;
        }
        for (ChannelRecordContainer container : containers) {
            if (!Stream.of(container.getChannelAddress().split(";")).findFirst().orElse(ChannelConfig.ADDRESS_DEFAULT).equals(topic)) {
                continue;
            }
            Channel channel = container.getChannel();
            Record record = getRecord(message, container);
            if (recordIsOld(channel.getId(), record)) {
                continue;
            }
            addMessageToContainerList(record, container);
        }
        if (recordContainerList.size() >= Integer.parseInt(settings.getProperty("recordCollectionSize", "1"))) {
            notifyListenerAndPurgeList(listener);
        }
    });
}
Also used : ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) Channel(org.openmuc.framework.dataaccess.Channel) ArrayList(java.util.ArrayList) Record(org.openmuc.framework.data.Record)

Example 5 with Channel

use of org.openmuc.framework.dataaccess.Channel in project OpenMUC by isc-konstanz.

the class DeviceResourceServlet method getChannelList.

private List<Channel> getChannelList(String deviceId) {
    List<Channel> channels = new ArrayList<>();
    Collection<ChannelConfig> channelConfigs = rootConfig.getDevice(deviceId).getChannels();
    for (ChannelConfig channelConfig : channelConfigs) {
        channels.add(dataAccess.getChannel(channelConfig.getId()));
    }
    return channels;
}
Also used : ChannelConfig(org.openmuc.framework.config.ChannelConfig) Channel(org.openmuc.framework.dataaccess.Channel) ArrayList(java.util.ArrayList)

Aggregations

Channel (org.openmuc.framework.dataaccess.Channel)10 JsonArray (com.google.gson.JsonArray)3 ArrayList (java.util.ArrayList)3 Record (org.openmuc.framework.data.Record)3 RestChannel (org.openmuc.framework.lib.rest.objects.RestChannel)3 JsonObject (com.google.gson.JsonObject)2 ChannelConfig (org.openmuc.framework.config.ChannelConfig)2 IOException (java.io.IOException)1 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)1 DeviceConfig (org.openmuc.framework.config.DeviceConfig)1 DriverConfig (org.openmuc.framework.config.DriverConfig)1 Flag (org.openmuc.framework.data.Flag)1 Value (org.openmuc.framework.data.Value)1 DataLoggerNotAvailableException (org.openmuc.framework.dataaccess.DataLoggerNotAvailableException)1 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)1