-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chat.cpp
131 lines (113 loc) · 2.61 KB
/
Chat.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
#include <EEPROM.h>
#include "TimeF.h"
#include "Chat.h"
#include "Defs.h"
#include "Message.h"
extern TimeFunctions timeFunc;
extern UniversalTelegramBot bot;
extern Message message;
extern ChatSet chats;
// ------------------------------------------------------------------------
int ChatSet::AddChat(String id, String name, bool isHChat)
{
Serial.println("*** ChatSet::AddChat()");
int i = 0;
while (i < CHAT_CACHE_SIZE)
{
if (chat[i].GetId().length() == 0)
{
// create new chat
chat[i].SetChatSet(this);
chat[i].SetId(id);
chat[i].SetName(name);
chat[i].SetHolderChat(isHChat);
Serial.print("- add ");
Serial.print(id);
Serial.print(", ");
Serial.print(name);
Serial.print(", ");
Serial.print(isHChat);
Serial.println();
count = i + 1;
// we need to make sure that the description is changed with every request and not identical to the existing one.
return i;
}
else if (chat[i].GetId() == id)
{
// update existing chat
chat[i].SetName(name);
Serial.print(id);
Serial.print(", ");
Serial.print(name);
Serial.println();
return i;
}
++i;
}
return CHAT_NONE;
}
// ------------------------------------------------------------------------
Chat * ChatSet::GetChatFromIndex(int i)
{
if (i >= 0)
return &chat[i];
else
return NULL;
}
// ------------------------------------------------------------------------
Chat * ChatSet::GetChatFromId(String id)
{
int i = 0;
while (i < CHAT_CACHE_SIZE)
{
if (chat[i].GetId() == id)
return &chat[i];
++i;
}
return NULL;
}
// ------------------------------------------------------------------------
Chat * ChatSet::SearchHolderChat()
{
int i = 0;
while (i < CHAT_CACHE_SIZE)
{
// only return those matches of valid chats
if ((chat[i].GetId().length() > 0) &&
chat[i].IsHolderChat())
{
holderChatIndex = i;
return &chat[i];
}
++i;
}
return NULL;
}
// ------------------------------------------------------------------------
int ChatSet::GetIndexFromId(String id)
{
int i = 0;
while (i < CHAT_CACHE_SIZE)
{
if (chat[i].GetId() == id)
return i;
++i;
}
return CHAT_NONE;
}
// ------------------------------------------------------------------------
String ChatSet::GetChatsInfo()
{
int i = 0;
String info;
while (i < CHAT_CACHE_SIZE)
{
if (chat[i].GetId().length() > 0)
{
info += chat[i].GetName() + ":" + chat[i].GetId() + ":" + (chat[i].IsHolderChat() ? "true" : "false") + "; ";
}
++i;
}
return info;
}
//