Open main menu

CDOT Wiki β

Changes

Teams Winter 2011/team2/project

16,899 bytes added, 16:13, 16 April 2011
no edit summary
</syntaxhighlight>
 
=Provider bundle=
 
1. Provider Activator
 
<syntaxhighlight lang="java">
 
package ecl.team2.project.weathersystem.provider;
 
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
 
import ecl.team2.project.weather.WeatherInterface;
import ecl.team2.project.weathersystem.provider.internal.WeatherImplementation;
 
public class ProviderActivator implements BundleActivator {
 
private static BundleContext context;
 
static BundleContext getContext() {
return context;
}
 
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
ProviderActivator.context = bundleContext;
WeatherInterface sws = new WeatherImplementation();
context.registerService(WeatherInterface.class.getName(), sws, null);
System.out.println("Weather Station On !");
}
 
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
ProviderActivator.context = null;
System.out.println("Weather Station Off !");
}
 
}
 
</syntaxhighlight>
2. Provider's implementation. The provider's implementation class WeatherImplementation.java uses a webservice from http://www.worldweatheronline.com/ get weather information. You will need to subscribe to their website to get an apikey to request and get weather information. Worldweatheronline.com provides data in both json and xml. We will use xml format in this project.
 
<syntaxhighlight lang="java">
 
package ecl.team2.project.weathersystem.provider.internal;
 
import java.util.ArrayList;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
import ecl.team2.project.weather.CurrentCondition;
import ecl.team2.project.weather.Location;
import ecl.team2.project.weather.Weather;
import ecl.team2.project.weather.WeatherAndCurrent;
import ecl.team2.project.weather.WeatherInterface;
 
public class WeatherImplementation implements WeatherInterface {
// ArrayList<Weather> weathers = new ArrayList<Weather>();
String url="";
String str_location="";
 
public WeatherImplementation(){
 
}
public WeatherAndCurrent getWeather(String city) throws Exception {
 
WeatherAndCurrent weatherAndCurrentTemp = new WeatherAndCurrent();
ArrayList<Weather> weathers = new ArrayList<Weather>();
CurrentCondition current = new CurrentCondition();
 
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("http://free.worldweatheronline.com/feed/weather.ashx?q="+city+"&num_of_days=4&date=today&format=xml&key=4aa3418d0a204342110904");
doc.getDocumentElement().normalize();
 
weathers = this.setWeathers(doc);
current = this.setCurrent(doc);
} catch (Exception e) {
e.printStackTrace();
}
weatherAndCurrentTemp.setCurrent(current);
weatherAndCurrentTemp.setWeathers(weathers);
return weatherAndCurrentTemp;
}
 
 
private ArrayList<Weather> setWeathers(Document doc) {
 
ArrayList<Weather> weathers = new ArrayList<Weather>();
 
 
 
NodeList nodeLst = doc.getElementsByTagName("weather");
//System.out.println("Information of all employees");
 
for (int s = 0; s < nodeLst.getLength(); s++) {
Weather w = new Weather();
Node fstNode = nodeLst.item(s);
 
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
 
Element fstElmnt = (Element) fstNode;
 
NodeList dateNmElmntLst = fstElmnt.getElementsByTagName("date");
Element dateNmElmnt = (Element) dateNmElmntLst.item(0);
NodeList dateNm = dateNmElmnt.getChildNodes();
w.setDate(((Node) dateNm.item(0)).getNodeValue());
 
NodeList tempMaxCNmElmntLst = fstElmnt.getElementsByTagName("tempMaxC");
Element tempMaxCNmElmnt = (Element) tempMaxCNmElmntLst.item(0);
NodeList tempMaxCNm = tempMaxCNmElmnt.getChildNodes();
w.setTempMaxC(((Node) tempMaxCNm.item(0)).getNodeValue());
 
NodeList tempMaxFNmElmntLst = fstElmnt.getElementsByTagName("tempMaxF");
Element tempMaxFNmElmnt = (Element) tempMaxFNmElmntLst.item(0);
NodeList tempMaxFNm = tempMaxFNmElmnt.getChildNodes();
w.setTempMaxF(((Node) tempMaxFNm.item(0)).getNodeValue());
 
NodeList tempMinCNmElmntLst = fstElmnt.getElementsByTagName("tempMinC");
Element tempMinCNmElmnt = (Element) tempMinCNmElmntLst.item(0);
NodeList tempMinCNm = tempMinCNmElmnt.getChildNodes();
w.setTempMinC(((Node) tempMinCNm.item(0)).getNodeValue());
 
NodeList tempMinFNmElmntLst = fstElmnt.getElementsByTagName("tempMinF");
Element tempMinFNmElmnt = (Element) tempMinFNmElmntLst.item(0);
NodeList tempMinFNm = tempMinFNmElmnt.getChildNodes();
w.setTempMinF(((Node) tempMinFNm.item(0)).getNodeValue());
 
NodeList windspeedMilesNmElmntLst = fstElmnt.getElementsByTagName("windspeedMiles");
Element windspeedMilesNmElmnt = (Element) windspeedMilesNmElmntLst.item(0);
NodeList windspeedMilesNm = windspeedMilesNmElmnt.getChildNodes();
w.setWindspeedMiles(((Node) windspeedMilesNm.item(0)).getNodeValue());
 
NodeList windspeedKmphNmElmntLst = fstElmnt.getElementsByTagName("windspeedKmph");
Element windspeedKmphNmElmnt = (Element) windspeedKmphNmElmntLst.item(0);
NodeList windspeedKmphNm = windspeedKmphNmElmnt.getChildNodes();
w.setWindspeedKmph(((Node) windspeedKmphNm.item(0)).getNodeValue());
 
NodeList winddirectionNmElmntLst = fstElmnt.getElementsByTagName("winddirection");
Element winddirectionNmElmnt = (Element) winddirectionNmElmntLst.item(0);
NodeList winddirectionNm = winddirectionNmElmnt.getChildNodes();
w.setWinddirection(((Node) winddirectionNm.item(0)).getNodeValue());
 
NodeList winddir16PointNmElmntLst = fstElmnt.getElementsByTagName("winddir16Point");
Element winddir16PointNmElmnt = (Element) winddir16PointNmElmntLst.item(0);
NodeList winddir16PointNm = winddir16PointNmElmnt.getChildNodes();
w.setWinddir16Point(((Node) winddir16PointNm.item(0)).getNodeValue());
 
NodeList winddirDegreeNmElmntLst = fstElmnt.getElementsByTagName("winddirDegree");
Element winddirDegreeNmElmnt = (Element) winddirDegreeNmElmntLst.item(0);
NodeList winddirDegreeNm = winddirDegreeNmElmnt.getChildNodes();
w.setWinddirDegree(((Node) winddirDegreeNm.item(0)).getNodeValue());
 
NodeList weatherCodeNmElmntLst = fstElmnt.getElementsByTagName("weatherCode");
Element weatherCodeNmElmnt = (Element) weatherCodeNmElmntLst.item(0);
NodeList weatherCodeNm = weatherCodeNmElmnt.getChildNodes();
w.setWeatherCode(((Node) weatherCodeNm.item(0)).getNodeValue());
 
NodeList weatherIconUrlNmElmntLst = fstElmnt.getElementsByTagName("weatherIconUrl");
Element weatherIconUrlNmElmnt = (Element) weatherIconUrlNmElmntLst.item(0);
NodeList weatherIconUrlNm = weatherIconUrlNmElmnt.getChildNodes();
// System.out.println(((Node) weatherIconUrlNm.item(0)).getNodeValue());
w.setWeatherIconUrl(((Node) weatherIconUrlNm.item(0)).getNodeValue());
 
NodeList weatherDescNmElmntLst = fstElmnt.getElementsByTagName("weatherDesc");
Element weatherDescNmElmnt = (Element) weatherDescNmElmntLst.item(0);
NodeList weatherDescNm = weatherDescNmElmnt.getChildNodes();
w.setWeatherDesc(((Node) weatherDescNm.item(0)).getNodeValue());
 
NodeList precipMMNmElmntLst = fstElmnt.getElementsByTagName("precipMM");
Element precipMMNmElmnt = (Element) precipMMNmElmntLst.item(0);
NodeList precipMMNm = precipMMNmElmnt.getChildNodes();
w.setPrecipMM(((Node) precipMMNm.item(0)).getNodeValue());
 
}
weathers.add(w);
}
return weathers;
 
 
}
 
private CurrentCondition setCurrent(Document doc) {
 
CurrentCondition current = new CurrentCondition();
 
NodeList nodeLst = doc.getElementsByTagName("current_condition");
//System.out.println("Information of all employees");
 
for (int s = 0; s < nodeLst.getLength(); s++) {
 
Node fstNode = nodeLst.item(s);
 
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
 
Element fstElmnt = (Element) fstNode;
 
 
 
NodeList observation_timeNmElmntLst = fstElmnt.getElementsByTagName("observation_time");
Element observation_timeNmElmnt = (Element) observation_timeNmElmntLst.item(0);
NodeList observation_timeNm = observation_timeNmElmnt.getChildNodes();
 
current.setObservation_time(((Node) observation_timeNm.item(0)).getNodeValue());
 
NodeList temp_CNmElmntLst = fstElmnt.getElementsByTagName("temp_C");
Element temp_CNmElmnt = (Element) temp_CNmElmntLst.item(0);
NodeList temp_CNm = temp_CNmElmnt.getChildNodes();
current.setTemp_C(((Node) temp_CNm.item(0)).getNodeValue());
 
NodeList temp_FNmElmntLst = fstElmnt.getElementsByTagName("temp_F");
Element temp_FNmElmnt = (Element) temp_FNmElmntLst.item(0);
NodeList temp_FNm = temp_FNmElmnt.getChildNodes();
current.setTemp_F(((Node) temp_FNm.item(0)).getNodeValue());
 
NodeList weatherCodeNmElmntLst = fstElmnt.getElementsByTagName("weatherCode");
Element weatherCodeNmElmnt = (Element) weatherCodeNmElmntLst.item(0);
NodeList weatherCodeNm = weatherCodeNmElmnt.getChildNodes();
current.setWeatherCode(((Node) weatherCodeNm.item(0)).getNodeValue());
 
NodeList weatherIconUrlNmElmntLst = fstElmnt.getElementsByTagName("weatherIconUrl");
Element weatherIconUrlNmElmnt = (Element) weatherIconUrlNmElmntLst.item(0);
NodeList weatherIconUrlNm = weatherIconUrlNmElmnt.getChildNodes();
current.setWeatherIconUrl(((Node) weatherIconUrlNm.item(0)).getNodeValue());
 
NodeList weatherDescNmElmntLst = fstElmnt.getElementsByTagName("weatherDesc");
Element weatherDescNmElmnt = (Element) weatherDescNmElmntLst.item(0);
NodeList weatherDescNm = weatherDescNmElmnt.getChildNodes();
current.setWeatherDesc(((Node) weatherDescNm.item(0)).getNodeValue());
 
NodeList windspeedMilesNmElmntLst = fstElmnt.getElementsByTagName("windspeedMiles");
Element windspeedMilesNmElmnt = (Element) windspeedMilesNmElmntLst.item(0);
NodeList windspeedMilesNm = windspeedMilesNmElmnt.getChildNodes();
current.setWindspeedMiles(((Node) windspeedMilesNm.item(0)).getNodeValue());
 
NodeList windspeedKmphNmElmntLst = fstElmnt.getElementsByTagName("windspeedKmph");
Element windspeedKmphNmElmnt = (Element) windspeedKmphNmElmntLst.item(0);
NodeList windspeedKmphNm = windspeedKmphNmElmnt.getChildNodes();
current.setWindspeedKmph(((Node) windspeedKmphNm.item(0)).getNodeValue());
 
NodeList winddirDegreeNmElmntLst = fstElmnt.getElementsByTagName("winddirDegree");
Element winddirDegreeNmElmnt = (Element) winddirDegreeNmElmntLst.item(0);
NodeList winddirDegreeNm = winddirDegreeNmElmnt.getChildNodes();
current.setWinddirDegree(((Node) winddirDegreeNm.item(0)).getNodeValue());
 
NodeList winddir16PointNmElmntLst = fstElmnt.getElementsByTagName("winddir16Point");
Element winddir16PointNmElmnt = (Element) winddir16PointNmElmntLst.item(0);
NodeList winddir16PointNm = winddir16PointNmElmnt.getChildNodes();
current.setWinddir16Point(((Node) winddir16PointNm.item(0)).getNodeValue());
 
NodeList precipMMNmElmntLst = fstElmnt.getElementsByTagName("precipMM");
Element precipMMNmElmnt = (Element) precipMMNmElmntLst.item(0);
NodeList precipMMNm = precipMMNmElmnt.getChildNodes();
current.setPrecipMM(((Node) precipMMNm.item(0)).getNodeValue());
 
NodeList humidityNmElmntLst = fstElmnt.getElementsByTagName("humidity");
Element humidityNmElmnt = (Element) humidityNmElmntLst.item(0);
NodeList humidityNm = humidityNmElmnt.getChildNodes();
current.setHumidity(((Node) humidityNm.item(0)).getNodeValue());
 
NodeList visibilityNmElmntLst = fstElmnt.getElementsByTagName("visibility");
Element visibilityNmElmnt = (Element) visibilityNmElmntLst.item(0);
NodeList visibilityNm = visibilityNmElmnt.getChildNodes();
current.setVisibility(((Node) visibilityNm.item(0)).getNodeValue());
 
NodeList pressureNmElmntLst = fstElmnt.getElementsByTagName("pressure");
Element pressureNmElmnt = (Element) pressureNmElmntLst.item(0);
NodeList pressureNm = pressureNmElmnt.getChildNodes();
current.setPressure(((Node) pressureNm.item(0)).getNodeValue());
 
NodeList cloudcoverNmElmntLst = fstElmnt.getElementsByTagName("cloudcover");
Element cloudcoverNmElmnt = (Element) cloudcoverNmElmntLst.item(0);
NodeList cloudcoverNm = cloudcoverNmElmnt.getChildNodes();
current.setCloudcover(((Node) cloudcoverNm.item(0)).getNodeValue());
 
}
}
return current;
 
 
}
 
public ArrayList <Location> getCity(String inCity) throws Exception {
 
ArrayList< Location> citys = new ArrayList<Location>();
 
try{
 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("http://www.worldweatheronline.com/feed/search.ashx?key=4aa3418d0a204342110904&query="+inCity+"&num_of_results=3&format=xml");
doc.getDocumentElement().normalize();
 
// System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("result");
//System.out.println("Information of all employees");
System.out.println("selected city is "+ inCity+ " result length is "+ nodeLst.getLength());
for (int s = 0; s < nodeLst.getLength(); s++) {
Location city = new Location();
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
 
Element fstElmnt = (Element) fstNode;
 
NodeList areaNameNmElmntLst = fstElmnt.getElementsByTagName("areaName");
Element areaNameNmElmnt = (Element) areaNameNmElmntLst.item(0);
NodeList areaNameNm = areaNameNmElmnt.getChildNodes();
System.out.println(((Node) areaNameNm.item(0)).getNodeValue());
city.setAreaName(((Node) areaNameNm.item(0)).getNodeValue());
 
NodeList countryNmElmntLst = fstElmnt.getElementsByTagName("country");
Element countryNmElmnt = (Element) countryNmElmntLst.item(0);
NodeList countryNm = countryNmElmnt.getChildNodes();
city.setCountry(((Node) countryNm.item(0)).getNodeValue());
 
NodeList latitudeNmElmntLst = fstElmnt.getElementsByTagName("latitude");
Element latitudeNmElmnt = (Element) latitudeNmElmntLst.item(0);
NodeList latitudeNm = latitudeNmElmnt.getChildNodes();
city.setLatitude(((Node) latitudeNm.item(0)).getNodeValue());
 
NodeList longitudeNmElmntLst = fstElmnt.getElementsByTagName("longitude");
Element longitudeNmElmnt = (Element) longitudeNmElmntLst.item(0);
NodeList longitudeNm = longitudeNmElmnt.getChildNodes();
city.setLongitude(((Node) longitudeNm.item(0)).getNodeValue());
 
NodeList populationNmElmntLst = fstElmnt.getElementsByTagName("population");
Element populationNmElmnt = (Element) populationNmElmntLst.item(0);
NodeList populationNm = populationNmElmnt.getChildNodes();
city.setPopulation(((Node) populationNm.item(0)).getNodeValue());
}
citys.add(city);
}
 
} catch (Exception e) {
e.printStackTrace();
}
return citys;
}
}
 
</syntaxhighlight>
 
=Consumer bundle=
 
1. Activator.java gets a refrence to the service provided by the provider bundle. Then it passes this object to the gui view. The gui will then display the information. In this context, the view can a gui, a console or even a portable device.
<syntaxhighlight lang="java">
 
package ecl.team2.project.weathersystem.client;
 
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
 
import ecl.team2.project.weather.WeatherInterface;
 
public class Activator implements BundleActivator {
 
private static BundleContext context;
private WeatherInterface sws;
 
static BundleContext getContext() {
return context;
}
 
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
ServiceReference reference = context.getServiceReference(WeatherInterface.class.getName());
if(reference != null){
sws = (WeatherInterface) context.getService(reference);
if(sws != null) {
WeatherDialog.runDialog(sws);
context.ungetService(reference);
} else
System.err.println("The Weather Station is down !!!");
} else
System.err.println("The Weather Station can not be found !!!");
}
 
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
System.err.println("Client Not Interested !");
}
}
 
</syntaxhighlight>
2. WeatherDialog.java
 
[]
1
edit