Search in sources :

Example 1 with ProwlClient

use of net.sourceforge.prowl.api.ProwlClient in project openhab1-addons by openhab.

the class Prowl method pushNotification.

/**
     * Pushes a Prowl notification
     * 
     * @param apiKey apiKey to use for the notification
     * @param subject the subject of the notification
     * @param message the message of the notification
     * @param priority the priority of the notification (a value between
     *            '-2' and '2')
     * 
     * @return <code>true</code>, if pushing the notification has been successful
     *         and <code>false</code> in all other cases.
     */
@ActionDoc(text = "Pushes a Prowl notification", returns = "<code>true</code>, if successful and <code>false</code> otherwise.")
public static boolean pushNotification(@ParamDoc(name = "apiKey", text = "apiKey to use for the notification.") String apiKey, @ParamDoc(name = "subject", text = "the subject of the notification.") String subject, @ParamDoc(name = "message", text = "the message of the notification.") String message, @ParamDoc(name = "priority", text = "the priority of the notification (a value between '-2' and '2'.") int priority) {
    boolean success = false;
    int normalizedPriority = priority;
    if (priority < -2) {
        normalizedPriority = -2;
        logger.info("Prowl-Notification priority '{}' is invalid - normalized value to '{}'", priority, normalizedPriority);
    } else if (priority > 2) {
        normalizedPriority = 2;
        logger.info("Prowl-Notification priority '{}' is invalid - normalized value to '{}'", priority, normalizedPriority);
    }
    if (ProwlActionService.isProperlyConfigured) {
        ProwlClient client = new ProwlClient();
        if (StringUtils.isNotBlank(Prowl.url)) {
            client.setProwlUrl(Prowl.url);
        }
        ProwlEvent event = new DefaultProwlEvent(apiKey, "openhab", subject, message, normalizedPriority);
        try {
            String returnMessage = client.pushEvent(event);
            logger.info(returnMessage);
            success = true;
        } catch (ProwlException pe) {
            logger.error("pushing prowl event throws exception", pe);
        }
    } else {
        logger.error("Cannot push Prowl notification because of missing configuration settings. The current settings are: " + "apiKey: '{}', priority: {}, url: '{}'", new Object[] { apiKey, String.valueOf(normalizedPriority), url });
    }
    return success;
}
Also used : ProwlClient(net.sourceforge.prowl.api.ProwlClient) DefaultProwlEvent(net.sourceforge.prowl.api.DefaultProwlEvent) ProwlException(net.sourceforge.prowl.exception.ProwlException) ProwlEvent(net.sourceforge.prowl.api.ProwlEvent) DefaultProwlEvent(net.sourceforge.prowl.api.DefaultProwlEvent) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Aggregations

DefaultProwlEvent (net.sourceforge.prowl.api.DefaultProwlEvent)1 ProwlClient (net.sourceforge.prowl.api.ProwlClient)1 ProwlEvent (net.sourceforge.prowl.api.ProwlEvent)1 ProwlException (net.sourceforge.prowl.exception.ProwlException)1 ActionDoc (org.openhab.core.scriptengine.action.ActionDoc)1