Rev 8554 | Rev 8556 | Go to most recent revision | Details | Compare with Previous | 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 | |||
8555 | superturbo | 18 | #define VERSION "Weather 1.3" |
8553 | superturbo | 19 | |
20 | enum BUTTONS{ |
||
21 | BTN_QUIT = 1, |
||
22 | BTN_UPDATE = 2 |
||
23 | }; |
||
24 | |||
25 | #define START_YPOS 34 |
||
8554 | superturbo | 26 | #define UTF8_W 8 |
27 | #define CP866_W 6 |
||
8555 | superturbo | 28 | #define JSON_OBJ(X) value->u.object.values[X] |
29 | #define OK 200 |
||
8553 | superturbo | 30 | |
8555 | superturbo | 31 | unsigned WINDOW_W = 200; |
8554 | superturbo | 32 | |
8555 | superturbo | 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 | |||
8553 | superturbo | 36 | Image *image; |
8554 | superturbo | 37 | Image *blend; |
8555 | superturbo | 38 | char *units; |
39 | unsigned char char_size=1; |
||
8554 | superturbo | 40 | |
8555 | superturbo | 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]; |
||
8554 | superturbo | 45 | char full_url[512]; |
8553 | superturbo | 46 | char full_url_image[256]; |
8555 | superturbo | 47 | |
48 | char temp_char='K'; |
||
49 | |||
8553 | superturbo | 50 | struct kolibri_system_colors sys_color_table; |
51 | |||
8554 | superturbo | 52 | pos_t win_pos; |
53 | |||
8553 | superturbo | 54 | #pragma pack(push,1) |
8555 | superturbo | 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; |
||
8553 | superturbo | 67 | #pragma pack(pop) |
68 | |||
69 | void notify_show(char *text) |
||
70 | { |
||
71 | start_app("/sys/@notify", text); |
||
72 | } |
||
73 | |||
8555 | superturbo | 74 | void* safe_malloc(size_t size) |
8553 | superturbo | 75 | { |
8555 | superturbo | 76 | void *p=user_alloc(size); |
8553 | superturbo | 77 | if(p==NULL){ |
78 | notify_show("'Memory allocation error!' -E"); |
||
79 | exit(0); |
||
80 | }else{ |
||
81 | return p; |
||
82 | } |
||
83 | } |
||
84 | |||
8555 | superturbo | 85 | char tmp_buff[100]; |
8554 | superturbo | 86 | |
8553 | superturbo | 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 | |||
8555 | superturbo | 96 | void find_and_set(json_value *value, struct open_weather_data* weather) |
8553 | superturbo | 97 | { |
98 | for(int i=0; i |
||
99 | if(!strcmp(JSON_OBJ(i).name, "main")){ |
||
8554 | superturbo | 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 | } |
||
8553 | superturbo | 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")){ |
||
8554 | superturbo | 110 | strcpy(weather->City,JSON_OBJ(i).value->u.string.ptr); |
8553 | superturbo | 111 | } |
112 | if(!strcmp(JSON_OBJ(i).name, "weather")){ |
||
8554 | superturbo | 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); |
||
8553 | superturbo | 115 | } |
116 | if(!strcmp(JSON_OBJ(i).name, "wind")){ |
||
8554 | superturbo | 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 | } |
||
8553 | superturbo | 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); |
||
8555 | superturbo | 134 | user_free(errmsg); |
8553 | superturbo | 135 | } |
136 | } |
||
137 | } |
||
138 | |||
8555 | superturbo | 139 | http_msg* get_json(char *City, char *Token, char* Units) |
8553 | superturbo | 140 | { |
8555 | superturbo | 141 | sprintf(full_url, API, City, Token, Units, lang); |
8553 | superturbo | 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) { |
||
8554 | superturbo | 145 | return h; |
8553 | superturbo | 146 | } else { |
8555 | superturbo | 147 | user_free(h->content_ptr); |
148 | user_free(h); |
||
8553 | superturbo | 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 | } |
||
8554 | superturbo | 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); |
||
8553 | superturbo | 173 | }else{ |
174 | notify_show("'Image not loaded!!' -W"); |
||
8555 | superturbo | 175 | } |
176 | user_free(h->content_ptr); |
||
177 | user_free(h); |
||
8553 | superturbo | 178 | } |
179 | |||
8555 | superturbo | 180 | void RedrawGUI() |
8553 | superturbo | 181 | { |
8555 | superturbo | 182 | begin_draw(); |
8554 | superturbo | 183 | |
8555 | superturbo | 184 | int new_win_w = (strlen(myw.City)/char_size+10)*(UTF8_W+char_size-1); |
8554 | superturbo | 185 | if(new_win_w |
186 | new_win_w=WINDOW_W; |
||
187 | } |
||
188 | |||
8555 | superturbo | 189 | sys_create_window(win_pos.x, win_pos.y, new_win_w, START_YPOS+200, VERSION, sys_color_table.work_area, 0x14); |
8553 | superturbo | 190 | |
8554 | superturbo | 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); |
||
8553 | superturbo | 193 | |
8554 | superturbo | 194 | img_draw(blend, 10, START_YPOS+30, 64,64,0,0); |
8555 | superturbo | 195 | |
8554 | superturbo | 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); |
||
8553 | superturbo | 198 | |
8555 | superturbo | 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); |
||
8553 | superturbo | 201 | |
8555 | superturbo | 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); |
||
8553 | superturbo | 206 | |
8555 | superturbo | 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); |
||
8553 | superturbo | 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 |
||
227 | if(!strcmp(JSON_OBJ(i).name, "Location")){ |
||
8555 | superturbo | 228 | *City = JSON_OBJ(i).value->u.string.ptr; |
8553 | superturbo | 229 | } |
230 | if(!strcmp(JSON_OBJ(i).name, "Token")){ |
||
8555 | superturbo | 231 | *Token = JSON_OBJ(i).value->u.string.ptr; |
8553 | superturbo | 232 | } |
8555 | superturbo | 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 | } |
||
8553 | superturbo | 245 | } |
246 | if(*City==NULL || *Token ==NULL){ |
||
8555 | superturbo | 247 | notify_show("'Invalid config!' -E"); |
8553 | superturbo | 248 | exit(0); |
249 | } |
||
8555 | superturbo | 250 | user_free(config_buff); |
8554 | superturbo | 251 | fclose(config_j); |
8553 | superturbo | 252 | } |
253 | |||
254 | void Update(char* city, char* token) |
||
255 | { |
||
8554 | superturbo | 256 | if(blend!=NULL){ |
257 | img_destroy(blend); |
||
8555 | superturbo | 258 | blend = NULL; |
8554 | superturbo | 259 | } |
8553 | superturbo | 260 | memset(&myw, 0, sizeof myw); |
8554 | superturbo | 261 | strcpy(myw.City,"None"); |
262 | strcpy(myw.weath_desc,"unknown"); |
||
8555 | superturbo | 263 | http_msg *json_file = get_json(city, token, units); |
8553 | superturbo | 264 | if(json_file != NULL){ |
8554 | superturbo | 265 | json_value* value=json_parse (json_file->content_ptr, json_file->content_length); |
8553 | superturbo | 266 | find_and_set(value, &myw); |
8555 | superturbo | 267 | sprintf(format_temp_str, "%s°%c","%d",temp_char); |
8553 | superturbo | 268 | get_image(); |
8554 | superturbo | 269 | json_value_free(value); |
270 | user_free(json_file->content_ptr); |
||
271 | user_free(json_file); |
||
8553 | superturbo | 272 | }else{ |
8554 | superturbo | 273 | notify_show("'Connection error!' -E"); |
8553 | superturbo | 274 | } |
275 | } |
||
276 | |||
8555 | superturbo | 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 | |||
8553 | superturbo | 303 | int main(){ |
8554 | superturbo | 304 | win_pos = get_mouse_pos(0); |
305 | if(!kolibri_libimg_init()){ |
||
306 | notify_show("Libimg.obj not loaded!' -E"); |
||
307 | exit(0); |
||
308 | } |
||
8553 | superturbo | 309 | get_system_colors(&sys_color_table); |
310 | char *City = NULL; |
||
311 | char *Token = NULL; |
||
8555 | superturbo | 312 | |
8553 | superturbo | 313 | get_config(&City, &Token); |
8555 | superturbo | 314 | set_lang(); |
8553 | superturbo | 315 | Update(City,Token); |
8554 | superturbo | 316 | |
8553 | superturbo | 317 | while(1){ |
318 | switch(get_os_event()){ |
||
319 | case KOLIBRI_EVENT_NONE: |
||
320 | break; |
||
321 | case KOLIBRI_EVENT_REDRAW: |
||
322 | RedrawGUI(); |
||
323 | break; |
||
8555 | superturbo | 324 | case KOLIBRI_EVENT_BUTTON: |
8553 | superturbo | 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 | } |