Exemplo n.º 1
0
 private void InternalClockInitialize()
 {
     _clockInitThread = new Thread(specific =>
     {
         CloudLog.Debug("InternalClockInitialize(), Waiting for seconds to be 0", GetType().Name);
         while (DateTime.Now.Second != 0)
         {
             Thread.Sleep(50);
             CrestronEnvironment.AllowOtherAppsToRun();
         }
         CloudLog.Notice("InternalClockInitialize(), Seconds should now be zero, time is now {1}, creating CTimer to track time",
                         GetType().Name,
                         DateTime.Now.ToString("T"));
         _clockTimer = new CTimer(s => OnTimeChange(), null, 60000, 60000);
         OnTimeChange();
         CloudLog.Info("InternalClockInitialize(), OnTimeChange() will be called every time time is 0 seconds", GetType().Name);
         return(null);
     }, null, Thread.eThreadStartOptions.CreateSuspended)
     {
         Name     = "Clock Init Thread",
         Priority = Thread.eThreadPriority.HighPriority
     };
     _clockInitThread.Start();
 }
Exemplo n.º 2
0
        private object SystemThreadProcess(object userSpecific)
        {
            try
            {
                while (true)
                {
                    if (_userPrompts.Count == 0)
                    {
                        _systemWait.Wait(2000);
                    }

                    if (_programStopping)
                    {
                        return(null);
                    }

                    if (_userPrompts.Count == 0)
                    {
                        goto CheckTimers;
                    }

                    try
                    {
                        var prompts = _userPrompts.ToArray();

                        // We have queued prompts that aren't showing as current system prompts
                        if (prompts.Any(p => p.State == PromptState.Queued) &&
                            !prompts.Any(p => p.State == PromptState.Shown && p.Room == null))
                        {
                            //Debug.WriteInfo("Prompts queued! System {0}, Room {1}",
                            //    _userPrompts.Count(p => p.State == PromptState.Queued && p.Room == null),
                            //    _userPrompts.Count(p => p.State == PromptState.Queued && p.Room != null));

                            // Look for any system prompts (not specific to a room)
                            var prompt = prompts.FirstOrDefault(p => p.State == PromptState.Queued && p.Room == null);

                            if (prompt != null)
                            {
                                var roomPrompts = prompts.Where(p => p.State == PromptState.Shown && p.Room != null);

                                // Close any active room prompts and reset to queue as system will override

                                var pArray = roomPrompts as UserPrompt[] ?? roomPrompts.ToArray();
                                foreach (var roomPrompt in pArray)
                                {
                                    roomPrompt.State = PromptState.Queued;
                                }

                                if (pArray.Any())
                                {
                                    Thread.Sleep(200);
                                }

                                prompt.State = PromptState.Shown;

                                //Debug.WriteInfo("Showing system prompt on all UIs!");

                                foreach (var uiController in UIControllers)
                                {
                                    uiController.ShowPrompt(prompt);
                                }
                            }

                            else
                            {
                                // No system prompts to show so we can show any room prompts

                                foreach (var room in Rooms)
                                {
                                    var room1 = room;

                                    // Already showing a prompt in this room ... skip the room
                                    if (_userPrompts.Any(p => p.State == PromptState.Shown && p.Room == room1))
                                    {
                                        continue;
                                    }

                                    prompt = prompts.FirstOrDefault(
                                        p => p.State == PromptState.Queued && p.Room == room1);

                                    if (prompt != null && prompt.State != PromptState.Cancelled)
                                    {
                                        prompt.State = PromptState.Shown;

                                        foreach (var uiController in UIControllers.ForRoom(room1))
                                        {
                                            uiController.ShowPrompt(prompt);
                                        }
                                    }
                                }
                            }
                        }

                        //if (prompts.Length > 0)
                        //    Debug.WriteInfo("Removing {0} expired prompts", prompts.Length);

                        foreach (var userPrompt in
                                 prompts.Where(p => p.State != PromptState.Queued && p.State != PromptState.Shown))
                        {
                            _userPromptsLock.Enter();
                            _userPrompts.Remove(userPrompt);
                            _userPromptsLock.Leave();
                        }
                    }
                    catch (Exception e)
                    {
                        CloudLog.Exception(e);
                        Thread.Sleep(1000);
                    }

CheckTimers:

                    if (!Booted)
                    {
                        continue;
                    }

                    if (SecondsSinceLastSystemTimerCallback > 10 && !_timersBrokenFlag)
                    {
                        _timersBrokenFlag = true;
                        CloudLog.Error(
                            "System Timer last checked in {0} seconds ago. Will reboot processor in 10 minutes if not fixed!!",
                            SecondsSinceLastSystemTimerCallback);
                    }
                    else if (SecondsSinceLastSystemTimerCallback > 600 && _timersBrokenFlag)
                    {
                        CloudLog.Error("Automatic reboot now!");
                        var response = string.Empty;
                        CrestronConsole.SendControlSystemCommand("REBOOT", ref response);
                    }
                    else if (_timersBrokenFlag && SecondsSinceLastSystemTimerCallback < 10)
                    {
                        _timersBrokenFlag = false;
                        CloudLog.Warn("System Timer checked in {0} seconds ago after a delay", SecondsSinceLastSystemTimerCallback);
                    }

                    CrestronEnvironment.AllowOtherAppsToRun();
                }
            }
            catch (Exception e)
            {
                CloudLog.Exception(e, "Unhandled Exception in SystemThreadProcess()!");
                return(null);
            }
        }
Exemplo n.º 3
0
        public static object UpdateFromPushedUpdateProcess(object o)
        {
            var random       = new Random();
            var attemptCount = 0;

            if (_status == UpdateStatus.Failed)
            {
                CloudLog.Notice("Previous update had failed, clearing status!");
                _status = UpdateStatus.NotRunning;
                Thread.Sleep(1000);
            }

            while (_status == UpdateStatus.NotRunning && !_programStopping)
            {
                try
                {
                    attemptCount++;

                    if (attemptCount == 1)
                    {
                        var ms = random.Next((int)TimeSpan.FromSeconds(30).TotalMilliseconds,
                                             (int)TimeSpan.FromMinutes(20).TotalMilliseconds);

                        Debug.WriteSuccess("Update push process running", "waiing for {0}",
                                           TimeSpan.FromMilliseconds(ms).ToString());

                        CrestronEnvironment.AllowOtherAppsToRun();
                        Thread.Sleep(ms);
                    }
                    else
                    {
                        Thread.Sleep(60000);
                    }

                    if (attemptCount > 5)
                    {
                        CloudLog.Error("Software update push failed after 5 attempts. Exiting process");
                        _status = UpdateStatus.Failed;
                        return(null);
                    }

                    if (_status != UpdateStatus.NotRunning)
                    {
                        return(null);
                    }

                    Debug.WriteInfo("Getting current list of updates...");

                    try
                    {
                        GetAppUpdates();
                    }
                    catch (Exception e)
                    {
                        CloudLog.Exception(e);
                        _httpClient.Dispose();
                        _httpClient = new HttpClient();
                    }

                    var info = _updates.FirstOrDefault(f => new Version(f.Version).CompareTo(_pushedVersion) == 0);

                    if (info != null)
                    {
                        Debug.WriteInfo("Found update available at URL", info.Url);
                        CloudLog.Info("Software update push can be downloaded from: {0}", info.Url);
                        var downloadResult = Download(info.Url);
                        if (downloadResult == 0)
                        {
                            try
                            {
                                OnUpdateShouldLoad();
                            }
                            catch (Exception e)
                            {
                                CloudLog.Exception(e);
                                continue;
                            }
                            return(null);
                        }

                        _status = UpdateStatus.NotRunning;

                        CloudLog.Warn("Attempt {0} to download update failed, Error {1}", attemptCount, downloadResult);
                        continue;
                    }

                    CloudLog.Error("Pushed update failed, could not find an update matching version {0}", _pushedVersion);
                    _status = UpdateStatus.Failed;
                    return(null);
                }
                catch (Exception e)
                {
                    if (attemptCount <= 5)
                    {
                        continue;
                    }
                    CloudLog.Error("Pushed update failed to get version {0}, {1}", _pushedVersion, e.Message);
                    _status = UpdateStatus.Failed;
                    return(null);
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        public static int Download(string url)
        {
            Debug.WriteInfo("SoftwareUpdate.Download() Called");

            if (_status == UpdateStatus.Pending)
            {
                throw new Exception("Download process already running");
            }
            try
            {
                ResetDownloads();

                _downloadCancelled = false;
                _status            = UpdateStatus.Pending;
                _progress          = 10;
                var attemptCount = 0;

                while (true)
                {
                    if (_downloadCancelled)
                    {
                        Debug.WriteWarn("SoftwareUpdate.Download()", "Cancelled");
                        _status = UpdateStatus.Cancelled;
                        return(-1);
                    }

                    attemptCount++;

                    if (attemptCount > 10)
                    {
                        Debug.WriteWarn("SoftwareUpdate.Download()", "Too many attempts, cancelling");
                        _status = UpdateStatus.Failed;
                        _httpClient.Dispose();
                        _httpClient = new HttpClient();
                        return(-3);
                    }

                    var getTask = _httpClient.GetAsync(url);
                    Debug.WriteInfo("SoftwareUpdate.Download()", "Awaiting response");
                    var response = getTask.Await();
                    var message  = response.EnsureSuccessStatusCode();
                    Debug.WriteWarn("SoftwareUpdate.Download()", "Status = {0}", message.StatusCode.ToString());
                    if (message.StatusCode == HttpStatusCode.OK)
                    {
                        _status = UpdateStatus.Waiting;
                    }
                    else
                    {
                        Thread.Sleep(5000);
                        continue;
                    }

                    if (response.Content.Headers.ContentLength == null)
                    {
                        Debug.WriteWarn("SoftwareUpdate.Download()", "ContentLength is null");
                        Thread.Sleep(5000);
                        continue;
                    }
                    var totalBytes = (long)response.Content.Headers.ContentLength;

                    Debug.WriteInfo("SoftwareUpdate.Download()", "Received {0} bytes, {1}", totalBytes,
                                    Tools.PrettyByteSize(totalBytes, 2));

                    if (_downloadCancelled)
                    {
                        Debug.WriteWarn("SoftwareUpdate.Download()", "Cancelled");
                        _status = UpdateStatus.Cancelled;
                        return(-1);
                    }

                    using (var contentStream = response.Content.ReadAsStreamAsync().Await())
                    {
                        _status = UpdateStatus.Downloading;
                        var totalBytesRead = 0L;
                        var readCount      = 0L;
                        var buffer         = new byte[8192];
                        var isMoreToRead   = true;

                        using (
                            var fileStream = new FileStream(InitialParametersClass.ProgramDirectory + @"\update.cpz",
                                                            FileMode.Create, FileAccess.Write, FileShare.None))
                        {
                            try
                            {
                                do
                                {
                                    var bytesRead = contentStream.ReadAsync(buffer, 0, buffer.Length).Await();
                                    if (bytesRead == 0)
                                    {
                                        isMoreToRead = false;
                                        _progress    = (int)Tools.ScaleRange(totalBytesRead, 0, totalBytes, 0, 100);
                                        Debug.WriteInfo("Download progress:", "{0}%", _progress);
                                        continue;
                                    }

                                    fileStream.WriteAsync(buffer, 0, bytesRead).Await();

                                    totalBytesRead += bytesRead;
                                    readCount      += 1;

                                    if (readCount % 100 != 0)
                                    {
                                        continue;
                                    }
                                    _progress = (int)Tools.ScaleRange(totalBytesRead, 0, totalBytes, 0, 100);
                                    Debug.WriteInfo("Download progress:", "{0}%", _progress);
                                } while (isMoreToRead);
                                Debug.WriteSuccess("Download complete");
                                _status   = UpdateStatus.Downloaded;
                                _progress = 100;
                                return(0);
                            }
                            catch (Exception e)
                            {
                                CloudLog.Exception(e);
                                _status = UpdateStatus.Failed;
                                _httpClient.Dispose();
                                _httpClient = new HttpClient();
                                return(-2);
                            }
                        }
                    }
                }
            }
            catch
            {
                _status = UpdateStatus.Failed;
                return(-2);
            }
        }