Simple-Home Biden vs. Trump Edition
This commit is contained in:
		@@ -11,6 +11,7 @@ class CronApi extends ApiController {
 | 
				
			|||||||
		  //echo (new VirtualDeviceManager)->fetch('');
 | 
							  //echo (new VirtualDeviceManager)->fetch('');
 | 
				
			||||||
		  echo (new Covid)->fetch('');
 | 
							  echo (new Covid)->fetch('');
 | 
				
			||||||
		  echo (new OpenWeatherMap)->fetch('');
 | 
							  echo (new OpenWeatherMap)->fetch('');
 | 
				
			||||||
 | 
							  echo (new UsaElection)->fetch('');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->response(['Value' => 'OK']);
 | 
					        $this->response(['Value' => 'OK']);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -40,7 +40,7 @@ class WidgetApi extends ApiController
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
 | 
							$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
 | 
				
			||||||
		$deviceData = DeviceManager::getDeviceById($subDeviceData['device_id']);
 | 
							$deviceData = DeviceManager::getDeviceById($subDeviceData['device_id']);
 | 
				
			||||||
		$events = RecordManager::getLastRecord($subDeviceId, 5);
 | 
							$events = RecordManager::getLastRecord($subDeviceId, 10);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$LastRecordTime = new DateTime($events[4]['time']);
 | 
							$LastRecordTime = new DateTime($events[4]['time']);
 | 
				
			||||||
		$niceTime = Utilities::ago($LastRecordTime);
 | 
							$niceTime = Utilities::ago($LastRecordTime);
 | 
				
			||||||
@@ -75,7 +75,10 @@ class WidgetApi extends ApiController
 | 
				
			|||||||
			'graph' => [
 | 
								'graph' => [
 | 
				
			||||||
				'data' => [
 | 
									'data' => [
 | 
				
			||||||
					'labels' => $labels,
 | 
										'labels' => $labels,
 | 
				
			||||||
					'dataset' => $values
 | 
										'datasets' => [[
 | 
				
			||||||
 | 
											//'label' => 'FUCK you',
 | 
				
			||||||
 | 
											'data' => $values,
 | 
				
			||||||
 | 
										]],
 | 
				
			||||||
				],
 | 
									],
 | 
				
			||||||
				'options' => [
 | 
									'options' => [
 | 
				
			||||||
					'scales' => [
 | 
										'scales' => [
 | 
				
			||||||
@@ -85,9 +88,9 @@ class WidgetApi extends ApiController
 | 
				
			|||||||
						],
 | 
											],
 | 
				
			||||||
						'yAxes' => [
 | 
											'yAxes' => [
 | 
				
			||||||
							'ticks' => [
 | 
												'ticks' => [
 | 
				
			||||||
								'min' => RANGES[$subDeviceData['type']]['min'],
 | 
													'min' => $this->getDeviceConfig($subDeviceData['type'])['min'],
 | 
				
			||||||
								'max' => RANGES[$subDeviceData['type']]['max'],
 | 
													'max' => $this->getDeviceConfig($subDeviceData['type'])['max'],
 | 
				
			||||||
								'steps' => RANGES[$subDeviceData['type']]['scale'],
 | 
													'steps' => $this->getDeviceConfig($subDeviceData['type'])['scale'],
 | 
				
			||||||
							]
 | 
												]
 | 
				
			||||||
						]
 | 
											]
 | 
				
			||||||
					],
 | 
										],
 | 
				
			||||||
@@ -108,4 +111,11 @@ class WidgetApi extends ApiController
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		$this->response($response);
 | 
							$this->response($response);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private function getDeviceConfig($type){
 | 
				
			||||||
 | 
							if (isset(RANGES[$type])){
 | 
				
			||||||
 | 
								return RANGES[$type];
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return RANGES[''];
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -167,7 +167,7 @@ class Utilities
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		// Optional Authentication:
 | 
							// Optional Authentication:
 | 
				
			||||||
		curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 | 
							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_URL, $url);
 | 
				
			||||||
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 | 
							curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -87,7 +87,7 @@ class SubDeviceManager
 | 
				
			|||||||
				GROUP BY subdevice_id
 | 
									GROUP BY subdevice_id
 | 
				
			||||||
			  )
 | 
								  )
 | 
				
			||||||
			GROUP BY subdevice_id
 | 
								GROUP BY subdevice_id
 | 
				
			||||||
			ORDER BY type DESC
 | 
								ORDER BY d.name DESC
 | 
				
			||||||
		", $roomIds);
 | 
							", $roomIds);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$ret = [];
 | 
							$ret = [];
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										65
									
								
								app/plugins/UsaElection.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								app/plugins/UsaElection.php
									
									
									
									
									
										Normal 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);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Reference in New Issue
	
	Block a user