-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.sqbpro
605 lines (409 loc) · 16.9 KB
/
db.sqbpro
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="C:/Users/treya/Documents/Dev/HAExpress/db.sqlite" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><main_tabs open="structure browser pragmas query" current="3"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="1015"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><current_table name="4,4:mainmqtt"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="mqtt" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="40"/><column index="2" value="158"/><column index="3" value="68"/><column index="4" value="154"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table></browse_table_settings></tab_browse><tab_sql><sql name="Query_Tests">select mqtt_ip, mqtt_id, mqtt_pw, JSON_EXTRACT(topics_json, '$[1].topic') from settings
select date()
select key, value from settings,json_tree(settings.topics_json, '$') as Topics
select distinct value from settings,json_tree(settings.topics_json, '$') as Topics where Topics.key = 'group'
select key, value from settings,json_tree(settings.topics_json, '$') as Topics where Topics.key = 'Topic' and Topics.value ='Tempurature'
select topic, message, datetime(date,'localtime') as date from mqtt where topic = 'home/door/front/open' ORDER by date desc LIMIT 10
select topic, message, datetime(date,'localtime') as date from mqtt where topic IN
(
SELECT
distinct
json_extract(value, '$.topic')
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.group') = (SELECT
json_extract(value, '$.group') as 'group'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.topic') = 'home/randomroom/temp')
order by Key
)
order by date desc
SELECT
key,
value,
json_extract(value, '$.group') as 'group'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.topic') = 'home/laptop/logoff'
SELECT
key,
value,
json_extract(value, '$.topic')
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.group') = 'Temparature'
SELECT
key,
value,
json_extract(value, '$.topic')
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.group') = 'Doors'
SELECT
key,
value,
json_extract(value, '$.topic')
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.group') = 'Laptop'
SELECT * FROM settings,json_tree(settings.topics_json);
select mqtt_ip, mqtt_id, mqtt_pw, JSON_EXTRACT(topics_json, '$[1].topic') from settings
select topic, message, datetime(date,'localtime') as date, strftime('%Y-%m-%d %H:%M:%S.%f',date) as fulldate from mqtt where topic like 'home/office/%' ORDER by date desc LIMIT 100
select topic, message, datetime(date,'localtime') as date from mqtt where topic = 'home/office/temp' ORDER by date desc LIMIT 10</sql><sql name="Get_1_Message">WITH topics AS
(
SELECT
distinct
json_extract(value, '$.topic') as topic
,json_extract(value, '$.group') as 'group'
,json_extract(value, '$.label') as 'label'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.group') = (SELECT
json_extract(value, '$.group') as 'group'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.topic') = 'home/randomroom/temp')
order by Key
),
messages as
(
select T.topic, MAX( date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
group by T.topic
)
Select T.topic, M.message,T.'group',T.'label', datetime(M2.date,'localtime') as date, STRFTIME('%Y-%m-%d %H:%M:%f', M2.date) as sdate from topics as T inner join mqtt as M on M.topic = T.topic
inner join messages as M2 on M2.date = M.date
--select message, date from messages
--Select * from topics
--select * from messages order by date desc
--select M.topic, M2.message,MAX( datetime(M.date,'localtime')) as date from mqtt as M
--inner join messages as M2 on M2.date = M.date
--inner join topics as T on T.topic = M.topic
--Group by M.topic,M2.message
--</sql><sql name="Get_Based_On_topic">WITH topics AS
(
SELECT
distinct
json_extract(value, '$.topic') as topic
,json_extract(value, '$.group') as 'group'
,json_extract(value, '$.label') as 'label'
,json_extract(value, '$.order') as 'order'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.group') = (SELECT
json_extract(value, '$.group') as 'group'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.topic') = 'home/door/front/vcc')
order by 'order'
),
ppmxdate as (
select T.Topic, MAX(mqtt.date) as date from mqtt inner join topics as T on T.topic = mqtt.topic
inner join pmxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
pmxdate as (
select T.Topic, MAX(mqtt.date) as date from mqtt inner join topics as T on T.topic = mqtt.topic
inner join mxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
mxdate as (
select T.Topic, MAX(date) as date from mqtt inner join topics as T on T.topic = mqtt.topic
group by T.topic
),
prevprevprevmessages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
inner join ppmxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
prevprevmessages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
inner join pmxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
prevmessages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
inner join mxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
)
,messages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
group by T.topic
),
TDAY as
(
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label',T.'order' from topics as T inner join mqtt as M on M.topic = T.topic
inner join messages as M2 on M2.date = M.date
order by CAST(t.'order' AS INTEGER)
)
,
PDAY as
(
-- datetime(M2.date,'localtime') as date,, M2.date as UTCDATE, STRFTIME('%m/%d/%Y %H:%M:%f', M2.date,'localtime') --ms
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label' from topics as T inner join mqtt as M on M.topic = T.topic
inner join prevmessages as M2 on M2.date = M.date
),
PPDAY as
(
-- datetime(M2.date,'localtime') as date,, M2.date as UTCDATE, STRFTIME('%m/%d/%Y %H:%M:%f', M2.date,'localtime') --ms
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label' from topics as T inner join mqtt as M on M.topic = T.topic
inner join prevprevmessages as M2 on M2.date = M.date
),
PPPDAY as
(
-- datetime(M2.date,'localtime') as date,, M2.date as UTCDATE, STRFTIME('%m/%d/%Y %H:%M:%f', M2.date,'localtime') --ms
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label' from topics as T inner join mqtt as M on M.topic = T.topic
inner join prevprevprevmessages as M2 on M2.date = M.date
)
select T.topic, T.message, T.date,P.message as pmessage, P.date as pdate,PP.message as ppmessage, PP.date as ppdate,PPP.message as pppmessage, PPP.date as pppdate,T.'group',T.'label' from TDAY as T
left join PDAY P on P.topic = T.topic
left join PPDAY PP on PP.topic = T.topic
left join PPPDAY PPP on PPP.topic = T.topic
order by CAST(T.'order' AS INTEGER)
--select T.topic, T.message, T.date,P.message as pmessage, P.date as pdate,T.'group',T.'label' from TDAY as T inner join PDAY P on P.topic = T.topic
</sql><sql name="Get_based_on_Group">WITH topics AS
(
SELECT
distinct
json_extract(value, '$.topic') as topic
,json_extract(value, '$.group') as 'group'
,json_extract(value, '$.label') as 'label'
,json_extract(value, '$.order') as 'order'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.group') = 'Temperature'
order by 'order'
),
ppmxdate as (
select T.Topic, MAX(mqtt.date) as date from mqtt inner join topics as T on T.topic = mqtt.topic
inner join pmxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
pmxdate as (
select T.Topic, MAX(mqtt.date) as date from mqtt inner join topics as T on T.topic = mqtt.topic
inner join mxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
mxdate as (
select T.Topic, MAX(date) as date from mqtt inner join topics as T on T.topic = mqtt.topic
group by T.topic
),
prevprevprevmessages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
inner join ppmxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
prevprevmessages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
inner join pmxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
prevmessages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
inner join mxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
)
,messages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
group by T.topic
),
TDAY as
(
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label',T.'order' from topics as T inner join mqtt as M on M.topic = T.topic
inner join messages as M2 on M2.date = M.date
order by CAST(t.'order' AS INTEGER)
)
,
PDAY as
(
-- datetime(M2.date,'localtime') as date,, M2.date as UTCDATE, STRFTIME('%m/%d/%Y %H:%M:%f', M2.date,'localtime') --ms
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label' from topics as T inner join mqtt as M on M.topic = T.topic
inner join prevmessages as M2 on M2.date = M.date
),
PPDAY as
(
-- datetime(M2.date,'localtime') as date,, M2.date as UTCDATE, STRFTIME('%m/%d/%Y %H:%M:%f', M2.date,'localtime') --ms
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label' from topics as T inner join mqtt as M on M.topic = T.topic
inner join prevprevmessages as M2 on M2.date = M.date
),
PPPDAY as
(
-- datetime(M2.date,'localtime') as date,, M2.date as UTCDATE, STRFTIME('%m/%d/%Y %H:%M:%f', M2.date,'localtime') --ms
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label' from topics as T inner join mqtt as M on M.topic = T.topic
inner join prevprevprevmessages as M2 on M2.date = M.date
)
select T.topic, T.message, T.date,P.message as pmessage, P.date as pdate,PP.message as ppmessage, PP.date as ppdate,PPP.message as pppmessage, PPP.date as pppdate,T.'group',T.'label' from TDAY as T
left join PDAY P on P.topic = T.topic
left join PPDAY PP on PP.topic = T.topic
left join PPPDAY PPP on PPP.topic = T.topic
order by CAST(T.'order' AS INTEGER)
--select T.topic, T.message, T.date,P.message as pmessage, P.date as pdate,T.'group',T.'label' from TDAY as T inner join PDAY P on P.topic = T.topic
</sql><sql name="JSON_as_Table">SELECT json_extract(x.value, '$.topic') as 'topic'
, json_extract(x.value, '$.query') as 'query'
, json_extract(x.value, '$.label') as 'label'
, json_extract(x.value, '$.group') as 'group'
, json_extract(x.value, '$.order') as 'order'
FROM settings, json_each(topics_json) AS x
order by json_extract(x.value, '$.group') , CAST(json_extract(x.value, '$.order') AS INTEGER)</sql><sql name="CREATE_SETTINGS">CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
mqtt_ip VARCHAR(15),
mqtt_id VARCHAR(50),
mqtt_pw VARCHAR(50),
topics_json text
)</sql><sql name="DETAIL_TESTS">SELECT
json_extract(value, '$.group') as 'group'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.topic') = 'home/door/front/open'
SELECT
distinct
json_extract(value, '$.topic') as topic
,json_extract(value, '$.group') as 'group'
,json_extract(value, '$.label') as 'label'
,json_extract(value, '$.order') as 'order'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.group') = (SELECT
json_extract(value, '$.group') as 'group'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.topic') = 'home/door/front/open')
order by 'order'
select id,topic, message, STRFTIME('%m/%d/%Y %H:%M:%S', date,'localtime') as date from mqtt where topic = 'home/door/front/open' order by date desc
WITH topics AS
(
SELECT
distinct
json_extract(value, '$.topic') as topic
,json_extract(value, '$.group') as 'group'
,json_extract(value, '$.label') as 'label'
,json_extract(value, '$.order') as 'order'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.group') = (SELECT
json_extract(value, '$.group') as 'group'
FROM
settings,
json_each(settings.topics_json)
WHERE json_extract(value, '$.topic') = 'home/door/front/open')
order by 'order'
),
ppmxdate as (
select T.Topic, MAX(mqtt.date) as date from mqtt inner join topics as T on T.topic = mqtt.topic
inner join pmxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
pmxdate as (
select T.Topic, MAX(mqtt.date) as date from mqtt inner join topics as T on T.topic = mqtt.topic
inner join mxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
mxdate as (
select T.Topic, MAX(date) as date from mqtt inner join topics as T on T.topic = mqtt.topic
group by T.topic
),
prevprevprevmessages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
inner join ppmxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
prevprevmessages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
inner join pmxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
),
prevmessages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
inner join mxdate as MX on MX.topic = mqtt.topic
where mqtt.date < MX.date
group by T.topic
)
,messages as
(
select T.topic, MAX(mqtt.date) as date from mqtt
inner join topics as T on T.topic = mqtt.topic
group by T.topic
),
TDAY as
(
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label',T.'order' from topics as T inner join mqtt as M on M.topic = T.topic
inner join messages as M2 on M2.date = M.date
order by CAST(t.'order' AS INTEGER)
)
,
PDAY as
(
-- datetime(M2.date,'localtime') as date,, M2.date as UTCDATE, STRFTIME('%m/%d/%Y %H:%M:%f', M2.date,'localtime') --ms
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label' from topics as T inner join mqtt as M on M.topic = T.topic
inner join prevmessages as M2 on M2.date = M.date
),
PPDAY as
(
-- datetime(M2.date,'localtime') as date,, M2.date as UTCDATE, STRFTIME('%m/%d/%Y %H:%M:%f', M2.date,'localtime') --ms
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label' from topics as T inner join mqtt as M on M.topic = T.topic
inner join prevprevmessages as M2 on M2.date = M.date
),
PPPDAY as
(
-- datetime(M2.date,'localtime') as date,, M2.date as UTCDATE, STRFTIME('%m/%d/%Y %H:%M:%f', M2.date,'localtime') --ms
Select T.topic, M.message,STRFTIME('%m/%d/%Y %H:%M:%S', M2.date,'localtime') as date,T.'group',T.'label' from topics as T inner join mqtt as M on M.topic = T.topic
inner join prevprevprevmessages as M2 on M2.date = M.date
)
select T.topic, T.message, T.date,P.message as pmessage, P.date as pdate,PP.message as ppmessage, PP.date as ppdate,PPP.message as pppmessage, PPP.date as pppdate,T.'group',T.'label' from TDAY as T
left join PDAY P on P.topic = T.topic
left join PPDAY PP on PP.topic = T.topic
left join PPPDAY PPP on PPP.topic = T.topic
order by CAST(T.'order' AS INTEGER)
</sql><current_tab id="0"/></tab_sql></sqlb_project>