/// <summary>
        /// Gets the broker URL from Helen's WebAPI server
        /// </summary>
        /// <param name="defaultValue">The value to return if Helen's API can't be reached.</param>
        /// <returns></returns>
        /// <exception cref="Exception">
        /// Connecting to Helen's server reached MAX_RETRIES
        /// or
        /// Helen's server didn't return a URL
        /// </exception>
        public string GetBrokerUrl(string defaultValue)
        {
            try
            {
                HttpResponseMessage response;
                int numTries = 0;
                do
                {
                    var route = "DeviceData/GetConnectionInfo/";
                    response = client.GetAsync(route).Result;
                    if (numTries++ > MAX_RETRIES)
                    {
                        throw new Exception("Connecting to Helen's server reached MAX_RETRIES");
                    }
                } while (!response.IsSuccessStatusCode);

                var msg = response.Content.ReadAsStringAsync().Result;
                if (string.IsNullOrEmpty(msg))
                {
                    throw new Exception("Helen's server didn't return a URL");
                }
                else
                {
                    return(msg.Trim().Trim('\"'));
                }
            }
            catch (Exception)
            {
                ConsoleExtensions.WriteLine("Failed to connect to Helen's API, falling back to the value in settings.");
                return(defaultValue);
            }
        }
        /// <summary>
        /// Gets the API identifier from Helen's WebAPI server
        /// </summary>
        /// <param name="name">The name to register</param>
        /// <param name="defaultValue">The value to return if Helen's API can't be reached</param>
        /// <returns></returns>
        /// <exception cref="Exception">Connecting to Helen's server reached MAX_RETRIES</exception>
        public int GetApiId(string name, int defaultValue)
        {
            try
            {
                HttpResponseMessage response;
                int numTries = 0;
                do
                {
                    var route = "DeviceList/RegisterDevice/";
                    route   += HttpUtility.UrlEncode(name);
                    response = client.PostAsJsonAsync(route, new JValue("")).Result;
                    if (numTries++ > MAX_RETRIES)
                    {
                        throw new Exception("Connecting to Helen's server reached MAX_RETRIES");
                    }
                } while (!response.IsSuccessStatusCode);

                var msg = response.Content.ReadAsStringAsync().Result;
                int id  = int.Parse(msg);
                return(id);
            }
            catch (Exception)
            {
                ConsoleExtensions.WriteLine("Failed to connect to Helen's API, falling back to the value in settings.");
                return(defaultValue);
            }
        }
Esempio n. 3
0
 public void Disconnect()
 {
     if (IsConnected)
     {
         Client.Disconnect();
         OnConnectionStatusChanged();
         ConsoleExtensions.WriteLine("Successfully disconnected from the broker.");
     }
 }
Esempio n. 4
0
        public void SaveSettings(string filePath = null)
        {
            if (filePath == null)
            {
                filePath = FilePath;
            }

            try
            {
                File.WriteAllText(filePath, Json);
            }
            catch (Exception ex)
            {
                ConsoleExtensions.WriteLine($"Could not write file: {ex.Message}");
            }
        }
Esempio n. 5
0
        public void ReadFromFile(string filePath = null)
        {
            if (filePath == null)
            {
                filePath = FilePath;
            }

            try
            {
                Json = File.ReadAllText(filePath);
            }
            catch (Exception ex)
            {
                ConsoleExtensions.WriteLine($"Could not populate settings from {filePath}: {ex.Message}");
            }
        }
        public void Publish(string content)
        {
            byte[] newContent = Encoding.UTF8.GetBytes(content);

            if (Client.IsConnected)
            {
                var bytes = Encoding.UTF8.GetBytes(content);
                try
                {
                    Client.Publish(Topic, bytes, (byte)QosLevel, Retain);
                }
                catch (Exception ex)
                {
                    ConsoleExtensions.WriteLine("Failed to publish data: " + ex.Message);
                }
            }
        }
Esempio n. 7
0
        private static void PrintOptions(AppSettings settings, bool showLinuxProbeOptions = false)
        {
            ConsoleExtensions.WriteLine("Main commands: ");
            ConsoleExtensions.WriteLine("  Msg: <blah> - sends a message to the broker (if connected)");
            ConsoleExtensions.WriteLine("  Start - starts the probe if it isn't already started");
            ConsoleExtensions.WriteLine("  Stop - stops the probe if it is running");
            ConsoleExtensions.WriteLine("  Reset - resets all options to defaults (option must be confirmed)");
            if (showLinuxProbeOptions)
            {
                ConsoleExtensions.WriteLine("  list probes - lists all available 1 wire probes");
            }
            ConsoleExtensions.WriteLine("  Clear - Clears the console screen");
            ConsoleExtensions.WriteLine("  Help - Displays this menu");
            ConsoleExtensions.WriteLine("  Exit - Exits the programe");

            ConsoleExtensions.WriteLine("\n\nOptions: ");
            ConsoleExtensions.WriteLine(settings.Json);
        }
Esempio n. 8
0
        private static bool HandleInput(AppSettings settings, SensorManager manager)
        {
            string input = Console.ReadLine();

            if (input.Equals("exit", StringComparison.InvariantCultureIgnoreCase))
            {
                return(false);
            }
            else if (input.Equals("help", StringComparison.InvariantCultureIgnoreCase))
            {
                PrintOptions(settings);
            }
            else if (input.Equals("clear", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.Clear();
            }
            else if (input.Equals("reset", StringComparison.InvariantCultureIgnoreCase))
            {
                ConsoleExtensions.WriteLine("Please confirm this action by typing 'yes please'");
                if (Console.ReadLine() == "yes please")
                {
                    settings.ResetToDefaults();
                    manager.UpdateFromSettings(settings);
                    settings.SaveSettings();
                }
            }
            else if (input.StartsWith("msg: "))
            {
                var msg = input.Substring(4).Trim();
                manager.ProbePublisher.Publish(msg);
            }
            else if (input.Equals("stop", StringComparison.InvariantCultureIgnoreCase))
            {
                manager.Probe.Stop();
                ConsoleExtensions.WriteDebugLocation("       ", 0);
            }
            else if (input.Equals("start", StringComparison.InvariantCultureIgnoreCase))
            {
                manager.Probe.Start();
            }
            else if (input.Equals("list probes", StringComparison.InvariantCultureIgnoreCase) && manager.Probe is Probes.LinuxTempProbe)
            {
                var lprobe = manager.Probe as Probes.LinuxTempProbe;
                foreach (var name in lprobe.GetOneWireDeviceNames())
                {
                    ConsoleExtensions.WriteLine("  " + name);
                }
            }
            else
            {
                if (ReadProperty(settings, input))
                {
                    manager.UpdateFromSettings(settings);
                    settings.SaveSettings();
                }
                else
                {
                    ConsoleExtensions.WriteLine("Unrecognized command");
                }
            }
            return(true);
        }