<commit_before>﻿using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Syndll2.Data;

namespace Syndll2
{
    /// <summary>
    /// Provides simple methods that perform specific actions on the terminal.
    /// </summary>
    public class TerminalOperations
    {
        private readonly SynelClient _client;

        internal TerminalOperations(SynelClient client)
        {
            _client = client;
        }

        /// <summary>
        /// Puts the terminal in programming mode, and exposes programming functions.
        /// Call this from a <code>using</code> block, or otherwise dispose of it to exit programming mode.
        /// </summary>
        public ProgrammingOperations Programming()
        {
            return new ProgrammingOperations(_client);
        }

        #region GetTerminalStatus
        /// <summary>
        /// Gets the status information from the terminal.
        /// </summary>
        public TerminalStatus GetTerminalStatus()
        {
            var response = _client.SendAndReceive(RequestCommand.GetStatus);
            return GetTerminalStatusResult(response);
        }

#if NET_45
        /// <summary>
        /// Returns an awaitable task that gets the status information from the terminal.
        /// </summary>
        public async Task<TerminalStatus> GetTerminalStatusAsync()
        {
            var response = await _client.SendAndReceiveAsync(RequestCommand.GetStatus);
            return GetTerminalStatusResult(response);
        }
#endif

        private static TerminalStatus GetTerminalStatusResult(Response response)
        {
            if (response.Command != PrimaryResponseCommand.TerminalStatus)
                throw new InvalidDataException(string.Format("Expected response of {0} but received {1}.",
                                                             PrimaryResponseCommand.TerminalStatus, response.Command));

            return TerminalStatus.Parse(response.Data);
        }
        #endregion

        #region SetTerminalStatus
        /// <summary>
        /// Sets the terminal's clock and active function.
        /// </summary>
        public void SetTerminalStatus(DateTime clockDateTime, char activeFunction)
        {
            var request = new TimeAndFunction(clockDateTime, activeFunction);
            var response = _client.SendAndReceive(RequestCommand.SetStatus, request.ToString());
            ValidateAcknowledgment(response);

            // pause to allow time for the terminal to update internally
            Thread.Sleep(20);
        }

#if NET_45
        /// <summary>
        /// Returns an awaitable task that sets the terminal's clock and active function.
        /// </summary>
        public async Task SetTerminalStatusAsync(DateTime clockDateTime, char activeFunction)
        {
            var request = new TimeAndFunction(clockDateTime, activeFunction);
            var response = await _client.SendAndReceiveAsync(RequestCommand.SetStatus, request.ToString());
            ValidateAcknowledgment(response);

            // pause to allow time for the terminal to update internally
            await Task.Delay(20);
        }
#endif
        #endregion

        #region SetTerminalClock
        /// <summary>
        /// Sets the terminal's clock.
        /// Does not change the currently active function.
        /// </summary>
        public void SetTerminalClock(DateTime clockDateTime)
        {
            var status = GetTerminalStatus();
            SetTerminalStatus(clockDateTime, status.ActiveFunction);
        }

#if NET_45
        /// <summary>
        /// Returns an awaitable task that sets the terminal's clock.
        /// Does not change the currently active function.
        /// </summary>
        public async Task SetTerminalClockAsync(DateTime clockDateTime)
        {
            var status = await GetTerminalStatusAsync();
            await SetTerminalStatusAsync(clockDateTime, status.ActiveFunction);
        }
#endif
        #endregion

        #region GetTechnicianModeSettings
        /// <summary>
        /// Gets the current "technician mode" settings from the terminal.
        /// </summary>
        public TechnicianModeSettings GetTechnicianModeSettings()
        {
            var response = _client.SendAndReceive(RequestCommand.SystemCommands, "HPG");
            return GetTechnicianModeSettingsResult(response);
        }

#if NET_45
        /// <summary>
        /// Returns an awaitable task that gets the current "technician mode" settings from the terminal.
        /// </summary>
        public async Task<TechnicianModeSettings> GetTechnicianModeSettingsAsync()
        {
            var response = await _client.SendAndReceiveAsync(RequestCommand.SystemCommands, "HPG");
            return GetTechnicianModeSettingsResult(response);
        }
#endif

        private static TechnicianModeSettings GetTechnicianModeSettingsResult(Response response)
        {
            if (response.Command != PrimaryResponseCommand.SystemCommands)
                throw new InvalidDataException(string.Format("Expected response of {0} but received {1}.",<commit_msg>Can set clock without getting status first.<commit_after>﻿using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Syndll2.Data;

namespace Syndll2
{
    /// <summary>
    /// Provides simple methods that perform specific actions on the terminal.
    /// </summary>
    public class TerminalOperations
    {
        private readonly SynelClient _client;

        internal TerminalOperations(SynelClient client)
        {
            _client = client;
        }

        /// <summary>
        /// Puts the terminal in programming mode, and exposes programming functions.
        /// Call this from a <code>using</code> block, or otherwise dispose of it to exit programming mode.
        /// </summary>
        public ProgrammingOperations Programming()
        {
            return new ProgrammingOperations(_client);
        }

        #region GetTerminalStatus
        /// <summary>
        /// Gets the status information from the terminal.
        /// </summary>
        public TerminalStatus GetTerminalStatus()
        {
            var response = _client.SendAndReceive(RequestCommand.GetStatus);
            return GetTerminalStatusResult(response);
        }

#if NET_45
        /// <summary>
        /// Returns an awaitable task that gets the status information from the terminal.
        /// </summary>
        public async Task<TerminalStatus> GetTerminalStatusAsync()
        {
            var response = await _client.SendAndReceiveAsync(RequestCommand.GetStatus);
            return GetTerminalStatusResult(response);
        }
#endif

        private static TerminalStatus GetTerminalStatusResult(Response response)
        {
            if (response.Command != PrimaryResponseCommand.TerminalStatus)
                throw new InvalidDataException(string.Format("Expected response of {0} but received {1}.",
                                                             PrimaryResponseCommand.TerminalStatus, response.Command));

            return TerminalStatus.Parse(response.Data);
        }
        #endregion

        #region SetTerminalStatus
        /// <summary>
        /// Sets the terminal's clock and active function.
        /// </summary>
        public void SetTerminalStatus(DateTime clockDateTime, char activeFunction)
        {
            var request = new TimeAndFunction(clockDateTime, activeFunction);
            var response = _client.SendAndReceive(RequestCommand.SetStatus, request.ToString());
            ValidateAcknowledgment(response);

            // pause to allow time for the terminal to update internally
            Thread.Sleep(20);
        }

#if NET_45
        /// <summary>
        /// Returns an awaitable task that sets the terminal's clock and active function.
        /// </summary>
        public async Task SetTerminalStatusAsync(DateTime clockDateTime, char activeFunction)
        {
            var request = new TimeAndFunction(clockDateTime, activeFunction);
            var response = await _client.SendAndReceiveAsync(RequestCommand.SetStatus, request.ToString());
            ValidateAcknowledgment(response);

            // pause to allow time for the terminal to update internally
            await Task.Delay(20);
        }
#endif
        #endregion

        #region SetTerminalClock
        /// <summary>
        /// Sets the terminal's clock.
        /// Does not change the currently active function.
        /// </summary>
        public void SetTerminalClock(DateTime clockDateTime)
        {
            SetTerminalStatus(clockDateTime, '#');
        }

#if NET_45
        /// <summary>
        /// Returns an awaitable task that sets the terminal's clock.
        /// Does not change the currently active function.
        /// </summary>
        public async Task SetTerminalClockAsync(DateTime clockDateTime)
        {
            await SetTerminalStatusAsync(clockDateTime, '#');
        }
#endif
        #endregion

        #region GetTechnicianModeSettings
        /// <summary>
        /// Gets the current "technician mode" settings from the terminal.
        /// </summary>
        public TechnicianModeSettings GetTechnicianModeSettings()
        {
            var response = _client.SendAndReceive(RequestCommand.SystemCommands, "HPG");
            return GetTechnicianModeSettingsResult(response);
        }

#if NET_45
        /// <summary>
        /// Returns an awaitable task that gets the current "technician mode" settings from the terminal.
        /// </summary>
        public async Task<TechnicianModeSettings> GetTechnicianModeSettingsAsync()
        {
            var response = await _client.SendAndReceiveAsync(RequestCommand.SystemCommands, "HPG");
            return GetTechnicianModeSettingsResult(response);
        }
#endif

        private static TechnicianModeSettings GetTechnicianModeSettingsResult(Response response)
        {
            if (response.Command != PrimaryResponseCommand.SystemCommands)
                throw new InvalidDataException(string.Format("Expected response of {0} but received {1}.",
{'fraction_non_alphanumeric': 0.08196082253191697, 'fraction_numerical': 0.0035084299775850307, 'mean_word_length': 2.185967090965539, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7434122', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2330, 'n_words': 700}