sbarski / dashing.net

dashing.net is a direct .NET port of the dashing framework originally created by Shopify
MIT License
93 stars 56 forks source link

Dashing.net weather app #8

Closed gsaxam closed 11 years ago

gsaxam commented 11 years ago

Hi Peter,

My next challenge is to built a weather app on dashing.net. We are a huge fan of Dashing.net already!

I have the following code I tried to port from Ruby into .NET based on this example https://gist.github.com/mjamieson/5274790

I have been trying to fix it for a past few days but no luck!

  namespace dashing.net.jobs
   {
     [Export(typeof(IJob))]
    public class Weather : IJob
    {

    private const string f_lat = "45.429522";
    private const string f_long = "-75.689613";
    private const string f_unit = "si";

    public Lazy<Timer> Timer { get; private set; }

    public Weather()
    {
        Timer = new Lazy<Timer>(() => new Timer(SendMessage, null, TimeSpan.Zero, TimeSpan.FromMinutes(15)));
    }

    private void SendMessage(object message)
    {
        using (var client = new WebClient())
        {
            var url = string.Format("http://forecast.io/embed/#lat={0}&lon={1}", HttpUtility.UrlEncode(f_lat), HttpUtility.UrlEncode(f_long));

            using (var data = client.OpenRead(url))
            {
                if (data == null)
                {
                    return;
                }

                var reader = new StreamReader(data);

                var feed = reader.ReadToEnd();
                //var weatherData = new {};
               // var weatherLocation = new {};

                var results = JsonConvert.DeserializeObject<Weather>(reader.ReadToEnd());

                Dashing.SendMessage(new { id = "weather", temp = results});
            }
        }
    }
}