Simple-Home Biden vs. Trump Edition

This commit is contained in:
JonatanRek 2020-10-15 18:55:16 +02:00
parent 5d32c2bfa4
commit 740a2debf7
5 changed files with 83 additions and 7 deletions

View File

@ -11,6 +11,7 @@ class CronApi extends ApiController {
//echo (new VirtualDeviceManager)->fetch('');
echo (new Covid)->fetch('');
echo (new OpenWeatherMap)->fetch('');
echo (new UsaElection)->fetch('');
$this->response(['Value' => 'OK']);
}

View File

@ -40,7 +40,7 @@ class WidgetApi extends ApiController
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
$deviceData = DeviceManager::getDeviceById($subDeviceData['device_id']);
$events = RecordManager::getLastRecord($subDeviceId, 5);
$events = RecordManager::getLastRecord($subDeviceId, 10);
$LastRecordTime = new DateTime($events[4]['time']);
$niceTime = Utilities::ago($LastRecordTime);
@ -75,7 +75,10 @@ class WidgetApi extends ApiController
'graph' => [
'data' => [
'labels' => $labels,
'dataset' => $values
'datasets' => [[
//'label' => 'FUCK you',
'data' => $values,
]],
],
'options' => [
'scales' => [
@ -85,9 +88,9 @@ class WidgetApi extends ApiController
],
'yAxes' => [
'ticks' => [
'min' => RANGES[$subDeviceData['type']]['min'],
'max' => RANGES[$subDeviceData['type']]['max'],
'steps' => RANGES[$subDeviceData['type']]['scale'],
'min' => $this->getDeviceConfig($subDeviceData['type'])['min'],
'max' => $this->getDeviceConfig($subDeviceData['type'])['max'],
'steps' => $this->getDeviceConfig($subDeviceData['type'])['scale'],
]
]
],
@ -108,4 +111,11 @@ class WidgetApi extends ApiController
$this->response($response);
}
private function getDeviceConfig($type){
if (isset(RANGES[$type])){
return RANGES[$type];
}
return RANGES[''];
}
}

View File

@ -167,7 +167,7 @@ class Utilities
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
//curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

View File

@ -87,7 +87,7 @@ class SubDeviceManager
GROUP BY subdevice_id
)
GROUP BY subdevice_id
ORDER BY type DESC
ORDER BY d.name DESC
", $roomIds);
$ret = [];

View File

@ -0,0 +1,65 @@
<?php
class UsaElection extends VirtualDeviceManager
{
private $api_uri = 'https://ft-ig-content-prod.s3.eu-west-1.amazonaws.com/v2/Financial-Times/ig-rcp-polls-backend/2020-presidential/latest/presidential-races.json'; // Your redirect uri
private $virtual_device_name = "Election";
private $subdevice_type = "election";
function fetch($url = 'true')
{
if (DeviceManager::registeret($this->virtual_device_name)) {
$deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id'];
$dataItems = ['Trump', 'Biden', 'Unknown'];
foreach ($dataItems as $dataItem) {
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($dataItem))) {
SubDeviceManager::create($deviceId, strtolower($dataItem), '% ' . $dataItem);
sleep(1);
}
}
if (!$this->fetchEnabled($deviceId, $subDevice['subdevice_id'])) die();
$finalUrl = $this->api_uri;
$json = json_decode(Utilities::CallAPI('GET', $finalUrl), true);
$voteSpectrum = [
'republican' => [
'solid' => 0,
'leaning' => 0,
],
'democrat' => [
'solid' => 0,
'leaning' => 0,
],
'tossup' => 0
];
foreach ($json as $state){
if ($state['raceCategory'] != 'tossup'){
$raceCategory = explode('-',$state['raceCategory']);
$voteSpectrum[$raceCategory[0]][$raceCategory[1]] = $voteSpectrum[$raceCategory[0]][$raceCategory[1]] + $state['raceDelegates'];
} else {
$voteSpectrum['tossup'] = $voteSpectrum['tossup'] + $state['raceDelegates'];
}
}
$Trump = $voteSpectrum['republican']['solid'] + $voteSpectrum['republican']['leaning'];
$Biden = $voteSpectrum['democrat']['solid'] + $voteSpectrum['democrat']['leaning'];
$Unknown = $voteSpectrum['tossup'];
$OnePercent = ($Trump + $Biden + $Unknown) / 100;
foreach ($dataItems as $Category) {
RecordManager::create($deviceId, strtolower($Category), round(($$Category / $OnePercent)));
}
} else {
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name);
DeviceManager::approved($this->virtual_device_name);
}
}
}