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