InfiniTime.git

ref: 9196c18d376d4f18c686bcfec8550f9c8659d5ea

src/displayapp/screens/Weather.cpp


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*  Copyright (C) 2021 Avamander

    This file is part of InfiniTime.

    InfiniTime is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    InfiniTime is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#include "Weather.h"
#include <lvgl/lvgl.h>
#include <components/ble/weather/WeatherService.h>
#include "Label.h"
#include "components/battery/BatteryController.h"
#include "components/ble/BleController.h"
#include "components/ble/weather/WeatherData.h"

using namespace Pinetime::Applications::Screens;

Weather::Weather(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::WeatherService& weather)
  : app {app},
    weatherService(weather),
    screens {app,
             0,
             {[this]() -> std::unique_ptr<Screen> {
                return CreateScreenTemperature();
              },
              [this]() -> std::unique_ptr<Screen> {
                return CreateScreenAir();
              },
              [this]() -> std::unique_ptr<Screen> {
                return CreateScreenClouds();
              },
              [this]() -> std::unique_ptr<Screen> {
                return CreateScreenPrecipitation();
              },
              [this]() -> std::unique_ptr<Screen> {
                return CreateScreenHumidity();
              }},
             Screens::ScreenListModes::UpDown} {
}

Weather::~Weather() {
  lv_obj_clean(lv_scr_act());
}

void Weather::Refresh() {
  if (running) {
    // screens.Refresh();
  }
}

bool Weather::OnButtonPushed() {
  running = false;
  return true;
}

bool Weather::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
  return screens.OnTouchEvent(event);
}

std::unique_ptr<Screen> Weather::CreateScreenTemperature() {
  lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
  lv_label_set_recolor(label, true);
  std::unique_ptr<Controllers::WeatherData::Temperature>& current = weatherService.GetCurrentTemperature();
  if (current->timestamp == 0) {
    // Do not use the data, it's invalid
    lv_label_set_text_fmt(label,
                          "#FFFF00 Temperature#\n\n"
                          "#444444 %d#°C \n\n"
                          "#444444 %d#\n\n"
                          "%d\n"
                          "%d\n",
                          0,
                          0,
                          0,
                          0);
  } else {
    lv_label_set_text_fmt(label,
                          "#FFFF00 Temperature#\n\n"
                          "#444444 %d#°C \n\n"
                          "#444444 %hd#\n\n"
                          "%llu\n"
                          "%lu\n",
                          current->temperature / 100,
                          current->dewPoint,
                          current->timestamp,
                          current->expires);
  }
  lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
  lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
  return std::unique_ptr<Screen>(new Screens::Label(0, 5, label));
}

std::unique_ptr<Screen> Weather::CreateScreenAir() {
  lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
  lv_label_set_recolor(label, true);
  std::unique_ptr<Controllers::WeatherData::AirQuality>& current = weatherService.GetCurrentQuality();
  if (current->timestamp == 0) {
    // Do not use the data, it's invalid
    lv_label_set_text_fmt(label,
                          "#FFFF00 Air quality#\n\n"
                          "#444444 %s#\n"
                          "#444444 %d#\n\n"
                          "%d\n"
                          "%d\n",
                          "",
                          0,
                          0,
                          0);
  } else {
    lv_label_set_text_fmt(label,
                          "#FFFF00 Air quality#\n\n"
                          "#444444 %s#\n"
                          "#444444 %lu#\n\n"
                          "%llu\n"
                          "%lu\n",
                          current->polluter.c_str(),
                          (current->amount / 100),
                          current->timestamp,
                          current->expires);
  }
  lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
  lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
  return std::unique_ptr<Screen>(new Screens::Label(0, 5, label));
}

std::unique_ptr<Screen> Weather::CreateScreenClouds() {
  lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
  lv_label_set_recolor(label, true);
  std::unique_ptr<Controllers::WeatherData::Clouds>& current = weatherService.GetCurrentClouds();
  if (current->timestamp == 0) {
    // Do not use the data, it's invalid
    lv_label_set_text_fmt(label,
                          "#FFFF00 Clouds#\n\n"
                          "#444444 %d%%#\n\n"
                          "%d\n"
                          "%d\n",
                          0,
                          0,
                          0);
  } else {
    lv_label_set_text_fmt(label,
                          "#FFFF00 Clouds#\n\n"
                          "#444444 %hhu%%#\n\n"
                          "%llu\n"
                          "%lu\n",
                          current->amount,
                          current->timestamp,
                          current->expires);
  }
  lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
  lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
  return std::unique_ptr<Screen>(new Screens::Label(0, 5, label));
}

std::unique_ptr<Screen> Weather::CreateScreenPrecipitation() {
  lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
  lv_label_set_recolor(label, true);
  std::unique_ptr<Controllers::WeatherData::Precipitation>& current = weatherService.GetCurrentPrecipitation();
  if (current->timestamp == 0) {
    // Do not use the data, it's invalid
    lv_label_set_text_fmt(label,
                          "#FFFF00 Precipitation#\n\n"
                          "#444444 %d%%#\n\n"
                          "%d\n"
                          "%d\n",
                          0,
                          0,
                          0);
  } else {
    lv_label_set_text_fmt(label,
                          "#FFFF00 Precipitation#\n\n"
                          "#444444 %hhu%%#\n\n"
                          "%llu\n"
                          "%lu\n",
                          current->amount,
                          current->timestamp,
                          current->expires);
  }
  lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
  lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
  return std::unique_ptr<Screen>(new Screens::Label(0, 5, label));
}

std::unique_ptr<Screen> Weather::CreateScreenHumidity() {
  lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
  lv_label_set_recolor(label, true);
  std::unique_ptr<Controllers::WeatherData::Humidity>& current = weatherService.GetCurrentHumidity();
  if (current->timestamp == 0) {
    // Do not use the data, it's invalid
    lv_label_set_text_fmt(label,
                          "#FFFF00 Humidity#\n\n"
                          "#444444 %d%%#\n\n"
                          "%d\n"
                          "%d\n",
                          0,
                          0,
                          0);
  } else {
    lv_label_set_text_fmt(label,
                          "#FFFF00 Humidity#\n\n"
                          "#444444 %hhu%%#\n\n"
                          "%llu\n"
                          "%lu\n",
                          current->humidity,
                          current->timestamp,
                          current->expires);
  }
  lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
  lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
  return std::unique_ptr<Screen>(new Screens::Label(0, 5, label));
}