Search in sources :

Example 26 with OpenemsException

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));
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) OpenemsException(io.openems.common.exceptions.OpenemsException)

Example 27 with OpenemsException

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

Example 28 with OpenemsException

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

Example 29 with OpenemsException

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

Example 30 with OpenemsException

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

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