diff --git a/app/api/CronApi.php b/app/api/CronApi.php index 8df99ad..1f5adf2 100644 --- a/app/api/CronApi.php +++ b/app/api/CronApi.php @@ -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']); } diff --git a/app/api/WidgetApi.php b/app/api/WidgetApi.php index 41ad8aa..cfd1da7 100644 --- a/app/api/WidgetApi.php +++ b/app/api/WidgetApi.php @@ -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['']; + } } diff --git a/app/models/Utilities.php b/app/models/Utilities.php index caddfe7..9beac1c 100644 --- a/app/models/Utilities.php +++ b/app/models/Utilities.php @@ -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); diff --git a/app/models/managers/SubDeviceManager.php b/app/models/managers/SubDeviceManager.php index 7fef551..85af59c 100644 --- a/app/models/managers/SubDeviceManager.php +++ b/app/models/managers/SubDeviceManager.php @@ -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 = []; diff --git a/app/plugins/UsaElection.php b/app/plugins/UsaElection.php new file mode 100644 index 0000000..fea0dda --- /dev/null +++ b/app/plugins/UsaElection.php @@ -0,0 +1,65 @@ +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); + } + } +} + + +