Skip to content

Commit

Permalink
Added SerialCommandTask
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszbielawski committed Mar 30, 2020
1 parent f457a73 commit 48b85c8
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
74 changes: 74 additions & 0 deletions src/SerialCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* SerialCommand.cpp
*
* Created on: 30.04.2020
* Author: Bartosz Bielawski
*/

#include <SerialCommand.h>
#include <utils.h>
#include <DataStore.h>

SerialCommandTask::SerialCommandTask() {}

void SerialCommandTask::run()
{
while (Serial.available())
{
auto c = Serial.read();
if (c == '\r')
continue;

if (c != '\n')
{
cumulatedInput += (char)c;
continue;
}

logPrintfX(F("SCT"), F("Command received: %s"), cumulatedInput.c_str());

auto cmdAndParam = splitLine(cumulatedInput);
cumulatedInput.clear();

const String& cmd = cmdAndParam.first;
const String& param = cmdAndParam.second;
const char* c_param = param.c_str();

if (cmd == "reset")
{
rebootClock();
continue;
}

if (cmd[0] == '$')
{
String variableName = cmd.substring(1);
const char* variableNameC = variableName.c_str();

//read
if (param.length() == 0)
{
logPrintfX(F("SCT"), F("%s = '%s'"), variableNameC, dataSource(variableNameC).c_str());
continue;
}

//write
DataStore::value(variableName) = param;
continue;
}

if (cmd == "variables")
{
for (const auto& k: DataStore::availableKeys())
{
logPrintfX(F("SCT"), F("%s = '%s'"), k.c_str(), dataSource(k.c_str()).c_str());
}
continue;
}

logPrintfX(F("SCT"), F("Unknown commmand!"));

}
sleep(0.1_s);
}

24 changes: 24 additions & 0 deletions src/SerialCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SerialCommand.h
*
* Created on: 30.04.2020
* Author: Bartosz Bielawski
*/

#ifndef SERIALCOMMAND_H
#define SERIALCOMMAND_H

#include <tasks.hpp>
#include <Arduino.h>

class SerialCommandTask: public Tasks::Task
{
public:
SerialCommandTask();
virtual void run();
virtual ~SerialCommandTask() = default;
private:
String cumulatedInput;
};

#endif /* SERIALCOMMAND_H */
4 changes: 3 additions & 1 deletion src/tasks_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <MQTTTask.h>
#include <WeatherGetter.h>
#include <LocalSensorTask.h>
#include <SerialCommand.h>

//ESP8266 raw API
extern "C" {
Expand Down Expand Up @@ -54,8 +55,9 @@ void setupTasks()
addTask(&WebServerTask::getInstance());
addTask(&DisplayTask::getInstance());

addTask(new SerialCommandTask, 0);
// LHC task seems to be broken in some strange way - with an exception in the HTTP client
//addOptionalTask<LHCStatusReaderNew>(F("lhcEnabled"), TaskDescriptor::CONNECTED | TaskDescriptor::SLOW);
//addOptionalTask<LHCStatusReaderNew>(F("lhcEnabled"), TaskDescriptor::CONNECTED | TaskDescriptor::SLOW);
addOptionalTask<LEDBlinker>(F("ledEnabled"), 0);
addOptionalTask<WeatherGetter>(F("owmEnabled"), TaskDescriptor::SLOW | TaskDescriptor::CONNECTED);
addOptionalTask<MQTTTask>(F("mqttEnabled"), TaskDescriptor::CONNECTED);
Expand Down
9 changes: 6 additions & 3 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ bool checkFileSystem()
return alreadyFormatted;
}

std::pair<String, String> splitLine(String&& line)


std::pair<String, String> splitLine(String& line)
{
std::pair<String, String> result;

Expand Down Expand Up @@ -228,11 +230,12 @@ void readConfigFromFS()

while (file.available())
{
auto p = splitLine(readLine(file));
String l = readLine(file);
auto p = splitLine(l);
if (not p.second.length())
continue;

logPrintfX("UTL", F("Config: %s = %s"), p.first.c_str(), p.second.c_str());
logPrintfX("UTL", F("Config: %s = '%s'"), p.first.c_str(), p.second.c_str());
DataStore::value(p.first) = p.second;
}
SPIFFS.end();
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace fs
};

String readLine(fs::File& file);

std::pair<String, String> splitLine(String& line);
std::vector<String> tokenize(const String& s);

#endif /* UTILS_H_ */

0 comments on commit 48b85c8

Please sign in to comment.