make the code simpler

This commit is contained in:
Marcel Walter 2026-01-05 01:02:41 +01:00
parent 49d043bd5e
commit 453cf8094c
4 changed files with 62 additions and 66 deletions

View File

@ -1,14 +0,0 @@
#pragma once
#include <Arduino.h>
class ITrigger
{
public:
ITrigger(/* args */) = default;
~ITrigger() = default;
virtual void printText(String text) = 0;
virtual void printOpen() = 0;
virtual void printClose() = 0;
};

View File

@ -3,7 +3,11 @@
#include <UniversalTelegramBot.h>
#include <WiFiClientSecure.h>
#include "ITrigger.h"
struct TelegramCommand
{
const String command;
void (*const functionPointer) (const String arguments);
};
class Telegram
{
@ -13,14 +17,15 @@ private:
unsigned long bot_lasttime; // last time messages' scan has been done
ITrigger &trigger;
WiFiClientSecure &secured_client;
UniversalTelegramBot bot;
TelegramCommand *cmdList;
uint32_t cmdListCount;
void handleNewMessages(int numNewMessages);
public:
Telegram(WiFiClientSecure &sec_client, ITrigger &trig);
Telegram(WiFiClientSecure &sec_client, TelegramCommand commandList[], uint32_t commandListCount);
~Telegram();
void init();

View File

@ -36,41 +36,44 @@ void Telegram::handleNewMessages(int numNewMessages)
// more info here - https://core.telegram.org/bots/api#sendchataction
}
if (text == "/send_test_action2")
for (uint32_t i = 0; i < cmdListCount; i++)
{
bot.sendChatAction(chat_id, "find_location");
delay(4000);
bot.sendMessage(chat_id, "Did you see the action message?");
}
if (text.startsWith(cmdList[i].command))
{
String Arguments = "";
if (text.length() > cmdList[i].command.length() + 1)
{
//Arguments are on space behind the command string
Arguments = text.substring(cmdList[i].command.length() + 1);
}
if (text.startsWith("/txt "))
{
trigger.printText(text.substring(5));
}
if (cmdList[i].functionPointer != nullptr)
{
cmdList[i].functionPointer(Arguments);
}
if (text.startsWith("/open"))
{
trigger.printOpen();
break;
}
}
if (text.startsWith("/close"))
if (text == "/display_start")
{
trigger.printClose();
}
String welcome = "Willkommen beim Tür-Display-Bot," + from_name + ".\n";
welcome += "Hier kommen die möglichen Kommandos, die ich verstehe:\n";
for (uint32_t i = 0; i < cmdListCount; i++)
{
welcome += cmdList[i].command + "\n";
}
if (text == "/start")
{
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
welcome += "This is Chat Action Bot example.\n\n";
welcome += "/send_test_action : to send test chat action message\n";
welcome += "/send_test_action2 : to send test chat action message\n";
bot.sendMessage(chat_id, welcome);
}
}
}
Telegram::Telegram(WiFiClientSecure &sec_client, ITrigger &trig)
: trigger(trig), secured_client(sec_client), bot(BOT_TOKEN, sec_client) {}
Telegram::Telegram(WiFiClientSecure &sec_client, TelegramCommand commandList[], uint32_t commandListCount)
: secured_client(sec_client), bot(BOT_TOKEN, sec_client), cmdList(commandList), cmdListCount(commandListCount)
{ }
Telegram::~Telegram() {}

View File

@ -1,48 +1,50 @@
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoOTA.h>
#include "Telegram.h"
#include "ITrigger.h"
#include "FlipDotDrv.h"
#include "secrets.h"
bool OTAStarted;
void onOTAStart() {
OTAStarted = true;
}
// BearSSL::WiFiClientSecure secured_client;
WiFiClientSecure secured_client;
bool Start = false;
FlipDotDrv flip(96, 16, 1);
class myTrigger : public ITrigger
void printOpen(String Args)
{
private:
/* data */
public:
myTrigger(/* args */) = default;
~myTrigger() = default;
flip.printText("offen", 18, 0, 2);
}
void printText(String text) override
{
Serial.print("Display would show '");
Serial.print(text);
Serial.println("'");
flip.printText(text.c_str());
}
void printClose(String Args)
{
flip.printText("geschlossen", 15, 4, 1);
}
void printOpen() override
{
flip.printText("offen", 18, 0, 2);
}
void printText(String Args)
{
Serial.print("Display would show '");
Serial.print(Args);
Serial.println("'");
flip.printText(Args.c_str());
}
void printClose() override
{
flip.printText("geschlossen", 15, 4, 1);
}
TelegramCommand myCmdList[] {
{"/display_open", printOpen},
{"/display_close", printClose},
{"/display_text", printText},
};
} trigg;
uint32_t myCmdListCnt = sizeof(myCmdList) / sizeof(myCmdList[0]);
Telegram tele(secured_client, trigg);
Telegram tele(secured_client, myCmdList, myCmdListCnt);
uint8_t rawBuff[192] = {};