-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.lua
More file actions
1625 lines (1342 loc) · 54.5 KB
/
app.lua
File metadata and controls
1625 lines (1342 loc) · 54.5 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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local m_log,m_utils,m_tables,m_lib_file_parser,m_index_file,m_libgui, script_folder = ...
local M = {}
---- #########################################################################
---- # #
---- # License GPLv3: https://www.gnu.org/licenses/gpl-3.0.html #
---- # #
---- # This program is free software; you can redistribute it and/or modify #
---- # it under the terms of the GNU General Public License version 2 as #
---- # published by the Free Software Foundation. #
---- # #
---- # This program is distributed in the hope that it will be useful #
---- # but WITHOUT ANY WARRANTY; without even the implied warranty of #
---- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
---- # GNU General Public License for more details. #
---- # #
---- #########################################################################
-- This script display a log file as a graph
-- Original Author: Herman Kruisman (RealTadango) (original version: https://raw.githubusercontent.com/RealTadango/FrSky/master/OpenTX/LView/LView.lua)
-- Current Author: Offer Shmuely
-- Date: 2023
local app_ver = "1.17"
function M.getVer()
return app_ver
end
--m_log = require("LogViewer/lib_log")
--m_lib_file_parser = require("LogViewer/lib_file_parser")
--m_utils = require("LogViewer/lib_utils")
--m_tables = require("LogViewer/lib_tables")
--local m_index_file = require("LogViewer/lib_file_index")
--local m_libgui = require("LogViewer/libgui")
--function cache
local math_floor = math.floor
local math_fmod = math.fmod
local string_gmatch = string.gmatch
local string_gsub = string.gsub
local string_len = string.len
local string_sub = string.sub
local string_char = string.char
local string_byte = string.byte
local heap = 2048
local hFile
local min_log_sec_to_show = 60
-- read_and_index_file_list()
local log_file_list_raw = {}
local log_file_list_raw_idx = -1
local log_file_list_filtered = {}
local log_file_list_filtered2 = {}
local filter_model_name
local filter_model_name_idx = 1
local filter_date
local filter_date_idx = 1
local model_name_list = { "-- all --" }
local date_list = { "-- all --" }
local accuracy_list = { "1/1 (read every line)", "1/2 (every 2nd line)", "1/5 (every 5th line)", "1/10 (every 10th line)" }
local ddModel = nil
local ddLogFile = nil -- log-file dropDown object
local ddIndexType = nil
local INDEX_TYPE = {ALL=1, TODAY=2, LAST=3}
local index_type = INDEX_TYPE.ALL
local filename
local filename_idx = 1
local columns_by_header = {}
local columns_with_data = {}
local current_session = nil
local FIRST_VALID_COL = 2
-- state machine
local STATE = {
SPLASH = 0,
SELECT_INDEX_TYPE_INIT = 1,
SELECT_INDEX_TYPE = 2,
INDEX_FILES_INIT = 3,
INDEX_FILES = 4,
SELECT_FILE_INIT = 5,
SELECT_FILE = 6,
SELECT_SENSORS_INIT = 7,
SELECT_SENSORS = 8,
READ_FILE_DATA = 9,
PARSE_DATA = 10,
SHOW_GRAPH = 11
}
local state = STATE.SPLASH
--Graph data
local _values = {}
local _points = {}
local conversionSensorId = 0
local conversionSensorProgress = 0
--File reading data
local valPos = 0
local skipLines = 0
local lines = 0
local index = 0
local buffer = ""
--local prevTotalSeconds = 0
--Option data
--local maxLines
local current_option = 1
local sensorSelection = {
{ y = 80, label = "Field 1", values = {}, idx = 1, colId = 0, min = 0 },
{ y = 105, label = "Field 2", values = {}, idx = 1, colId = 0, min = 0 },
{ y = 130, label = "Field 3", values = {}, idx = 1, colId = 0, min = 0 },
{ y = 155, label = "Field 4", values = {}, idx = 1, colId = 0, min = 0 }
}
local graphConfig = {
--x_start = 60,
x_start = 0,
--x_end = 420,
x_end = LCD_W,
y_start = 40,
y_end = LCD_H - 32,
scale_ratio = LCD_H < 300 and 200 or 250,
DEFAULT_CENTER_Y = (LCD_H - 32) / 2,
{ color = GREEN, valx = 20, valy = LCD_H - 23, minx = 5, miny = 220, maxx = 5, maxy = 30 },
{ color = RED, valx = 130, valy = LCD_H - 23, minx = 5, miny = 205, maxx = 5, maxy = 45 },
{ color = WHITE, valx = 250, valy = LCD_H - 23, minx = 5, miny = 190, maxx = 5, maxy = 60 },
{ color = BLUE, valx = 370, valy = LCD_H - 23, minx = 5, miny = 175, maxx = 5, maxy = 75 }
}
local xStep = (graphConfig.x_end - graphConfig.x_start) / 100
local cursor = 0
local GRAPH_MODE = {
CURSOR = 0,
ZOOM = 1,
SCROLL = 2,
GRAPH_MINMAX = 3
}
local graphMode = GRAPH_MODE.CURSOR
local graphStart = 0
local graphSize = 0
local graphTimeBase = 0
local graphMinMaxEditorIndex = 0
local img_bg1 = bitmap.open("/SCRIPTS/TOOLS/LogViewer/bg1.png")
local img_bg2 = bitmap.open("/SCRIPTS/TOOLS/LogViewer/bg2.png")
local img_bg3 = LCD_H == 320 and bitmap.open("/SCRIPTS/TOOLS/LogViewer/bg3-320.png") or bitmap.open("/SCRIPTS/TOOLS/LogViewer/bg3.png")
-- Instantiate a new GUI object
local ctx1 = m_libgui.newPanel()
local ctx2 = m_libgui.newPanel()
local ctx3 = m_libgui.newPanel()
local select_file_gui_init = false
---- #########################################################################
--------------------------------------------------------------
local function log(fmt, ...)
m_log.info(fmt, ...)
end
--------------------------------------------------------------
local function doubleDigits(value)
if value < 10 then
return "0" .. value
else
return value
end
end
local function toDuration1(totalSeconds)
local hours = math_floor(totalSeconds / 3600)
totalSeconds = totalSeconds - (hours * 3600)
local minutes = math_floor(totalSeconds / 60)
local seconds = totalSeconds - (minutes * 60)
return doubleDigits(hours) .. ":" .. doubleDigits(minutes) .. ":" .. doubleDigits(seconds);
end
local function collectData()
if hFile == nil then
buffer = ""
hFile = io.open("/LOGS/" .. filename, "r")
io.seek(hFile, current_session.startIndex)
index = current_session.startIndex
valPos = 0
lines = 0
log(string.format("current_session.total_lines: %d", current_session.total_lines))
_points = {}
_values = {}
for varIndex = 1, 4, 1 do
if sensorSelection[varIndex].idx >= FIRST_VALID_COL then
_points[varIndex] = {}
_values[varIndex] = {}
end
end
end
local read = io.read(hFile, heap)
if read == "" then
io.close(hFile)
hFile = nil
return true
end
buffer = buffer .. read
local i = 0
for line in string_gmatch(buffer, "([^\n]+)\n") do
if math.fmod(lines, skipLines) == 0 then
local vals = m_utils.split(line)
--log(string.format("collectData: 1: %s, 2: %s, 3: %s, 4: %s, line: %s", vals[1], vals[2], vals[3], vals[4], line))
for varIndex = 1, 4, 1 do
if sensorSelection[varIndex].idx >= FIRST_VALID_COL then
local colId = sensorSelection[varIndex].colId
--log(string.format("collectData: varIndex: %d, sensorSelectionId: %d, colId: %d, val: %d", varIndex, sensorSelection[varIndex].colId, colId, vals[colId]))
_values[varIndex][valPos] = vals[colId]
end
end
valPos = valPos + 1
end
lines = lines + 1
if lines > current_session.total_lines then
io.close(hFile)
hFile = nil
return true
end
i = i + string.len(line) + 1 --dont forget the newline ;)
end
buffer = string.sub(buffer, i + 1) --dont forget the newline ;
index = index + heap
io.seek(hFile, index)
return false
end
-- ---------------------------------------------------------------------------------------------------------
local function compare_dates_inc(a, b)
return a < b
end
local function compare_dates_dec(a, b)
return a < b
end
local function compare_names(a, b)
return a < b
end
local function drawProgress(x, y, current, total)
--log(string.format("drawProgress(%d. %d, %d)", y, current, total))
--local x = 140
local pct = (total>0) and (current / total) or 0
lcd.drawFilledRectangle(x + 1, y + 1, (470 - x - 2) * pct, 14, TEXT_INVERTED_BGCOLOR)
lcd.drawRectangle(x, y, 470 - x, 16, COLOR_THEME_SECONDARY1)
end
local function get_log_files_list()
-- find latest log and latest day
local last_day = "1970-01-01"
local on_disk_date_list = {}
local last_log_day_time = "1970-01-01-00-00-00"
for fn in dir("/LOGS") do
local modelName, year, month, day, hour, min, sec, m, d, y = string.match(fn, "^(.*)-(%d+)-(%d+)-(%d+)-(%d%d)(%d%d)(%d%d).csv$")
if year~=nil and month~=nil and day~=nil then
local log_day = string.format("%s-%s-%s", year, month, day)
local log_day_time = string.format("%s-%s-%s-%s-%s-%s", year, month, day, hour, min, sec)
--log("log_day: %s", log_day)
if log_day > last_day then
last_day = log_day
--log("last_day: %s", last_day)
end
if log_day_time > last_log_day_time then
last_log_day_time = log_day_time
--log("last_log: %s", last_log)
end
m_tables.list_ordered_insert(on_disk_date_list, log_day, compare_dates_inc, 2)
--m_tables.table_print("on_disk_date_list", on_disk_date_list)
end
end
log("latest day: %s", last_day)
log("last_log: %s", last_log_day_time)
local log_files_list_all = {}
local log_files_list_today = {}
local log_files_list_latest = {}
for fn in dir("/LOGS") do
--log("fn: %s", fn)
local modelName, year, month, day, hour, min, sec, m, d, y = string.match(fn, "^(.*)-(%d+)-(%d+)-(%d+)-(%d%d)(%d%d)(%d%d).csv$")
local log_day = string.format("%s-%s-%s", year, month, day)
local log_day_time = string.format("%s-%s-%s-%s-%s-%s", year, month, day, hour, min, sec)
log_files_list_all[#log_files_list_all+1] = fn
if log_day==last_day then
log_files_list_today[#log_files_list_today+1] = fn
end
if log_day_time==last_log_day_time then
log_files_list_latest[#log_files_list_latest+1] = fn
end
end
--m_tables.table_print("log_files_list_all", log_files_list_all)
m_tables.table_print("log_files_list_today", log_files_list_today)
m_tables.table_print("log_files_list_latest", log_files_list_latest)
if index_type == INDEX_TYPE.ALL then
log("using files for index of type ALL")
return log_files_list_all
elseif index_type == INDEX_TYPE.TODAY then
log("using files for index of type TODAY")
return log_files_list_today
elseif index_type == INDEX_TYPE.LAST then
log("using files for index of type LAST")
return log_files_list_latest
end
log("internal error, unknown index_type: %s", index_type)
return nil
end
-- read log file list
local function read_and_index_file_list()
--log("read_and_index_file_list(%d, %d)", log_file_list_raw_idx, #log_file_list_raw)
if (#log_file_list_raw == 0) then
log("read_and_index_file_list: init")
m_index_file.indexInit()
--log_file_list_raw = dir("/LOGS")
--for fn in dir("/LOGS") do
-- --m_tables.table_print("log_file_list_raw", log_file_list_raw)
-- log("fn: %s", fn)
--
-- -- format: year (16) / month (8) / day (8) / hour (8) / min (8) / sec (8)
-- local now = getDateTime()
-- --local year = now.year
--
-- local modelName, year, month, day, hour, min, sec, m, d, y = string.match(fn, "^(.*)-(%d+)-(%d+)-(%d+)-(%d%d)(%d%d)(%d%d).csv$")
-- log("is_file_needed_by_index_type: %s-%s %s", year, now.year, fn)
--
-- log_file_list_raw[#log_file_list_raw + 1] = fn
--end
log_file_list_raw = get_log_files_list()
log_file_list_raw_idx = 0
--m_tables.table_print("log_file_list_raw", log_file_list_raw)
m_index_file.indexRead(log_file_list_raw)
end
for i = 1, 10, 1 do
log_file_list_raw_idx = log_file_list_raw_idx + 1
local filename = log_file_list_raw[log_file_list_raw_idx]
if filename ~= nil then
-- draw top-bar
lcd.clear()
lcd.drawFilledRectangle(0, 0, LCD_W, 20, TITLE_BGCOLOR)
lcd.drawBitmap(img_bg2, 0, 0)
lcd.drawText(440, 1, "v" .. app_ver, WHITE + SMLSIZE)
-- draw state
lcd.drawText(5, 30, "Analyzing & indexing files", COLOR_THEME_SECONDARY1 + BOLD)
lcd.drawText(5, 60, string.format("indexing files: (%d/%d)", log_file_list_raw_idx, #log_file_list_raw), COLOR_THEME_SECONDARY1 + SMLSIZE)
lcd.drawText(5, 90, string.format("* %s", filename), COLOR_THEME_SECONDARY1 + SMLSIZE)
lcd.drawText(30, 1, "/LOGS/" .. filename, WHITE + SMLSIZE)
drawProgress(160, 60, log_file_list_raw_idx, #log_file_list_raw)
log("log file: (%d/%d) %s (detecting...)", log_file_list_raw_idx, #log_file_list_raw, filename)
local modelName, year, month, day, hour, min, sec, m, d, y = string.match(filename, "^(.*)-(%d+)-(%d+)-(%d+)-(%d%d)(%d%d)(%d%d).csv$")
if modelName == nil then
goto continue
end
--log("log file: %s (is csv)", fileName)
local model_day = string.format("%s-%s-%s", year, month, day)
-- read file
local is_new, start_time, end_time, total_seconds, total_lines, start_index, col_with_data_str, all_col_str = m_index_file.getFileDataInfo(filename)
--log("read_and_index_file_list: total_lines: %s, total_seconds: %s, col_with_data_str: [%s], all_col_str: [%s]", total_lines, total_seconds, col_with_data_str, all_col_str)
log("read_and_index_file_list: total_seconds: %s", total_seconds)
m_tables.list_ordered_insert(model_name_list, modelName, compare_names, 2)
m_tables.list_ordered_insert(date_list, model_day, compare_dates_inc, 2)
-- due to cpu load, early exit
if is_new then
return false
end
end
if log_file_list_raw_idx >= #log_file_list_raw then
return true
end
:: continue ::
end
return false
end
local function onLogFileChange(obj)
--m_tables.table_print("log_file_list_filtered", log_file_list_filtered)
local i = obj.getSelected()
filename = log_file_list_filtered[i]
log("Selected file index: %d", i)
log("Selected file: %s", log_file_list_filtered[i])
filename_idx = i
--log("filename: " .. filename)
end
local function onAccuracyChange(obj)
local i = obj.selected1
local accuracy = i
log("Selected accuracy: %s (%d)", accuracy_list[i], i)
if accuracy == 4 then
skipLines = 10
heap = 2048 * 16
elseif accuracy == 3 then
skipLines = 5
heap = 2048 * 16
elseif accuracy == 2 then
skipLines = 2
heap = 2048 * 8
else
skipLines = 1
heap = 2048 * 4
end
end
local function filter_log_file_list(filter_model_name, filter_date, need_update)
log("need to filter by: [%s] [%s] [%s]", filter_model_name, filter_date, need_update)
m_tables.table_clear(log_file_list_filtered)
local log_files_index_info = m_index_file.getFileListDec()
for i = 1, #log_files_index_info do
local log_file_info = log_files_index_info[i]
--log("filter_log_file_list: %d. %s", i, log_file_info.file_name)
local modelName, year, month, day, hour, min, sec, m, d, y = string.match(log_file_info.file_name, "^(.*)-(%d+)-(%d+)-(%d+)-(%d%d)(%d%d)(%d%d).csv$")
local is_model_name_ok
if filter_model_name == nil or string.sub(filter_model_name, 1, 2) == "--" then
is_model_name_ok = true
else
is_model_name_ok = (modelName == filter_model_name)
end
local is_date_ok
if filter_date == nil or string.sub(filter_date, 1, 2) == "--" then
is_date_ok = true
else
local model_day = string.format("%s-%s-%s", year, month, day)
is_date_ok = (model_day == filter_date)
end
local is_duration_ok = true
if log_file_info.total_seconds < min_log_sec_to_show then
is_duration_ok = false
end
local is_have_data_ok = true
if log_file_info.col_with_data_str == nil or log_file_info.col_with_data_str == "" then
is_have_data_ok = false
end
if is_model_name_ok and is_date_ok and is_duration_ok and is_have_data_ok then
--log("filter_log_file_list: [%s] - OK (%s,%s)", log_file_info.file_name, filter_model_name, filter_date)
table.insert(log_file_list_filtered, log_file_info.file_name)
else
--log("filter_log_file_list: [%s] - FILTERED-OUT (filters:%s,%s) (model_name_ok:%s,date_ok:%s,duration_ok:%s,have_data_ok:%s)", log_file_info.file_name, filter_model_name, filter_date, is_model_name_ok, is_date_ok, is_duration_ok, is_have_data_ok)
end
end
m_tables.table_clear(log_file_list_filtered2)
if #log_file_list_filtered == 0 then
table.insert(log_file_list_filtered, "not found")
table.insert(log_file_list_filtered2, "not found")
else
-- prepare list with friendly names
for i=1, #log_file_list_filtered do
-- get duration
local is_new, start_time, end_time, total_seconds, total_lines, start_index, col_with_data_str, all_col_str = m_index_file.getFileDataInfo(log_file_list_filtered[i])
log_file_list_filtered2[#log_file_list_filtered2 +1] = string.format("%s (%.0fmin)", log_file_list_filtered[i], total_seconds/60)
end
--m_tables.table_print("prepare friendly names", log_file_list_filtered2)
end
--m_tables.table_print("filter_log_file_list", log_file_list_filtered)
-- update the log combo to first
if need_update == true then
onLogFileChange(ddLogFile)
ddLogFile.selected1 = 1
end
end
local splash_start_time = 0
local function state_SPLASH(event, touchState)
if splash_start_time == 0 then
splash_start_time = getTime()
end
local elapsed = getTime() - splash_start_time;
--log('elapsed: %d (t.durationMili: %d)', elapsed, splash_start_time)
local elapsedMili = elapsed * 10;
-- was 1500, but most the time will go anyway from the load of the scripts
if (elapsedMili >= 500) then
state = STATE.SELECT_INDEX_TYPE_INIT
end
return 0
end
local function onButtonIndexTypeAll()
log("onButtonIndexTypeAll")
index_type = INDEX_TYPE.ALL
state = STATE.INDEX_FILES_INIT
end
local function onButtonIndexTypeToday()
log("onButtonIndexTypeToday")
index_type = INDEX_TYPE.TODAY
state = STATE.INDEX_FILES_INIT
end
local function onButtonIndexTypeLastFlight()
log("onButtonIndexTypeLastFlight")
index_type = INDEX_TYPE.LAST
state = STATE.INDEX_FILES_INIT
end
local function state_SELECT_INDEX_TYPE_init(event, touchState)
log("state_SELECT_INDEX_TYPE_init()")
log("creating new window gui")
ctx3.newControl.ctl_label(ctx3, nil, {x=40, y=30, w=70, h=24, text="Indexing selection:"}, ctx3.FONT_SIZES.FONT_8 + COLOR_THEME_SECONDARY1)
ctx3.newControl.ctl_button(ctx3, nil, {x=90, y= 60, w=320, h=55, text="Only last flight (fast)", onPress=onButtonIndexTypeLastFlight})
ctx3.newControl.ctl_button(ctx3, nil, {x=90, y=130, w=320, h=55, text="Last flights day", onPress=onButtonIndexTypeToday})
ctx3.newControl.ctl_button(ctx3, nil, {x=90, y=200, w=320, h=55, text="All flights (slow)", onPress=onButtonIndexTypeAll})
-- default is ALL
--index_type = INDEX_TYPE.ALL
--index_type = INDEX_TYPE.TODAY
index_type = INDEX_TYPE.LAST
log_file_list_raw = {}
state = STATE.SELECT_INDEX_TYPE
return 0
end
local function state_SELECT_INDEX_TYPE_refresh(event, touchState)
if event == EVT_VIRTUAL_NEXT_PAGE then
state = STATE.INDEX_FILES_INIT
return 0
end
lcd.drawText(30, 1, "Indexing type for new logs", WHITE + SMLSIZE)
ctx3.run(event, touchState)
return 0
end
local function state_INDEX_FILES_INIT(event, touchState)
log("state_INDEX_FILES_INIT()")
state = STATE.INDEX_FILES
return 0
end
local function state_INDEX_FILES(event, touchState)
if event == EVT_VIRTUAL_EXIT or event == EVT_VIRTUAL_PREV_PAGE then
state = STATE.SELECT_INDEX_TYPE
return 0
end
-- start init
local is_done = read_and_index_file_list()
collectgarbage("collect")
if (is_done == true) then
state = STATE.SELECT_FILE_INIT
end
return 0
end
local function state_SELECT_FILE_init(event, touchState)
m_tables.table_clear(log_file_list_filtered)
filter_log_file_list(nil, nil, false)
if select_file_gui_init == false then
select_file_gui_init = true
-- creating new window gui
log("creating new window gui")
--ctx1 = libGUI.newGUI()
ctx1.newControl.ctl_label(ctx1, nil, {x=20, y=25, text="log file..."}, COLOR_THEME_SECONDARY1 + BOLD)
--log("setting model filter...")
ctx1.newControl.ctl_label(ctx1, nil, {x=20, y=55, text="Model"})
ddModel = ctx1.newControl.ctl_dropdown(ctx1, nil, {x=90, y=55, w=380, h=24,
items=model_name_list, selected=1,
callback=function(obj)
local i = obj.selected1
filter_model_name = model_name_list[i]
filter_model_name_idx = i
log("Selected model-name: " .. filter_model_name)
filter_log_file_list(filter_model_name, filter_date, true)
end
})
--log("setting date filter...")
ctx1.newControl.ctl_label(ctx1, nil, {x=20, y=90, text="Date"})
ctx1.newControl.ctl_dropdown(ctx1, nil, {x=90, y=90, w=380, h=24,
items=date_list, selected=1,
callback=function(obj)
local i = obj.selected1
filter_date = date_list[i]
filter_date_idx = i
log("Selected filter_date: " .. filter_date)
filter_log_file_list(filter_model_name, filter_date, true)
end
})
log("setting file combo...")
ctx1.newControl.ctl_label(ctx1, nil, {x=20, y=125, text="Log file"})
ddLogFile = ctx1.newControl.ctl_dropdown(ctx1, nil, {x=90,y=125,w=380,h=24,
items=log_file_list_filtered2, selected=filename_idx,
callback=onLogFileChange
})
onLogFileChange(ddLogFile)
ctx1.newControl.ctl_label(ctx1, nil, {x=20, y=160, text="Accuracy"})
dd4 = ctx1.newControl.ctl_dropdown(ctx1, nil, {x=90, y=160, w=380, h=24, items=accuracy_list, selected=1, callback=onAccuracyChange})
onAccuracyChange(dd4)
end
--filter_model_name_i
ddModel.selected1 = filter_model_name_idx
--filter_date_i
--ddLogFile.selected1 = filename_idx
filter_log_file_list(filter_model_name, filter_date, true)
ddLogFile.selected1 = filename_idx
state = STATE.SELECT_FILE
return 0
end
local function state_SELECT_FILE_refresh(event, touchState)
-- ## file selected
if event == EVT_VIRTUAL_NEXT_PAGE or index_type == INDEX_TYPE.LAST then
log("state_SELECT_FILE_refresh --> EVT_VIRTUAL_NEXT_PAGE: filename: %s", filename)
if filename == "not found" then
m_log.warn("state_SELECT_FILE_refresh: trying to next-page, but no logfile available, ignoring.")
return 0
end
--Reset file load data
log("Reset file load data")
buffer = ""
lines = 0
heap = 2048 * 12
--prevTotalSeconds = 0
local is_new, start_time, end_time, total_seconds, total_lines, start_index, col_with_data_str, all_col_str = m_index_file.getFileDataInfo(filename)
current_session = {
startTime = start_time,
endTime = end_time,
total_seconds = total_seconds,
total_lines = total_lines,
startIndex = start_index,
col_with_data_str = col_with_data_str,
all_col_str = all_col_str
}
-- update columns
local columns_temp, cnt = m_utils.split_pipe(col_with_data_str)
log("state_SELECT_FILE_refresh: #col: %d", cnt)
m_tables.table_clear(columns_with_data)
columns_with_data[1] = "---"
for i = 1, #columns_temp, 1 do
local col = columns_temp[i]
if m_utils.trim_safe(col) ~= "" then
columns_with_data[#columns_with_data + 1] = col
log("state_SELECT_FILE_refresh: col: [%s]", col)
end
end
--log("state_SELECT_FILE_refresh: #columns_with_data: %d", #columns_with_data)
--for i = #columns_temp, 4, 1 do
-- columns_with_data[#columns_with_data + 1] = "---"
-- log("state_SELECT_FILE_refresh: add empty field: %d", i)
--end
--m_tables.table_print("state_SELECT_FILE_refresh columns_with_data", columns_with_data)
local columns_temp, cnt = m_utils.split_pipe(all_col_str)
log("state_SELECT_FILE_refresh: #col: %d", cnt)
m_tables.table_clear(columns_by_header)
for i = 1, #columns_temp, 1 do
local col = columns_temp[i]
columns_by_header[#columns_by_header + 1] = col
-- log("state_SELECT_FILE_refresh: col: %s", col)
end
state = STATE.SELECT_SENSORS_INIT
return 0
end
ctx1.run(event, touchState)
return 0
end
local function colWithData2ColByHeader(colWithDataId)
local sensorName = columns_with_data[colWithDataId]
local colByHeaderId = 0
log("colWithData2ColByHeader: byData - idx: %d, name: %s", colWithDataId, sensorName)
log("#columns_by_header: %d", #columns_by_header)
for i = 1, #columns_by_header do
if columns_by_header[i] == sensorName then
colByHeaderId = i
log("colWithData2ColByHeader: byHeader - colId: %d, name: %s", colByHeaderId, columns_by_header[colByHeaderId])
return colByHeaderId
end
end
return -1
end
local function select_sensors_preset_first_4()
if sensorSelection[1].idx ~= 1 or sensorSelection[2].idx ~= 1 or sensorSelection[3].idx ~= 1 or sensorSelection[4].idx ~= 1 then
return -- keep the last selection
end
------------------------------------
-----------------------------------
-----------------------------------
local info = model.getInfo()
if info and info.name then
modelName = info.name
end
local file = io.open(script_folder .. "/CONFIG/" .. modelName .. ".txt", "r")
log("Reading model config file %s", script_folder .. "/CONFIG/" .. modelName .. ".txt")
local indexes = { 2,3,4,5 }
if not file then
log("Model config file not found, trying global file %s", script_folder .. "/CONFIG/all.txt")
file = io.open(script_folder .. "/CONFIG/all.txt", "r")
end
if file then
local configFileContent = ""
while true do
local data = io.read(file, 20)
if #data == 0 then
break
end
configFileContent = configFileContent .. data
end
io.close(file)
log("Configuration from file %s", configFileContent)
local newValues = {}
for value in string.gmatch(configFileContent, "[^,]+") do
table.insert(newValues, tonumber(value))
end
for i = 1, math.min(#indexes, #newValues) do
indexes[i] = newValues[i]
end
else
log("No config file found")
end
-----------------------------------
-----------------------------------
-----------------------------------
for i = 1, 4, 1 do
if i < #columns_with_data then
sensorSelection[i].idx = indexes[i]
log("%d. sensors is: %s", i, columns_with_data[i])
sensorSelection[i].values[i - 1] = columns_with_data[i]
else
sensorSelection[i].idx = 1
sensorSelection[i].values[0] = "---"
end
log("state_SELECT_SENSORS_INIT %d. <= %d (%d)", i , sensorSelection[i].idx, #columns_with_data)
end
end
local function state_SELECT_SENSORS_INIT(event, touchState)
log("state_SELECT_SENSORS_INIT")
m_tables.table_print("sensors-init columns_with_data", columns_with_data)
-- select default sensor
select_sensors_preset_first_4()
m_tables.table_print("sensors-init columns_with_data", columns_with_data)
current_option = 1
-- creating new window gui
log("creating new window gui")
ctx2 = nil
ctx2 = m_libgui.newPanel()
ctx2.newControl.ctl_label(ctx2, nil, {x=10, y=25, w=120, h=24, text="Select sensors..."}, BOLD)
log("setting field1...")
ctx2.newControl.ctl_label(ctx2, nil, {x=10, y=55, w=60, h=24, text="Field 1"})
ctx2.newControl.ctl_dropdown(ctx2, nil, {x=90, y=55, w=380, h=24,
items=columns_with_data, selected=sensorSelection[1].idx,
callback=function(obj)
local i = obj.selected1
local var1 = columns_with_data[i]
log("Selected var1: " .. var1)
sensorSelection[1].idx = i
sensorSelection[1].colId = colWithData2ColByHeader(i)
end
})
ctx2.newControl.ctl_label(ctx2, nil, {x=10, y=90, w=60, h=24, text="Field 2"})
ctx2.newControl.ctl_dropdown(ctx2, nil, {x=90, y=90, w=380, h=24,
items=columns_with_data, selected=sensorSelection[2].idx,
callback=function(obj)
local i = obj.selected1
local var2 = columns_with_data[i]
log("Selected var2: " .. var2)
sensorSelection[2].idx = i
sensorSelection[2].colId = colWithData2ColByHeader(i)
end
})
ctx2.newControl.ctl_label(ctx2, nil, {x=10, y=125, w=60, h=24, text="Field 3"})
ctx2.newControl.ctl_dropdown(ctx2, nil, {x=90, y=125, w=380, h=24,
items=columns_with_data, selected=sensorSelection[3].idx,
callback=function(obj)
local i = obj.selected1
local var3 = columns_with_data[i]
log("Selected var3: " .. var3)
sensorSelection[3].idx = i
sensorSelection[3].colId = colWithData2ColByHeader(i)
end
})
ctx2.newControl.ctl_label(ctx2, nil, {x=10, y=160, w=60, h=24, text="Field 4"})
ctx2.newControl.ctl_dropdown(ctx2, nil, {x=90, y=160, w=380, h=24,
items=columns_with_data, selected=sensorSelection[4].idx,
callback=function(obj)
local i = obj.selected1
local var4 = columns_with_data[i]
log("Selected var4: " .. var4)
sensorSelection[4].idx = i
sensorSelection[4].colId = colWithData2ColByHeader(i)
end
})
sensorSelection[1].colId = colWithData2ColByHeader(sensorSelection[1].idx)
sensorSelection[2].colId = colWithData2ColByHeader(sensorSelection[2].idx)
sensorSelection[3].colId = colWithData2ColByHeader(sensorSelection[3].idx)
sensorSelection[4].colId = colWithData2ColByHeader(sensorSelection[4].idx)
state = STATE.SELECT_SENSORS
return 0
end
local function state_SELECT_SENSORS_refresh(event, touchState)
if event == EVT_VIRTUAL_EXIT or event == EVT_VIRTUAL_PREV_PAGE then
state = STATE.SELECT_FILE_INIT
return 0
elseif event == EVT_VIRTUAL_NEXT_PAGE then
state = STATE.READ_FILE_DATA
return 0
end
ctx2.run(event, touchState)
return 0
end
local function display_read_data_progress(conversionSensorId, conversionSensorProgress)
--log("display_read_data_progress(%d, %d)", conversionSensorId, conversionSensorProgress)
lcd.drawText(5, 25, "Reading data from file...", COLOR_THEME_SECONDARY1)
lcd.drawText(5, 60, "Reading line: " .. lines, COLOR_THEME_SECONDARY1)
drawProgress(140, 60, lines, current_session.total_lines)
local done_var_1 = 0
local done_var_2 = 0
local done_var_3 = 0
local done_var_4 = 0
if conversionSensorId == 1 then
done_var_1 = conversionSensorProgress
end
if conversionSensorId == 2 then
done_var_1 = valPos
done_var_2 = conversionSensorProgress
end
if conversionSensorId == 3 then
done_var_1 = valPos
done_var_2 = valPos
done_var_3 = conversionSensorProgress
end
if conversionSensorId == 4 then
done_var_1 = valPos
done_var_2 = valPos
done_var_3 = valPos
done_var_4 = conversionSensorProgress
end
local y = 85
local dy = 25
lcd.drawText(5, y, "Parsing Field 1: ", COLOR_THEME_SECONDARY1)
drawProgress(140, y, done_var_1, valPos)
y = y + dy
lcd.drawText(5, y, "Parsing Field 2: ", COLOR_THEME_SECONDARY1)
drawProgress(140, y, done_var_2, valPos)
y = y + dy
lcd.drawText(5, y, "Parsing Field 3: ", COLOR_THEME_SECONDARY1)
drawProgress(140, y, done_var_3, valPos)
y = y + dy
lcd.drawText(5, y, "Parsing Field 4: ", COLOR_THEME_SECONDARY1)
drawProgress(140, y, done_var_4, valPos)
end
local function state_READ_FILE_DATA_refresh(event, touchState)
if event == EVT_VIRTUAL_EXIT or event == EVT_VIRTUAL_PREV_PAGE then
state = STATE.SELECT_SENSORS_INIT
return 0
end
display_read_data_progress(0, 0)
local is_done = collectData()
if is_done then
conversionSensorId = 0
state = STATE.PARSE_DATA
end
return 0
end
local function state_PARSE_DATA_refresh(event, touchState)
if event == EVT_VIRTUAL_EXIT then
return 2
elseif event == EVT_VIRTUAL_EXIT or event == EVT_VIRTUAL_PREV_PAGE then
state = STATE.SELECT_SENSORS_INIT
return 0
end
display_read_data_progress(conversionSensorId, conversionSensorProgress)
local cnt = 0
-- prepare
if conversionSensorId == 0 then
conversionSensorId = 1
conversionSensorProgress = 0
local fileTime = m_lib_file_parser.getTotalSeconds(current_session.endTime) - m_lib_file_parser.getTotalSeconds(current_session.startTime)