1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| <?php
/*
Plugin Name: Tokyo Electric Power Supply Meter
Plugin URI:
Description:
Author:
Version: 0.0.1
Author URI:
*/
add_action( 'wp_head', 'electricity_header_content');
add_shortcode('elec_meter', 'elec_meter_func');
function electricity_header_content(){
?>
<style type="text/css">
.off {display:none}
.electricity,.tepco,.kilowatt2,.kilowatt1{font-size:10px}
.percent {margin:0}
.electricity,.tepco {padding:0}
.electricity{
margin:6px 0;
width:236px;
height:100px;
display:inline-block;
border:solid
}
.electricity,.text{
border-style:solid;
border-color:silver
}
.electricity,.text,.kilowatt1{border-width:1px}
.tepco,.percent{font-weight:bold}
.tepco,.meter{margin:0 6px}
.text{
background:#c2ed9a;
top:-25px;
right:0;
margin:0 0 0 160px;
padding:0 6px;
width:55px;
height:55px
}
.text,.savings {position:relative}
.kilowatt2,.kilowatt1 {
margin:-2px 0 1px 0;
line-height:1.5
}
.kilowatt1{
border-bottom:solid;
border-width:1px;
}
.percent {
font-size:14px;
text-align:center;
line-height:20px
}
.savings {
top:-60px;
left:6px;
font-size:12px
}
</style>
<?php
}
//------------
function h($s) {
return esc_html($s);
}
//------------
function elec_meter_func(){
$cache_file = ABSPATH.'wp-content/uploads/tokyopower.html';
if(file_exists($cache_file)){
$time_newest = @filemtime($cache_file);
}
$timedif = @(time() - $time_newest);
if ($timedif < 1800) {
/*
// If-Modified-Since
$request_headers = apache_request_headers();
$etag = md5( $_SERVER["REQUEST_URI"] . $time_newest );
if( $request_headers["If-Modified-Since"] ) {
$since = parse_http_date( $request_headers["If-Modified-Since"] );
if( $since["timestamp"] >= $time_newest ) {
header( "HTTP/1.1 304 Not Modified" );
header( "Etag: \"$etag\"" );
exit();
}
}*/
// Return cache
$data = file_get_contents($cache_file);
} else {
// Create new data
$data = createOutputData();
// save data as cache file
if ($f = @fopen($cache_file, 'w')) {
fwrite ($f, $data, strlen($data));
fclose($f);
}
}
return $data;
}
//------------
function createOutputData(){
$data = json_decode(file_get_contents("http://tepco-usage-api.appspot.com/latest.json"));
$percentage = $data->usage / $data->capacity * 100;
$usagecolor = sprintf("%d", pow(2, (9 * $percentage / 100)) - 1 );
$usagecolor = ($usagecolor > 255) ? 255 : $usagecolor;
$s = '';
$s .= '<div class="electricity">' . "\n";
$s .= sprintf('<p><span class="tepco">東京電力の電力使用状況</span> (%s/%s/%s %s時台)</p>', h($data->year), h($data->month), h($data->day), h($data->hour));
$s .= '<div class="meter" style="position: relative; width: 150px; padding: 0; background: none repeat scroll 0% 0% rgb(217, 217, 217);">
<span style="width:' . sprintf("%.1f%%", $percentage). '; display: block; position: relative; background: none repeat scroll 0% 0% rgb('.h($usagecolor).', 200, 0); text-align: center; color: rgb(51, 51, 51); height: 15px; line-height: 15px;">';
$s .= '</span></div>';
$s .= '<dl class="text" style="background-color:rgb('.h($usagecolor).', 200, 0);">'."\n";
$s .= '<dt class="off">使用率</dt><dd class="percent">' . sprintf("%.1f%%", $percentage) . "</dd>\n";
$s .= '<dt class="off">使用量</dt><dd class="kilowatt1">' . h($data->usage) . "万kW</dd>\n";
$s .= '<dt class="off">供給可能</dt><dd class="kilowatt2">' . h($data->capacity) . "万kW</dd>\n";
$s .= "</dl>\n";
$s .= '<p class="savings">計画停電 ';
$s .= ($data->savings ? '実施中' : '実施せず');
$s .= '</p>' ."\n";
$s .= "</div>\n";
return $s;
}
//------------ |