Java tutorial
/* Copyright (c) Matteo Mazzoni <matteo@bestmazzo.it> 2013 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 3 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>. */ package com.freedomotic.plugins.devices.twilight; import com.freedomotic.api.EventTemplate; import com.freedomotic.api.Protocol; import com.freedomotic.exceptions.UnableToExecuteException; import com.freedomotic.reactions.Command; import com.freedomotic.plugins.devices.twilight.providersit.mazzoni.twilight.providers.EarthToolsWI; import com.freedomotic.plugins.devices.twilight.providersit.mazzoni.twilight.providers.OpenWeatherMapWI; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import org.joda.time.DateTime; public class Twilight extends Protocol { private int POLLING_WAIT; private String Latitude; private String Longitude; private TwilightUtils TLU; private WeatherInfo provider; private String providerName; public Twilight() { //every plugin needs a name and a manifest XML file super("Twilight", "/twilight/twilight-manifest.xml"); //read a property from the manifest file below which is in //FREEDOMOTIC_FOLDER/plugins/devices/it.freedomotic.hello/hello-world.xml POLLING_WAIT = configuration.getIntProperty("polling-time", 10000); Latitude = configuration.getStringProperty("latitude", "0.0"); Longitude = configuration.getStringProperty("longitude", "0.0"); providerName = configuration.getStringProperty("provider", "openweathermap"); if (providerName.equalsIgnoreCase("openweathermap")) { provider = new OpenWeatherMapWI(Latitude, Longitude); } else { provider = new EarthToolsWI(Latitude, Longitude); } TLU = new TwilightUtils(POLLING_WAIT, provider); //default value if the property does not exist in the manifest setPollingWait(-1); //millisecs interval between hardware device status reads } @Override protected void onShowGui() { /** * uncomment the line below to add a GUI to this plugin the GUI can be * started with a right-click on plugin list on the desktop frontend * (it.freedomotic.jfrontend plugin) */ //bindGuiToPlugin(new HelloWorldGui(this)); } @Override protected void onHideGui() { //implement here what to do when the this plugin GUI is closed //for example you can change the plugin description setDescription("My GUI is now hidden"); } @Override protected void onRun() { EventTemplate ev = TLU.prepareEvent(DateTime.now()); LOG.info(ev.getPayload().toString().replace("\n", " ")); notifyEvent(ev); } @Override protected void onStart() { try { LOG.info("Twilight plugin is started"); provider.updateData(); setPollingWait(POLLING_WAIT); setDescription("Sunrise: " + provider.getNextSunrise().toLocalTime() + " Sunset: " + provider.getNextSunset().toLocalTime()); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); stop(); } } @Override protected void onStop() { LOG.info("Twilight plugin is stopped "); setPollingWait(-1); } @Override protected void onCommand(Command c) throws IOException, UnableToExecuteException { String command = c.getProperty("command"); if (command.equals("Update Twilight Data")) { try { provider.updateData(); setDescription("Sunrise: " + provider.getNextSunrise().toLocalTime() + " Sunset: " + provider.getNextSunset().toLocalTime()); } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } } } @Override protected boolean canExecute(Command c) { //don't mind this method for now throw new UnsupportedOperationException("Not supported yet."); } @Override protected void onEvent(EventTemplate event) { //don't mind this method for now throw new UnsupportedOperationException("Not supported yet."); } private static final Logger LOG = Logger.getLogger(Twilight.class.getName()); }