<commit_before><?php

// Hardware-pairs Pico remotes to a set of Caseta dimmers.
// Useful for bypassing SmartThings' considerable latency and establishing
// instant and reliable Pico-to-Caseta communication.

// Pass comma delimited Pico serials in the pico parameter.
if (!isset($_REQUEST["pico"])) {
  emsg('No pico parameter');
}

// And a comma delimited set of Caseta serials as the caseta parameter.
if (!isset($_REQUEST["caseta"])) {
  emsg("No caseta parameter specified (even empty).");
}

header('Content-Type: application/json');
header('X-Response: DEVICE_LINKED');

$picos = $_REQUEST["pico"] ? array_map("intval", explode(",", $_REQUEST["pico"])) : array();
$new_dimmers = $_REQUEST["caseta"] ? array_map("intval", explode(",", $_REQUEST["caseta"])) : array();

function emsg($msg) {
  echo $msg;
  exit;
}

// Get existing links.
$existing_dimmers = array();
foreach ($picos as $pico) {
  $existing_dimmers[$pico] = array();
}

$db = "/database/lutron-db.sqlite";
$dbh = new SQLite3($db);
$dbh->busyTimeout(15000);

$picos_str = implode(",", $picos);
$query = "SELECT DISTINCT GetPresetComponentOwner.SerialNumber AS Pico, GetAssignableObjects.SerialNumber AS Caseta FROM GetPresetComponentOwner,PresetAssignment,GetAssignableObjects WHERE PresetAssignment.PresetID=GetPresetComponentOwner.PresetID AND GetAssignableObjects.AssignableObjectID=PresetAssignment.AssignableObjectID AND GetPresetComponentOwner.SerialNumber IN ($picos_str)";
$device_result = $dbh->query($query);
while ($row = $device_result->fetchArray(SQLITE3_ASSOC)) {
  $existing_dimmers[$row["Pico"]][] = $row["Caseta"];
}

$device_result->finalize();
$dbh->close();

$to_add = array();
$to_remove = array();
$to_lookup = array();

foreach ($picos as $pico) {
  $to_add[$pico] = array_diff($new_dimmers, $existing_dimmers[$pico]);
  $to_remove[$pico] = array_diff($existing_dimmers[$pico], $new_dimmers);
  $to_lookup += $to_add[$pico] + $to_remove[$pico];
  $to_lookup[] = $pico;
}

// Look up master IDs.

$db = "/database/apron.db";
$dbh = new SQLite3($db);
$dbh->busyTimeout(15000);

function query_masters($dbh, $list, &$master_array) {<commit_msg>Fix unioning bug in pico_dimmers.php.<commit_after><?php

// Hardware-pairs Pico remotes to a set of Caseta dimmers.
// Useful for bypassing SmartThings' considerable latency and establishing
// instant and reliable Pico-to-Caseta communication.

// Pass comma delimited Pico serials in the pico parameter.
if (!isset($_REQUEST["pico"])) {
  emsg('No pico parameter');
}

// And a comma delimited set of Caseta serials as the caseta parameter.
if (!isset($_REQUEST["caseta"])) {
  emsg("No caseta parameter specified (even empty).");
}

header('Content-Type: application/json');
header('X-Response: DEVICE_LINKED');

$picos = $_REQUEST["pico"] ? array_map("intval", explode(",", $_REQUEST["pico"])) : array();
$new_dimmers = $_REQUEST["caseta"] ? array_map("intval", explode(",", $_REQUEST["caseta"])) : array();

function emsg($msg) {
  echo $msg;
  exit;
}

// Get existing links.
$existing_dimmers = array();
foreach ($picos as $pico) {
  $existing_dimmers[$pico] = array();
}

$db = "/database/lutron-db.sqlite";
$dbh = new SQLite3($db);
$dbh->busyTimeout(15000);

$picos_str = implode(",", $picos);
$query = "SELECT DISTINCT GetPresetComponentOwner.SerialNumber AS Pico, GetAssignableObjects.SerialNumber AS Caseta FROM GetPresetComponentOwner,PresetAssignment,GetAssignableObjects WHERE PresetAssignment.PresetID=GetPresetComponentOwner.PresetID AND GetAssignableObjects.AssignableObjectID=PresetAssignment.AssignableObjectID AND GetPresetComponentOwner.SerialNumber IN ($picos_str)";
$device_result = $dbh->query($query);
while ($row = $device_result->fetchArray(SQLITE3_ASSOC)) {
  $existing_dimmers[$row["Pico"]][] = $row["Caseta"];
}

$device_result->finalize();
$dbh->close();

$to_add = array();
$to_remove = array();
$to_lookup = array();

foreach ($picos as $pico) {
  $to_add[$pico] = array_diff($new_dimmers, $existing_dimmers[$pico]);
  $to_remove[$pico] = array_diff($existing_dimmers[$pico], $new_dimmers);
  $to_lookup = array_merge($to_lookup, $to_add[$pico], $to_remove[$pico]);
  $to_lookup[] = $pico;
}

$to_lookup = array_unique($to_lookup);

// Look up master IDs.

$db = "/database/apron.db";
$dbh = new SQLite3($db);
$dbh->busyTimeout(15000);

function query_masters($dbh, $list, &$master_array) {
{'fraction_non_alphanumeric': 0.1680036672014669, 'fraction_numerical': 0.005959202383680954, 'mean_word_length': 6.45982905982906, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '1197073', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1515, 'n_words': 329}