Skip to content

Commit

Permalink
01.07.2022
Browse files Browse the repository at this point in the history
  • Loading branch information
oskar-anderson committed Jul 1, 2022
1 parent beb4a3c commit ae0f727
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 90 deletions.
23 changes: 23 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Options -MultiViews
# RewriteEngine on

# Disable Directory Listings in this Directory and Subdirectories
# This will hide the files from the public unless they know direct URLs
Options -Indexes

#I think this sets the base url of the site?
# RewriteBase http://localhost/myApps/Tempsens/site/

# RewriteCond %{REQUEST_FILENAME} !d
# RewriteCond ^(.+)/$ $1 [NE,R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Redirecting an old directory to new directory
RewriteRule ^(.+)$ site/ServerRouting.php?url=$1 [QSA,L]





# http://localhost/myApps/Tempsens/site/
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,32 @@ Change initializer.php to load data from real DB.

Remove bash crud job (scripts/alarms.php) that will not work anymore - I don't think it ever did.

Merge current document in _documents folder with the latest production documentation.
Merge current document in _documents folder with the latest production documentation.


## Notes

Good:
* Project structure

Bad:
* PHP
* Types and type hinting tricks
* Assoc array vs indexed array
* Using PHPDoc for array init: @return Sensor[]
* echo wrapping Console::Writeline()
* no namespaces for builtin functions
* too many parenthesis: (new DalSensors())->GetAll();
* Feels like an 8-year-old language
* python list comprehension > C# Linq > Java Streams > PHP array_map()

Confusing:
* PHP
* echo (syntax and manual newline)
* array_push(myArr, 7) vs myArr[7]
* array creation: array() vs []
* PHP manual user notes
* Routing (.htaccess vs index.php)
* PHP and JS data transfer (REST vs echo into JS var)
* PHP vs JS for site rendering
* Cookies - ended making a cache.json file
4 changes: 4 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// this kinda works, but messes relative paths up
include_once "site/viewController/index.php";
15 changes: 15 additions & 0 deletions site/ServerRouting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

$requestedPage = $_SERVER["REQUEST_URI"];
$router = str_replace('/myApps/Tempsens/site/', '', $requestedPage);
var_dump($requestedPage);
echo "<br>";
var_dump($router);
echo "<br>";
$allowDebug = true;
if ($router == "page1"){
echo "page1";
exit();
}
include_once "viewController/index.php";
exit();
2 changes: 1 addition & 1 deletion site/db/DbHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function GetDevPDO(): PDO
public static function GetPdoByKey($configKey): PDO
{
$dbconf = Config::GetConfig()[$configKey];
Console::WriteLine("New PDO(connectUrl= {$dbconf['connectUrl']}, username=***, password=***)");
// Console::WriteLine("New PDO(connectUrl= {$dbconf['connectUrl']}, username=***, password=***)");
try {
$pdo = new PDO($dbconf['connectUrl'], $dbconf['username'], $dbconf['password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Expand Down
84 changes: 44 additions & 40 deletions site/db/Initializer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpArrayPushWithOneElementInspection */


namespace App\db;
Expand All @@ -20,8 +20,8 @@
use App\Util\Console;


// Initializer::Initialize('tempsens20210530');

Initializer::Initialize('tempsens20210530');
// Initializer::InitializeData();

// Script class to generate initial database, call from command line
class Initializer
Expand All @@ -38,30 +38,32 @@ public static function Initialize(string $name) {

Console::WriteLine("Creating tables...", true);
DbHelper::CreateTables();
// Console::WriteLine("Initialisizing data...", true);
// Initializer::InitializeData();
Console::WriteLine("Initialisizing data...", true);
Initializer::InitializeData();
Console::WriteLine("All good!", true);
}

public static function InitializeData() {
// $csv = array_map('str_getcsv', file('backupCSV/202104201605-V1_0_0/sensorsV1_0_0.csv'));
$file = fopen("backupCSV/202104201605-V1_0_0/sensors.csv","r");
$sensors = [];
for($i = 0; ! feof($file); $i++)
$sensors = Sensor::NewArray();
for($i = 0; $line = fgetcsv($file, separator: ";"); $i++)
{
$line = fgetcsv($file, separator: ";");
// var_dump($line);
if ($i === 0) continue; // skip first line

$id = $line[0];
$name = $line[1];
$serial = $line[2];
$model = $line[3];
$ip = $line[4];
$location = $line[5];
$isPortable = $line[6];
$minTemp = $line[7];
$maxTemp = $line[8];
$minRelHum = $line[9];
$maxRelHum = $line[10];
$readingIntervalMinutes = $line[11];
$id = (string) $line[0];
$name = (string) $line[1];
$serial = (string) $line[2];
$model = (string) $line[3];
$ip = (string) $line[4];
$location = (string) $line[5];
$isPortable = boolval($line[6]);
$minTemp = floatval($line[7]);
$maxTemp = floatval($line[8]);
$minRelHum = floatval($line[9]);
$maxRelHum = floatval($line[10]);
$readingIntervalMinutes = intval($line[11]);
$sensor_V1_0_0 = new SensorV1_0_0(
$id,
$name,
Expand All @@ -76,31 +78,31 @@ public static function InitializeData() {
$maxRelHum,
$readingIntervalMinutes
);
array_push($sensors, $sensor_V1_0_0);
array_push($sensors, $sensor_V1_0_0->MapToModel());
}

fclose($file);

$file = fopen("backupCSV/202104201605-V0_3_4/sensor-readings.csv","r");
$sensorReadings = [];
for($i = 0; ! feof($file); $i++)
$sensorReadings = SensorReading::NewArray();
for($i = 0; $line = fgetcsv($file, separator: ";"); $i++)
{
$line = fgetcsv($file, separator: ";");
// var_dump($line);
if ($i === 0) continue; // skip first line

$id = $line[0];
$passKey = $line[1];
$device = $line[2];
$temp = $line[3];
$relHum = $line[4];
$compQuant = $line[5];
$pressure = $line[6];
$alarms = $line[7];
$compType = $line[8];
$tempU = $line[9];
$pressureU = $line[10];
$timer = $line[11];
$dactdate = $line[12];
$id = (string) $line[0];
$passKey = (string) $line[1];
$device = (string) $line[2];
$temp = floatval($line[3]);
$relHum = floatval($line[4]);
$compQuant = floatval($line[5]);
$pressure = floatval($line[6]);
$alarms = (string) $line[7];
$compType = (string) $line[8];
$tempU = (string) $line[9];
$pressureU = (string) $line[10];
$timer = intval($line[11]);
$dactdate = (string) $line[12];
$sensorReadingV0_3_4 = new SensorReadingV0_3_4(
$id,
$passKey,
Expand All @@ -117,19 +119,21 @@ public static function InitializeData() {
$dactdate
);
$sensor = SensorReading::GetSensorBySerial($sensors, $passKey);
array_push($sensorReadings, $sensorReadingV0_3_4->GetUp($sensor->id, $sensor->portable));
array_push($sensorReadings, $sensorReadingV0_3_4->GetUp($sensor->id, $sensor->isPortable)->MapToModel());
}
fclose($file);

$pdo = DbHelper::GetDevPDO();
$pdo->beginTransaction();

Console::WriteLine('Transaction adding sensors: ' . sizeof($sensors), true);
foreach ($sensors as $sensor) {
(new DalSensors())->Create($sensor, $pdo);
}

foreach ($sensorReadings as $sensorReadingV1_0_0) {
(new DalSensorReading())->Create($sensorReadingV1_0_0, $pdo);
Console::WriteLine('Transaction adding sensorReadings: ' . sizeof($sensorReadings), true);
foreach ($sensorReadings as $sensorReading) {
(new DalSensorReading())->Create($sensorReading, $pdo);
}

$pdo->commit();
Expand Down
11 changes: 10 additions & 1 deletion site/db/dal/DalSensorReading.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static function ResetCache(array $sensors): void
*/
public static function SetLastReadingsCache(SensorReading $sensorReading): void
{
if (! CacheJsonDTO::DoesFileExist()) {
(new CacheJsonDTO((new DalSensorReading())->GetLastSensorReadings()))->Save();
}
$cache = CacheJsonDTO::Read();
$cache->sensorReadings[$sensorReading->sensorId] = $sensorReading;
$cache->Save();
Expand All @@ -66,7 +69,9 @@ public static function SetLastReadingsCache(SensorReading $sensorReading): void
*/
public static function GetLastReadingsFromCacheOrDatabase(array $sensors): array
{
(new CacheJsonDTO(DalSensorReading::GetLastSensorReadings()))->Save();
if (! CacheJsonDTO::DoesFileExist()) {
(new CacheJsonDTO((new DalSensorReading())->GetLastSensorReadings()))->Save();
}
$cache = CacheJsonDTO::Read();
$isDirty = false;
$lastReadings = [];
Expand All @@ -89,6 +94,10 @@ public static function GetLastReadingsFromCacheOrDatabase(array $sensors): array
return $lastReadings;
}

/**
* Get assoc array of sensor id and sensor's last SensorReading? (SensorReading|null)[]
* @return array<SensorReading|null>
*/
public function GetLastSensorReadings(): array {
$sensors = (new DalSensors())->GetAll();
$lastReadings = [];
Expand Down
5 changes: 3 additions & 2 deletions site/db/migrations/V0_3_4/SensorReadingV0_3_4.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ function __construct(
public string $tempU,
public string $pressureU,
public int $timer,
public ?string $dactdate,
public ?string $dactdate
) {}


/**
* @return SensorReadingV1_0_0
*/
public function GetUp($sensorId, $isPortable) {
public function GetUp(string $sensorId, bool $isPortable): SensorReadingV1_0_0
{
// $sensorId = SensorReadingV0_3_4::GetSensorIdBySerial($sensors, $this->passkey);
$sensorReading = new SensorReadingV1_0_0(
id: Base64::GenerateId(),
Expand Down
14 changes: 14 additions & 0 deletions site/db/migrations/V1_0_0/SensorReadingV1_0_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\db\migrations\V1_0_0;

use App\model\SensorReading;

class SensorReadingV1_0_0 {

// PHP 8: Constructor property promotion - pretty nice
Expand All @@ -13,4 +15,16 @@ function __construct(
public string $dateRecorded,
public ?string $dateAdded)
{ }

public function MapToModel(): SensorReading
{
return new SensorReading(
$this->id,
$this->sensorId,
$this->temp,
$this->relHum,
$this->dateRecorded,
$this->dateAdded
);
}
}
20 changes: 20 additions & 0 deletions site/db/migrations/V1_0_0/SensorV1_0_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\db\migrations\V1_0_0;

use App\model\Sensor;

class SensorV1_0_0 {

// PHP 8: Constructor property promotion - pretty nice
Expand All @@ -20,4 +22,22 @@ function __construct(
public int $readingIntervalMinutes
)
{ }

public function MapToModel(): Sensor
{
return new Sensor(
$this->id,
$this->name,
$this->serial,
$this->model,
$this->ip,
$this->location,
$this->isPortable,
$this->minTemp,
$this->maxTemp,
$this->minRelHum,
$this->maxRelHum,
$this->readingIntervalMinutes
);
}
}
Loading

0 comments on commit ae0f727

Please sign in to comment.