-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.h
97 lines (76 loc) · 2.3 KB
/
Network.h
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
#pragma once
#include "Config.h"
#include <Ethernet.h>
#include <src/PubSubClient.h>
#include "Pzem004t.h"
#include "DisplaySSD1306.h"
#include "EepromConfiguration.h"
enum class NetworkEventType
{
viewChange,
phaseChange,
switchStateChange,
resetEnergy,
customViewValue1Changed,
customViewValue2Changed,
customViewValue3Changed,
phase1OffsetChanged,
phase2OffsetChanged,
phase3OffsetChanged
};
struct NetworkEvent
{
NetworkEventType type;
int paramInt;
bool paramBool;
float paramFloat;
};
class Network
{
private:
public:
explicit Network(EepromConfiguration* configuration,
void (*networkEventCallback)(const NetworkEvent& value)
);
virtual ~Network();
void initialize();
void update(const hw::pzem004tvalues& phasesCombined, const hw::pzem004tvalues& phase1, const hw::pzem004tvalues& phase2, const hw::pzem004tvalues& phase3);
void configurationChanged();
EthernetClient* ethernetClient();
void enableConfigMode();
void disableConfigMode();
const IPAddress ipAddress();
void publishView(int value);
void publishPhase(int value);
void publishSwitchState(bool value);
private:
void initializeEthernet();
void ethernetHardwareReset();
void publishFloat(const char* topic, const float& value, const float& precision);
void publishInt(const char* topic, const int& value);
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);
void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length);
void reconnect();
void nwDelay(unsigned long ms);
unsigned long _lastPublish = 0;
unsigned long _lastMaintain = 0;
const char _space = ' ';
char _charVal[21];
int _charIndex;
bool _fromTask = false;
bool _reconnectRequested = false;
uint32_t _reconnectCount = 0;
bool _configMode = false;
bool _updating = false;
bool _firstConnect = true;
void (*_networkEventCallback)(const NetworkEvent& value);
int _currentView = 0;
int _currentPhase = 0;
bool _switchState = true;
bool _viewChanged = false;
bool _phaseChanged = false;
bool _switchStateChanged = false;
EthernetClient* _ethClient;
PubSubClient* _mqttClient;
EepromConfiguration* _configuration;
};