Search in sources :

Example 11 with OpenemsException

use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.

the class EdgeWebsocketHandler method historicData.

private void historicData(JsonObject jMessageId, JsonObject jHistoricData) {
    // select first QueryablePersistence (by default the running InfluxdbPersistence)
    TimedataService timedataSource = null;
    for (QueryablePersistence queryablePersistence : ThingRepository.getInstance().getQueryablePersistences()) {
        timedataSource = queryablePersistence;
        break;
    }
    if (timedataSource == null) {
        WebSocketUtils.sendNotificationOrLogError(this.websocket, new JsonObject(), LogBehaviour.WRITE_TO_LOG, Notification.NO_TIMEDATA_SOURCE_AVAILABLE);
        return;
    }
    JsonArray jData;
    try {
        jData = timedataSource.queryHistoricData(jHistoricData);
        WebSocketUtils.send(this.websocket, DefaultMessages.historicDataQueryReply(jMessageId, jData));
    } catch (OpenemsException e) {
        WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.UNABLE_TO_QUERY_HISTORIC_DATA, e.getMessage());
    }
    return;
}
Also used : JsonArray(com.google.gson.JsonArray) QueryablePersistence(io.openems.api.persistence.QueryablePersistence) JsonObject(com.google.gson.JsonObject) WriteJsonObject(io.openems.core.utilities.api.WriteJsonObject) OpenemsException(io.openems.common.exceptions.OpenemsException) TimedataService(io.openems.backend.timedata.api.TimedataService)

Example 12 with OpenemsException

use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.

the class ChannelRegisterMap method getChannel.

/**
 * Returns the channel for the set ChannelAddress
 *
 * @return
 * @throws OpenemsException
 */
protected Channel getChannel() throws OpenemsException {
    Optional<Channel> channelOpt = ThingRepository.getInstance().getChannel(this.channelAddress);
    if (!channelOpt.isPresent()) {
        throw new OpenemsException("Channel does not exist [" + this.channelAddress + "]");
    }
    Channel channel = channelOpt.get();
    return channel;
}
Also used : WriteChannel(io.openems.api.channel.WriteChannel) ConfigChannel(io.openems.api.channel.ConfigChannel) Channel(io.openems.api.channel.Channel) OpenemsException(io.openems.common.exceptions.OpenemsException)

Example 13 with OpenemsException

use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.

the class Config method addDefaultConfig.

private JsonObject addDefaultConfig(JsonObject jConfig) {
    try {
        /*
			 * Things
			 */
        JsonArray jThings;
        if (!jConfig.has("things") || !jConfig.get("things").isJsonArray()) {
            jThings = new JsonArray();
        } else {
            jThings = JsonUtils.getAsJsonArray(jConfig, "things");
        }
        {
            /*
				 * Add SystemBridge
				 */
            if (!JsonUtils.hasElement(jConfig, "things", "class", "io.openems.impl.protocol.system.SystemBridge")) {
                JsonObject jBridge = new JsonObject();
                {
                    jBridge.addProperty("class", "io.openems.impl.protocol.system.SystemBridge");
                    JsonArray jDevices = new JsonArray();
                    {
                        JsonObject jSystem = new JsonObject();
                        jSystem.addProperty("class", "io.openems.impl.device.system.System");
                        {
                            JsonObject jSystemNature = new JsonObject();
                            {
                                jSystemNature.addProperty("id", "system0");
                            }
                            jSystem.add("system", jSystemNature);
                        }
                        jDevices.add(jSystem);
                    }
                    jBridge.add("devices", jDevices);
                }
                jThings.add(jBridge);
            }
        }
        jConfig.add("things", jThings);
        /*
			 * Scheduler
			 */
        JsonObject jScheduler;
        if (!jConfig.has("scheduler") || !jConfig.get("scheduler").isJsonObject()) {
            jScheduler = new JsonObject();
            jScheduler.addProperty("class", "io.openems.impl.scheduler.SimpleScheduler");
        } else {
            jScheduler = JsonUtils.getAsJsonObject(jConfig, "scheduler");
        }
        {
            /*
				 * Controller
				 */
            JsonArray jControllers;
            if (!jScheduler.has("controllers") || !jScheduler.get("controllers").isJsonArray()) {
                jControllers = new JsonArray();
            } else {
                jControllers = JsonUtils.getAsJsonArray(jScheduler, "controllers");
            }
            {
                /*
					 * WebsocketApiController
					 */
                if (!JsonUtils.hasElement(jControllers, "class", "io.openems.impl.controller.api.websocket.WebsocketApiController")) {
                    JsonObject jWebsocketApiController = new JsonObject();
                    jWebsocketApiController.addProperty("class", "io.openems.impl.controller.api.websocket.WebsocketApiController");
                    jWebsocketApiController.addProperty("priority", Integer.MIN_VALUE);
                    jControllers.add(jWebsocketApiController);
                }
                /*
					 * RestApiController
					 */
                if (!JsonUtils.hasElement(jControllers, "class", "io.openems.impl.controller.api.rest.RestApiController")) {
                    JsonObject jRestApiController = new JsonObject();
                    jRestApiController.addProperty("class", "io.openems.impl.controller.api.rest.RestApiController");
                    jRestApiController.addProperty("priority", Integer.MIN_VALUE);
                    jControllers.add(jRestApiController);
                }
            }
            jScheduler.add("controllers", jControllers);
        }
        jConfig.add("scheduler", jScheduler);
    } catch (OpenemsException e) {
        log.warn("Error applying default config: " + e.getMessage());
    }
    return jConfig;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) OpenemsException(io.openems.common.exceptions.OpenemsException)

Example 14 with OpenemsException

use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.

the class ThingRepository method applyChannelAnnotation.

public void applyChannelAnnotation(Thing thing) {
    ThingDoc thingDoc = classRepository.getThingDoc(thing.getClass());
    for (ChannelDoc channelDoc : thingDoc.getChannelDocs()) {
        try {
            Channel channel = getChannel(thing, channelDoc.getMember());
            channel.setChannelDoc(channelDoc);
        } catch (OpenemsException e) {
            log.debug(e.getMessage());
        }
    }
}
Also used : ReadChannel(io.openems.api.channel.ReadChannel) WriteChannel(io.openems.api.channel.WriteChannel) ConfigChannel(io.openems.api.channel.ConfigChannel) ThingStateChannel(io.openems.api.channel.ThingStateChannel) Channel(io.openems.api.channel.Channel) OpenemsException(io.openems.common.exceptions.OpenemsException) ChannelDoc(io.openems.api.doc.ChannelDoc) ThingDoc(io.openems.api.doc.ThingDoc)

Example 15 with OpenemsException

use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.

the class MyRegister method toBytes.

@Override
public byte[] toBytes() {
    Channel channel;
    try {
        channel = parent.getChannel();
    } catch (OpenemsException e) {
        log.warn(e.getMessage());
        return VALUE_ON_ERROR;
    }
    Optional<?> valueOpt = Databus.getInstance().getValue(channel);
    if (valueOpt.isPresent()) {
        // we got a value
        Object object = valueOpt.get();
        try {
            byte[] b = BitUtils.toBytes(object);
            return new byte[] { b[this.registerNo * 2], b[this.registerNo * 2 + 1] };
        } catch (NotImplementedException e) {
            // unable to convert value to byte
            try {
                throw new OpenemsException("Unable to convert Channel [" + this.parent.getChannelAddress() + "] value [" + object + "] to byte format.");
            } catch (OpenemsException e2) {
                log.warn(e2.getMessage());
            }
        }
    } else {
        // we got no value
        try {
            throw new OpenemsException("Value for Channel [" + this.parent.getChannelAddress() + "] is not available.");
        } catch (OpenemsException e) {
            log.warn(e.getMessage());
        }
    }
    return VALUE_ON_ERROR;
}
Also used : Channel(io.openems.api.channel.Channel) NotImplementedException(io.openems.common.exceptions.NotImplementedException) OpenemsException(io.openems.common.exceptions.OpenemsException)

Aggregations

OpenemsException (io.openems.common.exceptions.OpenemsException)52 JsonObject (com.google.gson.JsonObject)25 JsonElement (com.google.gson.JsonElement)11 Edge (io.openems.backend.metadata.api.Edge)8 HashMap (java.util.HashMap)8 JsonArray (com.google.gson.JsonArray)7 Channel (io.openems.api.channel.Channel)7 ConfigChannel (io.openems.api.channel.ConfigChannel)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 WriteChannel (io.openems.api.channel.WriteChannel)4 User (io.openems.api.security.User)4 Role (io.openems.common.session.Role)4 WriteJsonObject (io.openems.core.utilities.api.WriteJsonObject)4 JsonParser (com.google.gson.JsonParser)3 ChannelDoc (io.openems.api.doc.ChannelDoc)3 ConfigException (io.openems.api.exception.ConfigException)3 User (io.openems.backend.metadata.api.User)3 Map (java.util.Map)3 UUID (java.util.UUID)3