-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsw.cpp
82 lines (67 loc) · 2.09 KB
/
dsw.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
#include "dsw.h"
#include "const.h"
#include <EEPROM.h>
#include "types.h"
DSW_Temp::DSW_Temp() : value(-12700), index(255)
{
memset(info.address, 0, sizeof(DeviceAddress)) ;
#ifdef MEGA
memset(info.comment, 0, sizeof(DswComment)) ;
#endif
info.state = DISABLE;
this->_mqtt = NULL;
this->_device = NULL;
}
void DSW_Temp::setMqttClient(PubSubClient *mqtt, const char *device){
this->_mqtt = mqtt;
this->_device = device;
}
void DSW_Temp::publish() {
if ( index > (MAX_DSW_SENSORS - 1) ) return;
if ( value <= -12700 || value >= 8500 ) return;
char *temp = (char*)malloc(7);
if ( temp == NULL) return;
sprintf_P(temp, PSTR("%d.%02d"), (int)value / 100, (int)(value) % 100);
byte i = strlen(_device);
byte l = strlen(P_DSW);
char* buffer = (char*)malloc( i + l+1+2 );
if ( buffer == NULL) return;
strcpy(buffer, _device);
strcat_P(buffer, (char*)P_DSW);
sprintf_P(buffer, PSTR("%s%d"), buffer, index+1);
_mqtt->publish( buffer, temp);
#ifdef DEBUG1
WRITE_TO_SERIAL(F("MQTT topic: "), buffer, F(" value: "), temp);
#endif
free(temp);
free(buffer);
}
void DSW_Temp::load_eeprom() {
int a = EEPROM_ADDRESS_DSW_START + index*DSW_DATA_SIZE;
EEPROM.get(a, info);
}
void DSW_Temp::save_eeprom() {
int a = EEPROM_ADDRESS_DSW_START + index*DSW_DATA_SIZE;
DSW_Info tmp;
EEPROM.get(a, tmp );
if ( memcpy(&tmp, &info, sizeof(DSW_Info)) != 0 ) EEPROM.put(a, info );
#ifdef DEBUG1
WRITE_TO_SERIAL(F("DSW_Temp::save_eeprom state "), (State)info.state, F(" tmp: "), (State)tmp.state);
WRITE_TO_SERIAL(F("DSW_Temp::save_eeprom address "), (char *)info.address, F(" tmp: "), (char *)tmp.address);
WRITE_TO_SERIAL(F("DSW_Temp::save_eeprom state "), (char *)info.state, F(" tmp: "), (char *)tmp.state);
#endif
}
void DSW_Temp::setState(State st) {
info.state = st;
save_eeprom();
}
void DSW_Temp::setDeviceAddress(DeviceAddress addr) {
memcpy(info.address, addr, sizeof(DeviceAddress));
save_eeprom();
}
#ifdef MEGA
void DSW_Temp::setDeviceComment(DswComment comm) {
memcpy(info.comment, comm, sizeof(DswComment));
save_eeprom();
}
#endif