Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Threading;
- using System.IO;
- using System.Net;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Presentation;
- using Microsoft.SPOT.Presentation.Controls;
- using Microsoft.SPOT.Presentation.Media;
- using Microsoft.SPOT.Touch;
- // add the time methods
- using Microsoft.SPOT.Time;
- using Gadgeteer.Networking;
- using GT = Gadgeteer;
- using GTM = Gadgeteer.Modules;
- using Gadgeteer.Modules.GHIElectronics;
- namespace Gadgeteer_Simple_Wifi_01
- {
- public partial class Program
- {
- // create new timer event
- // to start the wifi
- GT.Timer wifiTimer = new GT.Timer(4000, GT.Timer.BehaviorType.RunOnce);
- // create new timer event
- // to trigger the calendar query every 15 minutes
- //GT.Timer calendarTimer = new GT.Timer(15 * 60 * 1000, GT.Timer.BehaviorType.RunContinuously);
- GT.Timer calendarTimer = new GT.Timer(1 * 60 * 1000, GT.Timer.BehaviorType.RunContinuously);
- // is the wifi connected
- bool wifiConnected = false;
- // current time and date
- // AM or PM marker
- bool morningMarker;
- // offsets for creating the clock slots
- int pmOffset = 12;
- int dayOffset = 1;
- // 30 minute meeting slot arrays
- // for 12 hours
- DateTime[] slotStartTime = new DateTime[24];
- DateTime[] slotEndTime = new DateTime[24];
- bool[] eventSlot = new bool[24];
- bool[] eventEdge = new bool[24];
- int currentSlot = 0;
- //DispatcherTimer timer;
- bool timeSynchronized;
- // This method is run when the mainboard is powered up or reset.
- void ProgramStarted()
- {
- /*******************************************************************************************
- Modules added in the Program.gadgeteer designer view are used by typing
- their name followed by a period, e.g. button. or camera.
- Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
- button.ButtonPressed +=<tab><tab>
- If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
- GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
- timer.Tick +=<tab><tab>
- timer.Start();
- *******************************************************************************************/
- // jump out of the program start to startWifi later
- // create a timer event handler robot
- wifiTimer.Tick += new GT.Timer.TickEventHandler(wifiTimer_Tick);
- // start the timer
- wifiTimer.Start();
- // query the calendar every 15 minutes
- // create a timer event handler robot
- calendarTimer.Tick += new GT.Timer.TickEventHandler(calendarTimer_Tick);
- //DateTime from the internet
- TimeService.SystemTimeChanged += new SystemTimeChangedEventHandler(TimeService_SystemTimeChanged);
- TimeService.TimeSyncFailed += new TimeSyncFailedEventHandler(TimeService_TimeSyncFailed);
- // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
- Debug.Print("Program Started");
- }
- void calendarTimer_Tick(GT.Timer timer)
- {
- // query the web based calendar
- getFromWeb();
- }
- void wifiTimer_Tick(GT.Timer timer)
- {
- // start the wifi
- startWifi();
- // make sure the timer stops
- timer.Stop();
- }
- void startWifi()
- {
- if (!wifi_RS21.Interface.IsOpen)
- {
- wifi_RS21.Interface.Open();
- }
- if (!wifi_RS21.Interface.NetworkInterface.IsDhcpEnabled)
- {
- wifi_RS21.Interface.NetworkInterface.EnableDhcp();
- }
- GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(wifi_RS21.Interface);
- wifi_RS21.Interface.WirelessConnectivityChanged += new GHI.Premium.Net.WiFiRS9110.WirelessConnectivityChangedEventHandler(Interface_WirelessConnectivityChanged);
- //wifi_RS21.Interface.NetworkAddressChanged += new GHI.Premium.Net.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Interface_NetworkAddressChanged);
- // Your router SSID needed here
- //String networkName = "RISD-MiscDevices";
- String networkName = "networkname";
- // Your network password here
- //String networkPassword = "R1sdB34ch2012";
- String networkPassword = "password";
- try
- {
- Debug.Print("Trying the network");
- // wifi needs your SSID network name
- GHI.Premium.Net.WiFiNetworkInfo[] ScanResp = wifi_RS21.Interface.Scan(networkName);
- if (ScanResp != null && ScanResp.Length > 0)
- {
- // wifi needs the password key
- wifi_RS21.Interface.Join(ScanResp[0], networkPassword);
- Debug.Print("***Connected to network...");
- }
- else
- {
- Debug.Print("***Wireless network was not found");
- }
- }
- catch (Exception e)
- {
- Debug.Print("!!! Exception : " + e.Message);
- }
- // connected!
- Debug.Print("Network connected here first?");
- wifiConnected = true;
- // set the time
- setWorldTime();
- }
- void setWorldTime()
- {
- // Configure TimeService settings
- // create a settings object
- TimeServiceSettings settings = new TimeServiceSettings();
- // fill the settings
- settings.ForceSyncAtWakeUp = true;
- // refresh time in seconds
- settings.RefreshTime = 1800;
- // set up some time servers
- IPAddress[] address = Dns.GetHostEntry("time-a.nist.gov").AddressList;
- if (address != null && address.Length > 0)
- {
- settings.PrimaryServer = address[0].GetAddressBytes();
- }
- address = Dns.GetHostEntry("pool.ntp.org").AddressList;
- if (address != null && address.Length > 0)
- {
- settings.AlternateServer = address[0].GetAddressBytes();
- }
- // load the settings
- TimeService.Settings = settings;
- // Add the time zone offset to the retrieved UTC Time
- // (in this example, it assumes that time zone is GMT - 4 hours).
- // this doesn't work?
- TimeService.SetTimeZoneOffset(60 * -4);
- // start the time service running
- TimeService.Start();
- }
- void TimeService_TimeSyncFailed(object sender, TimeSyncFailedEventArgs e)
- {
- Debug.Print("Failed: " + e.ErrorCode);
- }
- void TimeService_SystemTimeChanged(object sender, SystemTimeChangedEventArgs e)
- {
- // the time has been returned
- Debug.Print("Time set at: " + e.EventTime);
- //Debug.Print("Network time received.");
- if (!timeSynchronized)
- {
- // make a note that the time has been set
- timeSynchronized = true;
- // get the calendar info for the first time
- getFromWeb();
- }
- }
- void Interface_WirelessConnectivityChanged(object sender, GHI.Premium.Net.WiFiRS9110.WirelessConnectivityEventArgs e)
- {
- // network up
- if (e.IsConnected)
- {
- Debug.Print("Network connected!");
- // set the time here?
- }
- else
- {
- Debug.Print("Not conneced");
- }
- }
- void getFromWeb()
- {
- // ask the calendar for the previous and next 24 hours
- WebClient.GetFromWeb("http://inbetwixt.com/calendar/calendar.json").ResponseReceived += new HttpRequest.ResponseHandler(Program_ResponseReceived);
- Debug.Print("***Waiting for response...");
- }
- void Program_ResponseReceived(HttpRequest sender, HttpResponse response)
- {
- // do something with the responce
- //Debug.Print("response: " + response.Text);
- // create all the clock face time slots
- createClock();
- // then process the events
- processEvents(response);
- }
- void createClock()
- {
- //eventSlotObject[0].slotStartTime;
- //Debug.Print("Test " + slotStartTime[0]);
- // is it currently AM or PM on the clock face?
- if (DateTime.Now.ToString("tt") == "AM")
- {
- Debug.Print("The current slot is AM");
- morningMarker = true;
- }
- else if (DateTime.Now.ToString("tt") == "PM")
- {
- Debug.Print("The current slot is PM");
- morningMarker = false;
- }
- // what is the current clock hour for the current time?
- currentSlot = Convert.ToInt32(DateTime.Now.ToString("hh")) * 2;
- // check if the time is past the half hour
- if (Convert.ToInt32(DateTime.Now.ToString("mm")) >= 30)
- {
- currentSlot++;
- }
- Debug.Print("currentSlot: " + currentSlot);
- // reset the data in the eventObjects
- // DO NOT use this on the first or last day of the month!!
- Debug.Print("Current time: " + DateTime.Now.ToString("HH:mm"));
- // count forwards towards the 24th slot
- for (int increment = 0; increment < 24; increment++)
- {
- Debug.Print("Calculating slot: " + increment);
- if (increment == currentSlot)
- {
- Debug.Print("This is the current slot!");
- }
- // if this slot is before the current slot
- if (increment < currentSlot)
- {
- // if the current slot is AM and this slot is before the current slot
- // then all these dates and times are in today PM
- if (morningMarker)
- {
- Debug.Print("The current slot is AM and this slot is before it");
- // fix the 12 o'clock weirdness
- if (currentSlot == 0 || currentSlot == 1)
- {
- pmOffset = 0;
- dayOffset = 0;
- }
- // add 12 hours for PM
- else
- {
- pmOffset = 12;
- dayOffset = 0;
- }
- // slot start time
- slotStartTime[increment] = new DateTime(
- DateTime.Now.Year,
- DateTime.Now.Month,
- DateTime.Now.Day + dayOffset,
- // here is the slot hour time for PM
- ((increment - (increment % 2)) / 2) + pmOffset,
- // here are the minutes example 9 = 30, 6 = 0
- (30 * (currentSlot % 2)),
- // zero seconds
- 0);
- Debug.Print("Slot: " + increment + " start time " + slotStartTime[increment]);
- // slot end time
- slotEndTime[increment] = new DateTime(
- DateTime.Now.Year,
- DateTime.Now.Month,
- DateTime.Now.Day + dayOffset,
- // here is the slot hour time for PM
- ((increment - (increment % 2)) / 2) + pmOffset,
- // here are the minutes example 9 = 59, 6 = 29
- ((30 * (currentSlot % 2)) + 29),
- // 29 seconds
- 29);
- Debug.Print("Slot: " + increment + " end time " + slotEndTime[increment]);
- }
- // if the current slot is PM and this slot is before the current slot
- // then all these dates and times are in tomorrow AM
- else
- {
- Debug.Print("The current slot is PM and this slot is before it");
- // fix the 12 o'clock weirdness
- if (increment == 0 || increment == 1)
- {
- pmOffset = 0;
- dayOffset = 1;
- }
- // don't add 12 hours
- else
- {
- pmOffset = 0;
- dayOffset = 1;
- }
- // slot start time
- slotStartTime[increment] = new DateTime(
- DateTime.Now.Year,
- DateTime.Now.Month,
- DateTime.Now.Day + dayOffset,
- // here is the slot hour time for PM
- ((increment - (increment % 2)) / 2) + pmOffset,
- // here are the minutes example 9 = 30, 6 = 0
- (30 * (increment % 2)),
- // zero seconds
- 0);
- Debug.Print("Slot: " + increment + " start time " + slotStartTime[increment]);
- //eventSlotObject[increment].slotStartYear = 2013;
- //eventSlotObject[increment].slotStartMonth = DateTime.Now.Month;
- //eventSlotObject[increment].slotStartDay = DateTime.Now.Day + dayOffset;
- //// here is the slot hour time for PM
- //eventSlotObject[increment].slotStartHour = (increment / 2) - (increment % 2) + pmOffset;
- //// here are the minutes example 9 = 30, 6 = 0
- //eventSlotObject[increment].slotStartMinute = (30 * (currentSlot % 2));
- //// zero seconds
- //eventSlotObject[increment].slotStartSecond = 0;
- //Debug.Print("Slot: " + increment + " start time " + eventSlotObject[increment].slotStartHour + ":" + eventSlotObject[increment].slotStartMinute);
- // slot end time
- slotEndTime[increment] = new DateTime(
- DateTime.Now.Year,
- DateTime.Now.Month,
- DateTime.Now.Day + dayOffset,
- // here is the slot hour time for PM
- ((increment - (increment % 2)) / 2) + pmOffset,
- // here are the minutes example 9 = 59, 6 = 29
- ((30 * (increment % 2)) + 29),
- // 29 seconds
- 29);
- Debug.Print("Slot: " + increment + " end time " + slotEndTime[increment]);
- //eventSlotObject[increment].slotEndYear = DateTime.Now.Year;
- //eventSlotObject[increment].slotEndMonth = DateTime.Now.Month;
- //eventSlotObject[increment].slotEndDay = DateTime.Now.Day + dayOffset;
- //// here is the slot hour time for PM
- //eventSlotObject[increment].slotEndHour = (increment / 2) - (increment % 2) + pmOffset;
- //// here are the minutes example 9 = 59, 6 = 29
- //eventSlotObject[increment].slotEndMinute = ((30 * (currentSlot % 2)) + 29);
- //// zero seconds
- //eventSlotObject[increment].slotEndSecond = 0;
- //Debug.Print("Slot: " + increment + " end time " + eventSlotObject[increment].slotEndHour + ":" + eventSlotObject[increment].slotEndMinute);
- }
- }
- // if this slot is the current slot or after the current slot
- else
- {
- // if the current slot is AM and this slot is after the current slot
- // then all these dates and times are in today AM
- if (morningMarker)
- {
- Debug.Print("The current slot is AM and this slot is after it");
- // fix the 12 o'clock weirdness
- if (increment == 0 || increment == 1)
- {
- pmOffset = 0;
- dayOffset = 0;
- }
- // don't add 12 hours
- else
- {
- pmOffset = 0;
- dayOffset = 0;
- }
- // slot start time
- slotStartTime[increment] = new DateTime(
- DateTime.Now.Year,
- DateTime.Now.Month,
- DateTime.Now.Day + dayOffset,
- // here is the slot hour time for PM
- ((increment - (increment % 2)) / 2) + pmOffset,
- // here are the minutes example 9 = 30, 6 = 0
- (30 * (increment % 2)),
- // zero seconds
- 0);
- Debug.Print("Slot: " + increment + " start time " + slotStartTime[increment]);
- // slot end time
- slotEndTime[increment] = new DateTime(
- DateTime.Now.Year,
- DateTime.Now.Month,
- DateTime.Now.Day + dayOffset,
- // here is the slot hour time for PM
- ((increment - (increment % 2)) / 2) + pmOffset,
- // here are the minutes example 9 = 59, 6 = 29
- ((30 * (increment % 2)) + 29),
- // 29 seconds
- 29);
- Debug.Print("Slot: " + increment + " end time " + slotEndTime[increment]);
- }
- // if the current slot is PM and this slot is after the current slot
- // then all these dates and times are in today PM
- else
- {
- Debug.Print("The current slot is PM and this slot is after it");
- // fix the 12 o'clock weirdness
- if (increment == 0 || increment == 1)
- {
- pmOffset = 0;
- dayOffset = 0;
- }
- // add 12 hours for PM
- else
- {
- pmOffset = 12;
- dayOffset = 0;
- }
- // slot start time
- slotStartTime[increment] = new DateTime(
- DateTime.Now.Year,
- DateTime.Now.Month,
- DateTime.Now.Day + dayOffset,
- // here is the slot hour time for PM
- ((increment - (increment % 2)) / 2) + pmOffset,
- // here are the minutes example 9 = 30, 6 = 0
- (30 * (increment % 2)),
- // zero seconds
- 0);
- Debug.Print("Slot: " + increment + " start time " + slotStartTime[increment]);
- // slot end time
- slotEndTime[increment] = new DateTime(
- DateTime.Now.Year,
- DateTime.Now.Month,
- DateTime.Now.Day + dayOffset,
- // here is the slot hour time for PM
- ((increment - (increment % 2)) / 2) + pmOffset,
- // here are the minutes example 9 = 59, 6 = 29
- ((30 * (increment % 2)) + 29),
- // 29 seconds
- 29);
- Debug.Print("Slot: " + increment + " end time " + slotEndTime[increment]);
- }
- }
- }
- }
- void processEvents(HttpResponse response)
- {
- // run through each calendar event and ask if each eventSlot is involved
- // if the reponse contains some event items then update the eventObject data
- if (response.Text.IndexOf("items") < 0)
- {
- // if no items then do nothing
- Debug.Print("No event items!");
- }
- else
- {
- //Debug.Print("response " + response.Text);
- // trim the response to the event items
- string[] responseString = (response.Text.Substring(response.Text.IndexOf("items") - 1)).Split("\r\n".ToCharArray());
- bool eventMapping = false;
- bool startHunting = false;
- bool endHunting = false;
- for (int increment = 0; increment < responseString.Length; increment++)
- {
- //Debug.Print("increment: "+increment);
- // find "kind": "calendar#event", and trigger event mapping
- if (eventMapping == false && responseString[increment].IndexOf("calendar#event") > -1)
- {
- // new event!
- Debug.Print("New event!");
- eventMapping = true;
- startHunting = true;
- }
- // if event mapping
- // then look for the start and stop times
- // begin start hunting
- // find "start": {
- if (eventMapping == true && startHunting == true)
- {
- if (responseString[increment].IndexOf("start") > -1)
- {
- // found the event start
- // jump to next line
- Debug.Print("Start time: " + responseString[increment + 1].Substring(responseString[increment + 1].IndexOf("dateTime") + 12, responseString[increment + 1].Length - (responseString[increment + 1].IndexOf("dateTime") + 12) - 7));
- startHunting = false;
- endHunting = true;
- }
- }
- // find "end": {
- if (eventMapping == true && endHunting == true)
- {
- if (responseString[increment].IndexOf("end") > -1)
- {
- // found the event start
- // jump to next line
- Debug.Print("End time: " + responseString[increment + 1].Substring(responseString[increment + 1].IndexOf("dateTime") + 12, responseString[increment + 1].Length - (responseString[increment + 1].IndexOf("dateTime") + 12) - 7));
- eventMapping = false;
- endHunting = false;
- }
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment