public function Execute($db, $params) { $sql = "SELECT * FROM Sessions WHERE"; if (isset($params["UserID"]) && UUID::TryParse($params["UserID"], $this->ID)) { $sql .= " UserID=:ID"; } else { if (isset($params["SessionID"]) && UUID::TryParse($params["SessionID"], $this->ID)) { $sql .= " SessionID=:ID"; } else { header("Content-Type: application/json", true); echo '{ "Message": "Invalid parameters" }'; exit; } } $sth = $db->prepare($sql); if ($sth->execute(array(':ID' => $this->ID))) { if ($sth->rowCount() > 0) { $obj = $sth->fetchObject(); $session = new Session(); $session->UserID = $obj->UserID; $session->SessionID = $obj->SessionID; $session->SecureSessionID = $obj->SecureSessionID; $session->SceneID = $obj->SceneID; $session->ScenePosition = Vector3::Parse($obj->ScenePosition); $session->SceneLookAt = Vector3::Parse($obj->SceneLookAt); $session->LastUpdate = $obj->LastUpdate; $session->ExtraData = $obj->ExtraData; if (empty($session->ExtraData)) { $session->ExtraData = "{}"; } $output = sprintf('{ "Success": true, "UserID": "%s", "SessionID": "%s", "SecureSessionID": "%s", "SceneID": "%s", "ScenePosition": %s, "SceneLookAt": %s, "ExtraData": %s }', $session->UserID, $session->SessionID, $session->SecureSessionID, $session->SceneID, $session->ScenePosition->toOSD(), $session->SceneLookAt->toOSD(), $session->ExtraData); header("Content-Type: application/json", true); echo $output; exit; } else { header("Content-Type: application/json", true); echo '{ "Message": "Session not 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(); while ($obj = $sth->fetchObject()) { $session = new Session(); $session->UserID = $obj->UserID; $session->SessionID = $obj->SessionID; $session->SecureSessionID = $obj->SecureSessionID; $session->SceneID = $obj->SceneID; $session->ScenePosition = Vector3::Parse($obj->ScenePosition); $session->SceneLookAt = Vector3::Parse($obj->SceneLookAt); $session->LastUpdate = $obj->LastUpdate; if (!is_null($obj->ExtraData)) { $session->ExtraData = $obj->ExtraData; } else { $session->ExtraData = "{}"; } $found[] = $session->toOSD(); } log_message('debug', 'returning ' . count($found)); header("Content-Type: application/json", true); echo '{ "Success": true, "Sessions": [' . implode(',', $found) . '] }'; exit; }
public static function fromOSD($osd) { if (!isset($osd)) { return NULL; } if (!is_array($osd)) { $osd = json_decode($osd, true); } if (!isset($osd)) { return NULL; } $scene = new Scene(); $scene->SceneID = $osd["SceneID"]; $scene->Name = $osd["Name"]; $scene->MinPosition = Vector3::Parse('<' . implode(',', $osd["MinPosition"]) . '>'); $scene->MaxPosition = Vector3::Parse('<' . implode(',', $osd["MaxPosition"]) . '>'); $scene->Address = $osd["Address"]; $scene->Enabled = $osd["Enabled"]; $scene->ExtraData = $osd["ExtraData"]; return $scene; }
public static function fromOSD($osd) { if (!isset($osd)) { return NULL; } if (!is_array($osd)) { $osd = json_decode($osd, true); } if (!isset($osd)) { return NULL; } $session = new Session(); $session->UserID = UUID::Parse($osd['UserID']); $session->SessionID = UUID::Parse($osd['SessionID']); $session->SecureSessionID = UUID::Parse($osd['SecureSessionID']); $session->SceneID = UUID::Parse($osd['SceneID']); $session->ScenePosition = Vector3::Parse($osd['ScenePosition']); $session->SceneLookAt = Vector3::Parse($osd['SceneLookAt']); $session->ExtraData = json_decode($osd['ExtraData']); return $session; }
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; }