#include "room.h" #include #include #include #include #include #include room::room() { this->name = "Sin nombre"; this->co2_ppm=0; this->temp=0; this->noise=0; this->fan_percent=0; } QJsonObject room::toJSON() const { QJsonObject obj; obj["name"] = this->name; obj["co2"] = this->co2_ppm; obj["temp"] = this->temp; obj["noise"] = this->noise; obj["fan_percent"] = this->fan_percent; QJsonArray ruleArray; for (const rule& r : rules) { ruleArray.append(r.toJSON()); } obj["rules"] = ruleArray; return obj; } room room::fromJSON(const QJsonObject& obj) { room r(obj["name"].toString()); r.co2_ppm = static_cast(obj["co2"].toDouble()); r.temp = static_cast(obj["temp"].toDouble()); r.noise = static_cast(obj["noise"].toDouble()); r.fan_percent = obj["fan_percent"].toInt(); QJsonArray ruleArray = obj["rules"].toArray(); for ( const QJsonValue& val : ruleArray) { r.addRule(rule::fromJSON(val.toObject())); } return r; } room::room(QString name){ this->name=name; this->co2_ppm=0; this->temp=0; this->noise=0; this->fan_percent=0; } void room::adjustFanChange(int fan_change){ if(fan_change<=100 && fan_change >= 0){ this->fan_change=fan_change; } } int room::getFanPercent() const{ return this->fan_percent; } void room::adjust(){ //controllador no se PID o histeresis o que //obtener hora //usar rule adecuado QTime now = QTime::currentTime(); bool active=false; rule* r; for (int i=0; irules.size(); i++){ r=&(this->rules[i]); active=false; if (r->start <= r->stop) { active = (now >= r->start && now <= r->stop); } else { active = (now >= r->start || now <= r->stop); } if(active){ /* QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec();*/ if(this->getCO2() > r->max_co2 || this->getTemp() > r->max_temp){ this->forceAdjust(this->getFanPercent() + this->fan_change); }else if(this->getNoise() > r->max_noise || this->getTemp() < r->min_temp){ this->forceAdjust(this->getFanPercent() - this->fan_change); } } } } void room::updateInfo(){ this->co2_ppm+=(float)QRandomGenerator::global()->bounded(-500, 500)/10; this->temp+=(float)QRandomGenerator::global()->bounded(-50, 50)/10; this->noise+=(float)QRandomGenerator::global()->bounded(-100, 100)/10; if(this->co2_ppm >2000){ this->co2_ppm=2000; } if(this->co2_ppm <100){ this->co2_ppm=100; } if(this->temp >40){ this->temp=40; } if(this->temp <0){ this->temp=0; } if(this->noise >100){ this->noise=100; } if(this->noise < 20){ this->noise=20; } } void room::addRule(const rule new_rule){ rules.append(new_rule); } void room::removeRule(int rule_index){ if (rule_index >= 0 && rule_index < rules.size()) { rules.removeAt(rule_index); }else{ //out of range. qWarning()<<"Attempt delete rule out of range"; } } void room::modifyRule(int rule_index, const rule new_rule){ if (rule_index >= 0 && rule_index < rules.size()) { rules[rule_index]=new_rule; }else{ //out of range. qWarning()<<"Attempt modify rule out of range"; } } void room::forceAdjust(int fan_percent){ if(0<=fan_percent && fan_percent<=100){ this->fan_percent=fan_percent; }else if (fan_percent>100){ this->fan_percent=100; }else{ this->fan_percent=0; } } QString room::getName() const { return name; } float room::getCO2() const { return co2_ppm; } float room::getTemp() const { return temp; } float room::getNoise() const { return noise; } rule room::getRule(int rule_index) const { if (rule_index >= 0 && rule_index < rules.size()) { return rules[rule_index]; } else { qWarning() << "getRule out of index"; return rule(); } } QVector room::getRules() const{ return rules; }