DEBUG_PRINT(F(
"MEM "
));
DEBUG_PRINTLN(ESP.getFreeHeap());
if
(lds.needNotify){
DEBUG_PRINT(F(
"Open config file..."
));
fs::File configFile = SPIFFS.open(F(
"/mc/config.txt"
),
"r"
);
if
(configFile) {
DEBUG_PRINTLN(F(
"done."
));
DynamicJsonDocument doc(CONFIG_FILE_HEAP);
DeserializationError error = deserializeJson(doc, configFile);
if
(error) {
DEBUG_PRINT(F(
"Error parsing JSON "
));
DEBUG_PRINTLN(error.c_str());
}
configFile.close();
DEBUG_PRINT(F(
"MEM "
));
DEBUG_PRINTLN(ESP.getFreeHeap());
JsonObject rootObj = doc.as<JsonObject>();
DEBUG_PRINT(F(
"After read config check serverSMTP and emailNotification "
));
DEBUG_PRINTLN(rootObj.containsKey(F(
"serverSMTP"
)) && rootObj.containsKey(F(
"emailNotification"
)));
if
(rootObj.containsKey(F(
"serverSMTP"
)) && rootObj.containsKey(F(
"emailNotification"
))){
JsonObject serverSMTP = rootObj[F(
"serverSMTP"
)];
JsonObject emailNotification = rootObj[F(
"emailNotification"
)];
bool
isNotificationEnabled = (emailNotification.containsKey(F(
"isNotificationEnabled"
)))?emailNotification[F(
"isNotificationEnabled"
)]:
false
;
DEBUG_PRINT(F(
"isNotificationEnabled "
));
DEBUG_PRINTLN(isNotificationEnabled);
if
(isNotificationEnabled){
const
char
* serverSMTPAddr = serverSMTP[F(
"server"
)];
emailSend.setSMTPServer(serverSMTPAddr);
uint16_t
portSMTP = serverSMTP[F(
"port"
)];
emailSend.setSMTPPort(portSMTP);
const
char
* loginSMTP = serverSMTP[F(
"login"
)];
emailSend.setEMailLogin(loginSMTP);
const
char
* passwordSMTP = serverSMTP[F(
"password"
)];
emailSend.setEMailPassword(passwordSMTP);
const
char
* fromSMTP = serverSMTP[F(
"from"
)];
emailSend.setEMailFrom(fromSMTP);
DEBUG_PRINT(F(
"server "
));
DEBUG_PRINTLN(serverSMTPAddr);
DEBUG_PRINT(F(
"port "
));
DEBUG_PRINTLN(portSMTP);
DEBUG_PRINT(F(
"login "
));
DEBUG_PRINTLN(loginSMTP);
DEBUG_PRINT(F(
"password "
));
DEBUG_PRINTLN(passwordSMTP);
DEBUG_PRINT(F(
"from "
));
DEBUG_PRINTLN(fromSMTP);
EMailSender::EMailMessage message;
const
String sub = emailNotification[F(
"subject"
)];
message.subject = sub;
JsonArray emailList = emailNotification[F(
"emailList"
)];
DEBUG_PRINT(F(
"Email list "
));
for
(
uint8_t
i=0; i<emailList.size(); i++){
JsonObject emailElem = emailList[i];
const
String alarm = emailElem[F(
"alarm"
)];
const
String ch1 = emailElem[F(
"ch1"
)];
const
String ch2 = emailElem[F(
"ch2"
)];
const
String state = emailElem[F(
"state"
)];
DEBUG_PRINT(F(
"State value "
));
DEBUG_PRINTLN(state);
DEBUG_PRINT(F(
"State value on_problem comparison "
));
DEBUG_PRINTLN(state==F(
"on_problem"
));
DEBUG_PRINT(F(
"Alarm value "
));
DEBUG_PRINTLN(alarm);
DEBUG_PRINT(F(
"Alarm all comparison "
));
DEBUG_PRINTLN(alarm==F(
"all"
));
bool
allNotification = (
(alarm==F(
"all"
) && lds.asp != dataState.alarmState)
||
(ch1==F(
"all"
) && lds.c1sp != dataState.channel1State)
||
(ch2==F(
"all"
) && lds.c2sp != dataState.channel2State)
||
(state==F(
"all"
) && lds.isp != dataState.inverterState)
);
bool
onProblem = (
(alarm==F(
"on_problem"
) && dataState.alarmState > 0)
||
(ch1==F(
"on_problem"
) && dataState.channel1State != 2)
||
(ch2==F(
"on_problem"
) && dataState.channel1State != 2)
||
(state==F(
"on_problem"
) && (dataState.inverterState != 2 && dataState.inverterState != 1))
);
DEBUG_PRINT(F(
"Check allNotification "
));
DEBUG_PRINTLN(allNotification);
DEBUG_PRINT(F(
"Check onProblem "
));
DEBUG_PRINTLN(onProblem);
DEBUG_PRINT(F(
"MEM "
));
DEBUG_PRINTLN(ESP.getFreeHeap());
if
(
allNotification
||
onProblem
){
const
String mp = emailNotification[F(
"messageProblem"
)];
const
String mnp = emailNotification[F(
"messageNoProblem"
)];
message.message = ((lds.inverterProblem)?mp:mnp)+
F(
"<br>Alarm: "
)+dataState.getAlarmState()+
F(
"<br>CH1: "
)+dataState.getDcDcChannel1State() +
F(
"<br>CH2: "
)+dataState.getDcDcChannel2State()+
F(
"<br>Stato: "
)+dataState.getInverterState();
EMailSender::Response resp = emailSend.send(emailElem[F(
"email"
)], message);
DEBUG_PRINTLN(F(
"Sending status: "
));
const
String em = emailElem[F(
"email"
)];
DEBUG_PRINTLN(em);
DEBUG_PRINTLN(resp.status);
DEBUG_PRINTLN(resp.code);
DEBUG_PRINTLN(resp.desc);
}
}
}
}
}
else
{
DEBUG_PRINTLN(F(
"fail."
));
}
}