예제 #1
0
        public void Setup(string filename)
        {
            _config   = new Dictionary <string, string>();
            _filename = filename;

            ReloadConfiguration();

            var watcher = new FileSystemWatcher {
                NotifyFilter = NotifyFilters.LastWrite, Filter = filename
            };

            var path = Path.GetDirectoryName(filename);

            if (string.IsNullOrEmpty(path))
            {
                path = AppDomain.CurrentDomain.BaseDirectory;
            }

            watcher.Path = path;

            watcher.Changed += (sender, args) =>
            {
                // prevent double notify (VS Code, etc.)
                if ((DateTime.Now - _lastUpdate).TotalSeconds < 0.8)
                {
                    return;
                }

                Thread.Sleep(5);
                ReloadConfiguration();
                ConfigurationUpdated?.Invoke();
            };

            watcher.EnableRaisingEvents = true;
        }
예제 #2
0
        private void UpdateConfiguration()
        {
            m_isDebugEnabled = m_core.IsDebugEnabled;
            m_isInfoEnabled  = m_core.IsInfoEnabled;
            m_isWarnEnabled  = m_core.IsWarnEnabled;
            m_isErrorEnabled = m_core.IsErrorEnabled;

            ConfigurationUpdated.Raise(this);
        }
예제 #3
0
 private void SetupReloadCallback()
 {
     ChangeToken.OnChange(_configuration.GetReloadToken, () =>
     {
         // Currently triggers twice, but should be OK for now.
         // More info: https://github.com/aspnet/Home/issues/2542
         LoadConfiguration();
         ConfigurationUpdated?.Invoke(this, new EventArgs());
     });
 }
예제 #4
0
 /// <summary>
 /// Raises the ConfigurationUpdated event; should be called when any of the logging enabled properties change.
 /// </summary>
 protected void RaiseConfigurationUpdated()
 {
     ConfigurationUpdated.Raise(this);
 }
예제 #5
0
 protected virtual void OnConfigurationChanged(TrafficSegmentConfiguration e)
 {
     ConfigurationUpdated?.Invoke(this, e);
 }
        private void HandleRequest(HttpListenerContext context)
        {
            try
            {
                log.Info("Connected to : " + context.Request.LocalEndPoint.ToString() + " : " + context.Request.Url.ToString());

                context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                context.Response.Headers.Add("Access-Control-Allow-Methods", "POST, GET, DELETE");

                var uri    = context.Request.Url;
                var method = context.Request.HttpMethod;

                switch (method)
                {
                case "GET":

                    using (var stream = context.Response.OutputStream)
                    {
                        var segments = uri.Segments;
                        if (segments.Length > 0)
                        {
                            string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Configuration.FILENAME);
                            var    config     = Configuration.Read(configPath);
                            if (config != null)
                            {
                                // Check if Find Devices is requested
                                if (segments[segments.Length - 1].ToLower().Trim('/') == "devices")
                                {
                                    if (!config.DeviceFinders.IsNullOrEmpty())
                                    {
                                        var devices = new List <MTConnect.MTConnectConnection>();

                                        foreach (var deviceFinder in config.DeviceFinders)
                                        {
                                            deviceFinder.DeviceFound += (o, i) =>
                                            {
                                                var deviceId = Server.GenerateDeviceId(i);
                                                devices.Add(new MTConnect.MTConnectConnection(deviceId, i.IpAddress.ToString(), i.Port, i.MacAddress.ToString(), i.DeviceName));
                                            };
                                            deviceFinder.Start(false);
                                        }

                                        if (!devices.IsNullOrEmpty())
                                        {
                                            var json = Json.Convert.ToJson(devices);
                                            if (!string.IsNullOrEmpty(json))
                                            {
                                                var bytes = Encoding.UTF8.GetBytes(json);
                                                stream.Write(bytes, 0, bytes.Length);

                                                context.Response.StatusCode = 200;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    var json = Json.Convert.ToJson(config);
                                    if (!string.IsNullOrEmpty(json))
                                    {
                                        var bytes = Encoding.UTF8.GetBytes(json);
                                        stream.Write(bytes, 0, bytes.Length);

                                        context.Response.StatusCode = 200;
                                    }
                                    else
                                    {
                                        context.Response.StatusCode = 500;
                                    }
                                }
                            }
                            else
                            {
                                context.Response.StatusCode = 404;
                            }
                        }

                        log.Info("Rest Response : " + context.Response.StatusCode);
                    }

                    break;

                case "POST":

                    using (var stream = context.Request.InputStream)
                        using (var streamReader = new StreamReader(stream))
                        {
                            var json = streamReader.ReadToEnd();
                            if (!string.IsNullOrEmpty(json))
                            {
                                json = HttpUtility.UrlDecode(json);

                                var config = Json.Convert.FromJson <Configuration>(json);
                                if (config != null)
                                {
                                    context.Response.StatusCode = 200;
                                    ConfigurationUpdated?.Invoke(config);
                                }
                                else
                                {
                                    context.Response.StatusCode = 400;
                                }
                            }
                            else
                            {
                                context.Response.StatusCode = 400;
                            }

                            log.Info("Rest Response : " + context.Response.StatusCode);
                        }

                    break;
                }

                context.Response.Close();
            }
            catch (Exception ex)
            {
                log.Debug(ex);
            }
        }
예제 #7
0
 /// <summary>
 ///     Initializes configuration manager
 /// </summary>
 /// <param name="name">The name</param>
 public AppConfiguration(string name)
 {
     _manager = new T();
     _manager.Setup(name);
     _manager.ConfigurationUpdated += () => ConfigurationUpdated?.Invoke();
 }