tcMenu
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 
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 
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 
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 
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 
89  private:
90  TagValueRemoteConnector remoteConnector;
91  TagValueTransport &remoteTransport;
92  CombinedMessageProcessor messageProcessor;
93  public:
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 
147 
151  uint8_t remoteCount() const { return remotesAdded; }
152 
159  if(num >= remotesAdded || connections[num]->getRemoteServerType() != TAG_VAL_REMOTE_SERVER) return nullptr;
160  return reinterpret_cast<TagValueRemoteServerConnection*>(connections[num])->connector();
161  }
162 
169  if(num >= remotesAdded || connections[num]->getRemoteServerType() != TAG_VAL_REMOTE_SERVER) return nullptr;
170  return reinterpret_cast<TagValueRemoteServerConnection*>(connections[num])->transport();
171  }
172 
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
Definition: MessageProcessors.h:146
Definition: RemoteConnector.h:187
Definition: RemoteConnector.h:141
Definition: BaseRemoteComponents.h:64
Definition: BaseRemoteComponents.h:33
Definition: BaseRemoteComponents.h:46
Definition: BaseRemoteComponents.h:88
Definition: BaseRemoteComponents.h:117
TagValueRemoteConnector * getRemoteConnector(int num)
Definition: BaseRemoteComponents.h:158
BaseRemoteServerConnection * getRemoteServerConnection(int num)
Definition: BaseRemoteComponents.h:179
uint8_t addConnection(BaseRemoteServerConnection *toAdd)
Definition: BaseRemoteComponents.cpp:63
uint8_t remoteCount() const
Definition: BaseRemoteComponents.h:151
void clearRemotes()
Definition: BaseRemoteComponents.h:134
TagValueTransport * getTransport(int num)
Definition: BaseRemoteComponents.h:168
TcMenuRemoteServer(const ConnectorLocalInfo &appInfo)
Definition: BaseRemoteComponents.h:129
Definition: tcUtil.h:25