/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-04-17     lijian       the first version
 */

#include <console.h>

/**
 * 十六进制转十进制
 * @param c
 * @return
 */
rt_uint8_t hex_char_to_value(char c)
{
    if (c >= '0' && c <= '9')
    {
        return c - '0';
    }
    else if (c >= 'A' && c <= 'F')
    {
        return 10 + (c - 'A');
    }
    else if (c >= 'a' && c <= 'f')
    {
        return 10 + (c - 'a');
    }
    return 0;
}

/*
 * 读取配置
 */
void read_config()
{
//    char wifi_ssid[9];
//    char wifi_pwd[16];
    char mqtt_addr[16];
    char mqtt_port[9];
    char mqtt_cli_id[16];
    char mqtt_username[16];
    char mqtt_pwd[16];
    char modbus_addr[3];
    char modbus_reg_addr[3];
    char modbus_reg_num[3];
//    char speed_num[3];
//    char need_num[3];
//    char base[1];
//    char wireless[1];
    char adc_1[2];
    char adc_2[2];

    rt_uint8_t err[10];

//    err[0] = mb85rs_read_bytes(i2c_bus, 0, (rt_uint8_t *) base, sizeof(base));
//    err[1] = mb85rs_read_bytes(i2c_bus, 1, (rt_uint8_t *) wireless, sizeof(wireless));
//
//    //2,3预留
//    err[2] = mb85rs_read_bytes(i2c_bus, 4, (rt_uint8_t *) wifi_ssid, sizeof(wifi_ssid));
//    err[3] = mb85rs_read_bytes(i2c_bus, 13, (rt_uint8_t *) wifi_pwd, sizeof(wifi_pwd));

    err[0] = mb85rs_read_bytes(8, (rt_uint8_t *) mqtt_addr, sizeof(mqtt_addr));
    err[1] = mb85rs_read_bytes(24, (rt_uint8_t *) mqtt_port, sizeof(mqtt_port));
    err[2] = mb85rs_read_bytes(40, (rt_uint8_t *) mqtt_cli_id, sizeof(mqtt_cli_id));
    err[3] = mb85rs_read_bytes(56, (rt_uint8_t *) mqtt_username, sizeof(mqtt_username));
    err[4] = mb85rs_read_bytes(72, (rt_uint8_t *) mqtt_pwd, sizeof(mqtt_pwd));

    err[5] = mb85rs_read_bytes(88, (rt_uint8_t *) modbus_addr, sizeof(modbus_addr));
    err[6] = mb85rs_read_bytes(91, (rt_uint8_t *) modbus_reg_addr, sizeof(modbus_reg_addr));
    err[7] = mb85rs_read_bytes(94, (rt_uint8_t *) modbus_reg_num, sizeof(modbus_reg_num));

    err[8] = mb85rs_read_bytes(97, (rt_uint8_t *) adc_1, sizeof(adc_1));
    err[9] = mb85rs_read_bytes(99, (rt_uint8_t *) adc_2, sizeof(adc_2));
//    err[12] = mb85rs_read_bytes(i2c_bus, 98, (rt_uint8_t *) speed_num, sizeof(speed_num));
//    err[13] = mb85rs_read_bytes(i2c_bus, 101, (rt_uint8_t *) need_num, sizeof(need_num));


    rt_kprintf("\r\n");
    for (rt_uint8_t var = 0; var < 10; var++)
    {
        if (err[var] != RT_EOK)
        {
            rt_kprintf(" Read Fail !  Please Check Hardware !  is in %d config \r\n", err[var]);
            return;
        }
    }

    rt_kprintf(" Read Config Successes ! \r\n");

//    //hw_config
//    set_hw_base(base[0] - '0');
//    rt_kprintf(" Base : %d \r\n", get_hw_base());
//    set_hw_wireless(wireless[0] - '0');
//    rt_uint8_t wirel_temp = get_hw_wireless();
//    rt_kprintf(" wireless : %d \r\n", wirel_temp);
//    //设置不同设备使用标准位
//    if(wirel_temp == 1){
//        set_WIFI_Key(1);
//    } else if(wirel_temp==2){
//        set_ZigBee_Key(1);
//    } else if(wirel_temp==3){
//        set_LTE_Key(1);
//    }
//
//    //wifi config
//    set_WIFI_SSID(wifi_ssid);
//    rt_kprintf(" wifi_ssid : %s \r\n", get_WIFI_SSID());
//    set_WIFI_PWD(wifi_pwd);
//    rt_kprintf(" wifi_pwd : %s \r\n", get_WIFI_PWD());

//mqtt config
    set_MQTT_ADDR(mqtt_addr);
    rt_kprintf(" mqtt_addr : %s \r\n", get_MQTT_ADDR());
    set_MQTT_PORT(mqtt_port);
    rt_kprintf(" mqtt_port : %s \r\n", get_MQTT_PORT());
    set_MQTT_CLI_ID(mqtt_cli_id);
    rt_kprintf(" mqtt_cli_id : %s \r\n", get_MQTT_CLI_ID());
    set_MQTT_USERNAME(mqtt_username);
    rt_kprintf(" mqtt_username : %s \r\n", get_MQTT_USERNAME());
    set_MQTT_PWD(mqtt_pwd);
    rt_kprintf(" mqtt_pwd : %s \r\n", get_MQTT_PWD());

    //modbus config
    rt_uint8_t high_temp;
    rt_uint8_t low_temp;
    high_temp = hex_char_to_value(modbus_addr[0]);
    low_temp = hex_char_to_value(modbus_addr[1]);
    set_Modbus_Addr((high_temp << 4) | low_temp);
    rt_kprintf(" modbus_addr : 0x%X\r\n", get_Modbus_Addr());

    high_temp = hex_char_to_value(modbus_reg_addr[0]);
    low_temp = hex_char_to_value(modbus_reg_addr[1]);
    set_Modbus_Reg_Addr((high_temp << 4) | low_temp);
    rt_kprintf(" modbus_reg_addr : 0x%X\r\n", get_Modbus_Reg_Addr());

    high_temp = hex_char_to_value(modbus_reg_num[0]);
    low_temp = hex_char_to_value(modbus_reg_num[1]);
    set_Modbus_Reg_Num((high_temp << 4) | low_temp);
    rt_kprintf(" modbus_reg_num : 0x%X\r\n", get_Modbus_Reg_Num());

    high_temp = hex_char_to_value(adc_1[0]);
    set_ADC1_mode(high_temp);
    rt_kprintf(" ADC1 mode (0:0-10V  1:4-20ma) : %d\r\n", get_ADC1_mode());

    high_temp = hex_char_to_value(adc_2[0]);
    set_ADC2_mode(high_temp);
    rt_kprintf(" ADC2 mode (0:0-10V  1:4-20ma) : %d\r\n", get_ADC2_mode());

//    //IO
//    set_Flag_Speed_IO((((speed_num[0] - '0') * 10) + (speed_num[1] - '0'))-1);
//    rt_kprintf(" speed io: %d\r\n", get_Flag_Speed_IO()+1);
//    set_Flag_Needle_IO((((need_num[0] - '0') * 10) + (need_num[1] - '0'))-1);
//    rt_kprintf(" needl io: %d\r\n ", get_Flag_Needle_IO()+1);

}

//配置函数
void change_config()
{
    //char buffer[128]; // 输入缓冲区
    rt_uint8_t var = 0;
//    char wifi_ssid[9];
//    char wifi_pwd[16];
    char mqtt_addr[16];
    char mqtt_port[9];
    char mqtt_cli_id[16];
    char mqtt_username[16];
    char mqtt_pwd[16];
    char modbus_addr[3];
    char modbus_reg_addr[3];
    char modbus_reg_num[3];
//    char speed_num[3];
//    char need_num[3];
//    char base[1];
//    char wireless[1];
    // char mqtt_pub_topic[9];
    char adc_1[2];
    char adc_2[2];

    //退格删除的实现
//    while (var < sizeof(ch) - 1)
//    {
//        ch[var] = finsh_getchar();
//
//        if (ch[var] == '\r' || ch[var] == '\n')
//        {
//            rt_kprintf("\n");
//            break;
//        }
//
//        if (ch[var] == '\b')
//        {
//            if (var > 0)
//            {
//                rt_kprintf("\b \b");
//                var--;
//                continue;
//            }
//            else
//            {
//                continue;
//            }
//        }
//
//        rt_kprintf("%c", ch[var]);
//        var++;
//    }

    rt_kprintf("-----------------------\r\n");
    rt_kprintf("       GetonAgain         \r\n");
    rt_kprintf("         GOATS          \r\n");
    rt_kprintf("       CAMS-DATU       \r\n");
    rt_kprintf("     Setting Config      \r\n");
    rt_kprintf("-----------------------\r\n");

//    //Base
    rt_kprintf("\r\n");
//    rt_kprintf("-----------------------\r\n");
//    rt_kprintf(" Set HardWare config : \r\n");
//    rt_kprintf("  Base Plate (1.IO&485&4G  2.  3. ) : ");
//    base[0] = finsh_getchar();
//    rt_kprintf("%c", base[0]);
//    rt_kprintf("\r\n");
//    rt_kprintf("    base: %c \r\n", base[0]);
//
//    //wireless
//    rt_kprintf("\r\n");
//    rt_kprintf("  wireless (1.WIFI 2. ZigBee 3. 4G) : ");
//    wireless[0] = finsh_getchar();
//    rt_kprintf("%c", wireless[0]);
//    rt_kprintf("\r\n");
//    rt_kprintf("    wireless: %c \r\n", wireless[0]);
//
//    //Set WIFI
//    rt_kprintf("\r\n");
//    rt_kprintf("-----------------------\r\n");
//    rt_kprintf(" Set WIFI  (Length:16)    \r\n");
//
//    //WIFI SSID
//    rt_kprintf("  WIFI SSID: ");
//    for (var = 0; var < 8; var++)
//    {
//        wifi_ssid[var] = finsh_getchar();
//        rt_kprintf("%c", wifi_ssid[var]);
//       // rt_kprintf("%c %d\r\n", wifi_ssid[var] , var);
//
//        // 检查是否是回车或换行符
//        if (wifi_ssid[var] == '\r' || wifi_ssid[var] == '\n')
//        {
//            break;
//        }
//    }
//    wifi_ssid[var] = '\0';
//   // rt_kprintf("%c %d\r\n", wifi_ssid[var] , var);
//    rt_kprintf("    WIFI SSID: %s  num: %d\r\n", wifi_ssid, var);
//
//    // WIFI_PWD
//    rt_kprintf("  WIFI_PWD: ");
//    for (var = 0; var < 16; var++)
//    {
//        wifi_pwd[var] = finsh_getchar();
//        rt_kprintf("%c", wifi_pwd[var]);
//
//        // 检查是否是回车或换行符
//        if (wifi_pwd[var] == '\r' || wifi_pwd[var] == '\n')
//        {
//            break;
//        }
//    }
//    wifi_pwd[var] = '\0';
//    rt_kprintf("\r\n");
//    rt_kprintf("    WIFI PWD: %s  num: %d\r\n", wifi_pwd, var);
//    rt_kprintf("\r\n");
//
//    rt_kprintf("\r\n");
//    rt_kprintf("-----------------------\r\n");

    // Set MQTT
    rt_kprintf(" Set MQTT  (Length:16)\r\n");

    //MQTT_ADDR
    rt_kprintf("  MQTT_ADDR: ");
    for (var = 0; var < 16; var++)
    {
        mqtt_addr[var] = finsh_getchar();
        rt_kprintf("%c", mqtt_addr[var]);

        // 检查是否是回车或换行符
        if (mqtt_addr[var] == '\r' || mqtt_addr[var] == '\n')
        {
            break;
        }
    }
    mqtt_addr[var] = '\0';
    rt_kprintf("\r\n");
    rt_kprintf("    MQTT_ADDR: %s  num: %d\r\n", mqtt_addr, var);

    //MQTT_PORT
    rt_kprintf("  MQTT_PORT: ");
    for (var = 0; var < 8; var++)
    {
        mqtt_port[var] = finsh_getchar();
        rt_kprintf("%c", mqtt_port[var]);

        // 检查是否是回车或换行符
        if (mqtt_port[var] == '\r' || mqtt_port[var] == '\n')
        {
            break;
        }
    }
    mqtt_port[var] = '\0';
    rt_kprintf("\r\n");
    rt_kprintf("    MQTT_PORT: %s  num: %d\r\n", mqtt_port, var);

    //MQTT_CLI_ID
    rt_kprintf("  MQTT_CLI_ID: ");
    for (var = 0; var < 16; var++)
    {
        mqtt_cli_id[var] = finsh_getchar();
        rt_kprintf("%c", mqtt_cli_id[var]);

        // 检查是否是回车或换行符
        if (mqtt_cli_id[var] == '\r' || mqtt_cli_id[var] == '\n')
        {
            break;
        }
    }
    mqtt_cli_id[var] = '\0';
    rt_kprintf("\r\n");
    rt_kprintf("    MQTT_CLI_ID: %s  num: %d\r\n", mqtt_cli_id, var);

    //MQTT_USERNAME
    rt_kprintf("  MQTT_USERNAME: ");
    for (var = 0; var < 16; var++)
    {
        mqtt_username[var] = finsh_getchar();
        rt_kprintf("%c", mqtt_username[var]);

        // 检查是否是回车或换行符
        if (mqtt_username[var] == '\r' || mqtt_username[var] == '\n')
        {
            break;
        }
    }
    mqtt_username[var] = '\0';
    rt_kprintf("\r\n");
    rt_kprintf("    MQTT_USERNAME: %s  num: %d\r\n", mqtt_username, var);

    //MQTT_PWD
    rt_kprintf("  MQTT_PWD: ");
    for (var = 0; var < 16; var++)
    {
        mqtt_pwd[var] = finsh_getchar();
        rt_kprintf("%c", mqtt_pwd[var]);

        // 检查是否是回车或换行符
        if (mqtt_pwd[var] == '\r' || mqtt_pwd[var] == '\n')
        {
            break;
        }
    }
    mqtt_pwd[var] = '\0';
    rt_kprintf("\r\n");
    rt_kprintf("    MQTT_PWD: %s  %d num: %d\r\n", mqtt_pwd, mqtt_pwd[0], var);

    rt_kprintf("\r\n");
//    //mqtt_pub_topic
//    rt_kprintf("  MQTT_TOPIC: ");
//    for (var = 0; var < 8; var++)
//    {
//        mqtt_pub_topic[var] = finsh_getchar();
//        rt_kprintf("%c", mqtt_pub_topic[var]);
//
//        // 检查是否是回车或换行符
//        if (mqtt_pub_topic[var] == '\r' || mqtt_pub_topic[var] == '\n')
//        {
//            break;
//        }
//    }
//    mqtt_pub_topic[var] = '\0';
//    rt_kprintf("\r\n");
//    rt_kprintf(" MQTT_TOPIC: %s  num: %d\r\n", mqtt_pub_topic, var);
//    rt_kprintf("\r\n");

    rt_kprintf("\r\n");
    rt_kprintf("-----------------------\r\n");

//    //Set IO
//    rt_kprintf(" Set IO (Length:2  eg: 01 12)\r\n");
//
//    //Set Speed
//    rt_kprintf("  Speed IO : ");
//    for (var = 0; var < 2; var++)
//    {
//        speed_num[var] = finsh_getchar();
//        rt_kprintf("%c", speed_num[var]);
//
//        // 检查是否是回车或换行符
//        if (speed_num[var] == '\r' || speed_num[var] == '\n')
//        {
//            break;
//        }
//    }
//    speed_num[var] = '\0';
//    rt_kprintf("\r\n");
//    rt_kprintf("    Speed IO: %s num: %d\r\n", speed_num, var);
//
//    //Set NeedleCount
//    rt_kprintf("  Needle IO : ");
//    for (var = 0; var < 2; var++)
//    {
//        need_num[var] = finsh_getchar();
//        rt_kprintf("%c", need_num[var]);
//
//        // 检查是否是回车或换行符
//        if (need_num[var] == '\r' || need_num[var] == '\n')
//        {
//            break;
//        }
//    }
//    need_num[var] = '\0';
//    rt_kprintf("\r\n");
//    rt_kprintf("    Needle IO: %s num: %d\r\n", need_num, var);
//    rt_kprintf("\r\n");
//
//    rt_kprintf("\r\n");
//    rt_kprintf("-----------------------\r\n");
    //Set Modbus
    rt_kprintf(" Set Modbus  (Length:2  eg: AA 30)\r\n");

    //Set Modbus_addr
    rt_kprintf("  Modbus_addr : ");
    for (var = 0; var < 2; var++)
    {
        modbus_addr[var] = finsh_getchar();
        rt_kprintf("%c", modbus_addr[var]);

        // 检查是否是回车或换行符
        if (modbus_addr[var] == '\r' || modbus_addr[var] == '\n')
        {
            break;
        }
    }
    modbus_addr[var] = '\0';
    rt_kprintf("\r\n");
    rt_kprintf("    Modbus_addr: %s num: %d\r\n", modbus_addr, var);

    //Set Modbus_reg_addr
    rt_kprintf("  Modbus_reg_addr : ");
    for (var = 0; var < 2; var++)
    {
        modbus_reg_addr[var] = finsh_getchar();
        rt_kprintf("%c", modbus_reg_addr[var]);

        // 检查是否是回车或换行符
        if (modbus_reg_addr[var] == '\r' || modbus_reg_addr[var] == '\n')
        {
            break;
        }
    }
    modbus_reg_addr[var] = '\0';
    rt_kprintf("\r\n");
    rt_kprintf("    Modbus_reg_addr: %s num: %d\r\n", modbus_reg_addr, var);

    //Set Modbus_reg_num
    rt_kprintf("  Modbus_reg_num : ");
    for (var = 0; var < 2; var++)
    {
        modbus_reg_num[var] = finsh_getchar();
        rt_kprintf("%c", modbus_reg_num[var]);

        // 检查是否是回车或换行符
        if (modbus_reg_num[var] == '\r' || modbus_reg_num[var] == '\n')
        {
            break;
        }
    }
    modbus_reg_num[var] = '\0';
    rt_kprintf("\r\n");
    rt_kprintf("    Modbus_reg_num: %s num: %d\r\n", modbus_reg_num, var);

    rt_kprintf("\r\n");

    rt_kprintf("\r\n");
    rt_kprintf("-----------------------\r\n");

    //ADC
    rt_kprintf("\r\n");
    rt_kprintf(" Set ADC (0:0-10V  1:4-20ma) : \r\n");
    rt_kprintf("  ADC1_IN0 : ");
    adc_1[0] = finsh_getchar();
    rt_kprintf("%c\n", adc_1[0]);
    adc_1[1] = '\0';
    rt_kprintf("    ADC_1: %c \r\n", adc_1[0]);
    rt_kprintf("\r\n");

    rt_kprintf("\r\n");
    rt_kprintf("  ADC2_IN4 : ");
    adc_2[0] = finsh_getchar();
    rt_kprintf("%c\n", adc_2[0]);
    adc_2[1] = '\0';
    rt_kprintf("    ADC_2: %c \r\n", adc_2[0]);
    rt_kprintf("\r\n");

    rt_kprintf("-----------------------\r\n");
    rt_kprintf("\r\n");

    //SAVE
    rt_kprintf("  Save Setting ? y/n: ");
    char ch = finsh_getchar();
    rt_kprintf("%c", ch);

    if (ch == 'y')
    {
        rt_kprintf("\r\n");
        rt_kprintf(" please wait, Saving ......  \r\n");

        rt_uint8_t err[10];
//        err[0] = mb85rs_write_bytes(0, (rt_uint8_t *)base, sizeof(base));
//        err[1] = mb85rs_write_bytes(1, (rt_uint8_t *)wireless, sizeof(wireless));
//
//        //2,3预留
//        err[2] = mb85rs_write_bytes(4, (rt_uint8_t *)wifi_ssid, sizeof(wifi_ssid));
//        err[3] = mb85rs_write_bytes(13, (rt_uint8_t *)wifi_pwd, sizeof(wifi_pwd));

        err[0] = mb85rs_write_bytes(8, (rt_uint8_t *) mqtt_addr, sizeof(mqtt_addr));
        err[1] = mb85rs_write_bytes(24, (rt_uint8_t *) mqtt_port, sizeof(mqtt_port));
        err[2] = mb85rs_write_bytes(40, (rt_uint8_t *) mqtt_cli_id, sizeof(mqtt_cli_id));
        err[3] = mb85rs_write_bytes(56, (rt_uint8_t *) mqtt_username, sizeof(mqtt_username));
        err[4] = mb85rs_write_bytes(72, (rt_uint8_t *) mqtt_pwd, sizeof(mqtt_pwd));

        err[5] = mb85rs_write_bytes(88, (rt_uint8_t *) modbus_addr, sizeof(modbus_addr));
        err[6] = mb85rs_write_bytes(91, (rt_uint8_t *) modbus_reg_addr, sizeof(modbus_reg_addr));
        err[7] = mb85rs_write_bytes(94, (rt_uint8_t *) modbus_reg_num, sizeof(modbus_reg_num));

        err[8] = mb85rs_write_bytes(97, (rt_uint8_t *) adc_1, sizeof(adc_1));
        err[9] = mb85rs_write_bytes(99, (rt_uint8_t *) adc_2, sizeof(adc_2));

//        err[12] = mb85rs_write_bytes(98, (rt_uint8_t *)speed_num, sizeof(speed_num));
//        err[13] = mb85rs_write_bytes(101, (rt_uint8_t *)need_num, sizeof(need_num));

        for (rt_uint8_t var = 0; var < 10; var++)
        {
            if (err[var] != RT_EOK)
            {
                rt_kprintf(" Config Save Fail !  Please Check Hardware !  is in %d config \r\n", err[var]);
                return;
            }
        }
        rt_kprintf(" Config Save Successes !  Please Reboot !\r\n");
    }
    else
    {
        rt_kprintf("\r\n");
        rt_kprintf("switch no is exit \r\n");
    }
    rt_kprintf("\r\n");
}

void log_modbus()
{
    set_modbus_log(1);
}

void log_lte()
{
    set_LTE_log(1);
    set_mqtt_log(1);
}

void log_adc()
{
    set_ADC_log(1);
}

void log_datu()
{
    set_DATU_log(1);
}

void log_io()
{
    set_IO_log(1);
}

void no_log()
{
    set_DATU_log(0);
    set_modbus_log(0);
    set_LTE_log(0);
    set_mqtt_log(0);
    set_ADC_log(0);
    set_IO_log(0);
}

MSH_CMD_EXPORT(log_io, save to flash);
MSH_CMD_EXPORT(log_datu, save to flash);
MSH_CMD_EXPORT(change_config, save to flash);
MSH_CMD_EXPORT(log_modbus, view_log to console);
MSH_CMD_EXPORT(log_lte, view_log to console);
MSH_CMD_EXPORT(log_adc, view_log to console);
MSH_CMD_EXPORT(no_log, no_log to console);
