Search in sources :

Example 46 with OpenemsException

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

the class ComponentSingleton method startComponent.

private static synchronized void startComponent(int port, ApiWorker apiWorker) throws OpenemsException {
    ComponentSingleton.instance = new Component();
    ComponentSingleton.instance.getServers().add(Protocol.HTTP, port);
    ComponentSingleton.instance.getDefaultHost().attach("/rest", new RestApiApplication(apiWorker));
    try {
        ComponentSingleton.instance.start();
        ComponentSingleton.port = port;
        log.info("REST-Api started on port [" + port + "].");
    } catch (Exception e) {
        throw new OpenemsException("REST-Api failed on port [" + port + "].", e);
    }
}
Also used : OpenemsException(io.openems.common.exceptions.OpenemsException) Component(org.restlet.Component) OpenemsException(io.openems.common.exceptions.OpenemsException)

Example 47 with OpenemsException

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

the class OpenemsEnroler method enrole.

@Override
public void enrole(ClientInfo clientInfo) {
    String username = clientInfo.getUser().getIdentifier();
    User user;
    try {
        user = User.getUserByName(username);
        // 
        clientInfo.getRoles().add(// 
        Role.get(Application.getCurrent(), user.getRole().name().toLowerCase()));
    } catch (OpenemsException e) {
    /* ignore, just don't enrole user in any group */
    }
}
Also used : User(io.openems.api.security.User) OpenemsException(io.openems.common.exceptions.OpenemsException)

Example 48 with OpenemsException

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

the class ModbusTcpApiController method restartSlave.

protected void restartSlave(Optional<Integer> portOpt) {
    // remove and close old slave if existing
    if (this.slaveOpt.isPresent()) {
        ModbusSlave oldSlave = this.slaveOpt.get();
        oldSlave.close();
        this.slaveOpt = Optional.empty();
    }
    // create new slave, initialize and start
    try {
        if (!portOpt.isPresent()) {
            throw new OpenemsException("Port was not set");
        }
        int port = portOpt.get();
        ModbusSlave newSlave = ModbusSlaveFactory.createTCPSlave(port, MAX_CONCURRENT_CONNECTIONS);
        newSlave.addProcessImage(UNIT_ID, this.processImage);
        newSlave.open();
        // TODO slave should be closed on dispose of Controller
        log.info("Modbus/TCP Api started on port [" + port + "] with UnitId [" + UNIT_ID + "].");
        this.slaveOpt = Optional.of(newSlave);
    } catch (OpenemsException | ModbusException e) {
        log.error("Unable to start Modbus/TCP slave: " + e.getMessage());
    }
}
Also used : ModbusSlave(com.ghgande.j2mod.modbus.slave.ModbusSlave) OpenemsException(io.openems.common.exceptions.OpenemsException) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Example 49 with OpenemsException

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

the class ModbusTcpApiController method updateChannelMapping.

protected void updateChannelMapping(Optional<JsonObject> jMappingOpt) {
    processImage.clearMapping();
    ThingRepository thingRepository = ThingRepository.getInstance();
    if (jMappingOpt.isPresent()) {
        JsonObject jMapping = jMappingOpt.get();
        for (Entry<String, JsonElement> entry : jMapping.entrySet()) {
            try {
                int ref = Integer.parseInt(entry.getKey());
                ChannelAddress channelAddress = ChannelAddress.fromString(JsonUtils.getAsString(entry.getValue()));
                Optional<ChannelDoc> channelDocOpt = thingRepository.getChannelDoc(channelAddress);
                if (channelDocOpt.isPresent()) {
                    processImage.addMapping(ref, channelAddress, channelDocOpt.get());
                } else {
                    Optional<Channel> channelOpt = thingRepository.getChannel(channelAddress);
                    if (channelOpt.isPresent()) {
                        throw new OpenemsException("ChannelDoc for channel [" + channelAddress + "] is not available.");
                    } else {
                        throw new OpenemsException("Channel [" + channelAddress + "] does not exist.");
                    }
                }
            } catch (Exception e) {
                log.error("Unable to add channel mapping: " + e.getMessage());
            }
        }
    }
}
Also used : ConfigChannel(io.openems.api.channel.ConfigChannel) Channel(io.openems.api.channel.Channel) JsonObject(com.google.gson.JsonObject) ChannelAddress(io.openems.common.types.ChannelAddress) OpenemsException(io.openems.common.exceptions.OpenemsException) ChannelDoc(io.openems.api.doc.ChannelDoc) OpenemsException(io.openems.common.exceptions.OpenemsException) ModbusException(com.ghgande.j2mod.modbus.ModbusException) ThingRepository(io.openems.core.ThingRepository) JsonElement(com.google.gson.JsonElement)

Example 50 with OpenemsException

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

the class UserChangePasswordRestlet method changePassword.

/**
 * handle HTTP POST request
 *
 * @param thingId
 * @param channelId
 * @param jHttpPost
 */
private void changePassword(User user, JsonObject jHttpPost) {
    // parse old and new password
    String oldPassword;
    String newPassword;
    try {
        oldPassword = JsonUtils.getAsString(jHttpPost, "oldPassword");
        newPassword = JsonUtils.getAsString(jHttpPost, "newPassword");
    } catch (OpenemsException e1) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Value is missing");
    }
    try {
        user.changePassword(oldPassword, newPassword);
        log.info("Changed password for user [" + user.getName() + "].");
    } catch (OpenemsException e) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Changing password failed: " + e.getMessage());
    }
}
Also used : ResourceException(org.restlet.resource.ResourceException) 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