Wetterdaten abfragen

Status
Nicht offen für weitere Antworten.
Was du da verlinkt hast, war anscheinend irgendein Programm zum auslesen der RSS-feeds von irgendeiner bestimmten wetter-seite. Das hat weder mit wetter noch mit java großartig viel zu tun, da muss man nur wissen wie dieser konkrete RSS aufgebaut ist (bei wikipedia ist zB. der allgemeine Aufbau gezeigt).

Dann lädt man so ein teil, etwa von hier: http://www.rssweather.com/wx/de//aachen/rss.php, parst es mit einem JDOM (gug dir direkt im browser den quelltext an, da siehst du ja wie dieser RSS aussieht) oder sonstwie (ist ja alles valides XML) und stellt das irgendwie dar, wie es einem grad gefällt. Um ein "umfassendes Bild" zu erhalten, müsste man aber ganz schön viele RSS runterladen... Das würde nur den Server der guten Leute, die Wetterdaten kostenlos online stellen, auslasten.

Wozu brauchst du das denn, falls kein Betriebsgeheimnis?

Also, irgendeine "spezielle Javaklasse" aus der StandardAPI wirst du dafür definitiv nicht finden, Sun betreibt ja afaik auch keine Wetterstationen^^ Und sonst muss man das für jeden RSS-feed ja sowieso speziell anpassen.

Vielleicht gibt es aber auch Wetterstationen, die sowas (etwa zu forschungszwecken) irgendwie direkter liefern, ohne Umweg über RSS und den ganzen kram. Vielleicht haben sich die Meteorologen sich da sogar auf irgendein weltweit anerkanntes Format geeinigt, keine Ahnung, google n bisschen rum, vielleicht findest du ja was...
 
So, hab mich dann doch mal selbst hingesetzt und was eigenes gebaut, ist doch immer noch das Beste...

Wohl nicht perfekt, aber fuer meine Zwecke reicht es allemal!

Die xml daten koennen entweder online geholt werden, oder per datei eingelesen werden (zum testen, da weather.com nur zugriffe in bestimmten abstaenden (siehe pdf) erlaubt).
GMXX0086 bei der URL repraesentiert die Stadt, kann man bei weather.com suchen.

Die Uebersetzung ins deutsch habe ich aus einem perl paket.

@Andrey: ich programmiere mir eine Haussteuerung, ein fuer mich schon sehr komplexes System. Gesteuert werden Heizung und Rollladen. Jetzt will ich den Sonnenschutz nur aktiv haben, wenn die Sonne scheint 😉
Bedient wird das ganze ueber ein Touchscreen, der eine HTML Seite darstellt. Php holt sich die daten von meinen Programm ueber Sockets. Das Programm steuert dann alles per modbus tcp (jamod.sf.net) mittels eines fatek moduls. Alles in allem sehr cool, da es jetzt ganz gut laeuft und ich solche features einbaue. 🙂
Vielleicht sollte ich doch mein Hobby zum beruf machen!

Code:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.*;
import org.xml.sax.helpers.*;



public class weathertest extends DefaultHandler
{
	  // bei weather.com kostenlos anmelden, da bekommt man die daten
	  // und ein sdk, wo naehere infos im pdf zu finden sind
	  public static String partnerID = "EureID";
	  public static String licenseKey = "EuerKey";
	  public static int depth=-1;
	  public static ArrayList<String> path = new ArrayList<String>();
	  public static HashMap<String, String> weatherData = new HashMap<String, String>();
	  public static HashMap<String, String> translation = new HashMap<String, String>();
	  public static String element = "";
	  
	   
	public static void main(String[] args) {
		
	    
		weatherData = getWeatherData();

		
		// abfragen:
		
		System.out.println("Mond: " + weatherData.get("weather-cc-moon-t"));
		System.out.println("Wetter: " + weatherData.get("weather-cc-t"));
		System.out.println("Temperatur: " + weatherData.get("weather-cc-tmp"));
		System.out.println("Icon: " + weatherData.get("weather-cc-icon"));
		System.out.println("Trend: " + weatherData.get("weather-cc-bar-d"));
	
	}


	private static HashMap<String, String> getWeatherData() {
		
	   XMLReader xr=null;
	   try{
		xr = XMLReaderFactory.createXMLReader();
		DefaultHandler  handler = new weathertest();
		xr.setContentHandler(handler);
		xr.setErrorHandler(handler);
	   } catch( Throwable t ) {
		      t.printStackTrace();
	   }

		// Parse each file provided on the
		// command line.
	    try {
			xr.parse(new InputSource(getXMLfromFile()));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
			
		return weatherData;

	}


	private static BufferedReader getXML() {
		BufferedReader input=null;
		   try
		   {
			   URL url = new URL("http://xoap.weather.com/weather/local/GMXX0086?par=partnerID&
key=licenseKey&prod=xoap&cc=*&link=xoap&unit=m");

			   URLConnection connection = url.openConnection();
			   connection.setDoInput(true);
			   InputStream inStream = connection.getInputStream();
			   input = new BufferedReader(new InputStreamReader(inStream));
		   	}
		   	catch (Exception e){
		   		System.out.println(e.toString());
		   	}
		   	
		   	return input;
	}


	private static DataInputStream getXMLfromFile() {
		File file = new File("C:/workspace/weathertest/GMXX0086.xml");
	    FileInputStream fis = null;
	    BufferedInputStream bis = null;
	    DataInputStream dis = null;

	    try {
	      fis = new FileInputStream(file);

	      // Here BufferedInputStream is added for fast reading.
	      bis = new BufferedInputStream(fis);
	      dis = new DataInputStream(bis);

	    } catch (FileNotFoundException e) {
	      e.printStackTrace();
	    }		
	    return dis;
	}


	public weathertest ()
	    {
		super();
		
		initializeTranslationTable();
		
	    }


	    ////////////////////////////////////////////////////////////////////
	    // Event handlers. for xml parsing
	    ////////////////////////////////////////////////////////////////////
	    public void startElement (String uri, String name,
				      String qName, Attributes atts)
	    {
		    depth++;
 		    //path.add("name:" + name  + " qName:" + qName );
		    path.add(qName );
	    }


	    public void endElement (String uri, String name, String qName)
	    {
	    	
	    	//System.out.println(toString(path) + " --> " + element);
	    	weatherData.put(toString(path), translateItem(element));
	    	path.remove(depth);
	    	depth--;
	    }




		private String toString(ArrayList<String> path2) {
			String output="";
			
	    	if(!path2.isEmpty()){
		    	// 	format allShutterMovements:{ shutterRef, dir, duration }  1:up, 0:down
				for (String tmp : path2) {
					output+=tmp+"-";
				}
	    	}    	
	    	output=output.substring(0, output.length()-1);
	    	
	    	return output;
		}

		public void characters (char ch[], int start, int length)
	    {
		    
	    	
			String orig = path.get(depth);
			String changed="", tmp="";
			//System.out.println(start);
			
			if(ch[start]==0)
				return;
			
			for (int i = start; i < start + length; i++) {
			    switch (ch[i]) {
			    case '\\':
				//System.out.print("\\\\");
				break;
			    case '"':
				//System.out.print("\\\"");
				break;
			    case '\n':
				//System.out.print("\\n");
				break;
			    case '\r':
				//System.out.print("\\r");
				break;
			    case '\t':
				//System.out.print("\\t");
				break;
			    default:
				tmp+=ch[i];
				break;
			    }
			}
			//System.out.print("\"\n");
			element = tmp;
			changed=orig + "(" + tmp + ")";
	    }


	    private String translateItem(String item) {
			
	    	item = item.toLowerCase();
	    	//System.out.println(item);
	    	if(translation.containsKey(item))
	    		return translation.get(item);
	    	else
	    		return item;
		}

		
		
	    private void initializeTranslationTable() {
			


	    	// some general things...
	    	translation.put("n/a","nicht verfgbar");
	    	translation.put("N/A","nicht verfgbar");
	    	translation.put("Not Available", "nicht verfgbar");
	    	translation.put("unknown", "unbekannt");
	    	translation.put("NONE", "keine");
	    	translation.put("day","Tag");
	    	translation.put("night", "Nacht");

	    	// first all about moon phases
	    	translation.put("new","Neumond");
	    	translation.put("first quarter", "Viertelmond");
	    	translation.put("full","Vollmond");
	    	translation.put("last quarter", "Dreiviertelmond");
	    	translation.put("waning crescent", "abnehmend");
	    	translation.put("waning gibbous", "abnehmend");
	    	translation.put("waxing crescent", "zunehmend");
	    	translation.put("waxing gibbous", "zunehmend");

	    	// about UV Index...
	    	translation.put("extreme", "extrem");
	    	translation.put("very high", "sehr hoch");
	    	translation.put("high", "hoch");
	    	translation.put("moderate", "moderat");
	    	translation.put("low", "niedrig");

	    	// tendencies used for barometric pressure
	    	translation.put("rising", "steigend");
	    	translation.put("falling", "fallend");
	    	translation.put("steady", "stabil");

	    	// all about weather conditions
	    	translation.put("blowing dust","Sandsturm");
	    	translation.put("blowing dust and windy", "Sandsturm und windig");
	    	translation.put("blowing snow","Schneesturm");
	    	translation.put("blowing snow and windy", "Schneestreiben und windig");
	    	translation.put("clear","klar");
	    	translation.put("clear and windy","klar und windig");
	    	translation.put("cloudy","bewlkt");
	    	translation.put("cloudy and windy","bewlkt und windig");
	    	translation.put("drifting snow","Schneetreiben");
	    	translation.put("drizzle","Nieselregen");
	    	translation.put("fair","heiter");
	    	translation.put("fair and windy","heiter und windig");
	    	translation.put("fog","Nebel");
	    	translation.put("fog and windy","Nebel und windig");
	    	translation.put("haze","trb");
	    	translation.put("haze and windy","trb und windig");
	    	translation.put("heavy drizzle","schwerer Sprhregen");
	    	translation.put("heavy rain","schwerer Regen");
	    	translation.put("heavy rain and windy", "schwerer Regen und windig");
	    	translation.put("heavy rain shower","schwere Regenschauer");
	    	translation.put("heavy rain shower and windy", "schwere Regenschauer und windig");
	    	translation.put("heavy snow","schwerer Schneefall");
	    	translation.put("heavy snow and windy", "schwerer Schneefall und windig");
	    	translation.put("heavy snow shower","schwere Schneeschauer");
	    	translation.put("heavy t-storm","schweres Gewitter");
	    	translation.put("heavy t-storm and windy", "schweres Gewitter und windig");
	    	translation.put("light drizzle","leichter Nieselregen");
	    	translation.put("light drizzle and windy", "leichter Nieselregen und windig");
	    	translation.put("light freezing drizzle", "leichter Eisregen");
	    	translation.put("light freezing drizzle and fog", "leichter Eisregen und Nebel");
	    	translation.put("light freezing rain", "leichter Eisregen");
	    	translation.put("light rain","leichter Regen");
	    	translation.put("light rain shower","leichte Regenschauer");
	    	translation.put("light rain shower and windy", "leichte Regenschauer und windig");
	    	translation.put("light rain and fog","leichter Regen und Nebel");
	    	translation.put("light rain and freezing rain", "leichter Regen und Eisregen");
	    	translation.put("light rain with thunder", "leichter Regen und Donner");
	    	translation.put("light rain and windy", "leichter Regen und windig");
	    	translation.put("light snow","leichter Schneefall");
	    	translation.put("light snow grains","leichte Schneegraupel");
	    	translation.put("light snow shower","leichte Schneeschauer");
	    	translation.put("light snow shower and windy", "leichte Schneeschauer und windig");
	    	translation.put("light snow and sleet", "leichter Schneefall und Schneeregen");
	    	translation.put("light snow and windy", "leichter Schneefall und windig");
	    	translation.put("mist","Sprhregen");
	    	translation.put("mostly cloudy","berwiegend bewlkt");
	    	translation.put("mostly cloudy and windy", "berwiegend bewlkt und windig");
	    	translation.put("partial fog","rtlich Nebel");
	    	translation.put("partly cloudy","teilweise bewlkt");
	    	translation.put("partly cloudy and windy", "teilweise bewlkt und windig");
	    	translation.put("patches of fog","Nebelfelder");
	    	translation.put("rain","Regen");
	    	translation.put("rain and sleet","Regen und Schneeregen");
	    	translation.put("rain and snow","Schneeregen");
	    	translation.put("rain shower","Regenschauern");
	    	translation.put("rain shower and windy", "Regenschauern und windig");
	    	translation.put("rain and fog","Regen und Nebel");
	    	translation.put("rain and freezing rain", "Regen und Eisregen");
	    	translation.put("rain and windy","Regen und windig");
	    	translation.put("sand","Sand");
	    	translation.put("sand and windy","Sand und windig");
	    	translation.put("shallow fog","Bodennebel");
	    	translation.put("showers in the vicinity", "rtliche Schauern");
	    	translation.put("sleet","Graupel");
	    	translation.put("smoke","Dunst");
	    	translation.put("snow","Schnee");
	    	translation.put("snow and fog","Schneefall und nebelig");
	    	translation.put("snow and freezing rain", "Schnee und Eisregen");
	    	translation.put("snow grains","Schneegraupel");
	    	translation.put("snow showers","Schneeschauern");
	    	translation.put("snow and windy and fog", "Schneefall, windig und nebelig");
	    	translation.put("squalls","Sturmben");
	    	translation.put("squalls and windy","Wind und Sturmben");
	    	translation.put("sunny","sonnig");
	    	translation.put("sunny and windy","sonnig und windig");
	    	translation.put("t-storm","Gewitter");
	    	translation.put("t-storm and windy","Gewitter und windig");
	    	translation.put("thunder","Gewitter");
	    	translation.put("thunder and wintry mix", "Gewitter und wechselnde Winde");
	    	translation.put("thunder in the vicinity", "rtliche Gewitter");
	    	translation.put("unknown precip","unbekannt");
	    	translation.put("widespread dust","verbreitet staubig");
	    	translation.put("widespread dust and windy", "verbreitet staubig und windig");
	    	translation.put("wintry mix","wechselnde Winde");

	    	// wind directions long
	    	translation.put("East","Osten");
	    	translation.put("East Northeast", "Ost Nordost");
	    	translation.put("East Southeast", "Ost Sdost");
	    	translation.put("North","Norden");
	    	translation.put("Northeast", "Nordosten");
	    	translation.put("North Northeast", "Nord Nordost");
	    	translation.put("North Northwest", "Nord Nordwest");
	    	translation.put("Northwest", "Nordwesten");
	    	translation.put("South","Sden");
	    	translation.put("Souteast", "Sdosten");
	    	translation.put("South Southeast", "Sd Sdost");
	    	translation.put("South Southwest", "Sd Sdwest");
	    	translation.put("Southwest", "Sdwesten");
	    	translation.put("variable", "wechselnd");
	    	translation.put("West","Westen");
	    	translation.put("West Northwest", "West Nordwest");
	    	translation.put("West Southwest", "West Sdwest");

	    	// wind directions short
	    	translation.put("E", "O");
	    	translation.put("ENE", "ONO");
	    	translation.put("ESE", "OSO");
	    	translation.put("N", "N");
	    	translation.put("NE", "NO");
	    	translation.put("NNE", "NNO");
	    	translation.put("NNW", "NNW");
	    	translation.put("NW", "NW");
	    	translation.put("S", "S");
	    	translation.put("SE", "SO");
	    	translation.put("SSE", "SSO");
	    	translation.put("SSW", "SSW");
	    	translation.put("SW", "SW");
	    	translation.put("VAR", "wechselnd");
	    	translation.put("W", "W");
	    	translation.put("WNW", "WNW");
	    	translation.put("WSW", "WSW");
	
		}

		
		

}//end class TestWeatherGateway
 
hmm... wenn das nur einmal die Stunde updated wird, wäre man vielleicht mit irgendeinem Lichtsensor bzw einer 15€-webcam, die gegen eine ebene weiße Wand gerichtet ist, besser dran?😕 Naja, viel Spaß jedenfallst, hört sich alles recht interessant an.
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben