Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # acaia_ios_process.sh 'J:/tmp/20180723_2'
- # Формат ячеек даты в экселе
- # ДД.ММ.ГГГГ ч:мм:сс,0
- #
- # Файлы с iphone экспортировать, например, iMazing, см https://imazing.com
- # Для работы используется sqlite3 command-line shell, см https://www.sqlite.org/download.html
- #
- script_dir=`dirname "$0"`; script_dir=`(cd "$script_dir"; pwd)`
- export brewmaster_root="$1/co.acaia.iphone.brewmaster/Documents"
- export acaiacoffee_root="$1/acaiacoffee/Documents"
- export work_dir="$script_dir/../_temp/acaia"
- mkdir -p "$work_dir"
- (
- echo '--brewmaster';
- "$script_dir/sqlite3.exe" -batch -readonly "$brewmaster_root/Test.sqlite" <<EOF
- .separator "\t"
- select round(zbrewedat*5,0)/5
- ,zname
- ,zbean
- ,zbeanweight
- ,ztemp
- ,zgrind
- ,zdripper
- ,zpot
- ,ztasty
- ,replace(znote, char(10), ' ')
- ,replace(replace(replace(replace(zweightdata, char(10), ''),' ', ''), '[', ''), ']', '')
- from zbrewprint;
- EOF
- echo '--acaiacoffee';
- "$script_dir/sqlite3.exe" -batch -readonly "$acaiacoffee_root/Test.sqlite" <<EOF
- .separator "\t"
- select round(zstarttime*5,0)/5-0.2
- ,zbeanweight
- ,zstarcount
- ,zgrind
- ,ztemp
- ,replace(znote, char(10), ' ')
- ,zuuid
- from zcoffeerecord;
- EOF
- ) | awk "-F\t" '
- BEGIN {
- split("", g_union_table_result);
- g_smooth_step = 5;
- g_union_table_result[0]= "Дата";
- g_union_table_result[1]= "Время";
- g_union_table_result[2]= "Вода"; # Всего
- g_union_table_result[3]= "Воронка";
- g_union_table_result[4]= "Кофе";
- g_union_table_result[5]= "Кофе2, г/с";
- }
- "--brewmaster" == $0 {state="brewmaster"; next;}
- "--acaiacoffee" == $0 {state="acaiacoffee"; next;}
- BEGIN {
- split("", data);
- split("", data_w);
- }
- "brewmaster" == state {
- start_time_ios = $1;
- start_time_posix = get_posixtime_by_iostime(start_time_ios);
- filename_only_name = "brewmaster_" get_timestamp_by_posix_time(start_time_posix) "_" $2;
- filename = ENVIRON["work_dir"] "/" filename_only_name ".txt";
- header_init(start_time_posix, filename, "brewmaster");
- header_add("Назначение", "Кофе");
- header_add("Приложение", "Brewmaster");
- header_add("Имя файла", filename_only_name);
- header_add("Формат даты", "ДД.ММ.ГГГГ ч:мм:сс,0");
- header_add("Начало", get_timestamp_by_posix_time(start_time_posix));
- header_add("Масса зерен", $4);
- header_add("Температура воды", $5);
- header_add("Вкус", $9);
- header_add("Помол", $6);
- header_add("Примечания", $10);
- header_add("Имя", $2);
- header_add("Зерна", $3);
- header_add("Воронка", $7);
- header_add("Чайник", $8);
- data_init();
- weight_size = split($11, weight, ",");
- for (i = 0; i < weight_size; ++i) {
- data_add(start_time_posix + i*0.2, weight[i+1]);
- }
- }
- "acaiacoffee" == state {
- start_time_ios = $1;
- start_time_posix = get_posixtime_by_iostime(start_time_ios);
- filename_only_name = "acaiacoffee_" get_timestamp_by_posix_time(start_time_posix);
- filename = ENVIRON["work_dir"] "/" filename_only_name ".txt";
- header_init(start_time_posix, filename, "acaiacoffee");
- if (index($6 , "Total")) {
- header_add("Назначение", "Вода");
- }
- else {
- header_add("Назначение", "Воронка");
- }
- header_add("Приложение", "Acaia coffee");
- header_add("Имя файла", filename_only_name);
- header_add("Формат даты", "ДД.ММ.ГГГГ ч:мм:сс,0");
- header_add("Начало", get_timestamp_by_posix_time(start_time_posix));
- header_add("Масса зерен", $2);
- header_add("Температура воды", $5);
- header_add("Вкус", $3);
- header_add("Помол", $4);
- header_add("Примечания", $6);
- data_init();
- uuid = $7;
- filename_weight = ENVIRON["acaiacoffee_root"] "/" uuid "_weight.csv";
- i = 0;
- while (1 == getline_result=getline < filename_weight) {
- if (match($0, "<real>([0-9.]+)</real>", m)) {
- data_add(start_time_posix + i++*0.2, m[1]);
- }
- }
- close(filename_weight);
- }
- END {
- # Находим пересечения
- last_ids = "empty";
- data_w_num = asorti(data_w, data_w_indices);
- for (j=1; j <= data_w_num; ++j) {
- time_posix_str = data_w_indices[j];
- size = length(data_w[time_posix_str]);
- if (size > 1) {
- ids = "";
- for (i = 0; i < size; ++i) {
- ids = (length(ids) ? ids "_" : "") data_w[time_posix_str][i]["data_id"];
- }
- if (last_ids != ids) {
- if ("empty" != last_ids) {
- union_fini();
- }
- union_init(time_posix_str);
- last_ids = ids;
- }
- union_add(time_posix_str);
- }
- }
- if ("empty" != last_ids) {
- union_fini();
- }
- }
- # Работа с заголовками
- function header_init(start_time_posix, filename, type) {
- g_header_index = 0;
- g_filename = filename;
- g_item_index = g_item_last_index++;
- data[g_item_index]["start_time_posix"] = start_time_posix;
- data[g_item_index]["type"] = type;
- }
- function header_add(header_name, header_value, _header_item) {
- _header_item = sprintf("%s\t%s", header_name, header_value);
- data[g_item_index]["header"][g_header_index] = _header_item;
- data[g_item_index]["header_map"][header_name] = header_value;
- ++g_header_index;
- # Запись в файл
- if (1 == g_header_index) {
- printf("%s\r\n", _header_item) > g_filename;
- }
- else {
- printf("%s\r\n", _header_item) >> g_filename;
- }
- }
- # Работа с данными
- function data_init() {
- }
- function data_add(time_posix, weight, _time_posix_str, _excel_time_str, _data_w_id, _text) {
- _time_posix_str = sprintf("%.1f", time_posix);
- _excel_time_str = get_exceltime_by_posixtime(time_posix);
- _data_w_id = 0;
- if (_time_posix_str in data_w) {
- _data_w_id = length(data_w[_time_posix_str]);
- }
- data_w[_time_posix_str][_data_w_id]["data_id"] = g_item_index;
- data_w[_time_posix_str][_data_w_id]["excel_time"] = _excel_time_str;
- data_w[_time_posix_str][_data_w_id]["time_posix"] = time_posix;
- data_w[_time_posix_str][_data_w_id]["weight"] = weight;
- # Запись в файл
- _text = sprintf("%s\t%s\r\n", _excel_time_str, weight);
- gsub("\\.", ",", _text);
- printf("%s", _text) >> g_filename;
- }
- # работа с объединениями
- function union_init(time_posix_str, _i, _size, _data_id, _empty_size, _header_index, _header, _header_arr, _header_result, _header_main) {
- g_union_timespamp = get_timestamp_by_posix_time(data_w[time_posix_str][0]["time_posix"]);
- union_filename_full = ENVIRON["work_dir"] "/" "union_" g_union_timespamp ".txt";
- union_filename_inited = 0;
- ##Add BOM to the new file
- #union_write("\xEF\xBB\xBF");
- _size = length(data_w[time_posix_str]);
- split("", _header_main);
- for (_i = 0; _i < _size; ++_i) {
- union_write(sprintf("%s%s", _i ? "\t" : "", "Время общего начала"));
- }
- for (; _i < _size * 2; ++_i) {
- union_write(sprintf("%s%s", _i ? "\t" : "", g_union_timespamp));
- }
- union_write("\r\n");
- for (_header_index = 0; ; ++_header_index) {
- _empty_size = 0;
- split("", _header_result);
- for (_i = 0; _i < _size; ++_i) {
- _header = "\t";
- _data_id = data_w[time_posix_str][_i]["data_id"];
- if (length(data[_data_id]["header"]) > _header_index) {
- _header = data[_data_id]["header"][_header_index];
- }
- else {
- ++_empty_size;
- }
- #union_write(sprintf("%s%s", _i ? "\t" : "", _header));
- split(_header, _header_arr, "\t");
- _header_result[_i] = _header_arr[1];
- _header_result[_i+_size] = _header_arr[2];
- if (!_header_index) {
- _header_main[_i] = g_union_timespamp " " _header_arr[2];
- }
- }
- for (_i = 0; _i < length(_header_result); ++_i) {
- union_write(sprintf("%s%s", _i ? "\t" : "", _header_result[_i]));
- }
- union_write(sprintf("\r\n"));
- # Допускаем печать пустой строчки
- if (_empty_size >= _size) {
- break;
- }
- }
- g_union_item_id = 0;
- split("", g_union_table_order);
- split("", g_union_table);
- g_union_table_row_post_process_type = 0;
- }
- function union_add(time_posix_str, _i, _size, _text) {
- union_table_row_init();
- union_table_row_add("Дата", data_w[time_posix_str][0]["excel_time"]);
- union_table_row_add("Время", sprintf("%.1f", g_union_item_id * 0.2));
- _size = length(data_w[time_posix_str]);
- for (_i = 0; _i < _size; ++_i) {
- _weight = data_w[time_posix_str][_i]["weight"];
- _data_id = data_w[time_posix_str][_i]["data_id"];
- _purpose = data[_data_id]["header_map"]["Назначение"];
- union_table_row_add(_purpose, _weight);
- }
- union_table_row_fini();
- ++g_union_item_id;
- }
- function union_fini(_i) {
- union_table_smooth_and_derivative();
- union_table_print_header();
- union_table_print_data();
- }
- function union_write(str) {
- if (union_filename_inited) {
- printf("%s", str) >> union_filename_full;
- }
- else {
- printf("%s", str) > union_filename_full;
- union_filename_inited = 1;
- }
- }
- function union_table_row_init() {
- }
- function union_table_row_add(column_name, value) {
- if (!(column_name in g_union_table)) {
- g_union_table_order[length(g_union_table_order)] = column_name;
- }
- g_union_table[column_name][g_union_item_id] = value;
- }
- function union_table_row_fini() {
- union_table_row_post_process();
- }
- function union_table_print_header(_i, _is_not_first) {
- # Печатаем заголовок
- _is_not_first = 0;
- if (length(g_union_table_result)) {
- for (_i = 0; _i < length(g_union_table_result); ++_i) {
- if (g_union_table_result[_i] in g_union_table) {
- union_write(sprintf("%s%s %s", _is_not_first++ ? "\t" : "", g_union_timespamp, g_union_table_result[_i]));
- }
- }
- } else {
- for (_i = 0; _i < length(g_union_table_order); ++_i) {
- union_write(sprintf("%s%s %s", _is_not_first++ ? "\t" : "", g_union_timespamp, g_union_table_order[_i]));
- }
- }
- union_write(sprintf("\r\n"));
- }
- function union_table_print_data(_c, _i, _value, _out_line) {
- # Печатаем данные
- if (length(g_union_table_order) <= 0) return;
- for (_c = 0; _c < length(g_union_table[g_union_table_order[0]]); ++_c) {
- _out_line = "";
- if (length(g_union_table_result)) {
- for (_i = 0; _i < length(g_union_table_result); ++_i) {
- if (g_union_table_result[_i] in g_union_table) {
- _value = g_union_table[g_union_table_result[_i]][_c];
- gsub("\\.", ",", _value);
- _out_line = ("" == _out_line ? "" : _out_line "\t") _value;
- }
- }
- } else {
- for (_i = 0; _i < length(g_union_table_order); ++_i) {
- _value = g_union_table[g_union_table_order[_i]][_c];
- gsub("\\.", ",", _value);
- _out_line = ("" == _out_line ? "" : _out_line "\t") _value;
- }
- }
- union_write(_out_line "\r\n");
- }
- }
- # Обработка данных
- function union_table_row_post_process() {
- if (!g_union_table_row_post_process_type) {
- if ("Кофе" in g_union_table && "Вода" in g_union_table) {
- g_union_table_row_post_process_type = 1;
- }
- else if ("Кофе" in g_union_table && "Воронка" in g_union_table) {
- g_union_table_row_post_process_type = 2;
- }
- }
- if (1 == g_union_table_row_post_process_type) {
- # Считаю разницу между весами - получаю, какая масса осталась в воронке
- union_table_row_add("Воронка", g_union_table["Вода"][g_union_item_id] - g_union_table["Кофе"][g_union_item_id]);
- }
- else if (2 == g_union_table_row_post_process_type) {
- # Считаю общую массу
- union_table_row_add("Вода", g_union_table["Воронка"][g_union_item_id] + g_union_table["Кофе"][g_union_item_id]);
- }
- }
- function union_table_smooth_and_derivative(_c, _i, _union_table_order_size, _union_table_size, _column_header, _column_header_new, _column_header_derivative, _value_old, _smooth_time_step, _value, _value_derivative, _column2_header_time, _column2_header_new, _column2_header_derivative) {
- # Укрупняю данные, чтобы уменьшить случайные колебания
- _union_table_order_size = length(g_union_table_order);
- if (_union_table_order_size <= 0) return;
- if (!("Время" in g_union_table)) return;
- _union_table_size = length(g_union_table[g_union_table_order[0]]);
- if (_union_table_size <= g_smooth_step) return;
- _smooth_time_step = g_union_table["Время"][g_smooth_step] - g_union_table["Время"][0];
- # Цикл по всем столбцам
- for (_i = 2; _i < _union_table_order_size; ++_i) {
- _column_header = g_union_table_order[_i];
- _column_header_new = _column_header ", г";
- _column_header_derivative = _column_header ", г/с";
- _value_old = 0.0;
- # Новые столбцы без повторений
- _column2_header_time = "Время2";
- _column2_header_new = _column_header "2, г";
- _column2_header_derivative = _column_header "2, г/с";
- for (_c = g_smooth_step - 1; _c < _union_table_size; _c += g_smooth_step) {
- _value = g_union_table[g_union_table_order[_i]][_c];
- _value_derivative = _value - _value_old / _smooth_time_step;
- _value_old = _value;
- for (g_union_item_id = _c + 1 - g_smooth_step; g_union_item_id <= _c; ++g_union_item_id) {
- union_table_row_add(_column_header_new, _value);
- union_table_row_add(_column_header_derivative, _value_derivative);
- }
- # Новые столбцы без повторений
- g_union_item_id = (_c + 1) / g_smooth_step - 1;
- union_table_row_add(_column2_header_time, g_union_item_id * _smooth_time_step);
- union_table_row_add(_column2_header_new, _value);
- union_table_row_add(_column2_header_derivative, _value_derivative);
- }
- }
- }
- # разные функции
- function get_posixtime_by_iostime(ios_time) {
- return ios_time + 975715200 + 30*24*60*60 ;
- }
- function get_exceltime_by_posixtime(posix_time) {
- return sprintf("%.10f", posix_time/(24*60*60) + 25569.125);
- }
- function get_timestamp_by_posix_time(posix_time) {
- return strftime("%Y%m%dT%H%M%S",sprintf("%.0f", posix_time));
- }
- '
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement