Example #1
0
        internal List <string> SendWifiCredentials(string uniqueDeviceId)
        {
            Device device = currDeviceList.GetDevice(uniqueDeviceId);

            if (device == null)
            {
                return new List <string>()
                       {
                           "Could not find the device in my current list"
                       }
            }
            ;

            //get the username and password for the camera
            var driverParams = platform.GetDeviceDriverParams(device);

            if (driverParams.Count != 3)
            {
                return new List <string>()
                       {
                           "DriverParams are in unknown state. Param count " + driverParams.Count.ToString()
                       }
            }
            ;

            string username = driverParams[1];
            string password = driverParams[2];

            //get the wifi credentials
            string wifiSsid = platform.GetPrivateConfSetting("WifiSsid");
            string wifiKey  = platform.GetPrivateConfSetting("WifiKey");

            if (string.IsNullOrWhiteSpace(wifiSsid))
            {
                return new List <string>()
                       {
                           "WifiSsid is not configured in the hub"
                       }
            }
            ;


            //now get scan results
            int maxScansLeft = 3;
            List <FoscamScannedNetwork> scanResults = null;

            while (maxScansLeft >= 1)
            {
                try
                {
                    scanResults = GetScanResult(device.DeviceIpAddress, username, password);
                }
                catch (Exception)
                {
                    return(new List <string>()
                    {
                        "Error in getting scan results from the camera"
                    });
                }

                if (scanResults.Count == 0)
                {
                    maxScansLeft--;
                }
                else
                {
                    break;
                }
            }

            if (scanResults == null || scanResults.Count == 0)
            {
                logger.Log("Skipping {0} as Wifi scan did not find any nework", device.UniqueName);

                return(new List <string>()
                {
                    "Wifi scan by camera did not find any network"
                });
            }

            //check if we the network we know was seen
            FoscamScannedNetwork targetNetwork = null;

            foreach (var network in scanResults)
            {
                if (network.ssid.Equals(wifiSsid))
                {
                    targetNetwork = network;
                    break;
                }
            }

            if (targetNetwork == null)
            {
                logger.Log("Skipping {0} as Wifi scan did not find our ssid {1}", device.UniqueName, wifiSsid);

                return(new List <string>()
                {
                    "Wifi scan by camera did not find our ssid " + wifiSsid
                });
            }

            // now configure the thing!
            bool result = false;

            try
            {
                result = SendWifiCredentials(targetNetwork, wifiKey, device.DeviceIpAddress, username, password);
            }
            catch (Exception)
            {
                return(new List <string>()
                {
                    "Error in configuring wifi for the camera"
                });
            }
            if (result)
            {
                logger.Log("Foscam configuration (seems to have) succeeded!");

                //we do not reboot, because we expect the user to power cycle the camera
                //in the process of moving it someplace else

                //Reboot(cameraIp, username, password);
                //System.Threading.Thread.Sleep(30 * 1000);

                return(new List <string>()
                {
                    ""
                });
            }
            else
            {
                logger.Log("Configuration failed!");

                return(new List <string>()
                {
                    "configuration failed due to unknown reason"
                });
            }
        }
    }
}
Example #2
0
        //NB: this function sends the wifi credentials to the device
        void ScanUsb()
        {
            //logger.Log("GadgeteerScout:ScanUSB\n");

            string[] portNames = SerialPort.GetPortNames();

            //we are putting this lock here just in case two of these are running at the same time (e.g., if the ScanNow timer was really short)
            //only one of these calls can be active because of socket conflicts
            lock (this)
            {
                foreach (string portName in portNames)
                {
                    try
                    {
                        int portNumber = int.Parse(portName.Substring(3));

                        //skip low nubmered ports; they cannot be our device
                        //we can't skip Com 1..16 because Win8 assigns low-numbered ports starting from COM3
                        if (portNumber < 3)
                        {
                            continue;
                        }

                        SerialPort port;

                        if (!serialPorts.ContainsKey(portName))
                        {
                            port             = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);
                            port.Handshake   = Handshake.None;
                            port.ReadTimeout = 500;  //enough time for the device to respond?

                            serialPorts.Add(portName, port);
                        }
                        else
                        {
                            port = serialPorts[portName];
                        }

                        int maxAttemptsToOpen = 1;

                        while (!port.IsOpen && maxAttemptsToOpen > 0)
                        {
                            //Thread.Sleep(100);
                            try
                            {
                                port.Open();
                                break;
                            }
                            catch (Exception e)
                            {
                                maxAttemptsToOpen--;

                                logger.Log("Got {0} exception while opening {1}. Num attempts left = {2}",
                                           e.Message, portName, maxAttemptsToOpen.ToString());

                                //sleep if we'll atttempt again
                                if (maxAttemptsToOpen > 0)
                                {
                                    System.Threading.Thread.Sleep(1 * 1000);
                                }
                            }
                        }

                        port.WriteLine("");
                        string serialString = port.ReadLine();

                        //check if this the device we want
                        if (serialString.Contains("HomeOSGadgeteerDevice"))
                        {
                            //we do not insert this device into our list
                            //if we manage to send it our wifi credentials; we will discover it over WiFi and then add

                            //Device device = CreateDeviceUsb(serialString);
                            //InsertDevice(device);

                            //send wifi credentials to the device
                            string wifiSsid = platform.GetPrivateConfSetting("WifiSsid");
                            string wifiKey  = platform.GetPrivateConfSetting("WifiKey");

                            if (wifiSsid == null)
                            {
                                logger.Log("Wifi is not configured for the hub. Cannot configure usb gadget");
                            }
                            else
                            {
                                string stringToWrite = String.Format("ssid {0} key {1}", wifiSsid, wifiKey);

                                port.WriteLine(stringToWrite);

                                logger.Log("Written to {0}. \n Got: {1} \n Wrote: {2}", portName, serialString, stringToWrite);
                            }
                        }

                        port.Close();
                    }
                    catch (TimeoutException)
                    {
                        logger.Log("Timed out while trying to read " + portName);
                    }
                    catch (Exception /*e*/)
                    {
                        // let us not print this
                        // logger.Log("Serial port exception for {0}: {1}", portName, e.ToString());
                    }
                }
            }
        }