Search in sources :

Example 36 with OpenemsException

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();
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) OpenemsException(io.openems.common.exceptions.OpenemsException) Edge(io.openems.backend.metadata.api.Edge)

Example 37 with OpenemsException

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());
    }
}
Also used : HashMap(java.util.HashMap) OpenemsException(io.openems.common.exceptions.OpenemsException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 38 with OpenemsException

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());
    }
}
Also used : HashMap(java.util.HashMap) OpenemsException(io.openems.common.exceptions.OpenemsException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 39 with OpenemsException

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());
    }
}
Also used : HashMap(java.util.HashMap) OpenemsException(io.openems.common.exceptions.OpenemsException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 40 with OpenemsException

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());
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) OpenemsException(io.openems.common.exceptions.OpenemsException) Controller(io.openems.api.controller.Controller)

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