wip commit
This commit is contained in:
@@ -131,9 +131,16 @@ SerialInput::doHelp(Args& args, Print& out)
|
||||
out.println("Available commands:");
|
||||
auto sched = MainLoop::instance()->scheduler;
|
||||
for(auto task : sched.tasks) {
|
||||
for(auto &command : task->commands()) {
|
||||
out.print(command.name);
|
||||
out.print(" ");
|
||||
const auto taskCommands = task->commands();
|
||||
if (!taskCommands.empty()) {
|
||||
out.print(task->name);
|
||||
out.println(":");
|
||||
out.print("\t");
|
||||
for(auto &command : task->commands()) {
|
||||
out.print(command.name);
|
||||
out.print(" ");
|
||||
}
|
||||
out.println();
|
||||
}
|
||||
}
|
||||
out.println();
|
||||
|
||||
147
src/inputs/Weather.cpp
Normal file
147
src/inputs/Weather.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
#include "Weather.h"
|
||||
#include "../Static.h"
|
||||
|
||||
Weather::Weather()
|
||||
: BufferedInputSource("Weather")
|
||||
{
|
||||
m_parser.setListener(this);
|
||||
}
|
||||
|
||||
void
|
||||
Weather::handleEvent(const InputEvent& evt)
|
||||
{
|
||||
BufferedInputSource::handleEvent(evt);
|
||||
OnlineTaskMixin::handleEvent(evt);
|
||||
if (evt.intent == InputEvent::IdleStart) {
|
||||
doWeatherEvent();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Weather::loop()
|
||||
{
|
||||
BufferedInputSource::loop();
|
||||
OnlineTaskMixin::loop();
|
||||
EVERY_N_SECONDS(3600) {
|
||||
update();
|
||||
}
|
||||
if (m_fetching) {
|
||||
parse();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Weather::update()
|
||||
{
|
||||
if (!m_fetching) {
|
||||
Log.info("Connecting to weather service");
|
||||
m_parser.reset();
|
||||
const String host = "api.openweathermap.org";
|
||||
const uint8_t port = 80;
|
||||
#ifdef ESP32
|
||||
m_fetching = m_client.connect(host.c_str(), port);
|
||||
#else
|
||||
m_fetching = m_client.connect(host, port);
|
||||
#endif
|
||||
if (m_fetching) {
|
||||
Log.info("Ok!");
|
||||
m_foundBody = false;
|
||||
m_client.print(
|
||||
"GET /data/2.5/weather?id=2950159&appid=f7aea43b54bcd27f542195a809c1463f&units=metric&lang=en HTTP/1.1\r\n"
|
||||
"Host: api.openweathermap.org\r\n"
|
||||
"Connection: close\r\n\r\n"
|
||||
);
|
||||
} else {
|
||||
Log.info("No :(");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Weather::parse()
|
||||
{
|
||||
if (m_client.available()) {
|
||||
char c = m_client.read();
|
||||
if (c == '{') {
|
||||
m_foundBody = true;
|
||||
}
|
||||
if (m_foundBody) {
|
||||
m_parser.parse(c);
|
||||
}
|
||||
} else if (!m_client.connected()) {
|
||||
Log.info("Disconnected");
|
||||
m_client.stop();
|
||||
m_fetching = false;
|
||||
doWeatherEvent();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Weather::key(String key)
|
||||
{
|
||||
m_curKey = key;
|
||||
}
|
||||
|
||||
void
|
||||
Weather::doWeatherEvent()
|
||||
{
|
||||
Log.info("Dispatching weather event for %d", m_weatherId);
|
||||
if (m_weatherId >= 200 && m_weatherId < 300 ) {
|
||||
setEvent(InputEvent::SetScene, "Thunderstorm");
|
||||
setEvent(InputEvent::SetColor, CRGB(255, 187, 0));
|
||||
} else if (m_weatherId >= 300 && m_weatherId < 400) {
|
||||
setEvent(InputEvent::SetScene, "Drizzle");
|
||||
setEvent(InputEvent::SetColor, CRGB(93, 183, 201));
|
||||
} else if (m_weatherId >= 500 && m_weatherId < 600) {
|
||||
setEvent(InputEvent::SetScene, "Rain");
|
||||
setEvent(InputEvent::SetColor, CRGB(64, 64, 255));
|
||||
} else if (m_weatherId >= 600 && m_weatherId < 700) {
|
||||
setEvent(InputEvent::SetScene, "Snow");
|
||||
setEvent(InputEvent::SetColor, CRGB(255, 255, 255));
|
||||
} else if (m_weatherId == 701) {
|
||||
setEvent(InputEvent::SetScene, "Mist");
|
||||
setEvent(InputEvent::SetColor, CRGB(77, 255, 124));
|
||||
} else if (m_weatherId == 800) {
|
||||
setEvent(InputEvent::SetScene, "Clear");
|
||||
setEvent(InputEvent::SetColor, CRGB(255, 249, 79));
|
||||
} else if (m_weatherId >= 801 && m_weatherId <= 804) {
|
||||
setEvent(InputEvent::SetScene, "Cloudy");
|
||||
setEvent(InputEvent::SetColor, CRGB(102, 110, 110));
|
||||
} else {
|
||||
setEvent(InputEvent::SetScene, "UnknownWeather");
|
||||
setEvent(InputEvent::SetColor, CRGB(255, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Weather::value(String value)
|
||||
{
|
||||
if (m_curField == "weather") {
|
||||
if (m_curKey == "id") {
|
||||
m_weatherId = atoi(value.c_str());
|
||||
} else if (m_curKey == "main") {
|
||||
Log.info("WEATHER ENCOUNTERED: %s", value.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Weather::startObject()
|
||||
{
|
||||
m_curField = m_curKey;
|
||||
}
|
||||
|
||||
void
|
||||
Weather::endObject()
|
||||
{
|
||||
m_curField = "";
|
||||
}
|
||||
|
||||
void Weather::endArray() {}
|
||||
void Weather::startArray() {}
|
||||
void Weather::endDocument() {}
|
||||
void Weather::whitespace(char c) {}
|
||||
void Weather::startDocument() {}
|
||||
|
||||
STATIC_ALLOC(Weather);
|
||||
STATIC_TASK(Weather);
|
||||
51
src/inputs/Weather.h
Normal file
51
src/inputs/Weather.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <Figments.h>
|
||||
#include <JsonListener.h>
|
||||
#include <JsonStreamingParser.h>
|
||||
#include <WiFiClient.h>
|
||||
|
||||
class Weather : public BufferedInputSource, OnlineTaskMixin, JsonListener {
|
||||
public:
|
||||
Weather();
|
||||
|
||||
void handleEvent(const InputEvent& evt) override;
|
||||
void loop() override;
|
||||
|
||||
void onOnline() override {
|
||||
update();
|
||||
}
|
||||
|
||||
void onStart() override {
|
||||
update();
|
||||
}
|
||||
|
||||
void loopOnline() override {
|
||||
}
|
||||
|
||||
void update();
|
||||
void parse();
|
||||
|
||||
virtual void whitespace(char c);
|
||||
virtual void startDocument();
|
||||
virtual void key(String key);
|
||||
virtual void value(String value);
|
||||
virtual void endArray();
|
||||
virtual void endObject();
|
||||
virtual void endDocument();
|
||||
virtual void startArray();
|
||||
virtual void startObject();
|
||||
|
||||
private:
|
||||
JsonStreamingParser m_parser;
|
||||
WiFiClient m_client;
|
||||
bool m_fetching = false;
|
||||
String m_curKey;
|
||||
String m_curField;
|
||||
bool m_foundBody = false;
|
||||
|
||||
int m_weatherId = 0;
|
||||
|
||||
void doWeatherEvent();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user