Verified Commit b965a909 authored by Löscher Mario's avatar Löscher Mario
Browse files

symlinks don't work with git. Again, basic webapp written and up and working....

symlinks don't work with git. Again, basic webapp written and up and working. Testenvironment is available here: https://distributed.mein-server.at/
parent df53a20f
/home/mario/PhpstormProjects/temperature/
\ No newline at end of file
<?php
include_once ("../../misc/settings.php");
include_once ("../../misc/curl.php");
header('Content-Type: application/json');
class rest
{
public static function startAPI()
{
self::selectMethod();
}
public static function getJsonInput()
{
return json_decode(file_get_contents('php://input'));
}
public static function jsonRespond($value)
{
echo json_encode($value);
}
public static function getError($status, $message = null)
{
http_response_code($status);
return $message;
}
public static function getPathInfo()
{
//From https://www.leaseweb.com/labs/2015/10/creating-a-simple-rest-api-in-php/
if (isset($_SERVER['PATH_INFO'])) {
$request = explode('/', $_SERVER['PATH_INFO']);
array_shift($request);
} else
$request = null;
return $request;
}
public static function selectMethod()
{
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
self::get(self::getPathInfo());
break;
case 'POST':
self::post(self::getPathInfo(), self::getJsonInput());
break;
default:
http_response_code(400);
}
}
public static function get($uri, $json = null)
{
switch ($uri[0]) {
case 'db':
$value = curl::executeCouch("/31292d3a4e53d1e286670664d80074e3");
break;
default:
$value = self::getError(400);
}
self::jsonRespond($value);
}
public static function post($uri, $json = null)
{
switch ($uri[0]) {
case 'find':
$data_string = json_encode(self::getJsonInput($json));
$value = curl::executeCouch("/_find", $data_string);
break;
default:
$value = self::getError(400);
}
self::jsonRespond($value);
}
}
rest::startAPI();
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>
<script src="/javascript/mainController.js"></script>
</head>
<body ng-app="temperature" ng-controller="MainController">
<table cellpadding="20px">
<tr>
<td>
<form>
Lower than: <input type="text" ng-model="lt"><br>
Bigger than: <input type="text" ng-model="gt"><br>
<button type="button" ng-click="one(lt, gt)">Show</button>
</form>
</td>
<td>
<form>
Starttime: <input type="text" ng-model="st"><br>
Endtime: <input type="text" ng-model="et"><br>
<button type="button" ng-click="two(st, et)">Show</button>
</form>
</td>
<td>
<form>
Starttime: <input type="text" ng-model="st"><br>
Endtime: <input type="text" ng-model="et"><br>
<button type="button" ng-click="three(st, et)">Show</button>
</form>
</td>
</tr>
</table>
<table ng-If="points.list">
<tr>
<th>ID</th>
<th>Producer</th>
<th>Consumer</th>
<th>Timestamp</th>
<th>Value</th>
</tr>
<tr ng-repeat="point in points.list">
<td>{{point.id}}</td>
<td align="center">{{point.producer}}</td>
<td align="center">{{point.consumer}}</td>
<td>{{point.timestamp}}</td>
<td align="right">{{point.value}}</td>
</tr>
</table>
</body>
</html>
\ No newline at end of file
var temperature = angular.module('temperature', ['ngRoute']);
temperature.controller('MainController', function ($scope, $route, $routeParams, $location, $http) {
$scope.$route = $route;
$scope.$location = $location;
$scope.$routeParams = $routeParams;
$scope.model = {};
$scope.model.user = {};
//I really don't like this method, but the json received from couch is pretty awful.
let arrayIFyCouchJson = function(response) {
$scope.points = {};
$scope.points.list = [];
//create new array entries for each element
let copy = response.data.split(/\r\n/);
//remove first one and last four as these are garbage only
copy.splice(0,1);
copy.splice(-4, 4);
//prepare my entry
for (let i = 0; i < copy.length; i++) {
//get key-value pairs
let tmp = copy[i].split(",");
let current = {};
current.id = tmp[0].split(":")[1];
current.id = current.id.slice(1, current.id.length-1);
current.producer = tmp[1].split(":")[1];
current.timestamp = tmp[2].split(":")[1];
current.value = tmp[3].split(":")[1];
current.consumer = tmp[4].split(":")[1].slice(0,1);
$scope.points.list.push(current);
}
};
let executeSearch =function (data) {
$http.post("https://distributed.mein-server.at/api/rest.php/find", data).then(
function (response) {
arrayIFyCouchJson(response);
})
};
$scope.getTester = function () {
$http.get("https://distributed.mein-server.at/api/rest.php/db").then(
function (response) {
console.log(response.data);
})
};
$scope.one = function(lower, higher) {
//parse numbers as otherwise input from html will not work correctly
lower = parseFloat(lower);
higher = parseFloat(higher);
let data = JSON.stringify({
"selector": {"_id": {"$gt": null}, "$or": [{"value": {"$lt" : lower}}, {"value": {"$gt": higher}}]},
"fields": ["_id", "producer", "timestamp", "value", "consumer"],
});
executeSearch(data);
};
$scope.two = function (startTime, endTime) {
//parse numbers as otherwise input from html will not work correctly
startTime = parseFloat(startTime);
endTime = parseFloat(endTime);
//exchange start- and endTime if necessary
if (endTime < startTime) {
let buf = endTime;
endTime = startTime;
startTime = buf;
}
let data = JSON.stringify({
"selector": {"producer": {"$eq": 0},
"consumer": {"$eq": 1},
"timestamp": {"$gte": startTime,"$lte": endTime}},
"fields": ["_id", "producer", "timestamp", "value", "consumer"],
"execution_stats": false,
// "sort": ["timestamp"],
});
executeSearch(data);
};
$scope.three = function (startTime, endTime) {
console.log("I'm a dummy function at the moment");
}
//$scope.one(0,35);
// $scope.two(1541511001222, 1541508600949);
});
\ No newline at end of file
<?php
include_once ("settings.php");
class curl
{
public static function executeCouch($urlAddition, $data_string = null) {
$URL = settings::$baseurl . $urlAddition;
$username = settings::$username;
$password = settings::$password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
if(!is_null($data_string))
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
return $result;
}
}
\ No newline at end of file
<?php
class settings
{
public static $username = 'heyedgivisaguseeparrywhi';
public static $password = '40868bfdce1b6046a07044f3dd51b43afad3d951';
public static $baseurl = 'https://2ce3adea-3579-4bbf-a74b-bfdc057c2806-bluemix.cloudant.com/sensors';
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment