-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cs
More file actions
214 lines (185 loc) · 5.79 KB
/
Copy pathDatabase.cs
File metadata and controls
214 lines (185 loc) · 5.79 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json;
namespace BluetoothPanel
{
public class Database
{
static Database Instance;
public Dictionary<String, Value> data;
public Dictionary<String, (string, int)> sendData;
public Database()
{
data = new Dictionary<String, Value>();
sendData = new Dictionary<String, (string, int)>();
}
public static Database GetInstance()
{
if (Instance == null)
Instance = new Database();
return Instance;
}
public void setString(String key, String value)
{
data[key] = new StringValue(value);
}
public void setNumber(String key, double value)
{
data[key] = new NumberValue(value);
}
public void setBoolean(String key, bool value)
{
data[key] = new BooleanValue(value);
}
public void updateDB(Dictionary<String, object> newData)
{
foreach (KeyValuePair<String, object> kv in newData)
{
if (kv.Key.StartsWith("get") && data.ContainsKey(kv.Key)) return;
String value = kv.Value.ToString();
if (double.TryParse(value, out double dRes))
{
data[kv.Key] = new NumberValue(dRes);
} else if (int.TryParse(value, out int iRes))
{
data[kv.Key] = new NumberValue((double)iRes);
} else if (bool.TryParse(value, out bool bRes))
{
data[kv.Key] = new BooleanValue(bRes);
} else
{
data[kv.Key] = new StringValue(value);
}
}
}
public void updateDBFromApp(Dictionary<String, object> newData)
{
foreach (KeyValuePair<String, object> kv in newData)
{
String value = kv.Value.ToString();
if (double.TryParse(value, out double dRes))
{
data[kv.Key] = new NumberValue(dRes);
}
else if (int.TryParse(value, out int iRes))
{
data[kv.Key] = new NumberValue((double)iRes);
}
else if (bool.TryParse(value, out bool bRes))
{
data[kv.Key] = new BooleanValue(bRes);
}
else
{
data[kv.Key] = new StringValue(value);
}
}
}
public void printDB()
{
Debug.WriteLine("printing");
foreach (KeyValuePair<string, Value> kvp in data)
{
Debug.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
}
public override String ToString()
{
Dictionary<String, object> retVal = new Dictionary<string, object>();
foreach (var item in data)
{
if (item.Key.StartsWith("get") && shouldSendData(item.Key, item.Value.ToString()))
{
if (item.Value is NumberValue numberValue)
{
retVal.Add(item.Key, numberValue.Number);
} else if (item.Value is BooleanValue bValue)
{
retVal.Add(item.Key, bValue.Boolean);
} else
{
retVal.Add(item.Key, item.Value.ToString());
}
}
}
// Serialize the 'data' dictionary to a JSON string
return JsonSerializer.Serialize(retVal);
}
private int EXTRA_SENDS = 4;
private bool shouldSendData(String key, String value)
{
if (sendData.ContainsKey(key))
{
(string, int) temp = sendData[key];
if (value == temp.Item1)
{
if (sendData[key].Item2 > EXTRA_SENDS)
{
return false;
}
sendData[key] = (value, temp.Item2 + 1);
return true;
} else
{
sendData[key] = (value, 0);
return true;
}
} else
{
sendData[key] = (value, 0);
return true;
}
}
public void resetShouldSendData()
{
sendData.Clear();
}
}
public abstract class Value
{
// This is an abstract base class for representing different value types.
// Subclasses will define the specific value types.
// You can add common methods or properties here if needed.
}
public class NumberValue : Value
{
public double Number { get; }
public NumberValue(double number)
{
Number = number;
}
public override string ToString()
{
return Number.ToString();
}
public double getNumber()
{
return Number;
}
}
public class StringValue : Value
{
public string String { get; }
public StringValue(string str)
{
String = str;
}
public override String ToString()
{
return String;
}
}
public class BooleanValue : Value
{
public bool Boolean { get; }
public BooleanValue(bool boolean)
{
Boolean = boolean;
}
public override string ToString()
{
return Boolean.ToString();
}
}
}