public function Execute($db, $params) { $sql = ""; if (isset($params["SceneID"]) && UUID::TryParse($params["SceneID"], $this->SceneID)) { log_message('debug', "GetScene by SceneID " . $params["SceneID"]); $sql = "SELECT ID, Name, Address, Enabled, ExtraData, LastUpdate,\n CONCAT('<', MinX, ',', MinY, ',', MinZ, '>') AS MinPosition, \n CONCAT('<', MaxX, ',', MaxY, ',', MaxZ, '>') AS MaxPosition\n FROM Scenes WHERE ID='" . $this->SceneID . "'"; if (isset($params["Enabled"]) && $params["Enabled"]) { $sql .= " AND Enabled=1"; } } else { if (isset($params["Name"])) { log_message('debug', "GetScene by Name " . $params["Name"]); $sql = "SELECT ID, Name, Address, Enabled, ExtraData, LastUpdate,\n CONCAT('<', MinX, ',', MinY, ',', MinZ, '>') AS MinPosition, \n CONCAT('<', MaxX, ',', MaxY, ',', MaxZ, '>') AS MaxPosition\n FROM Scenes WHERE Name='" . $params["Name"] . "'"; if (isset($params["Enabled"]) && $params["Enabled"]) { $sql .= " AND Enabled=1"; } } else { if (isset($params["Position"]) && Vector3::TryParse($params["Position"], $this->Position)) { if (isset($params["FindClosest"]) && $params["FindClosest"]) { log_message('debug', "GetScene (closest) by position " . $this->Position); $sql = "SELECT ID, Name, Address, Enabled, ExtraData, LastUpdate,\n CONCAT('<', MinX, ',', MinY, ',', MinZ, '>') AS MinPosition, \n CONCAT('<', MaxX, ',', MaxY, ',', MaxZ, '>') AS MaxPosition,\n GLength(LineString(GeomFromText('POINT(" . $this->Position->X . " " . $this->Position->Y . ")'), Centroid(XYPlane)))\n AS dist FROM Scenes"; $sql .= " WHERE ExtraData NOT REGEXP :RegExp"; if (isset($params["Enabled"]) && $params["Enabled"]) { $sql .= " AND Enabled=1"; } $sql .= " ORDER BY dist LIMIT 1"; } else { log_message('debug', "GetScene by position " . $this->Position); $sql = "SELECT ID, Name, Address, Enabled, ExtraData, LastUpdate,\n CONCAT('<', MinX, ',', MinY, ',', MinZ, '>') AS MinPosition, \n CONCAT('<', MaxX, ',', MaxY, ',', MaxZ, '>') AS MaxPosition\n FROM Scenes WHERE MBRContains(XYPlane, GeomFromText('POINT(" . $this->Position->X . " " . $this->Position->Y . ")'))"; if (isset($params["Enabled"]) && $params["Enabled"]) { $sql .= " AND Enabled=1"; } $sql .= " LIMIT 1"; } } else { header("Content-Type: application/json", true); echo '{ "Message": "Invalid parameters" }'; exit; } } } log_message('debug', "what {$sql}"); $sth = $db->prepare($sql); if (isset($params["FindClosest"]) && $params["FindClosest"]) { $sth->bindValue(':RegExp', '.+\\"HyperGrid\\":true.+'); } if ($sth->execute()) { if ($sth->rowCount() > 0) { $obj = $sth->fetchObject(); $scene = new Scene(); $scene->SceneID = $obj->ID; $scene->Name = $obj->Name; $scene->Enabled = $obj->Enabled; $scene->MinPosition = Vector3::Parse($obj->MinPosition); $scene->MaxPosition = Vector3::Parse($obj->MaxPosition); $scene->Address = $obj->Address; $scene->LastUpdate = $obj->LastUpdate; if (!is_null($obj->ExtraData)) { $scene->ExtraData = $obj->ExtraData; } else { $scene->ExtraData = "{}"; } $out = $scene->toOSD(); $out = substr($out, 0, -1); $out .= ',"Success":true}'; log_message('debug', "Found scene " . $scene->Name); header("Content-Type: application/json", true); echo $out; exit; } else { header("Content-Type: application/json", true); echo '{ "Message": "No matching scene found" }'; exit; } } else { log_message('error', sprintf("Error occurred during query: %d %s", $sth->errorCode(), print_r($sth->errorInfo(), true))); log_message('debug', sprintf("Query: %s", $sql)); header("Content-Type: application/json", true); echo '{ "Message": "Database query error" }'; exit; } }
private function HandleQueryResponse($sth) { $found = array(); $scenelist = ""; while ($obj = $sth->fetchObject()) { $scene = new Scene(); $scene->SceneID = $obj->ID; $scene->Name = $obj->Name; $scene->Enabled = $obj->Enabled; $scene->MinPosition = Vector3::Parse($obj->MinPosition); $scene->MaxPosition = Vector3::Parse($obj->MaxPosition); $scene->Address = $obj->Address; $scene->LastUpdate = $obj->LastUpdate; if (!is_null($obj->ExtraData)) { $scene->ExtraData = $obj->ExtraData; } else { $scene->ExtraData = "{}"; } $found[] = $scene->toOSD(); $scenelist = $scenelist . $scene->Name . ","; } log_message('debug', 'returning ' . count($found) . ' results : ' . $scenelist); header("Content-Type: application/json", true); echo '{ "Success": true, "Scenes": [' . implode(',', $found) . '] }'; exit; }