refactoring

This commit is contained in:
livello
2018-10-08 00:48:56 +03:00
parent fecdd737d0
commit ebc7c57fd6
2 changed files with 22 additions and 37 deletions

View File

@@ -153,23 +153,15 @@ bool wifiInitialized;
int mqttErrorRate; int mqttErrorRate;
void watchdogSetup(void) { void watchdogSetup(void) {} //Do not remove - strong re-definition WDT Init for DUE
//Serial.begin(115200);
//debugSerial.println("Watchdog armed.");
} //Do not remove - strong re-definition WDT Init for DUE
// MQTT Callback routine
void mqttCallback(char *topic, byte *payload, unsigned int length) { void mqttCallback(char *topic, byte *payload, unsigned int length) {
debugSerial.print(F("\n[")); debugSerial.print(F("\n["));
debugSerial.print(topic); debugSerial.print(topic);
debugSerial.print(F("] ")); debugSerial.print(F("] "));
if (!payload) return; if (!payload) return;
payload[length] = 0;
payload[length] = 0;
int fr = freeRam(); int fr = freeRam();
if (fr < 250) { if (fr < 250) {
debugSerial.println(F("OOM!")); debugSerial.println(F("OOM!"));
@@ -186,17 +178,15 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
return; return;
} }
boolean retaining = (lanStatus == RETAINING_COLLECTING);
//Check if topic = Command topic //Check if topic = Command topic
short intopic = 0; short intopic = 0;
{ {
char buf[MQTT_TOPIC_LENGTH + 1]; char buf[MQTT_TOPIC_LENGTH + 1];
strncpy_P(buf, inprefix, sizeof(buf)); strncpy_P(buf, inprefix, sizeof(buf));
intopic = strncmp(topic, buf, strlen(inprefix)); intopic = strncmp(topic, buf, strlen(inprefix));
} }
// in Retaining status - trying to restore previous state from retained output topic. Retained input topics are not relevant. // in Retaining status - trying to restore previous state from retained output topic. Retained input topics are not relevant.
if (retaining && !intopic) { if ((lanStatus == RETAINING_COLLECTING) && !intopic) {
debugSerial.println(F("Skipping..")); debugSerial.println(F("Skipping.."));
return; return;
} }
@@ -208,9 +198,9 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
strncpy(subtopic, t + 1, MQTT_SUBJECT_LENGTH - 1); strncpy(subtopic, t + 1, MQTT_SUBJECT_LENGTH - 1);
Item item(subtopic); Item item(subtopic);
if (item.isValid()) { if (item.isValid()) {
if (item.itemType == CH_GROUP && retaining) if (item.itemType == CH_GROUP && (lanStatus == RETAINING_COLLECTING))
return; //Do not restore group channels - they consist not relevant data return; //Do not restore group channels - they consist not relevant data
item.Ctrl((char *)payload, !retaining); item.Ctrl((char *)payload, !(lanStatus == RETAINING_COLLECTING));
} //valid item } //valid item
} }
@@ -249,7 +239,7 @@ lan_status lanLoop() {
case HAVE_IP_ADDRESS: case HAVE_IP_ADDRESS:
if (!configOk) if (!configOk)
lanStatus = getConfig(0, NULL); //got config from server or load from NVRAM lanStatus = loadConfigFromHttp(0, NULL);
else lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER; else lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
#ifdef _artnet #ifdef _artnet
if (artnet) artnet->begin(); if (artnet) artnet->begin();
@@ -621,12 +611,22 @@ void Changed(int i, DeviceAddress addr, int val) {
#endif //_owire #endif //_owire
void cmdFunctionHelp(int arg_cnt, char **args) void cmdFunctionHelp(int arg_cnt, char **args)
//(char* tokens)
{ {
printFirmwareVersionAndBuildOptions(); printFirmwareVersionAndBuildOptions();
#ifndef SYSLOG_DISABLE #ifndef SYSLOG_DISABLE
// udpSyslog.logf(LOG_INFO, "free RAM: %d",freeRam()); // udpSyslog.logf(LOG_INFO, "free RAM: %d",freeRam());
#endif #endif
debugSerial.print(F("Current LAN config(ip,dns,gw,subnet):"));
printIPAddress(Ethernet.localIP());
debugSerial.print(F(" ,"));
printIPAddress(Ethernet.dnsServerIP());
debugSerial.print(F(" ,"));
printIPAddress(Ethernet.gatewayIP());
debugSerial.print(F(" ,"));
printIPAddress(Ethernet.subnetMask());
debugSerial.println(F(";"));
debugSerial.print(F(" free RAM: "));debugSerial.print(freeRam()); debugSerial.print(F(" free RAM: "));debugSerial.print(freeRam());
debugSerial.println(F(" Use the commands: 'help' - this text\n" debugSerial.println(F(" Use the commands: 'help' - this text\n"
"'mac de:ad:be:ef:fe:00' set and store MAC-address in EEPROM\n" "'mac de:ad:be:ef:fe:00' set and store MAC-address in EEPROM\n"
@@ -756,7 +756,6 @@ void cmdFunctionLoad(int arg_cnt, char **args) {
} }
int loadConfigFromEEPROM(int arg_cnt, char **args) int loadConfigFromEEPROM(int arg_cnt, char **args)
//(char* tokens)
{ {
char ch; char ch;
debugSerial.println(F("loading Config")); debugSerial.println(F("loading Config"));
@@ -778,7 +777,6 @@ int loadConfigFromEEPROM(int arg_cnt, char **args)
} else { } else {
debugSerial.println(F("No stored config")); debugSerial.println(F("No stored config"));
return 0; return 0;
} }
} }
@@ -789,7 +787,6 @@ void cmdFunctionReq(int arg_cnt, char **args) {
int mqttConfigRequest(int arg_cnt, char **args) int mqttConfigRequest(int arg_cnt, char **args)
//(char* tokens)
{ {
char buf[25] = "/"; char buf[25] = "/";
debugSerial.println(F("request MQTT Config")); debugSerial.println(F("request MQTT Config"));
@@ -802,16 +799,11 @@ int mqttConfigRequest(int arg_cnt, char **args)
strncat(buf, "/req/conf", 25); strncat(buf, "/req/conf", 25);
debugSerial.println(buf); debugSerial.println(buf);
mqttClient.publish(buf, "1"); mqttClient.publish(buf, "1");
} }
int mqttConfigResp(char *as) { int mqttConfigResp(char *as) {
debugSerial.println(F("got MQTT Config")); debugSerial.println(F("got MQTT Config"));
//aJsonEEPROMStream as=aJsonEEPROMStream(EEPROM_offset);
//aJson.deleteItem(root);
root = aJson.parse(as); root = aJson.parse(as);
debugSerial.println(); debugSerial.println();
if (!root) { if (!root) {
@@ -824,7 +816,6 @@ int mqttConfigResp(char *as) {
} }
void cmdFunctionSave(int arg_cnt, char **args) void cmdFunctionSave(int arg_cnt, char **args)
//(char* tokens)
{ {
aJsonEEPROMStream jsonEEPROMStream = aJsonEEPROMStream(EEPROM_offset); aJsonEEPROMStream jsonEEPROMStream = aJsonEEPROMStream(EEPROM_offset);
debugSerial.println(F("Saving config to EEPROM..")); debugSerial.println(F("Saving config to EEPROM.."));
@@ -834,7 +825,6 @@ void cmdFunctionSave(int arg_cnt, char **args)
} }
void cmdFunctionIp(int arg_cnt, char **args) void cmdFunctionIp(int arg_cnt, char **args)
//(char* tokens)
{ {
IPAddress ip0(0, 0, 0, 0); IPAddress ip0(0, 0, 0, 0);
IPAddress ip; IPAddress ip;
@@ -913,7 +903,7 @@ void cmdFunctionSetMac(int arg_cnt, char **args) {
} }
void cmdFunctionGet(int arg_cnt, char **args) { void cmdFunctionGet(int arg_cnt, char **args) {
lanStatus=getConfig(arg_cnt, args); lanStatus= loadConfigFromHttp(arg_cnt, args);
ethClient.stop(); //Refresh MQTT connect to get retained info ethClient.stop(); //Refresh MQTT connect to get retained info
//restoreState(); //restoreState();
} }
@@ -948,7 +938,7 @@ int ipLoadFromFlash(short n, IPAddress &ip) {
return 0; return 0;
} }
lan_status getConfig(int arg_cnt, char **args) lan_status loadConfigFromHttp(int arg_cnt, char **args)
{ {
int responseStatusCode = 0; int responseStatusCode = 0;
char ch; char ch;
@@ -1287,7 +1277,7 @@ void setupCmdArduino() {
void loop_main() { void loop_main() {
wdt_res(); wdt_res();
cmdPoll(); cmdPoll();
if (lanLoop() > 1) { if (lanLoop() > HAVE_IP_ADDRESS) {
mqttClient.loop(); mqttClient.loop();
#ifdef _artnet #ifdef _artnet
if (artnet) artnet->read(); if (artnet) artnet->read();
@@ -1302,7 +1292,6 @@ void loop_main() {
// unsigned long lastpacket = DMXSerial.noDataSince(); // unsigned long lastpacket = DMXSerial.noDataSince();
DMXCheck(); DMXCheck();
#endif #endif
// if (lastpacket && (lastpacket%10==0)) debugSerial.println(lastpacket);
if (items) { if (items) {
#ifndef MODBUS_DISABLE #ifndef MODBUS_DISABLE
@@ -1333,10 +1322,7 @@ void owIdle(void) {
#endif #endif
wdt_res(); wdt_res();
return; //TODO: unreached code return;
debugSerial.print(F("o"));
if (lanLoop() == 1) mqttClient.loop();
//if (owReady) owLoop();
#ifdef _dmxin #ifdef _dmxin
DMXCheck(); DMXCheck();
@@ -1349,7 +1335,6 @@ void owIdle(void) {
void ethernetIdle(void){ void ethernetIdle(void){
wdt_res(); wdt_res();
inputLoop(); inputLoop();
// debugSerial.print(".");
}; };
void modbusIdle(void) { void modbusIdle(void) {

View File

@@ -196,7 +196,7 @@ void saveFlash(short n, IPAddress& ip);
int ipLoadFromFlash(short n, IPAddress &ip); int ipLoadFromFlash(short n, IPAddress &ip);
lan_status getConfig(int arg_cnt=0, char **args=NULL); lan_status loadConfigFromHttp(int arg_cnt = 0, char **args = NULL);
void preTransmission(); void preTransmission();