From 3e2a27fafcf2c49fc50872413cb7c9aba1adb754 Mon Sep 17 00:00:00 2001 From: Offer Shmuely Date: Mon, 31 Jul 2023 03:11:29 +0300 Subject: [PATCH] feat: FlightsHistory version 1.1 (#112) * new Rotary Gauge widget * Rotary Gauge widget: less ticks on small size * new Rotary Gauge widget * new Rotary Gauge widget * FlightsHistory: reverse order of flight (last flight on top) * FlightsHistory: identention of flight number (since do not have yet gui control to present tables) * FlightsHistory: add missing file --- .../c480x272/SCRIPTS/TOOLS/FlightsHistory.lua | 128 +----------------- .../TOOLS/FlightsHistory/FlightsHistory3.lua | 23 ++-- .../FlightsHistory/lib_history_index.lua | 6 +- .../TOOLS/FlightsHistory/lib_tables.lua | 16 +-- .../SCRIPTS/TOOLS/FlightsHistory/main.lua | 125 +++++++++++++++++ 5 files changed, 152 insertions(+), 146 deletions(-) create mode 100644 sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/main.lua diff --git a/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory.lua b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory.lua index 461fc4bb..c1c36915 100644 --- a/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory.lua +++ b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory.lua @@ -1,127 +1,7 @@ --- TNS|Flights History v1.0|TNE +-- TNS|Flights History v1.1|TNE ----- ######################################################################### ----- # # ----- # 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 the flights history as kept using the "Flights" widget --- Author: Offer Shmuely --- Date: 2023 -local ver = "1.0" - --- to get help: --- change in lib_log.lua to "ENABLE_LOG_FILE=true" --- change in lib_log.lua to "ENABLE_LOG_TO_FILE= false" --- run the script ... --- send me the log file that will be created on: /SCRIPTS/TOOLS/FlightsHistory/app.log - -local app_name = "FlightsHistory" - -local m_log = nil -local m_utils = nil -local m_tables = nil -local m_index_file = nil -local m_libgui = nil -local m_log_viewer3 = nil - - -local error_desc = nil -local script_folder = "/SCRIPTS/TOOLS/FlightsHistory/" - -local function validate_image(file_name) - local img1 = Bitmap.open(script_folder .. file_name) - local w, h = Bitmap.getSize(img1) - if w == 0 and h == 0 then - error_desc = "File not found: " .. script_folder .. file_name - end - img1 = nil - - collectgarbage("collect") -end - -local function validate_script(file_name, expected_ver, ...) - -- validate module exist - local my_loading_flag = "tcd" - local code_chunk = loadScript(script_folder .. file_name, my_loading_flag) - if code_chunk == nil then - error_desc = "File not found: " .. script_folder .. file_name - return - end - - print(string.format("%s - loading, num args: %d", file_name, #{...})) - local m = code_chunk(...) - print(string.format("%s - loaded OK", file_name)) - if expected_ver == nil then - return m -- file exist, no specific version needed - end - - local the_ver = m.getVer() - print("the_ver: " .. the_ver) - if the_ver ~= expected_ver then - error_desc = "incorrect version of file:\n " .. script_folder .. file_name .. ".lua \n (" .. the_ver .. " <> " .. expected_ver .. ")" - return nil - end - return m - --collectgarbage("collect") -end - -local function validate_files() - m_log = validate_script("lib_log", nil, app_name, "/SCRIPTS/TOOLS/" .. app_name) - if error_desc ~= nil then return end - m_log.info("loaded") - - m_utils = validate_script("lib_utils", nil, m_log, app_name) - if error_desc ~= nil then return end - - m_tables = validate_script("lib_tables", nil, m_log, app_name) - if error_desc ~= nil then return end - - m_index_file = validate_script("lib_history_index", nil, m_log, app_name, m_utils, m_tables) - if error_desc ~= nil then return end - - m_libgui = validate_script("libgui", "1.0.2") - if error_desc ~= nil then return end - - m_log_viewer3 = validate_script("FlightsHistory3", ver, m_log, m_utils,m_tables,m_index_file,m_libgui) - if error_desc ~= nil then return end - - - validate_image("bg1.png") - if error_desc ~= nil then return end - - validate_image("bg2.png") - if error_desc ~= nil then return end -end - -local function init() - validate_files() - if error_desc ~= nil then return end - - return m_log_viewer3.init() -end - -local function run(event, touchState) - -- display if in error mode - if error_desc ~= nil then - print(error_desc) - lcd.clear() - lcd.drawText(5, 30, "Error:", TEXT_COLOR + BOLD) - lcd.drawText(5, 60, error_desc, TEXT_COLOR + BOLD) - return 0 - end - - return m_log_viewer3.run(event, touchState) +local function run() + return "/SCRIPTS/TOOLS/FlightsHistory/main.lua" end -return { init = init, run = run } +return { run = run } diff --git a/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/FlightsHistory3.lua b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/FlightsHistory3.lua index a45e7c20..646a5447 100644 --- a/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/FlightsHistory3.lua +++ b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/FlightsHistory3.lua @@ -1,4 +1,4 @@ -local m_log1,m_utils,m_tables,m_index_file,m_libgui = ... +local m_log,m_utils,m_tables,m_index_file,m_libgui = ... local M = {} @@ -21,17 +21,17 @@ local M = {} -- 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 ver = "1.0" +local ver = "1.1" function M.getVer() return ver end -local m_log2 = require("FlightsHistory/lib_log") -local m_log = m_log2 +--local m_log2 = require("lib_log") +--local m_log = m_log2 local _, rv = getVersion() if string.sub(rv, -5) ~= "-simu" then - m_log = m_log1 + --m_log = m_log1 --m_lib_file_parser = require("FlightsHistory/lib_file_parser") --m_utils = require("FlightsHistory/lib_utils") --m_tables = require("FlightsHistory/lib_tables") @@ -89,6 +89,12 @@ local function compare_names(a, b) return a < b end +local function compare_date_first(a1, b1) + local a = string.sub(a1, 0, 16) + local b = string.sub(b1, 0, 16) + return (a > b) +end + -- read log file list local function read_history_file() if (#log_file_list_raw > 0) then @@ -141,7 +147,7 @@ local function filter_log_file_list(filter_model_name, need_update) if is_model_name_ok then log("filter_log_file_list: [%s] - OK (%s)", log_file_info.file_name, filter_model_name) - table.insert(log_file_list_filtered, log_file_info.flight_date) + m_tables.list_ordered_insert(log_file_list_filtered, log_file_info.flight_date, compare_date_first, 1) else log("filter_log_file_list: [%s] - FILTERED-OUT (filters:%s) (model_name_ok:%s)", log_file_info.file_name, filter_model_name, is_model_name_ok) end @@ -161,6 +167,7 @@ local function filter_log_file_list(filter_model_name, need_update) -- get duration local f_info = m_index_file.getFileDataInfo(log_file_list_filtered[i]) log_file_list_filtered2[#log_file_list_filtered2 +1] = f_info.desc + m_tables.list_ordered_insert(log_file_list_filtered2, f_info.desc, compare_date_first, 1) end --m_tables.table_print("prepare friendly names", log_file_list_filtered2) end @@ -193,8 +200,8 @@ local function calculate_model_summary_list() m_tables.table_clear(model_summary_list) for k, v in pairs(model_flight_count) do - local inf = string.format("%-17s - %d flights", k, v) - --local inf = string.format("%d - %s", v, k) + --local inf = string.format("%-17s - %d flights", k, v) + local inf = string.format("%03d - %s", v, k) log("model_flight_count: %s", inf) model_summary_list[#model_summary_list +1] = inf end diff --git a/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/lib_history_index.lua b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/lib_history_index.lua index 83b2679b..bc264356 100644 --- a/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/lib_history_index.lua +++ b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/lib_history_index.lua @@ -6,11 +6,7 @@ M.app_name = app_name M.m_tables = m_tables M.m_utils = m_utils ---local m_tables = require("./FlightsViewer/utils_table") ---local m_log = require("./FlightsViewer/lib_log") ---local m_utils = require("FlightsViewer/utils") - -M.idx_file_name = "/app_data/flights/flights-history.csv" +--M.idx_file_name = "/app_data/flights/flights-history.csv" M.idx_file_name = "/flights-history.csv" M.log_files_index_info = {} diff --git a/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/lib_tables.lua b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/lib_tables.lua index 05842399..cbec3ef6 100644 --- a/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/lib_tables.lua +++ b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/lib_tables.lua @@ -4,7 +4,6 @@ local M = {} M.m_log = m_log M.app_name = app_name ---local m_log = require("./FlightsViewer/lib_log") function M.tprint(t, s) for k, v in pairs(t) do @@ -19,7 +18,7 @@ function M.tprint(t, s) if type(v) ~= 'string' then vfmt = tostring(v) end - print(type(t) .. (s or '') .. kfmt .. ' = ' .. vfmt) + M.m_log.info(type(t) .. (s or '') .. kfmt .. ' = ' .. vfmt) end end end @@ -32,8 +31,7 @@ function M.table_clear(tbl) end function M.table_print(prefix, tbl) - M.m_log.info("-------------") - M.m_log.info("table_print(%s)", prefix) + M.m_log.info(">>> table_print (%s)", prefix) for i = 1, #tbl, 1 do local val = tbl[i] if type(val) ~= "table" then @@ -47,7 +45,7 @@ function M.table_print(prefix, tbl) end end end - M.m_log.info("------------- table_print end") + M.m_log.info("<<< table_print end (%s) ", prefix) end function M.compare_file_names(a, b) @@ -65,19 +63,19 @@ function M.list_ordered_insert(lst, newVal, cmp, firstValAt) -- remove duplication --M.m_log.info("list_ordered_insert - %s ? %s", newVal, lst[i] ) if newVal == lst[i] then - --print_table("list_ordered_insert - duplicated", lst) + --M.table_print("list_ordered_insert - duplicated", lst) return end if cmp(newVal, lst[i]) == true then table.insert(lst, i, newVal) - --print_table("list_ordered_insert - inserted", lst) + --M.table_print("list_ordered_insert - inserted", lst) return end - --print_table("list_ordered_insert-loop", lst) + --M.table_print("list_ordered_insert-loop", lst) end table.insert(lst, newVal) - --print_table("list_ordered_insert-inserted-to-end", lst) + --M.table_print("list_ordered_insert-inserted-to-end", lst) end diff --git a/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/main.lua b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/main.lua new file mode 100644 index 00000000..6e91cfbd --- /dev/null +++ b/sdcard/c480x272/SCRIPTS/TOOLS/FlightsHistory/main.lua @@ -0,0 +1,125 @@ +---- ######################################################################### +---- # # +---- # 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 the flights history as kept using the "Flights" widget +-- Author: Offer Shmuely +-- Date: 2023 +local ver = "1.1" + +-- to get help: +-- change in lib_log.lua to "ENABLE_LOG_FILE=true" +-- change in lib_log.lua to "ENABLE_LOG_TO_FILE= false" +-- run the script ... +-- send me the log file that will be created on: /SCRIPTS/TOOLS/FlightsHistory/app.log + +local app_name = "FlightsHistory" + +local m_log = nil +local m_utils = nil +local m_tables = nil +local m_index_file = nil +local m_libgui = nil +local m_log_viewer3 = nil + + +local error_desc = nil +local script_folder = "/SCRIPTS/TOOLS/FlightsHistory/" + +local function validate_image(file_name) + local img1 = Bitmap.open(script_folder .. file_name) + local w, h = Bitmap.getSize(img1) + if w == 0 and h == 0 then + error_desc = "File not found: " .. script_folder .. file_name + end + img1 = nil + + collectgarbage("collect") +end + +local function validate_script(file_name, expected_ver, ...) + -- validate module exist + local my_loading_flag = "tcd" + local code_chunk = loadScript(script_folder .. file_name, my_loading_flag) + if code_chunk == nil then + error_desc = "File not found: " .. script_folder .. file_name + return + end + + print(string.format("%s - loading, num args: %d", file_name, #{...})) + local m = code_chunk(...) + print(string.format("%s - loaded OK", file_name)) + if expected_ver == nil then + return m -- file exist, no specific version needed + end + + local the_ver = m.getVer() + print("the_ver: " .. the_ver) + if the_ver ~= expected_ver then + error_desc = "incorrect version of file:\n " .. script_folder .. file_name .. ".lua \n (" .. the_ver .. " <> " .. expected_ver .. ")" + return nil + end + return m + --collectgarbage("collect") +end + +local function validate_files() + m_log = validate_script("lib_log", nil, app_name, "/SCRIPTS/TOOLS/" .. app_name) + if error_desc ~= nil then return end + m_log.info("loaded") + + m_utils = validate_script("lib_utils", nil, m_log, app_name) + if error_desc ~= nil then return end + + m_tables = validate_script("lib_tables", nil, m_log, app_name) + if error_desc ~= nil then return end + + m_index_file = validate_script("lib_history_index", nil, m_log, app_name, m_utils, m_tables) + if error_desc ~= nil then return end + + m_libgui = validate_script("libgui", "1.0.2") + if error_desc ~= nil then return end + + m_log_viewer3 = validate_script("FlightsHistory3", ver, m_log, m_utils,m_tables,m_index_file,m_libgui) + if error_desc ~= nil then return end + + + validate_image("bg1.png") + if error_desc ~= nil then return end + + validate_image("bg2.png") + if error_desc ~= nil then return end +end + +local function init() + validate_files() + if error_desc ~= nil then return end + + return m_log_viewer3.init() +end + +local function run(event, touchState) + -- display if in error mode + if error_desc ~= nil then + print(error_desc) + lcd.clear() + lcd.drawText(5, 30, "Error:", TEXT_COLOR + BOLD) + lcd.drawText(5, 60, error_desc, TEXT_COLOR + BOLD) + return 0 + end + + return m_log_viewer3.run(event, touchState) +end + +return { init = init, run = run }