timezone.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "timezone.h"
  2. #include <lib/toolbox/args.h>
  3. #include "../../../services/config/config.h"
  4. #include "../../../ui/scene_director.h"
  5. #include "../../cli_helpers.h"
  6. #define TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE "timezone"
  7. #ifdef TOTP_CLI_RICH_HELP_ENABLED
  8. void totp_cli_command_timezone_docopt_commands() {
  9. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_TIMEZONE ", " TOTP_CLI_COMMAND_TIMEZONE_ALT
  10. " Get or set current timezone\r\n");
  11. }
  12. void totp_cli_command_timezone_docopt_usage() {
  13. TOTP_CLI_PRINTF(
  14. " " TOTP_CLI_COMMAND_NAME
  15. " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_TIMEZONE " | " TOTP_CLI_COMMAND_TIMEZONE_ALT) " " DOCOPT_OPTIONAL(
  16. DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE)) "\r\n");
  17. }
  18. void totp_cli_command_timezone_docopt_arguments() {
  19. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE
  20. " Timezone offset in hours to be set\r\n");
  21. }
  22. #endif
  23. void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  24. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  25. return;
  26. }
  27. FuriString* temp_str = furi_string_alloc();
  28. if(args_read_string_and_trim(args, temp_str)) {
  29. char* strtof_endptr;
  30. float tz = strtof(furi_string_get_cstr(temp_str), &strtof_endptr);
  31. if(*strtof_endptr == 0 && tz >= -12.75f && tz <= 12.75f) {
  32. TOTP_CLI_LOCK_UI(plugin_state);
  33. plugin_state->timezone_offset = tz;
  34. if(totp_config_file_update_timezone_offset(plugin_state)) {
  35. TOTP_CLI_PRINTF_SUCCESS("Timezone is set to %f\r\n", (double)tz);
  36. } else {
  37. totp_cli_print_error_updating_config_file();
  38. }
  39. TOTP_CLI_UNLOCK_UI(plugin_state);
  40. } else {
  41. TOTP_CLI_PRINTF_ERROR("Invalid timezone offset\r\n");
  42. }
  43. } else {
  44. TOTP_CLI_PRINTF_INFO(
  45. "Current timezone offset is %f\r\n", (double)plugin_state->timezone_offset);
  46. }
  47. furi_string_free(temp_str);
  48. }