Subversion Repositories Kolibri OS

Rev

Rev 8554 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8553 superturbo 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 
11
#include 
12
#include "json/json.h"
13
#include 
14
#include 
15
#include 
16
#include 
17
 
18
#define VERSION  "Weather 1.0b"
19
 
20
enum BUTTONS{
21
    BTN_QUIT = 1,
22
    BTN_UPDATE = 2
23
};
24
 
25
#define JSON_OBJ(X) value->u.object.values[X]
26
#define OK 200
27
#define API       "http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric"
28
#define IMAGE_URL "http://openweathermap.org/img/w/%s.png"
29
#define START_YPOS 34
30
 
31
Image *image;
32
char full_url[256];
33
char full_url_image[256];
34
struct kolibri_system_colors sys_color_table;
35
 
36
#pragma pack(push,1)
37
typedef struct {
38
    char *City;
39
    float wind_speed;
40
    //int wind_deg;
41
    int pressure;
42
    int humidity;
43
    char *weath_main;
44
    char *weath_desc;
45
    int visibility;
46
    int timezone;
47
    char* image_code;
48
    double temp;
49
}open_weather_data;
50
#pragma pack(pop)
51
 
52
open_weather_data myw;
53
 
54
void notify_show(char *text)
55
{
56
   start_app("/sys/@notify", text);
57
}
58
 
59
void* safe_malloc(size_t size) // Безопасный malloc. Показывает уведомление об ошибке и закрывает программу если память не была выделена
60
{
61
    void *p=malloc(size);
62
    if(p==NULL){
63
       notify_show("'Memory allocation error!' -E");
64
       exit(0);
65
    }else{
66
        return p;
67
    }
68
}
69
 
70
static void draw_format_text_sys(int x, int y, color_t color, const char *format_str, ... )
71
{
72
    char tmp_buff[100];
73
    va_list ap;
74
    va_start (ap, format_str);
75
    vsnprintf(tmp_buff, sizeof tmp_buff ,format_str, ap);
76
    va_end(ap);
77
    draw_text_sys(tmp_buff, x, y , 0, color);
78
}
79
 
80
void find_and_set(json_value *value, open_weather_data* weather)
81
{
82
    for(int i=0; iu.object.length; i++){
83
        if(!strcmp(JSON_OBJ(i).name, "main")){
84
            weather->temp = JSON_OBJ(i).value->u.object.values[0].value->u.dbl;
85
            weather->pressure = JSON_OBJ(i).value->u.object.values[4].value->u.integer;
86
            weather->humidity = JSON_OBJ(i).value->u.object.values[5].value->u.integer;
87
        }
88
        if(!strcmp(JSON_OBJ(i).name, "name")){
89
            weather->City = JSON_OBJ(i).value->u.string.ptr;
90
        }
91
        if(!strcmp(JSON_OBJ(i).name, "weather")){
92
           weather->weath_desc = JSON_OBJ(i).value->u.array.values[0]->u.object.values[2].value->u.string.ptr;
93
           weather->image_code = JSON_OBJ(i).value->u.array.values[0]->u.object.values[3].value->u.string.ptr;
94
        }
95
        if(!strcmp(JSON_OBJ(i).name, "wind")){
96
            weather->wind_speed = JSON_OBJ(i).value->u.object.values[0].value->u.dbl;
97
        }
98
        if(!strcmp(JSON_OBJ(i).name, "visibility")){
99
            weather->visibility = JSON_OBJ(i).value->u.integer;
100
        }
101
        if(!strcmp(JSON_OBJ(i).name, "timezone")){
102
            weather->timezone = JSON_OBJ(i).value->u.integer/60/60;
103
        }
104
        if(!strcmp(JSON_OBJ(i).name, "message")){
105
            char *errmsg = safe_malloc(weather->timezone = JSON_OBJ(i).value->u.string.length+6);
106
            sprintf(errmsg,"'%s!' -E", JSON_OBJ(i).value->u.string.ptr);
107
            notify_show(errmsg);
108
        }
109
    }
110
}
111
 
112
http_msg* get_json(char *City, char *Token)
113
{
114
    sprintf(full_url, API, City, Token);
115
    http_msg *h = http_get(full_url, 0,  HTTP_FLAG_BLOCK, "");
116
    http_long_receive(h);
117
    if (h->status == OK || h->status == 404) {
118
       return h;
119
    } else {
120
        return NULL;
121
    }
122
}
123
 
124
void get_image(){
125
    sprintf(full_url_image, IMAGE_URL, myw.image_code);
126
    http_msg *h= http_get(full_url_image, 0,  HTTP_FLAG_BLOCK, "");
127
    http_long_receive(h);
128
 
129
    if (h->status == OK) {
130
        image = img_decode(h->content_ptr, h->content_length, 0); // Decode RAW data to Image data
131
        if (image->Type != IMAGE_BPP32) {
132
            image = img_convert(image, NULL, IMAGE_BPP32, 0, 0); // Convert image to format BPP32
133
                if (!image) {
134
                notify_show("'Convetring image error!' -E");
135
                exit(0);
136
            }
137
        }
138
        //blend = img_create(64, 64, IMAGE_BPP32);  // Create an empty layer
139
        //img_fill_color(blend, 64, 64, sys_color_table.work_area); // Fill the layer with one color
140
        image = img_scale(image, 0, 0, 50, 50, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, 64, 64);
141
        //img_blend(blend, image, 0, 0, 0, 0, 64, 64);  // Blending images to display the alpha channel.
142
    }else{
143
       notify_show("'Image not loaded!!' -W");
144
    }
145
}
146
 
147
void RedrawGUI() //Рисуем окно
148
{
149
    char buff[1000];
150
    pos_t win_pos = get_mouse_pos(0); // Получаем координаты курсора
151
    begin_draw(); //Начинаем рисование интерфейса )
152
    sys_create_window(win_pos.x, win_pos.y, 220, START_YPOS+200, VERSION, 0xffffff, 0x14); // Создаём окно.
153
 
154
    draw_format_text_sys(10, START_YPOS, 0xB0000000 /*| sys_color_table.work_text*/,  "%s (UTC%+d)\0", myw.City, myw.timezone);
155
    draw_format_text_sys(11, START_YPOS, 0xB0000000 /*| sys_color_table.work_text */, "%s (UTC%+d)\0", myw.City, myw.timezone);
156
 
157
    img_draw(image, 10, START_YPOS+30, 64,64,0,0);
158
 
159
    draw_format_text_sys(10, START_YPOS+20, 0xb0000000 /* | sys_color_table.work_text */, myw.weath_desc);
160
    draw_format_text_sys(11, START_YPOS+20, 0xb0000000 /*| sys_color_table.work_text */, myw.weath_desc);
161
 
162
    draw_format_text_sys(90, START_YPOS+45, 0xB1000000 /*| sys_color_table.work_text*/, "%.1f °C", myw.temp);
163
    draw_format_text_sys(91, START_YPOS+46, 0xB1000000 /*| sys_color_table.work_text*/, "%.1f °C", myw.temp);
164
 
165
    draw_format_text_sys(10, START_YPOS+80, 0x90000000 /*| sys_color_table.work_text*/,  "Pressure:   %d hPa",myw.pressure);
166
    draw_format_text_sys(10, START_YPOS+100, 0x90000000 /* | sys_color_table.work_text*/,"Humidity:   %d %s", myw.humidity, "%");
167
    draw_format_text_sys(10, START_YPOS+120, 0x90000000 /*| sys_color_table.work_text*/, "Wind speed: %.1f m/s", myw.wind_speed);
168
    draw_format_text_sys(10, START_YPOS+140, 0x90000000 /*| sys_color_table.work_text*/, "Visibility: %d m", myw.visibility);
169
 
170
    define_button(X_W(65,80), Y_H(START_YPOS+160,30), BTN_UPDATE, sys_color_table.work_button);
171
    draw_text_sys("Update",80 , START_YPOS+170, 0, 0x90000000 | sys_color_table.work_button_text);
172
    end_draw();
173
}
174
 
175
void get_config(char **City, char **Token)
176
{
177
    FILE *config_j = fopen("weather.json", "rb");
178
    if(config_j==NULL){
179
        notify_show("'Configuration file not found!' -E");
180
        exit(0);
181
    }
182
    size_t size = _ksys_get_filesize("weather.json");
183
    char *config_buff = safe_malloc(size+1);
184
    if(size != fread(config_buff, sizeof(char), size, config_j)){
185
        notify_show("'The configuration file was not fully read!' -E");
186
        exit(0);
187
    }
188
    json_value* value =json_parse (config_buff, size);
189
    for(int i=0; iu.object.length; i++){
190
        if(!strcmp(JSON_OBJ(i).name, "Location")){
191
          *City = JSON_OBJ(i).value->u.string.ptr;
192
        }
193
        if(!strcmp(JSON_OBJ(i).name, "Token")){
194
           *Token = JSON_OBJ(i).value->u.string.ptr;
195
        }
196
    }
197
    if(*City==NULL || *Token ==NULL){
198
         notify_show("'Invalid config!!' -E");
199
         exit(0);
200
    }
201
    free(config_buff);
202
}
203
 
204
void Update(char* city, char* token)
205
{
206
    memset(&myw, 0, sizeof myw);
207
    myw.City="None";
208
    myw.weath_desc="unknown";
209
    http_msg *json_file = get_json(city, token);
210
    if(json_file != NULL){
211
        json_value* value =json_parse (json_file->content_ptr, json_file->content_length);
212
        find_and_set(value, &myw);
213
        get_image();
214
    }else{
215
        notify_show("'Connection error!' -E");
216
    }
217
}
218
 
219
int main(){
220
    kolibri_libimg_init();
221
    get_system_colors(&sys_color_table);
222
 
223
    char *City = NULL;
224
    char *Token = NULL;
225
    get_config(&City, &Token);
226
    Update(City,Token);
227
 
228
    while(1){
229
        switch(get_os_event()){
230
            case KOLIBRI_EVENT_NONE:
231
                break;
232
            case KOLIBRI_EVENT_REDRAW:
233
                RedrawGUI();
234
                break;
235
            case KOLIBRI_EVENT_BUTTON: // Событие обработки кнопок
236
                switch (get_os_button()){
237
                    case BTN_UPDATE:
238
                        Update(City, Token);
239
                        RedrawGUI();
240
                        break;
241
                    case BTN_QUIT:
242
                        exit(0);
243
                        break;
244
                }
245
        }
246
    }
247
    return 0;
248
}