Click here to Skip to main content
15,893,161 members
Articles / Programming Languages / C#
Article

WeatherNotify

Rate me:
Please Sign up or sign in to vote.
4.06/5 (28 votes)
16 May 20052 min read 121.8K   2.6K   102   22
Show weather in status bar and/or in a window. Details can be shown on another window. This is a multiple languages/units software (to add more languages, just put an XML file in the language directory). This is a C# translation of phpweather.

Sample Image - WeatherNotify.jpg

Introduction

This shows the weather in the status bar and/or in a window.

Details can be shown on another window. This is a multiple languages/units software (to add more languages, just put an XML file in the language directory). This is a C# translation of phpweather.

This shows a good example for XML serialization, the use of notifyicons, dynamic image loading, multiple language interface and regular expressions.

First we download information from weather.noaa.gov. Next we parse this information using regular expressions to retrieve full weather information: clouds, temperature, pressure, wind information, precipitation, visibility. This helps us to associate the correct image to the corresponding weather.

Content

This application just connects to weather.noaa.gov every hour to retrieve weather information. All the information is in a string like "TFFR 281000Z 00000KT 9999 FEW020 19/18 Q1011 NOSIG" called METAR. (Information on METAR decoding can be found here.)

So we just parse this string using regular expressions to find weather information (temperature, wind, pressure ...). After we retrieve all the required information, we just show the image associated with the weather conditions and make a report containing all this information.

Project contents:

  • class METAR used for the parsing: only a big parsing of regular expressions.
  • ereg: use of Microsoft regular expression functions to work like the PHP one's.
  • class used to store decoded weather information: clouds, humidity, precipitation, runway, temp, visibility, weather, wind.
  • class to easily convert values: wind, pressure, temperature, length, height, wind speed.
  • xml_access: used for XML serialization for options and languages. It is used to save/restore object values in/from XML files.
  • tcp_socket: class to retrieve data on the Internet (part of the network stuff project available on this website).
  • class METAR download: launch download every hour or retry download 5 min after a failure.
  • forms for user interface.
  • other utility class (file_access, XP style).

Most important points:

The PHP ereg function emulation

We just try to make a similar function as that of PHP to avoid changing the parsing too much.

C#
public class ClassEreg
{
    // check if input_string match pattern and return groups in strregs
    public static bool ereg(string pattern,string input_string,
                           ref string[] strregs)    
    {
        if (input_string=="")
            return false;
        if (!System.Text.RegularExpressions.Regex.IsMatch(input_string,
           pattern,System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            return false;
        System.Text.RegularExpressions.Match m
              = System.Text.RegularExpressions.Regex.Match(input_string,
                     pattern, 
                     System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        strregs=new string[m.Groups.Count];
        for (int cpt=0;cpt<M.GROUPS.COUNT;CPT++)  
        {
            if {m.Groups[cpt].Captures.Count >0)
                strregs[cpt]=m.Groups[cpt].Captures[0].Value;
            else
                strregs[cpt]="";
        }

        return true;
    }
    // just check if input_string match pattern
    public static bool ereg(string pattern,string input_string)
    {
        return System.Text.RegularExpressions.Regex.IsMatch(input_string,
                      pattern,
                      System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    }
}

These functions are called during all the parsing (the METAR class is only a long boring parsing). Here's a sample usage:

C#
string[] regs=null;
string part="010V090";
if (ClassEreg.ereg("^([0-9]{3})V([0-9]{3})$", part,ref regs))
{
    /*
    * Variable wind-direction
    */
    this.wind.direction.var_beg = System.Convert.ToDouble(regs[1]);
    this.wind.direction.var_end = System.Convert.ToDouble(regs[2]);
}

Dynamic image loading

We use a PictureBox and its property PictureBox.Image to show images in forms.

C#
private System.Windows.Forms.PictureBox picturebox_weather;
public void set_img_weather(string fullpath)
{
    Image img=get_image(fullpath);
    if (img!=null)
        this.picturebox_weather.Image=img;
}
private Image get_image(string fullpath)
{
    try
    {
        return Image.FromFile(fullpath);
    }
    catch
    {
        MessageBox.Show( "File "+fullpath+" not found.","Error",
                         MessageBoxButtons.OK,MessageBoxIcon.Error);
        return null;
    }
}

Dynamic image loading for notify icon

C#
private System.Windows.Forms.NotifyIcon notify_icon;
string img_path="image.png";
// create a ComponentModel.Container if not exists
if (this.components==null)
    this.components = new System.ComponentModel.Container();
notify_icon=new System.Windows.Forms.NotifyIcon(this.components);
// use the GetThumb function to resize image with good quality
notify_icon.Icon=System.Drawing.Icon.FromHandle(
           this.GetThumb(new Bitmap(img_path),16,16).GetHicon());

Text for notify icon

C#
private void drawstring_in_notify_icon(string text,
                                       NotifyIcon notify_icon)
{
    // Create a graphics instance that draws to a bitmap
    Bitmap bitmap = new Bitmap( 16, 16,
                 System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    Graphics graphics = Graphics.FromImage(bitmap);

    // FONT
    System.Drawing.Font drawFont
               = new System.Drawing.Font("Arial Narrow",8,
                                    System.Drawing.FontStyle.Regular);

    // TEXT
    string drawString = text;

    SizeF drawStringSize = new SizeF();
    //Measure the Copyright string in this Font
    drawStringSize = graphics.MeasureString(drawString, drawFont);

    // BRUSH
    System.Drawing.SolidBrush drawBrush
                            = new System.Drawing.SolidBrush(
                                 Color.FromArgb( 255, 255, 255, 255));
    // center text in icon image
    graphics.DrawString(drawString, drawFont, drawBrush,
           16/2-drawStringSize.Width/2,16/2-drawStringSize.Height/2);

    notify_icon.Icon=System.Drawing.Icon.FromHandle(bitmap.GetHicon());

    drawFont.Dispose();
    drawBrush.Dispose();
    graphics.Dispose();
}

XML loading/saving

The trick is the same for options, language and language interface XML files. We just use XML serialization. The following sample is for the options class:

C#
public static ClassOptions load(string config_file_name)
{
    try
    {
        return (ClassOptions)
                    XML_access.XMLDeserializeObject(config_file_name,
                                               typeof(ClassOptions));
    }
    catch
    {
        return new ClassOptions();
    }
}
public bool save(string config_file_name)
{
    try
    {
        XML_access.XMLSerializeObject(config_file_name,this,
                                      typeof(ClassOptions));
        return true;
    }
    catch (Exception e)
    {
        System.Windows.Forms.MessageBox.Show(e.Message,"Error",
            System.Windows.Forms.MessageBoxButtons.OK,
            System.Windows.Forms.MessageBoxIcon.Error);
        return false;
    }
}

Where the XML serialization class is the following:

C#
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

public class XML_access
{
    /// simple sample of call
    /// XMLSerializeObject("samplei.xml",i,typeof(OrderedItem));
    /// sample of call for ArrayList
    /// XMLSerializeObject("sampleal.xml",
    ///            (OrderedItem[])al.ToArray(typeof(OrderedItem)),
    ///            typeof(OrderedItem[]));

    public static void XMLSerializeObject(string filename,object obj,
                                          System.Type typeof_object)
    {
        Stream fs=null;
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof_object);
            // Create an XmlTextWriter using a FileStream.
            fs = new FileStream(filename, FileMode.Create,
                                System.IO.FileAccess.ReadWrite );
            XmlWriter writer = new XmlTextWriter(fs,
                                System.Text.Encoding.Unicode);
            // Serialize using the XmlTextWriter.
            serializer.Serialize(writer, obj);
            writer.Close();
            fs.Close();
        }
        catch(Exception e)
        {
            if (fs!=null)
                fs.Close();
            System.Windows.Forms.MessageBox.Show( e.Message,
                               "Error",
                               System.Windows.Forms.MessageBoxButtons.OK,
                               System.Windows.Forms.MessageBoxIcon.Error);
        }
    }
    /// simple sample of call
    /// OrderedItem i=(OrderedItem)XMLDeserializeObject("simple.xml",
    ///                           typeof(OrderedItem));
    /// sample of call for ArrayList
    /// System.Collections.ArrayList al=
    ///       new System.Collections.ArrayList((OrderedItem[])
    ///       XMLDeserializeObject("sampleal.xml",typeof(OrderedItem[])));
    public static object XMLDeserializeObject(string filename,
                                              System.Type typeof_object)
    {
        // Create an instance of the XmlSerializer specifying type and
        //namespace.
        XmlSerializer serializer = new XmlSerializer(typeof_object);
        // A FileStream is needed to read the XML document.
        FileStream fs = new FileStream(filename, FileMode.Open);
        XmlReader reader = new XmlTextReader(fs);
        // Declare an object variable of the type to be deserialized.
        object obj;
        // Use the Deserialize method to restore the object's state.
        obj = serializer.Deserialize(reader);
        reader.Close();
        fs.Close();
        return obj;
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
France France

Comments and Discussions

 
GeneralSorted country/city combobox Pin
Coderider8-Feb-06 8:32
Coderider8-Feb-06 8:32 
GeneralRe: Sorted country/city combobox Pin
rantanplanisback10-Feb-06 9:44
rantanplanisback10-Feb-06 9:44 
GeneralCool article, i like it thanks. Pin
mrskyok17-Jul-05 4:35
mrskyok17-Jul-05 4:35 
GeneralAdding new city Pin
Yusuf D M27-Feb-05 17:06
Yusuf D M27-Feb-05 17:06 
GeneralRe: Adding new city Pin
jacquelin potier28-Feb-05 1:05
sussjacquelin potier28-Feb-05 1:05 
GeneralRe: Adding new city Pin
bxb19-May-05 3:12
bxb19-May-05 3:12 
rantanplanisback => terrible your pseudo Laugh | :laugh:

nice job,
congratulations



GeneralOpen Source Spirit Pin
Jens Scheidtmann25-Jan-05 20:56
Jens Scheidtmann25-Jan-05 20:56 
GeneralRe: Open Source Spirit Pin
Corinna John25-Jan-05 21:39
Corinna John25-Jan-05 21:39 
GeneralRe: Open Source Spirit Pin
rantanplanisback26-Jan-05 9:55
rantanplanisback26-Jan-05 9:55 
GeneralRe: Open Source Spirit Pin
rantanplanisback27-Feb-05 10:55
rantanplanisback27-Feb-05 10:55 
GeneralUnhandled Exception... Pin
Drew Stainton21-Jan-05 6:46
Drew Stainton21-Jan-05 6:46 
GeneralRe: Unhandled Exception... Pin
rantanplanisback21-Jan-05 8:23
rantanplanisback21-Jan-05 8:23 
GeneralRe: Unhandled Exception... Pin
Drew Stainton21-Jan-05 8:40
Drew Stainton21-Jan-05 8:40 
GeneralRe: Unhandled Exception... Pin
rantanplanisback25-Jan-05 10:40
rantanplanisback25-Jan-05 10:40 
GeneralRe: Unhandled Exception... Pin
Drew Stainton25-Jan-05 12:34
Drew Stainton25-Jan-05 12:34 
GeneralPlease explain the code Pin
Corinna John20-Jan-05 23:53
Corinna John20-Jan-05 23:53 
GeneralRe: Please explain the code Pin
toxcct21-Jan-05 2:29
toxcct21-Jan-05 2:29 
GeneralRe: Please explain the code Pin
rantanplanisback21-Jan-05 8:34
rantanplanisback21-Jan-05 8:34 
GeneralRe: Please explain the code Pin
Uwe Keim21-Jan-05 8:39
sitebuilderUwe Keim21-Jan-05 8:39 
GeneralRe: Please explain the code Pin
DavidNohejl25-Jan-05 11:56
DavidNohejl25-Jan-05 11:56 
GeneralRe: Please explain the code Pin
rantanplanisback26-Jan-05 10:02
rantanplanisback26-Jan-05 10:02 
GeneralRe: Please explain the code Pin
Todd Carnes19-Jun-10 6:20
Todd Carnes19-Jun-10 6:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.