コード例 #1
0
ファイル: WifiMessageSender.cs プロジェクト: pwalke/Pourcast
 private WiFlyGSX GetModule()
 {
     using (var initWatchdog = new Watchdog(new TimeSpan(0, 5, 0), () =>
     {
         Debug.Print("Spent 5 minutes trying to initialize, giving up....");
         PowerState.RebootDevice(false, 1000);
     }))
     {
         initWatchdog.Start();
         while (null == _module)
         {
             _module = SetupModule();
         }
         return(_module);
     }
 }
コード例 #2
0
        /// <summary>
        /// Main method runs on startup
        /// </summary>
        public static void RealMain()
        {
            Debug.EnableGCMessages(true);
            // START CONFIG
            var sf800pulseConfig = new PulseConfig()
            {
                PulsesPerOunce            = PulseConfig.SF800_PULSES_PER_OUNCE,
                PourStoppedDelay          = 250,
                PulsesPerStoppedExtension = 50,
                PulsesPerPouring          = 250,
            };
            var config = new Config
            {
                Connectivity = new ConnectivityConfig
                {
                    //Wifi = new WifiConfig
                    //{
                    //    Enabled = true,
                    //    SSID = "Rightpoint",
                    //    Password = "******",
                    //    SecurityMode = Toolbox.NETMF.Hardware.WiFlyGSX.AuthMode.MixedWPA1_WPA2
                    //},
                    Ethernet = new EthernetConfig
                    {
                        Enabled = true
                    },
                    BaseUrl   = "http://pourcast.labs.rightpoint.com/api/Tap/",
                    HttpLight = Pins.GPIO_PIN_D9
                },
                Taps = new[]
                {
                    new TapConfig
                    {
                        Input       = Pins.GPIO_PIN_D13,
                        Light       = Pins.GPIO_PIN_D11,
                        TapId       = "535c61a951aa0405287989ec",
                        PulseConfig = sf800pulseConfig,
                    },
                    new TapConfig
                    {
                        Input       = Pins.GPIO_PIN_D12,
                        Light       = Pins.GPIO_PIN_D10,
                        TapId       = "537d28db51aa04289027cde5",
                        PulseConfig = sf800pulseConfig,
                    },
                },
                WatchdogCheckInterval = new TimeSpan(0, 15, 0)
            };

#if true
            // a configuration for testing with a local server, buttons, and even the emulator
            var localButtonConfig = new PulseConfig()
            {
                PulsesPerOunce            = 0.1,
                PourStoppedDelay          = 1000,
                PulsesPerStoppedExtension = 1,
                PulsesPerPouring          = 5,
            };
            config = new Config
            {
                Connectivity = new ConnectivityConfig
                {
                    Wifi = new WifiConfig()
                    {
                        Enabled      = true,
                        SSID         = "XXX",
                        Password     = "******",
                        SecurityMode = Toolbox.NETMF.Hardware.WiFlyGSX.AuthMode.MixedWPA1_WPA2,
                    },
                    BaseUrl = "http://192.168.25.107:23456/api/Tap/",
#if true
                    HttpLight = Pins.GPIO_PIN_D9,
                },
                Taps = new[]
                {
                    new TapConfig
                    {
                        Input       = Pins.GPIO_PIN_D13,
                        Light       = Pins.GPIO_PIN_D11,
                        TapId       = "5396779faa6179467050c33a",
                        PulseConfig = localButtonConfig,
                    },
                    new TapConfig
                    {
                        Input       = Pins.GPIO_PIN_D12,
                        Light       = Pins.GPIO_PIN_D10,
                        TapId       = "539677a4aa6179467050c33d",
                        PulseConfig = localButtonConfig,
                    },
#else
                    HttpLight = Cpu.Pin.GPIO_Pin6,
                },
                Taps = new[]
                {
                    new TapConfig
                    {
                        Input       = Cpu.Pin.GPIO_Pin2, // emulator up
                        Light       = Cpu.Pin.GPIO_Pin1,
                        TapId       = "539638b03885a838541b880c",
                        PulseConfig = localButtonConfig,
                    },
                    new TapConfig
                    {
                        Input       = Cpu.Pin.GPIO_Pin4, // emulator down
                        Light       = Cpu.Pin.GPIO_Pin3,
                        TapId       = "539638bd3885a838541b8810",
                        PulseConfig = localButtonConfig,
                    },
#endif
                },
                WatchdogCheckInterval = new TimeSpan(0, 15, 0)
            };
#endif
            // END CONFIG

            // turn on the tap lights on - they'll turn off as they initialize
            LightsOn(config.Taps);
            // turn the HTTP light *off*, it'll go on as it initializes
            LightOff(config.Connectivity.HttpLight);

            var sender = BuildMessageSender(config.Connectivity);
            sender.Initalize();

            HttpMessageWriter writer = null;
            // first watchdog makes sure we send *something* every watchdogCheckInterval.  Second reboots us 30s later if the message hasn't been sent yet (ie. if networking dies)
            // sending *any* message resets both watchdogs
            var heartbeatWatchdog = new Watchdog(config.WatchdogCheckInterval, () => writer.SendHeartbeatAsync());
            var rebootWatchdog    = new RebootWatchdog(config.WatchdogCheckInterval + new TimeSpan(0, 0, 30));

            writer = new HttpMessageWriter(sender, config.Connectivity.BaseUrl, new OutputPort(config.Connectivity.HttpLight, true), new[] { heartbeatWatchdog, rebootWatchdog });
            var sensors = new FlowSensor[config.Taps.Length];
            for (var i = 0; i < config.Taps.Length; i++)
            {
                var tapConfig = config.Taps[i];
                sensors[i] = new FlowSensor(
                    new InterruptPort(tapConfig.Input, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh),
                    new OutputPort(tapConfig.Light, false),
                    writer,
                    tapConfig.TapId,
                    tapConfig.PulseConfig);
            }

            heartbeatWatchdog.Start();
            rebootWatchdog.Start();
            Debug.Print("Starting");

            while (true)
            {
                Thread.Sleep(Timeout.Infinite);
            }
        }
コード例 #3
0
ファイル: WifiMessageSender.cs プロジェクト: pwalke/Pourcast
        private WiFlyGSX SetupModule()
        {
            WiFlyGSX workingModule = null;

            var thread = new Thread(() =>
            {
                var module = new WiFlyGSX(DebugMode: true);
                try
                {
                    module.EnableDHCP();

                    var isConnected = false;

                    for (var i = 0; i < 3 && !(isConnected = module.JoinNetwork(_ssid, 0, _securityMode, _password)); i++)
                    {
                        Thread.Sleep(1000);
                    }

                    Debug.Print("isConnected: " + isConnected);

                    if (!isConnected)
                    {
                        module.Reboot();
                        module.Dispose();
                        return;
                    }

                    for (var i = 0; i < 10 && module.LocalIP == "0.0.0.0"; i++)
                    {
                        Thread.Sleep(1000);
                    }

                    Debug.Print("Local IP: " + module.LocalIP);
                    Debug.Print("MAC address: " + module.MacAddress);

                    if (module.LocalIP == "0.0.0.0")
                    {
                        module.Reboot();
                        module.Dispose();
                        return;
                    }
                }
                catch (ThreadAbortException)
                {
                    module.Dispose();
                    Debug.Print("Our watchdog got fired - sleep a bit and try rebooting the module before we return");
                    Thread.Sleep(2000);
                    using (module = new WiFlyGSX(DebugMode: true))
                    {
                        module.Reboot();
                    }
                    throw;
                }

                workingModule = module;
            });

            thread.Start();

            var fired = false;

            using (var setupWatchdog = new Watchdog(new TimeSpan(0, 0, 30), () =>
            {
                if (!fired)
                {
                    Debug.Print("Triggering setup watchdog");
                    thread.Abort();
                    fired = true;
                }
            }))
            {
                setupWatchdog.Start();
                thread.Join();
            }

            return(workingModule);
        }
コード例 #4
0
ファイル: WifiMessageSender.cs プロジェクト: pwalke/Pourcast
        public void FetchURL(Uri url)
        {
            bool success = false;

            while (!success)
            {
                var module = GetModule();

                var thread = new Thread(() =>
                {
                    var httpHost = url.Host;
                    var host     = httpHost;
                    var port     = (ushort)url.Port;

                    if (host == "pourcast.labs.rightpoint.com")
                    {
                        host = "192.168.100.114";
                    }

                    var request         = "GET " + url.AbsolutePath + " HTTP/1.1\r\nHost: " + httpHost + "\r\nConnection: Close\r\n\r\n";
                    SimpleSocket socket = new WiFlySocket(host, port, module);

                    try
                    {
                        // Connects to the socket
                        socket.Connect();
                        // Does a plain HTTP request
                        socket.Send(request);

                        // Prints all received data to the debug window, until the connection is terminated
                        while (socket.IsConnected)
                        {
                            var line = socket.Receive().Trim();
                            if (line != "" && line != null)
                            {
                                //Debug.Print(line);
                            }
                        }
                        success = true;
                    }
                    finally
                    {
                        socket.Close();
                    }
                });
                thread.Start();

                using (var fetchWatchdog = new Watchdog(new TimeSpan(0, 0, 5), () =>
                {
                    Debug.Print("Triggering fetch watchdog");
                    thread.Abort();
                    Thread.Sleep(500);
                    module.Reboot();
                    module.Dispose();
                    _module = null;
                }))
                {
                    thread.Join();
                }
            }
        }