Rev 9651 | 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. |
||
8566 | superturbo | 7 | weather.json configuration example: |
8 | |||
9 | { |
||
10 | "Celsius": false, // Enabled fahrenheit (Optional) |
||
8735 | turbocat | 11 | "Location": "Berlin", // city Berlin |
8566 | superturbo | 12 | "Token": "19ffa14b3dc0e238175829461d1788b8", // OpenWeatherMap token |
13 | "Lang": "ru", // Language (Optional) |
||
14 | "AutoUpdate": 5 // In minutes. 0 - disabled (Optional) |
||
15 | } |
||
16 | |||
8553 | superturbo | 17 | */ |
18 | |||
19 | #include |
||
20 | #include |
||
8566 | superturbo | 21 | #include |
22 | #include |
||
8553 | superturbo | 23 | #include "json/json.h" |
8735 | turbocat | 24 | #include |
8553 | superturbo | 25 | #include |
26 | #include |
||
27 | |||
8735 | turbocat | 28 | #define VERSION "Weather 1.6e" |
8553 | superturbo | 29 | |
30 | enum BUTTONS{ |
||
31 | BTN_QUIT = 1, |
||
32 | BTN_UPDATE = 2 |
||
33 | }; |
||
34 | |||
8556 | superturbo | 35 | #define START_YPOS 34 |
8554 | superturbo | 36 | #define UTF8_W 8 |
37 | #define CP866_W 6 |
||
8555 | superturbo | 38 | #define JSON_OBJ(X) value->u.object.values[X] |
39 | #define OK 200 |
||
8553 | superturbo | 40 | |
8556 | superturbo | 41 | unsigned WINDOW_W = 230; |
8554 | superturbo | 42 | |
8555 | superturbo | 43 | #define API "api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=%s&lang=%s" |
44 | #define IMAGE_URL "openweathermap.org/img/w/%s.png" |
||
8556 | superturbo | 45 | |
8566 | superturbo | 46 | Image *blend=NULL; |
9820 | dunkaist | 47 | const char *config1_name = "/sys/Settings/weather.json"; |
48 | const char *config2_name = "/kolibrios/Settings/weather.json"; |
||
8555 | superturbo | 49 | |
50 | unsigned char char_size=1; |
||
8735 | turbocat | 51 | uint64_t auto_update_time = 0; |
8554 | superturbo | 52 | |
8556 | superturbo | 53 | char *wind_speed_str, *pressure_str, *visibility_str, *humidity_str, *update_str, *wind_deg_str; |
8555 | superturbo | 54 | |
55 | char lang[3]="en"; |
||
56 | char format_temp_str[6]; |
||
8554 | superturbo | 57 | char full_url[512]; |
8553 | superturbo | 58 | char full_url_image[256]; |
8555 | superturbo | 59 | |
60 | char temp_char='K'; |
||
61 | |||
8735 | turbocat | 62 | ksys_colors_table_t sys_color_table; |
8553 | superturbo | 63 | |
8735 | turbocat | 64 | ksys_pos_t win_pos; |
8554 | superturbo | 65 | |
8553 | superturbo | 66 | #pragma pack(push,1) |
8555 | superturbo | 67 | struct open_weather_data{ |
8735 | turbocat | 68 | char city[100]; |
8555 | superturbo | 69 | int wind_speed; |
70 | int wind_deg; |
||
71 | int pressure; |
||
72 | int humidity; |
||
73 | char weath_desc[100]; |
||
74 | int visibility; |
||
75 | int timezone; |
||
76 | char image_code[4]; |
||
77 | int temp; |
||
78 | }myw; |
||
8553 | superturbo | 79 | #pragma pack(pop) |
80 | |||
8735 | turbocat | 81 | void notify_show(char *text){ |
82 | _ksys_exec("/sys/@notify", text); |
||
8553 | superturbo | 83 | } |
84 | |||
8555 | superturbo | 85 | void* safe_malloc(size_t size) |
8553 | superturbo | 86 | { |
8566 | superturbo | 87 | void *p=malloc(size); |
8553 | superturbo | 88 | if(p==NULL){ |
89 | notify_show("'Memory allocation error!' -E"); |
||
90 | exit(0); |
||
91 | }else{ |
||
92 | return p; |
||
93 | } |
||
94 | } |
||
95 | |||
8735 | turbocat | 96 | void draw_format_text_sys(int x, int y, ksys_color_t color, const char *format_str, ... ) // Форматированный вывод в окно |
8553 | superturbo | 97 | { |
8566 | superturbo | 98 | char tmp_buff[100]; |
8553 | superturbo | 99 | va_list ap; |
100 | va_start (ap, format_str); |
||
101 | vsnprintf(tmp_buff, sizeof tmp_buff ,format_str, ap); |
||
102 | va_end(ap); |
||
8735 | turbocat | 103 | _ksys_draw_text(tmp_buff, x, y , 0, color); |
8553 | superturbo | 104 | } |
105 | |||
8566 | superturbo | 106 | void find_and_set(json_value *value, struct open_weather_data* weather) // Ищем значения в json и заполняем структуру "myw" |
8553 | superturbo | 107 | { |
108 | for(int i=0; i |
||
109 | if(!strcmp(JSON_OBJ(i).name, "main")){ |
||
8554 | superturbo | 110 | if(JSON_OBJ(i).value->u.object.values[0].value->type==json_double) |
111 | { |
||
112 | weather->temp = (int)JSON_OBJ(i).value->u.object.values[0].value->u.dbl; |
||
113 | }else{ |
||
114 | weather->temp = JSON_OBJ(i).value->u.object.values[0].value->u.integer; |
||
115 | } |
||
8553 | superturbo | 116 | weather->pressure = JSON_OBJ(i).value->u.object.values[4].value->u.integer; |
117 | weather->humidity = JSON_OBJ(i).value->u.object.values[5].value->u.integer; |
||
118 | } |
||
119 | if(!strcmp(JSON_OBJ(i).name, "name")){ |
||
8566 | superturbo | 120 | if(!strcmp(&JSON_OBJ(i).value->u.string.ptr[JSON_OBJ(i).value->u.string.length-3], "’")){ |
8735 | turbocat | 121 | strncpy(weather->city, JSON_OBJ(i).value->u.string.ptr, JSON_OBJ(i).value->u.string.length-3); |
8566 | superturbo | 122 | }else{ |
8735 | turbocat | 123 | strcpy(weather->city, JSON_OBJ(i).value->u.string.ptr); |
8566 | superturbo | 124 | } |
8553 | superturbo | 125 | } |
126 | if(!strcmp(JSON_OBJ(i).name, "weather")){ |
||
8554 | superturbo | 127 | strcpy(weather->weath_desc, JSON_OBJ(i).value->u.array.values[0]->u.object.values[2].value->u.string.ptr); |
128 | strcpy(weather->image_code, JSON_OBJ(i).value->u.array.values[0]->u.object.values[3].value->u.string.ptr); |
||
8553 | superturbo | 129 | } |
130 | if(!strcmp(JSON_OBJ(i).name, "wind")){ |
||
8556 | superturbo | 131 | weather->wind_deg = JSON_OBJ(i).value->u.object.values[1].value->u.integer; |
8554 | superturbo | 132 | if(JSON_OBJ(i).value->u.object.values[0].value->type==json_double) |
133 | { |
||
134 | weather->wind_speed = (int)JSON_OBJ(i).value->u.object.values[0].value->u.dbl; |
||
135 | }else{ |
||
136 | weather->wind_speed = JSON_OBJ(i).value->u.object.values[0].value->u.integer; |
||
137 | } |
||
8553 | superturbo | 138 | } |
139 | if(!strcmp(JSON_OBJ(i).name, "visibility")){ |
||
140 | weather->visibility = JSON_OBJ(i).value->u.integer; |
||
141 | } |
||
142 | if(!strcmp(JSON_OBJ(i).name, "timezone")){ |
||
143 | weather->timezone = JSON_OBJ(i).value->u.integer/60/60; |
||
144 | } |
||
145 | if(!strcmp(JSON_OBJ(i).name, "message")){ |
||
146 | char *errmsg = safe_malloc(weather->timezone = JSON_OBJ(i).value->u.string.length+6); |
||
147 | sprintf(errmsg,"'%s!' -E", JSON_OBJ(i).value->u.string.ptr); |
||
148 | notify_show(errmsg); |
||
8566 | superturbo | 149 | free(errmsg); |
8553 | superturbo | 150 | } |
151 | } |
||
152 | } |
||
153 | |||
8735 | turbocat | 154 | http_msg* get_json(char *city, char *Token, char* Units) |
8553 | superturbo | 155 | { |
8735 | turbocat | 156 | sprintf(full_url, API, city, Token, Units, lang); |
8553 | superturbo | 157 | http_msg *h = http_get(full_url, 0, HTTP_FLAG_BLOCK, ""); |
158 | http_long_receive(h); |
||
159 | if (h->status == OK || h->status == 404) { |
||
8554 | superturbo | 160 | return h; |
8553 | superturbo | 161 | } else { |
8566 | superturbo | 162 | http_free(h); |
8553 | superturbo | 163 | return NULL; |
164 | } |
||
165 | } |
||
166 | |||
8566 | superturbo | 167 | void get_image() // Функция загрузки изображения |
168 | { |
||
8553 | superturbo | 169 | sprintf(full_url_image, IMAGE_URL, myw.image_code); |
8566 | superturbo | 170 | http_msg *h= http_get(full_url_image, 0, HTTP_FLAG_BLOCK, ""); |
8553 | superturbo | 171 | http_long_receive(h); |
172 | |||
173 | if (h->status == OK) { |
||
8566 | superturbo | 174 | Image *image = img_decode(h->content_ptr, h->content_length, 0); // Декодирование RAW данных в данные изображения |
8553 | superturbo | 175 | if (image->Type != IMAGE_BPP32) { |
8566 | superturbo | 176 | image = img_convert(image, NULL, IMAGE_BPP32, 0, 0); // Конвертируем картику в BPP32 |
8553 | superturbo | 177 | if (!image) { |
178 | notify_show("'Convetring image error!' -E"); |
||
179 | exit(0); |
||
180 | } |
||
181 | } |
||
8566 | superturbo | 182 | blend = img_create(64, 64, IMAGE_BPP32); // Создаём фон для картинки |
183 | img_fill_color(blend, 64, 64, sys_color_table.work_area); // Заливаем фон цветом окна |
||
184 | Image* image2 = img_scale(image, 0, 0, 50, 50, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, 64, 64); // Растягиваем изображение |
||
185 | img_blend(blend, image2, 0, 0, 0, 0, 64, 64); // Смешиваем растянутую картинку и фон для получения прозрачности |
||
186 | img_destroy(image); // Уничтожаем исходную картинку |
||
187 | img_destroy(image2); // Уничтажаем растянутую картинку |
||
8553 | superturbo | 188 | }else{ |
189 | notify_show("'Image not loaded!!' -W"); |
||
8566 | superturbo | 190 | } |
191 | if(h!=NULL){ |
||
192 | http_free(h); |
||
193 | } |
||
8553 | superturbo | 194 | } |
195 | |||
8735 | turbocat | 196 | void redraw_gui() // Перересовываем интерфейс |
8553 | superturbo | 197 | { |
8735 | turbocat | 198 | _ksys_start_draw(); // Начинам прорисовку |
8566 | superturbo | 199 | |
8735 | turbocat | 200 | int new_win_w = (strlen(myw.city)/char_size+12)*(UTF8_W+char_size-1); // Если название города не влезает в окно |
8554 | superturbo | 201 | if(new_win_w |
202 | new_win_w=WINDOW_W; |
||
203 | } |
||
8556 | superturbo | 204 | // Рисуем окно |
8735 | turbocat | 205 | _ksys_create_window(win_pos.x, win_pos.y, new_win_w, START_YPOS+220, VERSION, sys_color_table.work_area, 0x14); |
8556 | superturbo | 206 | // Выводим жирным шрифтом название локации и временной зоны |
8735 | turbocat | 207 | draw_format_text_sys(20, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.city, myw.timezone); |
208 | draw_format_text_sys(21, START_YPOS, 0xB0000000 | sys_color_table.work_text, "%s (UTC%+d)", myw.city, myw.timezone); |
||
8556 | superturbo | 209 | // Выводим изображение |
8554 | superturbo | 210 | img_draw(blend, 10, START_YPOS+30, 64,64,0,0); |
8556 | superturbo | 211 | // Выводим жирным шрифтом название локации и временной зоны |
8554 | superturbo | 212 | draw_format_text_sys(20, START_YPOS+20, 0xb0000000 | sys_color_table.work_text, myw.weath_desc); |
213 | draw_format_text_sys(21, START_YPOS+20, 0xb0000000 | sys_color_table.work_text, myw.weath_desc); |
||
8556 | superturbo | 214 | // Выводим жирным шрифтом название локации и временной зоны |
8555 | superturbo | 215 | draw_format_text_sys(100, START_YPOS+45, 0xb1000000 | sys_color_table.work_text, format_temp_str, myw.temp); |
216 | draw_format_text_sys(101, START_YPOS+46, 0xb1000000 | sys_color_table.work_text, format_temp_str, myw.temp); |
||
8556 | superturbo | 217 | // Выводим обычным шрифтом |
8555 | superturbo | 218 | draw_format_text_sys(20, START_YPOS+80, 0xb0000000 | sys_color_table.work_text, pressure_str,myw.pressure); |
219 | draw_format_text_sys(20, START_YPOS+100, 0xb0000000 | sys_color_table.work_text, humidity_str, myw.humidity, "%"); |
||
220 | draw_format_text_sys(20, START_YPOS+120, 0xb0000000 | sys_color_table.work_text, wind_speed_str, myw.wind_speed); |
||
8556 | superturbo | 221 | draw_format_text_sys(20, START_YPOS+140, 0xb0000000 | sys_color_table.work_text, wind_deg_str, myw.wind_deg); |
222 | draw_format_text_sys(20, START_YPOS+160, 0xb0000000 | sys_color_table.work_text, visibility_str, myw.visibility); |
||
223 | // Определяем кнопку |
||
8735 | turbocat | 224 | _ksys_define_button(new_win_w/2-60, START_YPOS+180, 120, 30, BTN_UPDATE, sys_color_table.work_button); |
225 | _ksys_draw_text(update_str, (new_win_w/2)-(UTF8_W*strlen(update_str)/2/char_size), START_YPOS+190, 0, 0xb0000000 | sys_color_table.work_button_text); |
||
226 | _ksys_end_draw(); |
||
8553 | superturbo | 227 | } |
228 | |||
8735 | turbocat | 229 | void get_config(char **city, char **token, char **units) // Загружаем конфиг |
8553 | superturbo | 230 | { |
9820 | dunkaist | 231 | ksys_ufile_t config_j; |
232 | config_j = _ksys_load_file(config1_name); |
||
8735 | turbocat | 233 | if(!config_j.size){ |
9820 | dunkaist | 234 | config_j = _ksys_load_file(config2_name); |
235 | if(!config_j.size){ |
||
236 | notify_show("'Configuration file not found!' -E"); |
||
237 | exit(0); |
||
238 | } |
||
8553 | superturbo | 239 | } |
8735 | turbocat | 240 | |
241 | json_value* value =json_parse (config_j.data, config_j.size); // Парсим конфиг |
||
8553 | superturbo | 242 | for(int i=0; i |
8566 | superturbo | 243 | if(!strcmp(JSON_OBJ(i).name, "Location") && JSON_OBJ(i).value->type==json_string){ |
8735 | turbocat | 244 | *city = JSON_OBJ(i).value->u.string.ptr; // Получаем название города |
8553 | superturbo | 245 | } |
8566 | superturbo | 246 | else if(!strcmp(JSON_OBJ(i).name, "Token") && JSON_OBJ(i).value->type==json_string){ |
8735 | turbocat | 247 | *token = JSON_OBJ(i).value->u.string.ptr; // Получаем токен |
8553 | superturbo | 248 | } |
8566 | superturbo | 249 | else if(!strcmp(JSON_OBJ(i).name, "Celsius") && JSON_OBJ(i).value->type==json_boolean){ |
8555 | superturbo | 250 | if(JSON_OBJ(i).value->u.boolean){ |
8735 | turbocat | 251 | *units = "metric"; |
8555 | superturbo | 252 | temp_char = 'C'; |
253 | }else{ |
||
8735 | turbocat | 254 | *units = "imperial"; |
8555 | superturbo | 255 | temp_char = 'F'; |
256 | } |
||
257 | } |
||
8566 | superturbo | 258 | else if(!strcmp(JSON_OBJ(i).name, "Lang") && JSON_OBJ(i).value->type==json_string){ |
259 | strncpy(lang, JSON_OBJ(i).value->u.string.ptr,2); // Получаем язык |
||
8555 | superturbo | 260 | } |
8566 | superturbo | 261 | else if(!strcmp(JSON_OBJ(i).name, "AutoUpdate") && JSON_OBJ(i).value->type==json_integer){ |
8735 | turbocat | 262 | auto_update_time = JSON_OBJ(i).value->u.integer; // Получаем время автообновлений данных |
8566 | superturbo | 263 | } |
8553 | superturbo | 264 | } |
8735 | turbocat | 265 | if(*city==NULL || *token ==NULL){ |
8555 | superturbo | 266 | notify_show("'Invalid config!' -E"); |
8553 | superturbo | 267 | exit(0); |
268 | } |
||
8735 | turbocat | 269 | free(config_j.data); |
8553 | superturbo | 270 | } |
271 | |||
8735 | turbocat | 272 | void update(char* city, char* token, char* units) // Обновление данных |
8553 | superturbo | 273 | { |
8554 | superturbo | 274 | if(blend!=NULL){ |
8566 | superturbo | 275 | img_destroy(blend); // Уничтожение картинику с прозрачностью |
8555 | superturbo | 276 | blend = NULL; |
8554 | superturbo | 277 | } |
8556 | superturbo | 278 | memset(&myw, 0, sizeof myw); // Обнуляем структуру |
8735 | turbocat | 279 | strcpy(myw.city,"None"); |
8554 | superturbo | 280 | strcpy(myw.weath_desc,"unknown"); |
8556 | superturbo | 281 | http_msg *json_file = get_json(city, token, units); // Получаем данные о погоде в формате json |
8553 | superturbo | 282 | if(json_file != NULL){ |
8566 | superturbo | 283 | json_value* value=json_parse(json_file->content_ptr, json_file->content_length); // Парсим json файл |
8556 | superturbo | 284 | find_and_set(value, &myw); // Ищем значения в json |
285 | sprintf(format_temp_str, "%s°%c","%d",temp_char); // Формируем строку для вывода температуры |
||
8566 | superturbo | 286 | get_image(); // Получаем картинку |
287 | json_value_free(value); // Уничтожаем полученные json значения |
||
288 | http_free(json_file); |
||
8553 | superturbo | 289 | }else{ |
8554 | superturbo | 290 | notify_show("'Connection error!' -E"); |
8553 | superturbo | 291 | } |
292 | } |
||
293 | |||
8555 | superturbo | 294 | void set_lang() |
295 | { |
||
296 | if(!strcmp(lang, "ru")){ |
||
8556 | superturbo | 297 | wind_speed_str = "Скорость ветра: %d м/с"; |
298 | pressure_str = "Давление: %d гПa"; |
||
299 | visibility_str = "Видимость: %d м"; |
||
300 | humidity_str = "Влажность: %d %s"; |
||
8555 | superturbo | 301 | update_str = "Обновить"; |
8556 | superturbo | 302 | wind_deg_str = "Направление ветра: %d°"; |
303 | WINDOW_W = 250; |
||
8555 | superturbo | 304 | char_size = 2; |
305 | }else if(!strcmp(lang, "de")){ |
||
306 | wind_speed_str = "Windgeschwindigkeit: %d m/s"; |
||
307 | pressure_str = "Druck: %d hPa"; |
||
308 | visibility_str = "Sichtbarkeit: %d m"; |
||
8556 | superturbo | 309 | humidity_str = "Luftfeuchtigkeit: %d %s"; |
310 | wind_deg_str = "Windrichtung %d°"; |
||
8555 | superturbo | 311 | WINDOW_W = 270; |
312 | update_str = "Aktualisieren"; |
||
313 | }else{ |
||
8556 | superturbo | 314 | pressure_str = "Pressure: %d hPa"; |
315 | humidity_str = "Humidity: %d %s"; |
||
316 | visibility_str = "Visibility: %d m"; |
||
317 | wind_speed_str = "Wind speed: %d m/s"; |
||
318 | wind_deg_str = "Wind direction: %d°"; |
||
8555 | superturbo | 319 | update_str = "Refresh"; |
320 | } |
||
321 | } |
||
322 | |||
8566 | superturbo | 323 | int main() |
324 | { |
||
8735 | turbocat | 325 | win_pos = _ksys_get_mouse_pos(KSYS_MOUSE_SCREEN_POS); // Получаем позицию курсора |
326 | _ksys_get_system_colors(&sys_color_table); // Получаем таблица цветов |
||
8555 | superturbo | 327 | |
8735 | turbocat | 328 | char *city=NULL, *token=NULL, *units=NULL; // Указатели на токен, название города, систему мер |
8554 | superturbo | 329 | |
8735 | turbocat | 330 | get_config(&city, &token, &units); // Загружаем конфиг |
8556 | superturbo | 331 | set_lang(); // Установить язык приложения |
8735 | turbocat | 332 | update(city, token, units); |
8556 | superturbo | 333 | |
8566 | superturbo | 334 | uint32_t (*event)(); |
335 | |||
8735 | turbocat | 336 | if(auto_update_time<=0){ |
337 | event = _ksys_get_event; |
||
8566 | superturbo | 338 | }else{ |
8735 | turbocat | 339 | event = _ksys_wait_event; |
8566 | superturbo | 340 | } |
341 | |||
8553 | superturbo | 342 | while(1){ |
8735 | turbocat | 343 | switch(event(6000*auto_update_time)){ // Получаем системное событие |
344 | case KSYS_EVENT_NONE: // Нет события |
||
345 | update(city, token, units); |
||
8566 | superturbo | 346 | debug_printf("Weather: Update\n"); |
8553 | superturbo | 347 | break; |
8735 | turbocat | 348 | case KSYS_EVENT_REDRAW: // Событие перерисовки |
349 | redraw_gui(); |
||
8553 | superturbo | 350 | break; |
8735 | turbocat | 351 | case KSYS_EVENT_BUTTON: // Событие кнопок |
352 | switch (_ksys_get_button()){ |
||
8566 | superturbo | 353 | case BTN_UPDATE: |
8735 | turbocat | 354 | update(city, token, units); |
355 | redraw_gui(); |
||
8553 | superturbo | 356 | break; |
8566 | superturbo | 357 | case BTN_QUIT: // Кнопка выхода |
8553 | superturbo | 358 | exit(0); |
359 | break; |
||
360 | } |
||
8566 | superturbo | 361 | break; |
362 | } |
||
8553 | superturbo | 363 | } |
364 | return 0; |
||
365 | }=0){> |