Пример #1
0
        public static async ValueTask <ATResult <OkResult> > SendSmsAsync(
            this ICommunicator <string> comm,
            ResponseFormat responseFormat,
            PhoneNumber phoneNumber,
            string message,
            CancellationToken cancellationToken = default)
        {
            if (message.Length > 160)
            {
                throw new ArgumentOutOfRangeException($"Message exceeded maximum length of 160");
            }

            await comm.Write($"AT+CMGS=\"{phoneNumber}\"\r", cancellationToken);

            Thread.Sleep(100);
            var response = await comm.ReadSingleMessageAsync((byte)'>', cancellationToken);

            await comm.Write($"{message}\r{0x1A}");

            Thread.Sleep(2000);
            response = await comm.ReadSingleMessageAsync((byte)'\r', cancellationToken);

            var result = OkParser.TryParse(response, responseFormat, out _);

            return(default);
Пример #2
0
        public static async ValueTask <ATResult <PinStatusResult> > GetPinStatusAsync(
            this ICommunicator <string> comm,
            ResponseFormat responseFormat,
            CancellationToken cancellationToken = default)
        {
            await comm.Write($"AT+CPIN?\r", cancellationToken);

            var message = await comm.ReadSingleMessageAsync(Constants.BYTE_LF, cancellationToken);

            if (PinStatusParser.TryParse(message, responseFormat, out ATResult <PinStatusResult> pinResult))
            {
                message = await comm.ReadSingleMessageAsync(Constants.BYTE_LF, cancellationToken);

                if (OkParser.TryParse(message, responseFormat, out ATResult <OkResult> _))
                {
                    return(pinResult);
                }
                else if (ErrorParser.TryParse(message, responseFormat, out ATResult <ErrorResult> errorResult))
                {
                    return(ATResult.Error <PinStatusResult>(errorResult.ErrorMessage));
                }
            }
            else if (ErrorParser.TryParse(message, responseFormat, out ATResult <ErrorResult> errorResult))
            {
                return(ATResult.Error <PinStatusResult>(errorResult.ErrorMessage));
            }
            return(ATResult.Error <PinStatusResult>(Constants.PARSING_FAILED));
        }
Пример #3
0
        public static async ValueTask <ATResult <OkResult> > InitializeAsync(
            this ICommunicator <string> comm,
            ResponseFormat responseFormat,
            CancellationToken cancellationToken = default)
        {
            await comm.Write("AT\r", cancellationToken);

            var message = await comm.ReadSingleMessageAsync(Constants.BYTE_LF, cancellationToken);

            if (OkParser.TryParse(message, responseFormat, out ATResult <OkResult> okResult))
            {
                return(okResult);
            }
            else if (ErrorParser.TryParse(message, responseFormat, out ATResult <ErrorResult> errorResult))
            {
                return(ATResult.Error <OkResult>(errorResult.ErrorMessage));
            }
            return(ATResult.Error <OkResult>(Constants.PARSING_FAILED));
        }
Пример #4
0
        public static async ValueTask <ATResult <OkResult> > SetModeAsync(
            this ICommunicator <string> comm,
            ResponseFormat responseFormat,
            Mode mode,
            CancellationToken cancellationToken = default)
        {
            await comm.Write($"AT+CMGF={(int)mode}\r", cancellationToken);

            var message = await comm.ReadSingleMessageAsync((byte)'\r', cancellationToken);

            if (OkParser.TryParse(message, responseFormat, out ATResult <OkResult> okResult))
            {
                return(okResult);
            }
            else if (ErrorParser.TryParse(message, responseFormat, out ATResult <ErrorResult> errorResult))
            {
                return(ATResult.Error <OkResult>(errorResult.ErrorMessage));
            }
            return(ATResult.Error <OkResult>(Constants.PARSING_FAILED));
        }
Пример #5
0
        public static async ValueTask <ATResult <OkResult> > EnableCommandEchoAsync(
            this ICommunicator <string> comm,
            ResponseFormat responseFormat,
            bool enable,
            CancellationToken cancellationToken = default)
        {
            byte parameter = (byte)(enable ? 1 : 0);
            await comm.Write($"ATE{parameter}\r", cancellationToken);

            var message = await comm.ReadSingleMessageAsync((byte)'\n', cancellationToken);

            if (OkParser.TryParse(message, responseFormat, out ATResult <OkResult> okResult))
            {
                return(okResult);
            }
            else if (ErrorParser.TryParse(message, responseFormat, out ATResult <ErrorResult> errorResult))
            {
                return(ATResult.Error <OkResult>(errorResult.ErrorMessage));
            }
            return(ATResult.Error <OkResult>(Constants.PARSING_FAILED));
        }
Пример #6
0
        public static async ValueTask <ATResult <BatteryStatusResult> > GetBatteryStatusAsync(
            this ICommunicator <string> comm,
            ResponseFormat responseFormat,
            CancellationToken cancellationToken = default)
        {
            await comm.Write("AT+CBC\r\n", cancellationToken);

            var message = await comm.ReadSingleMessageAsync(Constants.BYTE_LF, cancellationToken);

            if (BatteryStatusParser.TryParse(message, responseFormat, out ATResult <BatteryStatusResult> batteryResult))
            {
                message = await comm.ReadSingleMessageAsync(Constants.BYTE_LF, cancellationToken);

                if (OkParser.TryParse(message, responseFormat, out ATResult <OkResult> _))
                {
                    return(batteryResult);
                }
                else if (ErrorParser.TryParse(message, responseFormat, out ATResult <ErrorResult> errorResult))
                {
                    return(ATResult.Error <BatteryStatusResult>(errorResult.ErrorMessage));
                }
            }
            return(ATResult.Error <BatteryStatusResult>(batteryResult.ErrorMessage));
        }
Пример #7
0
        public static async ValueTask <ATResult <SignalQualityResult> > GetSignalQualityAsync(
            this ICommunicator <string> comm,
            ResponseFormat responseFormat,
            CancellationToken cancellationToken = default)
        {
            await comm.Write("AT+CSQ\r", cancellationToken);

            var message = await comm.ReadSingleMessageAsync(Constants.BYTE_LF, cancellationToken);

            if (SignalQualityParser.TryParse(message, responseFormat, out ATResult <SignalQualityResult> signalQualityResult))
            {
                message = await comm.ReadSingleMessageAsync(Constants.BYTE_LF, cancellationToken);

                if (OkParser.TryParse(message, responseFormat, out ATResult <OkResult> _))
                {
                    return(signalQualityResult);
                }
                else if (ErrorParser.TryParse(message, responseFormat, out ATResult <ErrorResult> errorResult))
                {
                    return(ATResult.Error <SignalQualityResult>(errorResult.ToString()));
                }
            }
            return(ATResult.Error <SignalQualityResult>(Constants.PARSING_FAILED));
        }