Subversion Repositories Kolibri OS

Rev

Rev 8554 | Rev 8556 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) 2019-2021 Logaev Maxim (turbocat2001), GPLv2 */
  2.  
  3. /*
  4.     Info: App uses api from openweathermap.org.
  5.     The standard configuration uses my token and the city of Moscow.
  6.     You can always change it in the weather.json file.
  7.     If you use UTF-8 encoding, then city names can be entered in different languages!
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "json/json.h"
  13. #include <kos32sys1.h>
  14. #include <kolibrisys.h>
  15. #include <clayer/http.h>
  16. #include <clayer/libimg.h>
  17.  
  18. #define VERSION  "Weather 1.3"
  19.  
  20. enum BUTTONS{
  21.     BTN_QUIT = 1,
  22.     BTN_UPDATE = 2
  23. };
  24.  
  25. #define START_YPOS 34
  26. #define UTF8_W 8
  27. #define CP866_W 6
  28. #define JSON_OBJ(X) value->u.object.values[X]
  29. #define OK 200
  30.  
  31. unsigned WINDOW_W = 200;
  32.  
  33. #define API       "api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=%s&lang=%s"
  34. #define IMAGE_URL "openweathermap.org/img/w/%s.png"
  35.  
  36. Image *image;
  37. Image *blend;
  38. char  *units;
  39. unsigned char char_size=1;
  40.  
  41. char *wind_speed_str, *pressure_str, *visibility_str, *humidity_str, *update_str;
  42.        
  43. char lang[3]="en";
  44. char format_temp_str[6];
  45. char full_url[512];
  46. char full_url_image[256];
  47.  
  48. char temp_char='K';
  49.  
  50. struct kolibri_system_colors sys_color_table;
  51.  
  52. pos_t win_pos;
  53.  
  54. #pragma pack(push,1)
  55. struct open_weather_data{
  56.     char    City[100];
  57.     int     wind_speed;
  58.     int     wind_deg;
  59.     int     pressure;
  60.     int     humidity;
  61.     char    weath_desc[100];
  62.     int     visibility;
  63.     int     timezone;
  64.     char    image_code[4];
  65.     int     temp;
  66. }myw;
  67. #pragma pack(pop)
  68.  
  69. void notify_show(char *text)
  70. {
  71.    start_app("/sys/@notify", text);
  72. }
  73.  
  74. void* safe_malloc(size_t size)
  75. {
  76.     void *p=user_alloc(size);
  77.     if(p==NULL){
  78.        notify_show("'Memory allocation error!' -E");
  79.        exit(0);
  80.     }else{
  81.         return p;
  82.     }
  83. }
  84.  
  85. char tmp_buff[100];
  86.  
  87. static void draw_format_text_sys(int x, int y, color_t color, const char *format_str, ... )
  88. {
  89.     va_list ap;
  90.     va_start (ap, format_str);
  91.     vsnprintf(tmp_buff, sizeof tmp_buff ,format_str, ap);
  92.     va_end(ap);
  93.     draw_text_sys(tmp_buff, x, y , 0, color);
  94. }
  95.  
  96. void find_and_set(json_value *value, struct open_weather_data* weather)
  97. {
  98.     for(int i=0; i<value->u.object.length; i++){
  99.         if(!strcmp(JSON_OBJ(i).name, "main")){
  100.             if(JSON_OBJ(i).value->u.object.values[0].value->type==json_double)
  101.             {
  102.                 weather->temp = (int)JSON_OBJ(i).value->u.object.values[0].value->u.dbl;
  103.             }else{
  104.                 weather->temp = JSON_OBJ(i).value->u.object.values[0].value->u.integer;
  105.             }
  106.             weather->pressure = JSON_OBJ(i).value->u.object.values[4].value->u.integer;
  107.             weather->humidity = JSON_OBJ(i).value->u.object.values[5].value->u.integer;
  108.         }
  109.         if(!strcmp(JSON_OBJ(i).name, "name")){
  110.             strcpy(weather->City,JSON_OBJ(i).value->u.string.ptr);
  111.         }
  112.         if(!strcmp(JSON_OBJ(i).name, "weather")){
  113.            strcpy(weather->weath_desc, JSON_OBJ(i).value->u.array.values[0]->u.object.values[2].value->u.string.ptr);
  114.            strcpy(weather->image_code, JSON_OBJ(i).value->u.array.values[0]->u.object.values[3].value->u.string.ptr);
  115.         }
  116.         if(!strcmp(JSON_OBJ(i).name, "wind")){
  117.             if(JSON_OBJ(i).value->u.object.values[0].value->type==json_double)
  118.             {
  119.                 weather->wind_speed = (int)JSON_OBJ(i).value->u.object.values[0].value->u.dbl;
  120.             }else{
  121.                 weather->wind_speed = JSON_OBJ(i).value->u.object.values[0].value->u.integer;
  122.             }  
  123.         }
  124.         if(!strcmp(JSON_OBJ(i).name, "visibility")){
  125.             weather->visibility = JSON_OBJ(i).value->u.integer;
  126.         }
  127.         if(!strcmp(JSON_OBJ(i).name, "timezone")){
  128.             weather->timezone = JSON_OBJ(i).value->u.integer/60/60;
  129.         }
  130.         if(!strcmp(JSON_OBJ(i).name, "message")){
  131.             char *errmsg = safe_malloc(weather->timezone = JSON_OBJ(i).value->u.string.length+6);
  132.             sprintf(errmsg,"'%s!' -E", JSON_OBJ(i).value->u.string.ptr);
  133.             notify_show(errmsg);
  134.             user_free(errmsg);
  135.         }
  136.     }
  137. }
  138.  
  139. http_msg* get_json(char *City, char *Token, char* Units)
  140. {
  141.     sprintf(full_url, API, City, Token, Units, lang);
  142.     http_msg *h = http_get(full_url, 0,  HTTP_FLAG_BLOCK, "");
  143.     http_long_receive(h);
  144.     if (h->status == OK || h->status == 404) {
  145.         return h;
  146.     } else {
  147.         user_free(h->content_ptr);
  148.         user_free(h);
  149.         return NULL;
  150.     }
  151. }
  152.  
  153. void get_image(){
  154.     sprintf(full_url_image, IMAGE_URL, myw.image_code);
  155.     http_msg *h= http_get(full_url_image, 0,  HTTP_FLAG_BLOCK, "");
  156.     http_long_receive(h);
  157.    
  158.     if (h->status == OK) {
  159.         image = img_decode(h->content_ptr, h->content_length, 0); // Decode RAW data to Image data
  160.         if (image->Type != IMAGE_BPP32) {
  161.             image = img_convert(image, NULL, IMAGE_BPP32, 0, 0); // Convert image to format BPP32
  162.                 if (!image) {
  163.                 notify_show("'Convetring image error!' -E");  
  164.                 exit(0);
  165.             }
  166.         }
  167.         blend = img_create(64, 64, IMAGE_BPP32);  // Create an empty layer
  168.         img_fill_color(blend, 64, 64, sys_color_table.work_area); // Fill the layer with one color
  169.         Image* image2 = img_scale(image, 0, 0, 50, 50, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, 64, 64);
  170.         img_blend(blend, image2, 0, 0, 0, 0, 64, 64);  // Blending images to display the alpha channel.
  171.         img_destroy(image);
  172.         img_destroy(image2);
  173.     }else{
  174.        notify_show("'Image not loaded!!' -W");
  175.     }
  176.     user_free(h->content_ptr);
  177.     user_free(h);
  178. }
  179.  
  180. void RedrawGUI()
  181. {
  182.     begin_draw();
  183.    
  184.     int new_win_w = (strlen(myw.City)/char_size+10)*(UTF8_W+char_size-1);
  185.     if(new_win_w<WINDOW_W){
  186.         new_win_w=WINDOW_W;
  187.     }
  188.    
  189.     sys_create_window(win_pos.x, win_pos.y, new_win_w, START_YPOS+200, VERSION, sys_color_table.work_area, 0x14);
  190.  
  191.     draw_format_text_sys(20, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.City, myw.timezone);
  192.     draw_format_text_sys(21, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.City, myw.timezone);
  193.  
  194.     img_draw(blend, 10, START_YPOS+30, 64,64,0,0);
  195.    
  196.     draw_format_text_sys(20, START_YPOS+20, 0xb0000000 | sys_color_table.work_text, myw.weath_desc);
  197.     draw_format_text_sys(21, START_YPOS+20, 0xb0000000 | sys_color_table.work_text, myw.weath_desc);
  198.  
  199.     draw_format_text_sys(100, START_YPOS+45, 0xb1000000 | sys_color_table.work_text, format_temp_str, myw.temp);  
  200.     draw_format_text_sys(101, START_YPOS+46, 0xb1000000 | sys_color_table.work_text, format_temp_str, myw.temp);
  201.  
  202.     draw_format_text_sys(20, START_YPOS+80,  0xb0000000 | sys_color_table.work_text, pressure_str,myw.pressure);
  203.     draw_format_text_sys(20, START_YPOS+100, 0xb0000000 | sys_color_table.work_text, humidity_str, myw.humidity, "%");
  204.     draw_format_text_sys(20, START_YPOS+120, 0xb0000000 | sys_color_table.work_text, wind_speed_str, myw.wind_speed);
  205.     draw_format_text_sys(20, START_YPOS+140, 0xb0000000 | sys_color_table.work_text, visibility_str, myw.visibility);
  206.    
  207.     define_button(X_W(new_win_w/2-60,120), Y_H(START_YPOS+160,30), BTN_UPDATE, sys_color_table.work_button);
  208.     draw_text_sys(update_str, (new_win_w/2)-(UTF8_W*strlen(update_str)/2/char_size), START_YPOS+170, 0, 0xb0000000 | sys_color_table.work_button_text);
  209.     end_draw();
  210. }
  211.  
  212. void get_config(char **City, char **Token)
  213. {
  214.     FILE *config_j = fopen("weather.json", "rb");
  215.     if(config_j==NULL){
  216.         notify_show("'Configuration file not found!' -E");
  217.         exit(0);
  218.     }
  219.     size_t size = _ksys_get_filesize("weather.json");
  220.     char *config_buff = safe_malloc(size+1);
  221.     if(size != fread(config_buff, sizeof(char), size, config_j)){
  222.         notify_show("'The configuration file was not fully read!' -E");
  223.         exit(0);    
  224.     }
  225.     json_value* value =json_parse (config_buff, size);
  226.     for(int i=0; i<value->u.object.length; i++){
  227.         if(!strcmp(JSON_OBJ(i).name, "Location")){  
  228.             *City = JSON_OBJ(i).value->u.string.ptr;
  229.         }
  230.         if(!strcmp(JSON_OBJ(i).name, "Token")){
  231.             *Token = JSON_OBJ(i).value->u.string.ptr;
  232.         }
  233.         if(!strcmp(JSON_OBJ(i).name, "Celsius")){
  234.             if(JSON_OBJ(i).value->u.boolean){
  235.                 units = "metric";
  236.                 temp_char = 'C';
  237.             }else{
  238.                 units = "imperial";
  239.                 temp_char = 'F';
  240.             }
  241.         }
  242.         if(!strcmp(JSON_OBJ(i).name, "Lang")){
  243.             strncpy(lang, JSON_OBJ(i).value->u.string.ptr,2);
  244.         }
  245.     }
  246.     if(*City==NULL || *Token ==NULL){
  247.          notify_show("'Invalid config!' -E");
  248.          exit(0);
  249.     }
  250.     user_free(config_buff);
  251.     fclose(config_j);
  252. }
  253.  
  254. void Update(char* city, char* token)
  255. {
  256.     if(blend!=NULL){
  257.         img_destroy(blend);
  258.         blend = NULL;
  259.     }
  260.     memset(&myw, 0, sizeof myw);
  261.     strcpy(myw.City,"None");
  262.     strcpy(myw.weath_desc,"unknown");
  263.     http_msg *json_file = get_json(city, token, units);
  264.     if(json_file != NULL){
  265.         json_value* value=json_parse (json_file->content_ptr, json_file->content_length);
  266.         find_and_set(value, &myw);
  267.         sprintf(format_temp_str, "%s°%c","%d",temp_char);
  268.         get_image();
  269.         json_value_free(value);
  270.         user_free(json_file->content_ptr);
  271.         user_free(json_file);
  272.     }else{
  273.        notify_show("'Connection error!' -E");
  274.     }
  275. }
  276.  
  277. void set_lang()
  278. {
  279.     if(!strcmp(lang, "ru")){
  280.         wind_speed_str = "Скорость ветра: %d м/с";
  281.         pressure_str   = "Давление:       %d гПa";
  282.         visibility_str = "Видимость:      %d м";
  283.         humidity_str   = "Влажность:      %d%s";
  284.         update_str     = "Обновить";
  285.         char_size = 2;
  286.         WINDOW_W = 230;
  287.     }else if(!strcmp(lang, "de")){
  288.         wind_speed_str = "Windgeschwindigkeit: %d m/s";
  289.         pressure_str   = "Druck:               %d hPa";
  290.         visibility_str = "Sichtbarkeit:        %d m";
  291.         humidity_str   = "Luftfeuchtigkeit:    %d%s";
  292.         WINDOW_W = 270;
  293.         update_str     = "Aktualisieren";
  294.     }else{
  295.         pressure_str   = "Pressure:   %d hPa";
  296.         humidity_str   = "Humidity:   %d%s";
  297.         visibility_str = "Visibility: %d m";
  298.         wind_speed_str = "Wind speed: %d m/s";
  299.         update_str     = "Refresh";
  300.     }
  301. }
  302.  
  303. int main(){
  304.     win_pos = get_mouse_pos(0);
  305.     if(!kolibri_libimg_init()){
  306.         notify_show("Libimg.obj not loaded!' -E");
  307.         exit(0);
  308.     }
  309.     get_system_colors(&sys_color_table);
  310.     char *City = NULL;
  311.     char *Token = NULL;
  312.  
  313.     get_config(&City, &Token);
  314.     set_lang();
  315.     Update(City,Token);
  316.  
  317.     while(1){
  318.         switch(get_os_event()){
  319.             case KOLIBRI_EVENT_NONE:
  320.                 break;
  321.             case KOLIBRI_EVENT_REDRAW:
  322.                 RedrawGUI();
  323.                 break;        
  324.             case KOLIBRI_EVENT_BUTTON:
  325.                 switch (get_os_button()){
  326.                     case BTN_UPDATE:
  327.                         Update(City, Token);
  328.                         RedrawGUI();
  329.                         break;
  330.                     case BTN_QUIT:
  331.                         exit(0);
  332.                         break;
  333.                 }
  334.         }
  335.     }
  336.     return 0;
  337. }
  338.