use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class Odoo method getEdgeForceRefresh.
/**
* Reads the Edge object from Odoo and stores it in the cache
*
* @param edgeId
* @return
*/
private Optional<Edge> getEdgeForceRefresh(int edgeId) {
try {
Map<String, Object> edgeMap = OdooUtils.readOne(this.url, this.database, this.uid, this.password, "fems.device", edgeId, Field.FemsDevice.NAME, Field.FemsDevice.COMMENT, Field.FemsDevice.PRODUCT_TYPE, Field.FemsDevice.OPENEMS_CONFIG);
Object configObj = edgeMap.get(Field.FemsDevice.OPENEMS_CONFIG.n());
JsonObject jConf;
if (configObj != null && configObj instanceof String) {
jConf = JsonUtils.getAsJsonObject(JsonUtils.parse((String) configObj));
} else {
jConf = new JsonObject();
}
Edge edge = new //
Edge(//
(Integer) edgeMap.get(Field.FemsDevice.ID.n()), //
(String) edgeMap.get(Field.FemsDevice.NAME.n()), //
(String) edgeMap.get(Field.FemsDevice.COMMENT.n()), //
(String) edgeMap.get(Field.FemsDevice.PRODUCT_TYPE.n()), jConf);
edge.onSetConfig(jConfig -> {
// Update Edge config in Odoo
String config = new GsonBuilder().setPrettyPrinting().create().toJson(jConfig);
this.write(edge, new FieldValue(Field.FemsDevice.OPENEMS_CONFIG, config));
});
edge.onSetLastMessage(() -> {
// Set LastMessage timestamp in Odoo
this.writeWorker.onLastMessage(edgeId);
});
edge.onSetLastUpdate(() -> {
// Set LastUpdate timestamp in Odoo
this.writeWorker.onLastUpdate(edgeId);
});
edge.onSetSoc(soc -> {
// Set SoC in Odoo
this.write(edge, new FieldValue(Field.FemsDevice.SOC, String.valueOf(soc)));
});
edge.onSetIpv4(ipv4 -> {
// Set IPv4 in Odoo
this.write(edge, new FieldValue(Field.FemsDevice.IPV4, String.valueOf(ipv4)));
});
edge.setOnline(this.edgeWebsocketService.isOnline(edge.getId()));
// store in cache
synchronized (this.edges) {
this.edges.put(edge.getId(), edge);
}
return Optional.ofNullable(edge);
} catch (OpenemsException e) {
log.error("Unable to read Edge [ID:" + edgeId + "]: " + e.getMessage());
return Optional.empty();
}
}
use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class OdooUtils method readMany.
/**
* Reads multiple records from 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 ids
* ids of model to read
* @param fields
* fields that should be read
* @return
* @throws OpenemsException
*/
protected static Map<String, Object>[] readMany(String url, String database, int uid, String password, String model, int[] ids, Field... fields) throws OpenemsException {
// Create request params
String action = "read";
// Add ids
Object[] paramsIds = new Object[ids.length];
for (int i = 0; i < ids.length; i++) {
paramsIds[i] = ids[i];
}
// Add fields
String[] fieldStrings = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
fieldStrings[i] = fields[i].toString();
}
Map<String, String[]> paramsFields = new HashMap<>();
paramsFields.put("fields", fieldStrings);
// Create request params
Object[] params = new Object[] { database, uid, password, model, action, paramsIds, paramsFields };
try {
// Execute XML request
Object[] resultObjs = (Object[]) executeKw(url, params);
// Parse results
@SuppressWarnings("unchecked") Map<String, Object>[] results = (Map<String, Object>[]) new Map[resultObjs.length];
for (int i = 0; i < resultObjs.length; i++) {
@SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) resultObjs[i];
results[0] = result;
}
return results;
} catch (Throwable e) {
throw new OpenemsException("Unable to read from Odoo: " + e.getMessage());
}
}
use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class OdooUtils method searchRead.
/**
* Search-Reads multiple records from 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 ids
* ids of model to read
* @param fields
* fields that should be read
* @return
* @throws OpenemsException
*/
protected static Map<String, Object>[] searchRead(String url, String database, int uid, String password, String model, Field[] fields, Domain... domains) throws OpenemsException {
// Create request params
String action = "search_read";
// 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 };
// Add fields
String[] fieldStrings = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
fieldStrings[i] = fields[i].toString();
}
Map<String, String[]> paramsFields = new HashMap<>();
paramsFields.put("fields", fieldStrings);
// Create request params
Object[] params = new Object[] { database, uid, password, model, action, paramsDomain, paramsFields };
try {
// Execute XML request
Object[] resultObjs = (Object[]) executeKw(url, params);
// Parse results
@SuppressWarnings("unchecked") Map<String, Object>[] results = (Map<String, Object>[]) new Map[resultObjs.length];
for (int i = 0; i < resultObjs.length; i++) {
@SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) resultObjs[i];
results[0] = result;
}
return results;
} catch (Throwable e) {
throw new OpenemsException("Unable to read from Odoo: " + e.getMessage());
}
}
use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class OdooUtils method readOne.
/**
* Reads a record from 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 ids
* ids of model to read
* @param fields
* fields that should be read
* @return
* @throws OpenemsException
*/
protected static Map<String, Object> readOne(String url, String database, int uid, String password, String model, int id, Field... fields) throws OpenemsException {
// Create request params
String action = "read";
// Add ids
Object[] paramsIds = new Object[1];
paramsIds[0] = id;
// Add fields
String[] fieldStrings = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
fieldStrings[i] = fields[i].n();
}
Map<String, String[]> paramsFields = new HashMap<>();
paramsFields.put("fields", fieldStrings);
// Create request params
Object[] params = new Object[] { database, uid, password, model, action, paramsIds, paramsFields };
try {
// Execute XML request
Object[] resultObjs = (Object[]) executeKw(url, params);
// Parse results
for (int i = 0; i < resultObjs.length; ) {
@SuppressWarnings("unchecked") Map<String, Object> result = (Map<String, Object>) resultObjs[i];
return result;
}
throw new OpenemsException("No matching entry found for id [" + id + "]");
} catch (Throwable e) {
throw new OpenemsException("Unable to read from Odoo: " + e.getMessage());
}
}
use of io.openems.common.exceptions.OpenemsException in project openems by OpenEMS.
the class WeekTimeScheduler method execute.
@Override
protected void execute() {
// kick the watchdog
SDNotify.sendWatchdog();
try {
List<Controller> controllers = getActiveControllers();
controllers.addAll(getAlwaysController());
Collections.sort(controllers, (c1, c2) -> {
if (c1 == null || c2 == null) {
return 0;
}
return c2.priority.valueOptional().orElse(Integer.MIN_VALUE) - c1.priority.valueOptional().orElse(Integer.MIN_VALUE);
});
for (Controller controller : controllers) {
// TODO: check if WritableChannels can still be changed, before executing
if (controller != null) {
controller.executeRun();
}
}
} catch (DateTimeParseException | OpenemsException e) {
log.error(e.getMessage());
}
}
Aggregations