Created
September 17, 2020 12:22
-
-
Save dejurin/e47510d17ca547ebc8092e69c2689f7e to your computer and use it in GitHub Desktop.
Insert Weather into Hikvision IP CAM via Text Overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://api.openweathermap.org/data/2.5/weather?id=709114&appid=&units=metric&mode=json | |
// This code you can add to CRON | |
Class hikOverlay { | |
function __construct(object $config = null) { | |
if (isset($config)) { | |
foreach($config as $key => $value) { | |
$this->$key = (object) $value; | |
} | |
} | |
$this->displayText = 'None'; | |
} | |
public function setOverlayText(object $params = null) { | |
$data = $this->_getWeather(); | |
if ($data) { | |
// Set params to CAM with new $this->displayText | |
$camName = $this->_curlReq((object) [ | |
'type' => 'overlay', | |
'id' => 1, | |
'url' => $params->url, | |
'ssl' => false, | |
'header' => false, | |
'auth' => $params->auth, | |
]); | |
$parseXml = new SimpleXMLElement($camName); | |
$this->_curlReq($params); | |
return '['.(string) $parseXml->name[0].'] '.$this->displayText; | |
} | |
} | |
private function _curlReq(object $params) { | |
if (is_null($params)) return false; | |
if ($params->type === 'cam') { | |
$query = 'ISAPI/System/Video/inputs/channels/'.$params->id.'/overlays/text'; | |
} | |
else if ($params->type === 'overlay') { | |
$query = 'ISAPI/System/Video/inputs/channels/'.$params->id; | |
} | |
else { | |
$query = (isset($params->query)) ? http_build_query($params->query) : ''; | |
} | |
$ch = curl_init($params->url.$query); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $params->ssl); | |
curl_setopt($ch, CURLOPT_HEADER, $params->header); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
if (isset($params->auth)) { | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); | |
curl_setopt($ch, CURLOPT_USERPWD, $params->auth['username'] . ":" . $params->auth['password']); | |
} | |
if (isset($params->method)) { | |
$xml = $this->_makeXMLBody($params->id, $this->displayText); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/xml']); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); | |
} | |
return curl_exec($ch); | |
curl_close($ch); | |
} | |
private function _getWeather() { | |
$data = $this->_curlReq($this->weather); | |
if ($data) { | |
$res = json_decode($data); | |
if ($res->cod === 200) { | |
$temp = round($res->main->temp, 1); | |
$temp = ($temp > 0) ? '+'.$temp : $temp; | |
$feels_like = round($res->main->feels_like, 1); | |
$feels_like = ($feels_like > 0) ? '+'.$feels_like : $feels_like; | |
$this->displayText = $temp.' ('.$feels_like.')C / H-'.$res->main->humidity.'% / W-'.$res->wind->speed.'m'; | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
return $data; | |
} | |
private function _makeXMLBody(int $id, string $displayText = '') { | |
$xml = new SimpleXMLElement("<TextOverlayList></TextOverlayList>"); | |
$TextOverlay = $xml->addChild('TextOverlay'); | |
$id = $TextOverlay->addChild('id', $id); | |
$enabled = $TextOverlay->addChild('enabled', 'true'); | |
$displayText = $TextOverlay->addChild('displayText', $displayText); | |
return $xml->asXML(); | |
} | |
} | |
// Init Class | |
$hikOverlay = new hikOverlay((object) [ | |
'weather' => [ | |
'type' => 'weather', | |
'url' => 'api.openweathermap.org/data/2.5/weather?', | |
'query' => [ | |
'id' => '', | |
'appid' => '', | |
'units' => 'metric', | |
'mode' => 'json', | |
], | |
'ssl' => false, | |
'header' => false, | |
], | |
]); | |
// Cams records | |
$cams = [ | |
[ | |
'id' => 1, | |
'url' => 'http://192.168.1.2/', | |
], | |
[ | |
'id' => 1, | |
'url' => 'http://192.168.1.3/', | |
], | |
]; | |
$count = count($cams); | |
// set text overlays | |
for($i = 0; $i < $count; $i++) { | |
echo 'CAM-',$i; | |
$params = (object) [ | |
'type' => 'cam', | |
'id' => $cams[$i]['id'], | |
'url' => $cams[$i]['url'], | |
'method' => 'put', | |
'ssl' => false, | |
'header' => false, | |
'auth' => [ | |
'username' => 'admin', | |
'password' => '12345', | |
], | |
]; | |
echo ': ', $hikOverlay->setOverlayText($params),PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment