<commit_before>#include <dht11.h>
#include "WaterMeterCounter.h"
#include "SyrotaAutomation1.h"
#include "config.h"

WaterMeterCounter meter = WaterMeterCounter();
SyrotaAutomation net = SyrotaAutomation(2);

// Buffer for char conversions
char buf [40];

// Indicates if one of valve commands was sent and at what time (millis)
unsigned long valveCommandTime = 0L;
// DHT-11 data and lib
TempSensorData dhtData;
dht11 tempSensor;

void setup()
{
  pinMode(VALVE_CLOSE_PIN, OUTPUT);
  pinMode(VALVE_OPEN_PIN, OUTPUT);
  digitalWrite(VALVE_CLOSE_PIN, LOW);
  digitalWrite(VALVE_OPEN_PIN, LOW);
  readTemp();
  strcpy(net.deviceID, "WtrMn");
  Serial.begin(14400);
}

void loop()
{
  if (net.messageReceived()) {
    if (net.assertCommand("getCount")) {
      sprintf(buf, "%lu", meter.getCounter());
      net.sendResponse(buf);
    } else if (net.assertCommand("getTemp")) {
//      int chk = tempSensor.read(9);
//      sprintf(buf, "%d, %d, %d", chk, tempSensor.temperature, tempSensor.humidity);
//      net.sendResponse(buf);
      if (dhtData.chk == DHTLIB_OK) {
        sprintf(buf, "%d", dhtData.temp);
        net.sendResponse(buf);
      } else{
        sprintf(buf, "ERROR: %d", dhtData.chk);
        net.sendResponse(buf);
      }
    } else if (net.assertCommand("getHumidity")) {
      if (dhtData.chk == DHTLIB_OK) {
        sprintf(buf, "%d", dhtData.humidity);
        net.sendResponse(buf);
      } else{
        sprintf(buf, "ERROR: %d", dhtData.chk);
        net.sendResponse(buf);
      }
    }  else if (net.assertCommand("valveClose")) {
      valveClose();
      net.sendResponse("OK");
    } else if (net.assertCommand("valveOpen")) {
      valveOpen();
      net.sendResponse("OK");
    } else {
      net.sendResponse("Unrecognized command");
    }
  }
  valveCheck();
  meter.reading(analogRead(METER_SENSOR_PIN));
  if (millis() - dhtData.lastReadTime > TEMP_READ_INTERVAL) {
    readTemp();
  }
}

void readTemp()
{
  dhtData.chk = tempSensor.read(TEMP_SENSOR_PIN);
  switch (dhtData.chk) {
    case DHTLIB_OK:
      dhtData.temp = tempSensor.temperature;
      dhtData.humidity = tempSensor.humidity;
      break;
    default:
      dhtData.temp = -1;
      dhtData.humidity = -1;
  }
  dhtData.lastReadTime = millis();
}

// Sends a command on the open pin
void valveOpen()
{
  digitalWrite(VALVE_CLOSE_PIN, LOW);
  digitalWrite(VALVE_OPEN_PIN, HIGH);
  valveCommandTime = millis();
}<commit_msg>Refactored duplicate handling of errors in climate sensors for water meter module<commit_after>#include <dht11.h>
#include "WaterMeterCounter.h"
#include "SyrotaAutomation1.h"
#include "config.h"

WaterMeterCounter meter = WaterMeterCounter();
SyrotaAutomation net = SyrotaAutomation(2);

// Buffer for char conversions
char buf [40];

// Indicates if one of valve commands was sent and at what time (millis)
unsigned long valveCommandTime = 0L;
// DHT-11 data and lib
TempSensorData dhtData;
dht11 tempSensor;

void setup()
{
  pinMode(VALVE_CLOSE_PIN, OUTPUT);
  pinMode(VALVE_OPEN_PIN, OUTPUT);
  digitalWrite(VALVE_CLOSE_PIN, LOW);
  digitalWrite(VALVE_OPEN_PIN, LOW);
  readTemp();
  strcpy(net.deviceID, "WtrMn");
  Serial.begin(14400);
}

void loop()
{
  if (net.messageReceived()) {
    if (net.assertCommand("getCount")) {
      sprintf(buf, "%lu", meter.getCounter());
      net.sendResponse(buf);
    } else if (net.assertCommand("getTemp")) {
      sendClimateResponse(dhtData.temp);
    } else if (net.assertCommand("getHumidity")) {
      sendClimateResponse(dhtData.humidity);
    }  else if (net.assertCommand("valveClose")) {
      valveClose();
      net.sendResponse("OK");
    } else if (net.assertCommand("valveOpen")) {
      valveOpen();
      net.sendResponse("OK");
    } else {
      net.sendResponse("Unrecognized command");
    }
  }
  valveCheck();
  meter.reading(analogRead(METER_SENSOR_PIN));
  if (millis() - dhtData.lastReadTime > TEMP_READ_INTERVAL) {
    readTemp();
  }
}

void sendClimateResponse(int data)
{
  if (dhtData.chk == DHTLIB_OK) {
    sprintf(buf, "%d", data);
    net.sendResponse(buf);
  } else{
    sprintf(buf, "ERROR: %d", dhtData.chk);
    net.sendResponse(buf);
  }
}

void readTemp()
{
  dhtData.chk = tempSensor.read(TEMP_SENSOR_PIN);
  switch (dhtData.chk) {
    case DHTLIB_OK:
      dhtData.temp = tempSensor.temperature;
      dhtData.humidity = tempSensor.humidity;
      break;
    default:
      dhtData.temp = -1;
      dhtData.humidity = -1;
  }
  dhtData.lastReadTime = millis();
}

// Sends a command on the open pin
void valveOpen()
{
  digitalWrite(VALVE_CLOSE_PIN, LOW);
  digitalWrite(VALVE_OPEN_PIN, HIGH);
  valveCommandTime = millis();
}
{'fraction_non_alphanumeric': 0.14091790562378798, 'fraction_numerical': 0.007972419737125619, 'mean_word_length': 3.609731876861966, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6598454', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1689, 'n_words': 328}