Skip to content

Commit 9fb2a67

Browse files
committed
example database, web API added
1 parent 289a7de commit 9fb2a67

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

example/sql/sim_test.sql

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 4.8.4
3+
-- https://www.phpmyadmin.net/
4+
--
5+
-- Gép: 127.0.0.1
6+
-- Létrehozás ideje: 2019. Jan 17. 20:15
7+
-- Kiszolgáló verziója: 10.1.37-MariaDB
8+
-- PHP verzió: 7.3.0
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
SET AUTOCOMMIT = 0;
12+
START TRANSACTION;
13+
SET time_zone = "+00:00";
14+
15+
16+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
17+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
18+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
19+
/*!40101 SET NAMES utf8mb4 */;
20+
21+
--
22+
-- Adatbázis: `sim_test`
23+
--
24+
25+
-- --------------------------------------------------------
26+
27+
--
28+
-- Tábla szerkezet ehhez a táblához `data`
29+
--
30+
31+
CREATE TABLE `data` (
32+
`id` bigint(20) NOT NULL,
33+
`date` date NOT NULL,
34+
`time` time NOT NULL,
35+
`sensors` text COLLATE utf8_bin NOT NULL
36+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
37+
38+
--
39+
-- A tábla adatainak kiíratása `data`
40+
--
41+
42+
INSERT INTO `data` (`id`, `date`, `time`, `sensors`) VALUES
43+
(1, '2018-04-17', '06:00:00', 'I=53.33;TW=20.98'),
44+
(2, '2018-04-17', '06:40:00', 'I=103.33;TW=21.95'),
45+
(3, '2018-04-17', '08:20:00', 'I=266.67;TW=24.15'),
46+
(4, '2018-04-17', '09:15:00', 'I=363.33;TW=24.88'),
47+
(5, '2018-04-17', '10:10:00', 'I=436.67;TW=26.34'),
48+
(6, '2018-04-17', '10:55:00', 'I=353.33;TW=27.32'),
49+
(7, '2018-04-17', '11:50:00', 'I=436.67;TW=28.54'),
50+
(8, '2018-04-17', '13:35:00', 'I=606.67;TW=28.54'),
51+
(9, '2018-04-17', '14:20:00', 'I=126.67;TW=27.56'),
52+
(10, '2018-04-17', '15:15:00', 'I=483.33;TW=28.05'),
53+
(11, '2018-04-17', '16:00:00', 'I=373.33;TW=27.07'),
54+
(12, '2018-04-17', '16:55:00', 'I=283.33;TW=27.07'),
55+
(13, '2018-04-17', '17:50:00', 'I=176.67;TW=26.58'),
56+
(14, '2018-04-17', '18:35:00', 'I=103.33;TW=26.58'),
57+
(15, '2018-04-17', '19:30:00', 'I=33.33;TW=25.85'),
58+
(16, '2018-04-17', '20:25:00', 'I=6.67;TW=25.12');
59+
60+
--
61+
-- Indexek a kiírt táblákhoz
62+
--
63+
64+
--
65+
-- A tábla indexei `data`
66+
--
67+
ALTER TABLE `data`
68+
ADD PRIMARY KEY (`id`);
69+
70+
--
71+
-- A kiírt táblák AUTO_INCREMENT értéke
72+
--
73+
74+
--
75+
-- AUTO_INCREMENT a táblához `data`
76+
--
77+
ALTER TABLE `data`
78+
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;
79+
COMMIT;
80+
81+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
82+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
83+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

example/webjson/api/api.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
// http://127.0.0.1/api/api.php?startDate=2018-04-17&startTime=06:00:00&endDate=2018-04-17&endTime=18:00:00&sensors=I,TW
4+
5+
if(isset($_GET["startDate"])){
6+
7+
} else {
8+
die('{ "error" : "startDate is needed" }');
9+
}
10+
11+
if(isset($_GET["startTime"])){
12+
13+
} else {
14+
die('{ "error" : "startTime is needed" }');
15+
}
16+
17+
if(isset($_GET["endDate"])){
18+
19+
} else {
20+
die('{ "error" : "endDate is needed" }');
21+
}
22+
23+
if(isset($_GET["endTime"])){
24+
25+
} else {
26+
die('{ "error" : "endTime is needed" }');
27+
}
28+
29+
if(isset($_GET["sensors"])){
30+
31+
} else {
32+
die('{ "error" : "sensors is needed" }');
33+
}
34+
35+
include('get_data.php');
36+
37+
echo get_data($_GET["startDate"], $_GET["startTime"], $_GET["endDate"], $_GET["endTime"], $_GET["sensors"]);
38+
39+
?>

example/webjson/api/connect.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
$host = "localhost";
3+
$user = "root";
4+
$pass = "";
5+
$db = "sim_test";
6+
$mysqli = new mysqli($host, $user, $pass, $db);
7+
if ($mysqli->connect_errno) {
8+
die('Colud not connect to host: '.$host.' :'. $mysqli->connect_error);
9+
}
10+
$result = $mysqli->query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
11+
if(!$result){
12+
die('Colud not set te charset :'. $mysqli->connect_error);
13+
}
14+
?>

example/webjson/api/get_data.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
require('connect.php');
3+
4+
function get_data($start_date, $start_time, $end_date, $end_time, $sensors_str){
5+
global $mysqli;
6+
$query_str = sprintf("SELECT * FROM `data` WHERE CONCAT(`date`,' ', `time`) >= '%s %s' AND CONCAT(`date`,' ', `time`) <= '%s %s'", $start_date, $start_time, $end_date, $end_time);
7+
$result = $mysqli->query($query_str);
8+
$JSON_str = '{"data":[';
9+
if($mysqli->field_count){
10+
while($row = $result->fetch_assoc()) {
11+
$obj_str = '{"date": "' . $row['date'] . '",';
12+
$obj_str .= '"time": "' . $row['time'] . '",';
13+
14+
$sensors = explode(",", $sensors_str);
15+
foreach ($sensors as &$sensor) {
16+
$data = get_sensor_data($row['sensors'], $sensor);
17+
18+
if(strlen($data) > 0){
19+
$obj_str .= $data . ',';
20+
}
21+
}
22+
23+
$obj_str = rtrim($obj_str, ",");
24+
$obj_str .= '}';
25+
26+
$JSON_str .= $obj_str . ',';
27+
}
28+
}
29+
$JSON_str = rtrim($JSON_str, ",");
30+
$JSON_str .= ']}';
31+
return $JSON_str;
32+
}
33+
34+
function get_sensor_data($str, $sensor){
35+
$sensor_data = explode(";", $str);
36+
foreach ($sensor_data as &$data) {
37+
$element = explode("=", $data);
38+
39+
if($element[0] == $sensor){
40+
return '"' . $sensor . '" : ' . $element[1];
41+
}
42+
}
43+
44+
return "";
45+
}
46+
?>

0 commit comments

Comments
 (0)