Emit() public static method

Emit a message to all listeners
public static Emit ( Channel channel, Newtonsoft.Json.Linq.JObject data, bool global = false ) : void
channel Channel The channel to emit on
data Newtonsoft.Json.Linq.JObject The data to send
global bool Should the data be sent to other processes
return void
コード例 #1
0
ファイル: Notification.cs プロジェクト: ipaq3870/zazzles
        public static void Emit(JObject data, bool onGoing = false, bool global = true)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            Bus.Emit(Bus.Channel.Notification, data, global);
        }
コード例 #2
0
ファイル: Power.cs プロジェクト: ipaq3870/zazzles
        public static void QueueShutdown(string parameters, ShutdownOptions options = ShutdownOptions.Abort,
                                         string message = null, int gracePeriod = -1)
        {
            // If no user is logged in, skip trying to notify users
            if (!User.AnyLoggedIn())
            {
                CreateTask(parameters, message);
                return;
            }

            // Check if a task is already in progress
            if (_timer != null && _timer.Enabled)
            {
                Log.Entry(LogName, "Power task already in-progress");
                return;
            }

            Requested           = true;
            _delayed            = false;
            AggregatedDelayTime = 0;

            // Load the grace period from Settings or use the default one
            try
            {
                if (gracePeriod == -1)
                {
                    gracePeriod = (!string.IsNullOrEmpty(Settings.Get("PromptTime")))
                        ? int.Parse(Settings.Get("PromptTime"))
                        : DefaultGracePeriod * 60;
                }
            }
            catch (Exception)
            {
                gracePeriod = DefaultGracePeriod * 60;
            }

            // Generate the request data
            Log.Entry(LogName, $"Creating shutdown command in {gracePeriod} seconds");

            _requestData                     = new JObject();
            _requestData.action              = "request";
            _requestData.period              = gracePeriod;
            _requestData.options             = options;
            _requestData.command             = parameters;
            _requestData.aggregatedDelayTime = AggregatedDelayTime;
            _requestData.message             = message ?? string.Empty;

            Bus.Emit(Bus.Channel.Power, _requestData, true);
            _timer          = new Timer(gracePeriod * 1000);
            _timer.Elapsed += TimerElapsed;
            _timer.Start();
        }
コード例 #3
0
ファイル: Power.cs プロジェクト: ipaq3870/zazzles
        /// <summary>
        ///     Abort a shutdown if it is not to late
        /// </summary>
        public static void AbortShutdown()
        {
            Log.Entry(LogName, "Aborting shutdown");
            ShuttingDown        = false;
            Requested           = false;
            AggregatedDelayTime = 0;

            if (_timer == null)
            {
                return;
            }

            _timer.Stop();
            _timer.Close();
            _timer = null;

            dynamic abortJson = new JObject();

            abortJson.action = "abort";
            _shouldAbortFunc = null;
            Bus.Emit(Bus.Channel.Power, abortJson, true);
        }
コード例 #4
0
ファイル: Power.cs プロジェクト: ipaq3870/zazzles
        /// <summary>
        ///     Create a shutdown command
        /// </summary>
        /// <param name="parameters">The parameters to use</param>
        public static void CreateTask(string parameters, string message = "")
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            _shouldAbortFunc = null;
            _requestData     = new JObject();

            Log.Entry(LogName, "Creating shutdown request");
            Log.Entry(LogName, "Parameters: " + parameters);

            dynamic json = new JObject();

            json.action = "shuttingdown";
            Bus.Emit(Bus.Channel.Power, json, true);

            Instance.CreateTask(parameters, message);

            Requested    = false;
            ShuttingDown = true;
        }
コード例 #5
0
ファイル: Log.cs プロジェクト: bmttek/zazzles
        /// <summary>
        ///     Write text to the log
        /// </summary>
        /// <param name="level">The logging level</param>
        /// <param name="text">The text to write</param>
        /// <param name="color">The color of the text if in Console mode, White will use the default color</param>
        public static void Write(Level level, string text, ConsoleColor color = ConsoleColor.White)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (level != Level.Debug)
            {
                Bus.Emit(Bus.Channel.Log, MessageToJSON(text));
            }

            switch (Output)
            {
            case Mode.Quiet:
                break;

            case Mode.Console:
                Console.ResetColor();

                if (color != ConsoleColor.White)
                {
                    Console.ForegroundColor = color;
                }

                if (level == Level.Error)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                }
                if (level == Level.Debug)
                {
                    Console.BackgroundColor = ConsoleColor.Blue;
                }

                Console.Write(text);

                break;

            default:
                try
                {
                    lock (locker)
                    {
                        var logDir = Path.GetDirectoryName(FilePath);

                        if (logDir != null && !Directory.Exists(logDir))
                        {
                            Directory.CreateDirectory(logDir);
                        }

                        var logFile = new FileInfo(FilePath);

                        //Delete the log file if it excedes the max log size
                        if (logFile.Exists && logFile.Length > MaxSize)
                        {
                            CleanLog(logFile);
                        }

                        //Write message to log file
                        var logWriter = new StreamWriter(FilePath, true);
                        logWriter.Write(text);
                        logWriter.Close();
                    }
                }
                catch
                {
                    //If logging fails then nothing can really be done to silently notify the user
                }
                break;
            }
        }