Skip to content

Commit 9dccd21

Browse files
committed
upd
1 parent be78217 commit 9dccd21

File tree

3 files changed

+63
-70
lines changed

3 files changed

+63
-70
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ void loop() {
835835
- добавил возможность редактирования файлов (из SPIFFS или буфера)
836836
- добавил пример отправки фото с камеры ESP32-CAM
837837
- v2.21: ускорил отправку файлов ботом в чат
838+
- v2.22: мелкая оптимизация, исправил ошибку компиляции при дефайне FB_NO_OTA
838839
839840
<a id="feedback"></a>
840841
## Баги и обратная связь

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=FastBot
2-
version=2.21
2+
version=2.22
33
author=AlexGyver <[email protected]>
44
maintainer=AlexGyver <[email protected]>
5-
sentence=ESP8266/ESP32 library for Telegram bot (messages, menus, RTC time sync, OTA update)
6-
paragraph=ESP8266/ESP32 library for Telegram bot (messages, menus, RTC time sync, OTA update)
5+
sentence=Fast ESP8266/ESP32 library for Telegram bot (messages, menus, time sync, OTA update + SPIFFS, files upload and download)
6+
paragraph=Fast ESP8266/ESP32 library for Telegram bot (messages, menus, time sync, OTA update + SPIFFS, files upload and download)
77
category=Communication
88
url=https://github.com/GyverLibs/FastBot
99
architectures=esp8266, esp32

src/FastBot.h

Lines changed: 59 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
- добавил возможность редактирования файлов (из SPIFFS или буфера)
9595
- добавил пример отправки фото с камеры ESP32-CAM
9696
v2.21: ускорил отправку файлов ботом в чат
97+
v2.22: мелкая оптимизация, исправил ошибку компиляции при дефайне FB_NO_OTA
9798
*/
9899

99100
/*
@@ -115,12 +116,6 @@
115116
#define FB_BLOCK_SIZE 1024
116117

117118
// ============================================
118-
#ifdef ESP8266
119-
#define FB_SECURE_CLIENT BearSSL::WiFiClientSecure
120-
#else
121-
#define FB_SECURE_CLIENT WiFiClientSecure
122-
#endif
123-
124119
#define FB_TEXT 0
125120
#define FB_MARKDOWN 1
126121
#define FB_HTML 2
@@ -762,11 +757,12 @@ class FastBot {
762757

763758
// ===================== OTA =====================
764759
// ОТА обновление, вызывать внутри обработчика сообщения по флагу OTA
765-
uint8_t update(uint8_t type = FB_FIRMWARE) {
760+
uint8_t update(__attribute__((unused)) uint8_t type = FB_FIRMWARE) {
761+
#ifndef FB_NO_OTA
766762
if (!_file_ptr) return 8;
767763
OTAflag = type;
768764
sendMessage((type == FB_FIRMWARE) ? F("OTA firmware...") : F("OTA spiffs..."), _otaID);
769-
765+
770766
#ifdef ESP8266
771767
ESPhttpUpdate.rebootOnUpdate(false);
772768
#ifdef FB_DYNAMIC
@@ -782,6 +778,7 @@ class FastBot {
782778
if (OTAflag == FB_FIRMWARE) OTAstate = httpUpdate.update(client, *_file_ptr);
783779
else if (OTAflag == FB_SPIFFS) OTAstate = httpUpdate.updateSpiffs(client, *_file_ptr);
784780
#endif
781+
#endif
785782
return 1;
786783
}
787784

@@ -847,8 +844,18 @@ class FastBot {
847844
}
848845
#endif
849846

847+
// ============================ PRIVATE ============================
850848
private:
849+
// конечная строка запроса
851850
#define FB_END_REQ "\r\n" "--FAST_BOT--"
851+
852+
// тип клиента в зависимости от платформы
853+
#ifdef ESP8266
854+
#define FB_SECURE_CLIENT BearSSL::WiFiClientSecure
855+
#else
856+
#define FB_SECURE_CLIENT WiFiClientSecure
857+
#endif
858+
852859
// ============================ MULTIPART SEND ============================
853860
bool _multipartSend(FB_SECURE_CLIENT& client, uint32_t length, FB_FileType type, const String& name, const String& id) {
854861
if (!client.connect("api.telegram.org", 443)) return 0;
@@ -947,41 +954,21 @@ class FastBot {
947954
return (parseRequest(resp) ? 1 : 3); // 1 - ok, 3 - telegram err
948955
}
949956

950-
// ============================ SEND ============================
951-
uint8_t _sendFile(uint8_t* buf, uint32_t length, FB_FileType type, const String& name, const String& id) {
952-
#ifdef ESP8266
953-
#ifdef FB_DYNAMIC
954-
BearSSL::WiFiClientSecure client;
955-
client.setInsecure();
956-
#endif
957-
#else
958-
WiFiClientSecure client;
959-
client.setInsecure();
960-
#endif
961-
if (!_multipartSend(client, length, type, name, id)) return 4;
957+
// ============================ ROUTINE BUF ============================
958+
void _sendBufRoutine(FB_SECURE_CLIENT& client, uint8_t* buf, uint32_t length) {
962959
uint32_t sz = length;
963-
uint32_t st = 0;
960+
uint8_t* bufs = buf;
964961
while (sz) {
965962
uint32_t len = min((uint32_t)FB_BLOCK_SIZE, sz);
966-
client.write(buf + st, len);
967-
st += len;
963+
client.write(bufs, len);
964+
bufs += len;
968965
sz -= len;
969966
}
970-
return _multipartEnd(client);
971967
}
972968

969+
// ============================ ROUTINE FILE ============================
973970
#ifdef FS_H
974-
uint8_t _sendFile(File &file, FB_FileType type, const String& name, const String& id) {
975-
#ifdef ESP8266
976-
#ifdef FB_DYNAMIC
977-
BearSSL::WiFiClientSecure client;
978-
client.setInsecure();
979-
#endif
980-
#else
981-
WiFiClientSecure client;
982-
client.setInsecure();
983-
#endif
984-
if (!_multipartSend(client, file.size(), type, name, id)) return 4;
971+
void _sendFileRoutine(FB_SECURE_CLIENT& client, File &file) {
985972
uint8_t buf[FB_BLOCK_SIZE];
986973
uint32_t sz = file.size();
987974
while (sz) {
@@ -990,53 +977,58 @@ class FastBot {
990977
client.write(buf, len);
991978
sz -= len;
992979
}
993-
return _multipartEnd(client);
994980
}
995981
#endif
996982

997-
// ============================ EDIT ============================
998-
uint8_t _editFile(uint8_t* buf, uint32_t length, FB_FileType type, const String& name, int32_t msgid, const String& id) {
983+
// ============================ CLIENT MACRO ============================
984+
// макрос создания клиента в зависимости от платформы и настроек
999985
#ifdef ESP8266
1000986
#ifdef FB_DYNAMIC
1001-
BearSSL::WiFiClientSecure client;
1002-
client.setInsecure();
987+
#define FB_DECLARE_CLIENT() \
988+
BearSSL::WiFiClientSecure client; \
989+
client.setInsecure();
990+
#else
991+
#define FB_DECLARE_CLIENT()
1003992
#endif
1004993
#else
1005-
WiFiClientSecure client;
1006-
client.setInsecure();
994+
#define FB_DECLARE_CLIENT() \
995+
WiFiClientSecure client; \
996+
client.setInsecure();
1007997
#endif
998+
999+
// ============================ SEND BUF ============================
1000+
uint8_t _sendFile(uint8_t* buf, uint32_t length, FB_FileType type, const String& name, const String& id) {
1001+
FB_DECLARE_CLIENT();
1002+
if (!_multipartSend(client, length, type, name, id)) return 4;
1003+
_sendBufRoutine(client, buf, length);
1004+
return _multipartEnd(client);
1005+
}
1006+
1007+
// ============================ SEND FILE ============================
1008+
#ifdef FS_H
1009+
uint8_t _sendFile(File &file, FB_FileType type, const String& name, const String& id) {
1010+
FB_DECLARE_CLIENT();
1011+
if (!_multipartSend(client, file.size(), type, name, id)) return 4;
1012+
_sendFileRoutine(client, file);
1013+
return _multipartEnd(client);
1014+
}
1015+
#endif
1016+
1017+
// ============================ EDIT BUF ============================
1018+
uint8_t _editFile(uint8_t* buf, uint32_t length, FB_FileType type, const String& name, int32_t msgid, const String& id) {
1019+
FB_DECLARE_CLIENT();
10081020
if (!_multipartEdit(client, length, type, name, msgid, id)) return 4;
1009-
uint32_t sz = length;
1010-
uint32_t st = 0;
1011-
while (sz) {
1012-
uint32_t len = min((uint32_t)FB_BLOCK_SIZE, sz);
1013-
client.write(buf + st, len);
1014-
st += len;
1015-
sz -= len;
1016-
}
1021+
_sendBufRoutine(client, buf, length);
10171022
return _multipartEnd(client);
10181023
}
1024+
10191025

1026+
// ============================ EDIT FILE ============================
10201027
#ifdef FS_H
10211028
uint8_t _editFile(File& file, FB_FileType type, const String& name, int32_t msgid, const String& id) {
1022-
#ifdef ESP8266
1023-
#ifdef FB_DYNAMIC
1024-
BearSSL::WiFiClientSecure client;
1025-
client.setInsecure();
1026-
#endif
1027-
#else
1028-
WiFiClientSecure client;
1029-
client.setInsecure();
1030-
#endif
1029+
FB_DECLARE_CLIENT();
10311030
if (!_multipartEdit(client, file.size(), type, name, msgid, id)) return 4;
1032-
uint8_t buf[FB_BLOCK_SIZE];
1033-
uint32_t sz = file.size();
1034-
while (sz) {
1035-
uint32_t len = min((uint32_t)FB_BLOCK_SIZE, sz);
1036-
file.read(buf, len);
1037-
client.write(buf, len);
1038-
sz -= len;
1039-
}
1031+
_sendFileRoutine(client, file);
10401032
return _multipartEnd(client);
10411033
}
10421034
#endif

0 commit comments

Comments
 (0)