1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text.RegularExpressions;
5: using Microsoft.EntityFrameworkCore;
6:
7: namespace Ia.TentPlay.Cl.Model.Business.Nfc
8: {
9: ////////////////////////////////////////////////////////////////////////////
10:
11: /// <summary publish="true">
12: /// TAG NFC Near-Field Communication (NFC) Support Business functions
13: /// </summary>
14: /// <value>
15: /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
16: /// </value>
17: /// <remarks>
18: /// Copyright © 2020-2021 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
19: ///
20: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
21: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
22: ///
23: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
25: ///
26: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
27: ///
28: /// Copyright notice: This notice may not be removed or altered from any source distribution.
29: /// </remarks>
30: public partial class Tag
31: {
32: /*
33: * Don't remove items that are obsolete, just mark them with obsolete="true"
34: * Insertable is true if not explicitly specified
35: */
36:
37: ////////////////////////////////////////////////////////////////////////////
38:
39: /// <summary>
40: ///
41: /// </summary>
42: public class Type
43: {
44: public int Id { get; set; }
45: public string Name { get; set; }
46: }
47:
48: ////////////////////////////////////////////////////////////////////////////
49:
50: /// <summary>
51: ///
52: /// </summary>
53: public static List<Type> TypeList
54: {
55: get
56: {
57: List<Type> list;
58:
59: list = new List<Type>();
60:
61: list.Add(new Type { Id = 0, Name = "NTAG 213" });
62:
63: if (list.Select(u => u.Id).Distinct().Count() != list.Count())
64: {
65: throw new Exception("Duplicate Id in Ia.TentPlay.Cl.Model.Business.Nfc.Default.NfcTypeList.");
66: }
67:
68: return list;
69: }
70: }
71:
72: ////////////////////////////////////////////////////////////////////////////
73:
74: /// <summary>
75: ///
76: /// </summary>
77: public static string TypeNameById(int id)
78: {
79: string name;
80:
81: var type = (from t in Ia.TentPlay.Cl.Model.Business.Nfc.Tag.TypeList where t.Id == id select t).SingleOrDefault();
82:
83: name = type != null ? type.Name : string.Empty;
84:
85: return name;
86: }
87:
88: ////////////////////////////////////////////////////////////////////////////
89:
90: /// <summary>
91: ///
92: /// </summary>
93: public Tag() { }
94:
95: ////////////////////////////////////////////////////////////////////////////
96:
97: /// <summary>
98: ///
99: /// </summary>
100: public static Ia.Cl.Model.Db.Default.PersistentStorageState CreateOrUpdate(int projectId, string uId, int counter, string payload)
101: {
102: var dateTime = DateTime.UtcNow.AddHours(3);
103:
104: var id = Ia.TentPlay.Cl.Model.Business.Nfc.Default.Id(projectId, uId);
105:
106: var tagTypeId = 0;
107:
108: var tag = new Ia.TentPlay.Cl.Model.Nfc.Tag
109: {
110: Id = id,
111: UId = uId,
112: TypeId = tagTypeId,
113: ProjectId = projectId,
114: Counter = counter,
115: Payload = payload,
116: UserId = Guid.Empty,
117: Created = dateTime,
118: Updated = dateTime,
119: Interacted = dateTime
120: };
121:
122: var persistentStorageState = Ia.TentPlay.Cl.Model.Data.Nfc.Tag.CreateOrUpdate(tag);
123:
124: return persistentStorageState;
125: }
126:
127: ////////////////////////////////////////////////////////////////////////////
128:
129: /// <summary>
130: ///
131: /// </summary>
132: public static void ParseTagUIdAndCounter(string line, out string uId, out int counter)
133: {
134: var matchCollection = Regex.Matches(line, @"^(.+)x(\d+)$");
135:
136: if (matchCollection.Count > 0)
137: {
138: uId = matchCollection[0].Groups[1].Value;
139:
140: var hex = matchCollection[0].Groups[2].Value;
141:
142: counter = Ia.Cl.Model.Default.HexToDec(hex);
143: }
144: else
145: {
146: uId = line;
147: counter = -1;
148: }
149: }
150:
151: ////////////////////////////////////////////////////////////////////////////
152: ////////////////////////////////////////////////////////////////////////////
153: }
154:
155: ////////////////////////////////////////////////////////////////////////////
156: ////////////////////////////////////////////////////////////////////////////
157: }