use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class CurrentDataWorker method setChannels.
public synchronized void setChannels(JsonObject jSubscribeChannels, JsonObject jMessageId) {
// stop current thread
if (this.futureOpt.isPresent()) {
this.futureOpt.get().cancel(true);
this.futureOpt = Optional.empty();
}
// clear existing channels
this.channels.clear();
// parse and add subscribed channels
for (Entry<String, JsonElement> entry : jSubscribeChannels.entrySet()) {
String thing = entry.getKey();
try {
JsonArray jChannels = JsonUtils.getAsJsonArray(entry.getValue());
for (JsonElement jChannel : jChannels) {
String channel = JsonUtils.getAsString(jChannel);
channels.put(thing, channel);
}
} catch (OpenemsException e) {
Log.warn("Unable to add channel subscription: " + e.getMessage());
}
}
if (!channels.isEmpty()) {
// registered channels -> create new thread
this.futureOpt = Optional.of(this.executor.scheduleWithFixedDelay(() -> {
/*
* This task is executed regularly. Sends data to websocket.
*/
if (!this.websocket.isOpen()) {
// disconnected; stop worker
this.dispose();
return;
}
WebSocketUtils.sendOrLogError(this.websocket, DefaultMessages.currentData(jMessageId, getSubscribedData()));
}, 0, UPDATE_INTERVAL_IN_SECONDS, TimeUnit.SECONDS));
}
}
use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class OdooUtils method write.
/**
* Update a record in Odoo
*
* @param url
* URL of Odoo instance
* @param database
* Database name
* @param uid
* UID of user (e.g. '1' for admin)
* @param password
* Password of user
* @param model
* Odoo model to query (e.g. 'res.partner')
* @param id
* id of model to update
* @param fieldValues
* fields and values that should be written
* @return
* @throws OpenemsException
*/
protected static void write(String url, String database, int uid, String password, String model, Integer[] ids, FieldValue... fieldValues) throws OpenemsException {
// Create request params
String action = "write";
// Add fieldValues
Map<String, Object> paramsFieldValues = new HashMap<>();
for (FieldValue fieldValue : fieldValues) {
paramsFieldValues.put(fieldValue.getField().n(), fieldValue.getValue());
}
// Create request params
Object[] params = new Object[] { database, uid, password, model, action, new Object[] { ids, paramsFieldValues } };
try {
// Execute XML request
Boolean resultObj = (Boolean) executeKw(url, params);
if (!resultObj) {
throw new OpenemsException("Returned False.");
}
} catch (Throwable e) {
throw new OpenemsException("Unable to write to Odoo: " + e.getMessage());
}
}
use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class OdooUtils method search.
/**
* Executes a search on Odoo
*
* @param url
* URL of Odoo instance
* @param database
* Database name
* @param uid
* UID of user (e.g. '1' for admin)
* @param password
* Password of user
* @param model
* Odoo model to query (e.g. 'res.partner')
* @param domains
* Odoo domain filters
* @return Odoo object ids
* @throws OpenemsException
*/
protected static int[] search(String url, String database, int uid, String password, String model, Domain... domains) throws OpenemsException {
// Add domain filter
Object[] domain = new Object[domains.length];
for (int i = 0; i < domains.length; i++) {
Domain filter = domains[i];
domain[i] = new Object[] { filter.field, filter.operator, filter.value };
}
Object[] paramsDomain = new Object[] { domain };
// Create request params
HashMap<Object, Object> paramsRules = new HashMap<Object, Object>();
String action = "search";
Object[] params = new Object[] { database, uid, password, model, action, paramsDomain, paramsRules };
try {
// Execute XML request
Object[] resultObjs = (Object[]) executeKw(url, params);
// Parse results
int[] results = new int[resultObjs.length];
for (int i = 0; i < resultObjs.length; i++) {
results[i] = (int) resultObjs[i];
}
return results;
} catch (Throwable e) {
throw new OpenemsException("Unable to search from Odoo: " + e.getMessage());
}
}
use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class JsonUtils method getAsZonedDateTime.
/**
* Takes a json in the form 'YYYY-MM-DD' and converts it to a ZonedDateTime with
* hour, minute and second set to zero.
*
* @param jElement
* @param memberName
* @param timezone
* @return
* @throws OpenemsException
*/
public static ZonedDateTime getAsZonedDateTime(JsonElement jElement, String memberName, ZoneId timezone) throws OpenemsException {
String[] date = JsonUtils.getAsString(jElement, memberName).split("-");
try {
int year = Integer.valueOf(date[0]);
int month = Integer.valueOf(date[1]);
int day = Integer.valueOf(date[2]);
return ZonedDateTime.of(year, month, day, 0, 0, 0, 0, timezone);
} catch (ArrayIndexOutOfBoundsException e) {
throw new OpenemsException("Element [" + memberName + "] is not a Date: " + jElement + ". Error: " + e);
}
}
use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class ChannelAddress method fromString.
public static ChannelAddress fromString(String address) throws OpenemsException {
try {
String[] addressArray = address.split("/");
String thingId = addressArray[0];
String channelId = addressArray[1];
return new ChannelAddress(thingId, channelId);
} catch (Exception e) {
throw new OpenemsException("This [" + address + "] is not a valid channel address.");
}
}
Aggregations