This object is in archive! 
Get prices from Norwegian ST1 petrol stations
Under Consideration
I have written some rather rude PHP code to harvest the latest prices from the nearest ST1 petrol station in Norway. I'm not sure what I need it for yet :) but maybe some day Zipato can send me push messages (or Sonos speach) like "Your Aston Martin DB9 (I wish) has less than 30% petrol left. The price is rather good today at 10.50 kr/liter"
As I said, the code is rude, but it works. Here it is in case anybody is interested
- <?php
- define(OPPEGARD_STATION_ID, 46125, false); //You need to find the ID of your closest station. Look it up in the URL_STATIONS output
- define(TEMP_FILENAME, "/var/services/web/last_st1_price.txt", false);
- define(URL_B95_METER, "https://my.zipato.com/zipato-web/remoting/attribute/set?serial=REMOVED&apiKey=REMOVED&ep=REMOVED&value1=", false); //URL of Zipato meter
- define(URL_DIESEL_METER, "https://my.zipato.com/zipato-web/remoting/attribute/set?serial=REMOVED&apiKey=REMOVED&ep=REMOVED&value1=", false); //URL of Zipato meter
- define(URL_STATIONS, "http://st1.notch.no/prices.js", false);
- $force_update = false;
- $force_update = $_GET['force']; //You can force an update with ?force=true
- echo date("Y-m-d H:i:s") . ":"; //Output for my cron-job redirect log
- $price_b95 = 0.0;
- $price_diesel = 0.0;
- $json = GetStationsJSON(URL_STATIONS);
- foreach($json['stations'] as $station) {
- if ($station['stationId'] == OPPEGARD_STATION_ID) {
- $price_b95 = (double) str_replace(",", ".", $station['b95']);
- $price_diesel = (double) str_replace(",", ".", $station['diesel']);
- break;
- }
- }
- list($previous_b95, $previous_diesel) = explode(";", ReadPricesFromFile(TEMP_FILENAME));
- $previous_b95 = (double) str_replace(",",".", $previous_b95);
- $previous_diesel = (double) str_replace(",",".", $previous_diesel);
- if (($previous_b95 != $price_b95) || $force_update) { //write to zipato meter only if any of the price has changed
- UpdateZipatoMeter(URL_B95_METER, $price_b95);
- WritePricesToFile(TEMP_FILENAME, $price_b95, $price_diesel);
- echo " p_b: " . $previous_b95 . ", c_b: " . $price_b95;
- }
- else {
- echo " B95: no change"; //for the log
- }
- if (($previous_diesel != $price_diesel) || $force_update) { //Wirte to zipato meter only if the price has changed
- UpdateZipatoMeter(URL_DIESEL_METER, $price_diesel);
- WritePricesToFile(TEMP_FILENAME, $price_b95, $price_diesel);
- echo " p_D: " . $previous_diesel . ", c_D: " . $price_diesel; //for the log
- }
- else {
- echo " D: no change"; //for the log
- }
- echo "\n"; //newline for the log
- function GetStationsJSON($stations_url) { //strips JS code and returns JSON
- $content = file_get_contents($stations_url);
- $content = str_replace('var prices = \'', '', $content);
- $content = substr($content, 0, strlen($content)-2); //removing ';' at the end
- return json_decode($content, true);
- }
- function WritePricesToFile($filename, $b95_price, $diesel_price){ //Latest prices are written to file
- $Handle = fopen($filename, 'w');
- $Data = $b95_price . ';' . $diesel_price;
- fwrite($Handle, $Data);
- fclose($Handle);
- }
- function ReadPricesFromFile($filename) {
- if (!file_exists($filename)) {
- echo "\nFile " . $filename . "does not exist"; //for logging
- return "1000.0;1000.0"; //return a high price if no price have been recorded yet
- }
- $filehandler = fopen($filename, "r");
- $contents = fread($filehandler, filesize($filename));
- fclose($filehandler);
- return $contents;
- }
- function UpdateZipatoMeter($meter_url, $price) {
- $url = $meter_url . $price;
- $devnull = file_get_contents($url);
- }
- ?>
No connection
Real-time notifications may not work
I have no idea why also, I think we will all be in Tesla cars by then. But nice work. Could you put your coding skills to better use, such as helping to develop plugins for Sonos amp, or better Nest integration, or how about a plugin for the doorbird intercoms (they have an open api). Others would like a netamo plugin. I am sure everyone here has a million things you could do, keep you busy for years. By then all the oil would have dried up and we'll be needing a code for what days are sunny enough to drive around in our solar paneled cars. Just a thought. Cool job though.
I have no idea why also, I think we will all be in Tesla cars by then. But nice work. Could you put your coding skills to better use, such as helping to develop plugins for Sonos amp, or better Nest integration, or how about a plugin for the doorbird intercoms (they have an open api). Others would like a netamo plugin. I am sure everyone here has a million things you could do, keep you busy for years. By then all the oil would have dried up and we'll be needing a code for what days are sunny enough to drive around in our solar paneled cars. Just a thought. Cool job though.
I like fiddling with code, but normally my time is limited. Scripts like this is usually made after elleven o'clock at night :-)
I really miss scripting capabilities in Zipabox though. Python, javascript, lua or similar would be great.
I like fiddling with code, but normally my time is limited. Scripts like this is usually made after elleven o'clock at night :-)
I really miss scripting capabilities in Zipabox though. Python, javascript, lua or similar would be great.
I wish I wish.... for cristmas Logitech Harmony integration?
Did not even know this was possible with zipabox?
I wish I wish.... for cristmas Logitech Harmony integration?
Did not even know this was possible with zipabox?
me too Jacob.
Hey Marius, someone put me onto this, as I am no coder I do not understand it, but maybe you will.
https://www.npmjs.com/package/zipabox
me too Jacob.
Hey Marius, someone put me onto this, as I am no coder I do not understand it, but maybe you will.
https://www.npmjs.com/package/zipabox
Adrian and Jacob, all these integration scripts are running on separate devices. I write my scripts in PHP, simply because it is easily available on my NAS
The last link Adrian is something more comprehensive. As far as I understand. it is a Nodejs wrapper for the Zipato api. With this wrapper you can write server-side, event driven javascript code to integrate with the Zipabox. If the complete Api is covered, it will be possible to control the Zipabox almost entirely. It still needs to run on a separate server though. If someone has written a similar package (Nodejs api wrapper) for e.g. the Logitech (I guess someone already have :) ) it will make coding the communication between Zipabox and Logitech quite fast.
But still, it would need to run on a separate server.
Adrian and Jacob, all these integration scripts are running on separate devices. I write my scripts in PHP, simply because it is easily available on my NAS
The last link Adrian is something more comprehensive. As far as I understand. it is a Nodejs wrapper for the Zipato api. With this wrapper you can write server-side, event driven javascript code to integrate with the Zipabox. If the complete Api is covered, it will be possible to control the Zipabox almost entirely. It still needs to run on a separate server though. If someone has written a similar package (Nodejs api wrapper) for e.g. the Logitech (I guess someone already have :) ) it will make coding the communication between Zipabox and Logitech quite fast.
But still, it would need to run on a separate server.
Replies have been locked on this page!