All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.freedomotic.api.EventTemplate Maven / Gradle / Ivy

/**
 *
 * Copyright (c) 2009-2014 Freedomotic team http://freedomotic.com
 *
 * This file is part of Freedomotic
 *
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2, or (at your option) any later version.
 *
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Freedomotic; see the file COPYING. If not, see
 * .
 */
package com.freedomotic.api;

import com.freedomotic.app.Freedomotic;
import com.freedomotic.rules.Payload;
import com.freedomotic.rules.Statement;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;

/**
 *
 * @author enrico
 */
public class EventTemplate
        implements Serializable {

    private static final long serialVersionUID = -6726283450243677665L;
    protected String eventName;
    protected String sender;
    protected Payload payload = new Payload();
    protected boolean isValid;
    private long creation;

    protected void generateEventPayload() {
    }

    /**
     *
     * @return
     */
    public String getDefaultDestination() {
        return "app.event.sensor";
    }

    /**
     *
     */
    public EventTemplate() {
        fillPayloadWithDefaults();
    }

    /**
     *
     * @param source
     */
    public EventTemplate(Object source) {
        setSender(source);
        fillPayloadWithDefaults();
    }

    /**
     *
     * @return
     */
    public String getEventName() {
        return eventName;
    }

    /**
     *
     * @return
     */
    public long getCreation() {
        return creation;
    }

    /**
     *
     * @param key
     * @param value
     */
    public void addProperty(String key, String value) {
        payload.addStatement(key, value);
    }

    /**
     *
     * @param key
     * @return
     */
    public String getProperty(String key) {
        synchronized (payload) {
            List statements = payload.getStatements(key);

            if (statements.isEmpty()) {
                return "";
            } else {
                return statements.get(0).getValue();
            }
        }
    }

//    private void setUid() {
//        uid = UidGenerator.getNextUid();
//    }
    private void init() {
        eventName = this.getClass().getSimpleName();
        isValid = true;
        // setUid();
        //executed = true; //an event starts as executed as default value if an actuator don't deny it
        // isExecutable = true;
        creation = System.currentTimeMillis();
    }

    private final void fillPayloadWithDefaults() {
        init();

        try {
            Calendar rightNow = Calendar.getInstance();
            //adding date and time data
            payload.addStatement("date.day.name",
                    rightNow.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.UK));
            payload.addStatement("date.day",
                    rightNow.get(Calendar.DAY_OF_MONTH));
            payload.addStatement("date.month.name",
                    rightNow.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.UK));
            payload.addStatement("date.month", rightNow.get(Calendar.MONTH) + 1);
            payload.addStatement("date.year",
                    rightNow.get(Calendar.YEAR));
            payload.addStatement("date.dow",
                    rightNow.get(Calendar.DAY_OF_WEEK));
            payload.addStatement("time.hour",
                    rightNow.get(Calendar.HOUR_OF_DAY));
            payload.addStatement("time.minute",
                    rightNow.get(Calendar.MINUTE));
            payload.addStatement("time.second",
                    rightNow.get(Calendar.SECOND));

            DateFormat datefmt = new SimpleDateFormat("yyyyMMdd");
            DateFormat timefmt = new SimpleDateFormat("HHmmss");
            payload.addStatement("time",
                    timefmt.format(rightNow.getTime()));
            payload.addStatement("date",
                    datefmt.format(rightNow.getTime()));
            //adding event.sender to event payload. So it can be used by trigger
            payload.addStatement("sender",
                    getSender());
        } catch (Exception e) {
            LOG.severe(Freedomotic.getStackTraceInfo(e));
        }
    }

    private String getSender() {
        try {
            if (sender != null) {
                return sender;
            } else {
                return "UnknownSender";
            }
        } catch (Exception e) {
            return "UnknownSenderException";
        }
    }

    /**
     *
     * @param source
     */
    protected final void setSender(Object source) {
        if (source != null) {
            sender = source.getClass().getSimpleName();
        } else {
            sender = "UnknownSender";
        }
    }

    /**
     *
     * @return
     */
    public Payload getPayload() {
        return payload;
    }
    private static final Logger LOG = Logger.getLogger(EventTemplate.class.getName());
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy