-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
244 lines (180 loc) · 7.79 KB
/
main.ts
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//% color=190 weight=100 icon="\uf1c0" block="XinaBox JSON"
namespace xinabox_json
{
let sensor_id: string = ""
let name: string = ""
let scale: string
let value: string
let name_key_index: number = 0
let name_string: number = 0
let scale_id: string
let scale_string: number = 0
let scale_key_index: number = 0
let value_string: number = 0
let value_key_index: number = 0
let first_quotation = 0
let second_quotation = 0
let value_array: string[] = []
let scale_array: string[] = []
let uids: string[] = []
let count: number = 0
let count2: number = 0
let num: number = 0
let global_key1: string = ""
let global_key2: string = ""
let level_one_uid: string[] = []
let level_two_uid: string[] = []
let level_one_key: string[] = []
let level_two_key: string[] = []
let str2: string = "";
function init()
{
while(true)
{
// level_one id
first_quotation = str2.indexOf("\"", second_quotation + 1)
second_quotation = str2.indexOf("\"", first_quotation + 1)
sensor_id = str2.substr(first_quotation + 1, second_quotation - first_quotation - 1)
level_one_uid[count2] = sensor_id
// level_one first key
first_quotation = str2.indexOf("\"", second_quotation + 1)
second_quotation = str2.indexOf("\"", first_quotation + 1)
name = str2.substr(first_quotation + 1, second_quotation - first_quotation - 1)
level_one_key[count2] = name
if((str2.indexOf("{", second_quotation) - 20) >= 0)
{
second_quotation = str2.indexOf("{", second_quotation) - 20
second_quotation = str2.indexOf(",", second_quotation)
count2++
let right_bracket: number = 0
while(true)
{
// level_two id
first_quotation = str2.indexOf("\"", second_quotation + 1)
second_quotation = str2.indexOf("\"", first_quotation + 1)
scale_id = str2.substr(first_quotation + 1, second_quotation - first_quotation - 1)
level_two_uid[count] = scale_id
// level_two first key
first_quotation = str2.indexOf("\"", second_quotation + 1)
second_quotation = str2.indexOf("\"", first_quotation + 1)
value = str2.substr(first_quotation + 1, second_quotation - first_quotation - 1)
level_two_key[count] = value
second_quotation = str2.indexOf("}", second_quotation)
count++
if(str2.indexOf("}", second_quotation) > 0)
{
let right_bracket = str2.indexOf("}", second_quotation)
if(((str2.indexOf("}", right_bracket + 1) - right_bracket) >= 0) && ((str2.indexOf("}", right_bracket + 1) - right_bracket) < 5))
{
let second_right_bracket = str2.indexOf("}", right_bracket)
break
}
}
}
}
if((str2.indexOf("}", second_quotation) >= 0) && (str2.indexOf("}", second_quotation + 1) >= 0) && (str2.indexOf("}", second_quotation + 2) >= 0) && (str2.indexOf("{", second_quotation) < 0))
{
break
}
}
global_key1 = level_one_key[0]
global_key2 = level_two_key[0]
}
//% blockId=load_json
//% block="XinaBox load json %json"
export function load_json(json: string)
{
str2 = json
// extract level one and two first key names
init()
}
function parse_json(str: string)
{
// Search for particular key
second_quotation = 0
count = 0
count2 = 0
while(true)
{
// sensor id
first_quotation = str.indexOf("\"", second_quotation + 1)
second_quotation = str.indexOf("\"", first_quotation + 1)
sensor_id = str.substr(first_quotation + 1, second_quotation - first_quotation - 1)
level_one_uid[count2] = sensor_id
// sensor name
name_key_index = str.indexOf("\"" + global_key1 + "\"", second_quotation + 1)
name_string = ("\"" + global_key1 + "\"").length + name_key_index
first_quotation = str.indexOf("\"", name_string + 1)
second_quotation = str.indexOf("\"", first_quotation + 1)
name = str.substr(first_quotation + 1, second_quotation - first_quotation - 1)
level_one_key[count2] = name
if((str.indexOf("{", second_quotation) - 20) >= 0)
{
second_quotation = str.indexOf("{", second_quotation) - 20
second_quotation = str.indexOf(",", second_quotation)
count2++
let right_bracket: number = 0
while(true)
{
// scale id
first_quotation = str.indexOf("\"", second_quotation + 1)
second_quotation = str.indexOf("\"", first_quotation + 1)
scale_id = str.substr(first_quotation + 1, second_quotation - first_quotation - 1)
level_two_uid[count] = scale_id
// sensor value
value_key_index = str.indexOf("\"" + global_key2 + "\"", second_quotation)
value_string = ("\"" + global_key2 + "\"").length + value_key_index
first_quotation = str.indexOf("\"", value_string + 1)
second_quotation = str.indexOf("\"", first_quotation + 1)
value = str.substr(first_quotation + 1, second_quotation - first_quotation - 1)
level_two_key[count] = value
second_quotation = str.indexOf("}", second_quotation)
count++
if(str.indexOf("}", second_quotation) > 0)
{
let right_bracket = str.indexOf("}", second_quotation)
if(((str.indexOf("}", right_bracket + 1) - right_bracket) >= 0) && ((str.indexOf("}", right_bracket + 1) - right_bracket) < 5))
{
let second_right_bracket = str.indexOf("}", right_bracket)
break
}
}
}
}
if((str.indexOf("}", second_quotation) >= 0) && (str.indexOf("}", second_quotation + 1) >= 0) && (str.indexOf("}", second_quotation + 2) >= 0) && (str.indexOf("{", second_quotation) < 0))
{
break
}
}
}
//% blockId=getLevelOneValue
//% block="XinaBox get level one value of uid %uid and key %key"
export function getLevelOneValue(uid: string, key: string)
{
let index: number = 0
let value: string = ""
count = 0
count2 = 0
second_quotation = 0
global_key1 = key
parse_json(str2)
index = level_one_uid.indexOf(uid)
if(index >= 0)value = level_one_key[index]
return value
}
//% blockId=getLeveTwoValue
//% block="XinaBox get level two value of uid %uid and key %key"
export function getLevelTwoValue(uid: string, key: string)
{
let index: number = 0
let value: string = ""
count = 0
count2 = 0
second_quotation = 0
global_key2 = key
parse_json(str2)
index = level_two_uid.indexOf(uid)
if(index >= 0)value = level_two_key[index]
return value
}
}