#if !defined(DEBUG_FLAG) #define DEBUG_FLAG false #endif #include #include #include #include "display.h" #include "network.h" #include "rfid.h" #include "button.h" class ProgClass { private: static const int MAX_STORE_DATA = 8; HardwareSerial* _usb; DispClass* disp; NetClass* net; RfidClass* rfid; ButtonClass* bu; int8_t _storeCount = -1; int8_t _job = -1; int8_t _online = -1; bool _keep = false; String _storeData[MAX_STORE_DATA]; String _cmsg; void init(void); void parse(String text); bool keepAlive(void); void doJob(String task, String value); public: ProgClass(HardwareSerial *usb); void loop(void); void loadFromDisplay(void); }; ProgClass::ProgClass(HardwareSerial *usb) { this->_usb = usb; this->init(); } void ProgClass::init(void) { #if DEBUG_FLAG this->_usb->println("Init...."); this->disp = new DispClass(this->_usb); this->net = new NetClass(this->_usb); #else this->disp = new DispClass(); this->net = new NetClass(); #endif this->rfid = new RfidClass(); this->bu = new ButtonClass(); this->_usb->println("t="+String(now())); } void ProgClass::loop(void) { char c; if (this->rfid->hasData()) { if (this->rfid->allowed()) { this->_online = this->_online == 1 ? 0 : 1; this->doJob("tag", this->rfid->getData()); this->doJob("online", String(this->_online)); delay(1000); } this->rfid->clear(); } if (this->_usb->available()) { c = this->_usb->read(); if (c == '\n') { this->parse(this->_cmsg); this->_cmsg = ""; } else { this->_cmsg += c; } } if (this->bu->check(this->_job)) { this->_job = this->bu->read(); this->doJob("job", String(this->_job)); this->rfid->clear(); } } void ProgClass::loadFromDisplay(void) { this->disp->setTimeout(1000); this->disp->print("getJob=1"); this->parse(this->disp->readStringUntil('\n')); this->disp->print("getOnline=1"); this->parse(this->disp->readStringUntil('\n')); if (this->keepAlive()) { this->disp->print("getStoreCount=1"); this->parse(this->disp->readStringUntil('\n')); if (this->_storeCount > 0) { this->disp->print("getStoreData=1"); this->parse(this->disp->readStringUntil('\n')); } } if (this->keepAlive()) { this->doJob("getStore", "1"); } } void ProgClass::parse(String text) { #if DEBUG_FLAG this->_usb->println("i<-: " + text); #endif text.trim(); String task = ""; String value = ""; if (text.indexOf("=") != -1) { task = text.substring(0, text.indexOf("=")); value = text.substring(text.indexOf("=") + 1); this->doJob(task, value); } } bool ProgClass::keepAlive(void) { this->_usb->setTimeout(1000); this->_keep = false; this->_usb->println("requestKeep=1"); String a = this->_usb->readStringUntil('\n'); if (a != "") { this->parse(a); } return this->_keep; } void ProgClass::doJob(String task, String value) { if (task == "tag") { char hex[15]; value.toCharArray(hex, 14); String answ = "tag=" + String(strtol(hex, NULL, 16)); answ += ";time=" + String(now()); answ += ";job=" + String(this->_job); answ += ";online=" + String(this->_online); if (this->keepAlive()) { this->_usb->println(answ); } else { this->disp->print(answ); } } else if (task == "job") { this->disp->print("job=" + value); } else if (task == "online") { this->disp->print("online=" + value); } else if (task == "time") { setTime(value.toInt()); this->_usb->println("t=" + String(now())); } else if (task == "setJob") { this->_job = value.toInt(); } else if (task == "setOnline") { this->_online = value.toInt(); } else if (task == "keep") { this->_keep = true; } else if (task == "hasCount") { this->_storeCount = value.toInt(); } else if (task == "dataStore") { int i = 0; while (true) { if (i == MAX_STORE_DATA) { break; } if (value.indexOf('|') == -1) { this->_storeData[i] = value; i++; break; } this->_storeData[i] = value.substring(0, value.indexOf('|')); value = value.substring(value.indexOf('|') + 1); i++; } this->_storeCount = i; } else if (task == "getStore") { if (this->_storeCount > 0) { for (int i = 0; i < this->_storeCount; i++) { this->_usb->println(this->_storeData[i]); } this->_storeCount = 0; } } }