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