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);
}
}
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 */
}
}
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());
}
}
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());
}
}
}
}
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());
}
}
Aggregations