<?php

  //setting header to json
  header('Content-Type: application/json');
  header("Access-Control-Allow-Origin: *");

  require '../php/database.php';

  $query = 'SELECT rulesSensors.ruleID, rulesSensors.sensorPinID, rulesSensors.sensorAddress, rulesSensors.sensorDataType, rulesSensors.sensorValue, rulesSensors.lessGreater, rulesSensors.stabilitySeconds, rulesSensors.logic, pinsAvailable.name, pinsAvailable.pinTypeID, ds18b20.name AS name2, probes.name AS name3
  FROM rulesSensors
  LEFT JOIN pinsAvailable
  ON rulesSensors.sensorPinID = pinsAvailable.pinID
  LEFT JOIN ds18b20
  ON rulesSensors.sensorAddress = ds18b20.address
  LEFT JOIN probes
  ON rulesSensors.sensorAddress = probes.address
  ORDER BY rulesSensors.ruleID, rulesSensors.rulesSensorID ASC';

  //execute query
  $result = $mysqli->query($query);

  //loop through the returned data
  $dataWindow = array();
  foreach ($result as $row) {
    $dataWindow[] = $row;
  }

  //free memory associated with result
  $result->close();

  //close connection
  $mysqli->close();
  print json_encode($dataWindow);