forked from TRU-E-Bike-Project/bg96sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkRegistrationStatusResult.cs
119 lines (110 loc) · 4.6 KB
/
NetworkRegistrationStatusResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
namespace BG96Sharp
{
/// <summary>
/// Provides basic network registration status information. For more information, check if this object is of type <see cref="FullNetworkRegistrationStatus"/>
/// </summary>
public class BaseNetworkRegistrationStatus
{
public BaseNetworkRegistrationStatus(NetworkRegistrationStatusCode status)
{
Status = status;
}
/// <inheritdoc cref="UnsolicitedNetworkRegistrationCode"/>
public NetworkRegistrationStatusCode Status { get; private set; }
/// <summary>
/// Create a network registration status object.
/// </summary>
/// <param name="response">Response message from serial port</param>
/// <returns>A <see cref="FullNetworkRegistrationStatus" /> if the data is available; otherwise, creates a <see cref="BaseNetworkRegistrationStatus"/></returns>
public static BaseNetworkRegistrationStatus FromResponse(string response)
{
if (!response.StartsWith("+CREG"))
throw new ArgumentException("Input does not start with required message.");
var commaCount = response.Count(x => x == ',');
if (commaCount == 0)
{
//it's only a base
var result = Regex.Match(response, @"\+CREG: (\d)");
return new BaseNetworkRegistrationStatus((NetworkRegistrationStatusCode)int.Parse(result.Groups[1].Value));
}
else
{
var result = Regex.Match(response, @"\+CREG: (\d),(\d)");
return new FullNetworkRegistrationStatus((UnsolicitedNetworkRegistrationCode)int.Parse(result.Groups[1].Value), (NetworkRegistrationStatusCode)int.Parse(result.Groups[2].Value));
}
}
}
/// <summary>
/// Provides full network registration information.
/// </summary>
public class FullNetworkRegistrationStatus : BaseNetworkRegistrationStatus
{
public FullNetworkRegistrationStatus(UnsolicitedNetworkRegistrationCode networkRegistrationUnsolicitedResultCodeState, NetworkRegistrationStatusCode networkRegistrationStatusCode) : base(networkRegistrationStatusCode)
{
NetworkRegistrationUnsolicitedResultCodeState = networkRegistrationUnsolicitedResultCodeState;
}
/// <summary>
/// Current state of unsolicited network status messages. If enabled, the module will send messages when network status changes on its own.
/// </summary>
public UnsolicitedNetworkRegistrationCode NetworkRegistrationUnsolicitedResultCodeState { get; private set; } = UnsolicitedNetworkRegistrationCode.Unknown;
//TODO: implement the rest of the properties that are available
}
/// <summary>
/// Code representing whether the module will send unsolicited network registration information
/// </summary>
public enum UnsolicitedNetworkRegistrationCode
{
/// <summary>
/// (Used by library) Unknown registration status
/// </summary>
Unknown = -1,
/// <summary>
/// Disable network registration unsolicited result code
/// </summary>
Disabled = 0,
/// <summary>
/// Enable network registration unsolicited result code, with status only
/// </summary>
EnabledStatOnly = 1,
/// <summary>
/// Enable network registration and location information unsolicited result code will full information
/// </summary>
EnabledFull = 2
}
/// <summary>
/// Code representing current registration status on the mobile network.
/// </summary>
public enum NetworkRegistrationStatusCode
{
/// <summary>
/// Not registered. MT is not currently searching an operator to register to.
/// </summary>
Unregistered = 0,
/// <summary>
/// Registered, home network.
/// </summary>
RegisteredHomeNetwork = 1,
/// <summary>
/// Not registered, but MT is currently trying to attach or searching an operator to register to.
/// </summary>
NotRegisteredSearching = 2,
/// <summary>
/// Registration denied.
/// </summary>
RegistrationDenied = 3,
/// <summary>
/// Unknown
/// </summary>
Unknown = 4,
/// <summary>
/// Registered on a roaming network.
/// </summary>
RegisteredRoaming = 5
}
}