tcMenu
Toggle main menu visibility
Loading...
Searching...
No Matches
src
remote
BaseRemoteComponents.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4
*/
5
10
11
#ifndef TCMENU_BASEREMOTECOMPONENTS_H
12
#define TCMENU_BASEREMOTECOMPONENTS_H
13
14
#include <PlatformDetermination.h>
15
#include "
../RemoteConnector.h
"
16
17
// This defines the number of different connections that can be established, for example each websocket, or each
18
// tagval connection takes one of these, you can define this to a different number as a build flag. Web server static
19
// content servers don't count toward this.
20
#ifndef ALLOWED_CONNECTIONS
21
#define ALLOWED_CONNECTIONS 4
22
#endif
23
24
namespace
tcremote {
25
26
class
BaseRemoteServerConnection
;
27
33
class
DeviceInitialisation
{
34
protected
:
35
bool
initialised =
false
;
36
public
:
37
bool
isInitialised()
const
{
return
initialised; }
38
virtual
bool
attemptInitialisation()=0;
39
virtual
bool
attemptNewConnection(
BaseRemoteServerConnection
* remoteConnection)=0;
40
};
41
46
class
NoInitialisationNeeded :
public
DeviceInitialisation
{
47
private
:
48
bool
attemptConnectionReturn;
49
public
:
50
explicit
NoInitialisationNeeded(
bool
attemptConnectionReturn =
true
) : attemptConnectionReturn(attemptConnectionReturn) {}
51
52
bool
attemptInitialisation()
override
{
53
initialised =
true
;
54
return
true
;
55
}
56
57
bool
attemptNewConnection(
BaseRemoteServerConnection
*transport)
override
{
return
true
; }
58
};
59
60
enum
RemoteServerType: uint8_t {
61
TAG_VAL_REMOTE_SERVER, SIMHUB_CONNECTOR, TAG_VAL_WEB_SOCKET
62
};
63
64
class
BaseRemoteServerConnection {
65
protected
:
66
DeviceInitialisation
&initialisation;
67
RemoteServerType remoteServerType;
68
public
:
69
BaseRemoteServerConnection(
DeviceInitialisation
&initialisation, RemoteServerType remoteServerType)
70
: initialisation(initialisation), remoteServerType(remoteServerType) {}
71
72
RemoteServerType getRemoteServerType() {
return
remoteServerType; }
73
74
DeviceInitialisation
& getDeviceInitialisation()
const
{
return
initialisation; }
75
void
runLoop();
76
virtual
void
init(
int
remoteNumber,
const
ConnectorLocalInfo
& info) = 0;
77
virtual
void
tick() = 0;
78
virtual
bool
connected() = 0;
79
virtual
void
copyConnectionStatus(
char
*buffer,
int
bufferSize) = 0;
80
virtual
void
notifyRemoteHasClosed() {}
81
};
82
88
class
TagValueRemoteServerConnection :
public
BaseRemoteServerConnection {
89
private
:
90
TagValueRemoteConnector
remoteConnector;
91
TagValueTransport
&remoteTransport;
92
CombinedMessageProcessor
messageProcessor;
93
public
:
94
TagValueRemoteServerConnection(
TagValueTransport
&transport,
DeviceInitialisation
& initialisation);
95
96
void
init(
int
remoteNumber,
const
ConnectorLocalInfo
& info)
override
;
97
98
TagValueRemoteConnector
*connector() {
return
&remoteConnector; }
99
100
TagValueTransport
*transport() {
return
&remoteTransport; }
101
102
CombinedMessageProcessor
*messageProcessors() {
return
&messageProcessor; }
103
104
void
tick()
override
;
105
bool
connected()
override
{
return
remoteTransport.connected(); }
106
107
void
copyConnectionStatus(
char
*buffer,
int
bufferSize)
override
;
108
109
void
notifyRemoteHasClosed()
override
;
110
};
111
117
class
TcMenuRemoteServer
:
public
Executable {
118
BaseRemoteServerConnection
* connections[ALLOWED_CONNECTIONS];
119
const
ConnectorLocalInfo
& appInfo;
120
uint8_t remotesAdded;
121
public
:
129
explicit
TcMenuRemoteServer
(
const
ConnectorLocalInfo
& appInfo) : connections{}, appInfo(appInfo), remotesAdded(0) { }
130
134
void
clearRemotes
() {
135
remotesAdded = 0;
136
}
137
138
void
exec()
override
;
139
146
uint8_t
addConnection
(
BaseRemoteServerConnection
*toAdd);
147
151
uint8_t
remoteCount
()
const
{
return
remotesAdded; }
152
158
TagValueRemoteConnector
*
getRemoteConnector
(
int
num) {
159
if
(num >= remotesAdded || connections[num]->getRemoteServerType() != TAG_VAL_REMOTE_SERVER)
return
nullptr
;
160
return
reinterpret_cast<
TagValueRemoteServerConnection
*
>
(connections[num])->connector();
161
}
162
168
TagValueTransport
*
getTransport
(
int
num) {
169
if
(num >= remotesAdded || connections[num]->getRemoteServerType() != TAG_VAL_REMOTE_SERVER)
return
nullptr
;
170
return
reinterpret_cast<
TagValueRemoteServerConnection
*
>
(connections[num])->transport();
171
}
172
179
BaseRemoteServerConnection
*
getRemoteServerConnection
(
int
num) {
180
if
(num >= remotesAdded)
return
nullptr
;
181
return
connections[num];
182
}
183
184
void
setHeartbeatIntervalAll(uint16_t milli);
185
};
186
194
int
fromWiFiRSSITo4StateIndicator(
int
strength);
195
196
}
197
198
#endif
//TCMENU_BASEREMOTECOMPONENTS_H
RemoteConnector.h
Contains the base functionality for communication between the menu library and remote APIs.
CombinedMessageProcessor
Definition
MessageProcessors.h:146
TagValueRemoteConnector
Definition
RemoteConnector.h:187
TagValueTransport
Definition
RemoteConnector.h:141
tcremote::BaseRemoteServerConnection
Definition
BaseRemoteComponents.h:64
tcremote::DeviceInitialisation
Definition
BaseRemoteComponents.h:33
tcremote::TagValueRemoteServerConnection
Definition
BaseRemoteComponents.h:88
tcremote::TcMenuRemoteServer::getRemoteServerConnection
BaseRemoteServerConnection * getRemoteServerConnection(int num)
Definition
BaseRemoteComponents.h:179
tcremote::TcMenuRemoteServer::addConnection
uint8_t addConnection(BaseRemoteServerConnection *toAdd)
Definition
BaseRemoteComponents.cpp:63
tcremote::TcMenuRemoteServer::remoteCount
uint8_t remoteCount() const
Definition
BaseRemoteComponents.h:151
tcremote::TcMenuRemoteServer::clearRemotes
void clearRemotes()
Definition
BaseRemoteComponents.h:134
tcremote::TcMenuRemoteServer::TcMenuRemoteServer
TcMenuRemoteServer(const ConnectorLocalInfo &appInfo)
Definition
BaseRemoteComponents.h:129
tcremote::TcMenuRemoteServer::getTransport
TagValueTransport * getTransport(int num)
Definition
BaseRemoteComponents.h:168
tcremote::TcMenuRemoteServer::getRemoteConnector
TagValueRemoteConnector * getRemoteConnector(int num)
Definition
BaseRemoteComponents.h:158
ConnectorLocalInfo
Definition
tcUtil.h:25