Changeset 1649150
- Timestamp:
- 05/01/2017 06:54:49 PM (9 years ago)
- Location:
- live-widget-luftdaten
- Files:
-
- 3 added
- 2 edited
-
tags/1.1 (added)
-
tags/1.1/live-widget-luftdaten.php (added)
-
tags/1.1/readme.txt (added)
-
trunk/live-widget-luftdaten.php (modified) (9 diffs)
-
trunk/readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
live-widget-luftdaten/trunk/live-widget-luftdaten.php
r1632200 r1649150 4 4 Plugin URI: http://www.bleeptrack.de/feinstaub-widget/ 5 5 Description: Plugin with widget to show live data from a luftdaten.info sensor 6 Version: 1. 0.16 Version: 1.1.0 7 7 Author: Bleeptrack 8 8 Author URI: http://www.bleeptrack.de/ … … 11 11 12 12 13 class Luftdaten Widgetextends WP_Widget {13 class LuftdatenAmpel extends WP_Widget { 14 14 15 15 // constructor 16 function Luftdaten Widget() {17 parent::WP_Widget(false, $name = __('Luftdaten Live Widget', 'LuftdatenWidget') );16 function LuftdatenAmpel() { 17 parent::WP_Widget(false, $name = __('Luftdaten Ampel', 'LuftdatenAmpel') ); 18 18 } 19 19 20 20 // widget form creation 21 21 function form($instance) { 22 //print_r($instance); for easy debugging22 //print_r($instance); // for easy debugging 23 23 ?> 24 24 <p> … … 31 31 <input class="widefat" id="<?php echo $this->get_field_id('neuersensor'); ?>" name="<?php echo $this->get_field_name('neuersensor'); ?>" type="text" value="<?php echo $instance['neuersensor']; ?>" /> 32 32 </p> 33 <?php34 35 foreach ($instance as $key => $value) {36 if(strcmp($key,'title')&&strcmp($key,'neuersensor')){37 $this->printBackend($value->items, $value->id);38 }39 }40 ?>41 33 42 34 <?php … … 74 66 75 67 function widget($args, $instance) { 68 69 $p1g=50; 70 $p2g=20; 71 72 $red = "A30303"; 73 $yellow = "F4EC00"; 74 $green = "03A350"; 75 76 $p1arr = $this->Gradient3($green,$yellow,$red,$p1g+1); 77 $p2arr = $this->Gradient3($green,$yellow,$red,$p2g+1); 78 79 76 80 extract( $args ); 77 81 echo $before_widget; … … 81 85 } 82 86 83 foreach ($instance as $key => $value) { 87 $v1 = 0; 88 $v2 = 0; 89 $count = 0; 90 91 foreach ($instance as $key => $value) { 84 92 if(strcmp($key,'title')&&strcmp($key,'neuersensor')){ 85 $this->printData($value); 93 //var_dump($value); 94 $sensordata = $this->getData($value); 95 $v1 = $v1 + $sensordata['P1']; 96 $v2 = $v2 + $sensordata['P2']; 97 $count = $count+1; 86 98 } 87 99 } 100 101 $ampelvalue1 = $v1/$count; 102 $ampelvalue2 = $v2/$count; 103 104 $ampelcol1 = round($ampelvalue1,0); 105 if($ampelcol1>$p1g) $ampelcol1=$p1g; 106 $ampelcol2 = round($ampelvalue2,0); 107 if($ampelcol2>$p2g) $ampelcol2=$p2g; 108 109 if(round($ampelvalue1,1)>=$p1g || round($ampelvalue2,1)>=$p2g){ 110 $bordercol = $red; 111 }else{ 112 $bordercol = "000"; 113 } 114 115 ?> 116 117 118 <div class="feinstaubampel" style="margin: auto"> 119 <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="173" height="153" viewbox="-5 -5 170 148.56406460551017 "> 120 121 <path fill="#<?= $bordercol ?>" d="M-5 69.28203230275508L38 -5L122 -5L165 69.28203230275508L122 143.56406460551017L38 143.56406460551017Z"></path> 122 123 <path fill="#<?= $p1arr[$ampelcol1]?>" d="M0 69.28203230275508L40 0L120 0L160 69.28203230275508L120 138.56406460551017L40 138.56406460551017Z"></path> 124 <path fill="#<?= $p2arr[$ampelcol2]?>" d="M0 69.28203230275508L160 69.28203230275508L120 138.56406460551017L40 138.56406460551017Z"></path> 125 126 <line x1="0" y1="69" x2="160" y2="69" style="stroke:#<?= $bordercol ?>;stroke-width:3" /> 127 128 <text x="80" y="15" fill="black" text-anchor="middle" style="font-size:12px;" fill-opacity="0.5">PM10</text> 129 <text x="80" y="132" fill="black" text-anchor="middle" style="font-size:12px;" fill-opacity="0.5">PM2.5</text> 130 131 <text x="80" y="55" fill="black" text-anchor="middle" style="font-size:28px;font-weight:bold;"> 132 <?= round($ampelvalue1,1) ?> 133 </text> 134 <text x="80" y="105" fill="black" text-anchor="middle" style="font-size:28px;font-weight:bold;"> 135 <?= round($ampelvalue2,1) ?> 136 </text> 137 </svg> 138 </div> 139 140 <?php 88 141 89 142 echo $after_widget; … … 104 157 } 105 158 106 function printData($sensor){159 function getData($sensor){ 107 160 $v = array(); 108 161 … … 127 180 } 128 181 182 return $v; 183 184 } 185 186 function Gradient3($from, $to1, $to2, $steps){ 187 $arr1 = $this->Gradient($from,$to1,$steps/2); 188 $arr2 = $this->Gradient($to1,$to2,$steps/2); 189 return array_merge($arr1,$arr2); 190 191 } 192 193 function Gradient($HexFrom, $HexTo, $ColorSteps) { 194 $FromRGB['r'] = hexdec(substr($HexFrom, 0, 2)); 195 $FromRGB['g'] = hexdec(substr($HexFrom, 2, 2)); 196 $FromRGB['b'] = hexdec(substr($HexFrom, 4, 2)); 197 198 $ToRGB['r'] = hexdec(substr($HexTo, 0, 2)); 199 $ToRGB['g'] = hexdec(substr($HexTo, 2, 2)); 200 $ToRGB['b'] = hexdec(substr($HexTo, 4, 2)); 201 202 $StepRGB['r'] = ($FromRGB['r'] - $ToRGB['r']) / ($ColorSteps - 1); 203 $StepRGB['g'] = ($FromRGB['g'] - $ToRGB['g']) / ($ColorSteps - 1); 204 $StepRGB['b'] = ($FromRGB['b'] - $ToRGB['b']) / ($ColorSteps - 1); 205 206 $GradientColors = array(); 207 208 for($i = 0; $i <= $ColorSteps; $i++) { 209 $RGB['r'] = floor($FromRGB['r'] - ($StepRGB['r'] * $i)); 210 $RGB['g'] = floor($FromRGB['g'] - ($StepRGB['g'] * $i)); 211 $RGB['b'] = floor($FromRGB['b'] - ($StepRGB['b'] * $i)); 212 213 $HexRGB['r'] = sprintf('%02x', ($RGB['r'])); 214 $HexRGB['g'] = sprintf('%02x', ($RGB['g'])); 215 $HexRGB['b'] = sprintf('%02x', ($RGB['b'])); 216 217 $GradientColors[] = implode(NULL, $HexRGB); 218 } 219 //$GradientColors = array_filter($GradientColors, "len"); 220 return $GradientColors; 221 } 222 223 224 } 225 226 227 /////// 228 229 230 class LuftdatenWidget extends WP_Widget { 231 232 // constructor 233 function LuftdatenWidget() { 234 parent::WP_Widget(false, $name = __('Luftdaten Live Widget', 'LuftdatenWidget') ); 235 } 236 237 // widget form creation 238 function form($instance) { 239 //print_r($instance); for easy debugging 240 ?> 241 <p> 242 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Titel', 'LuftdatenWidget'); ?></label> 243 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" /> 244 </p> 245 246 <p> 247 <label for="<?php echo $this->get_field_id('neuersensor'); ?>"><?php _e('Sensor IDs Kommagetrennt', 'LuftdatenWidget'); ?></label> 248 <input class="widefat" id="<?php echo $this->get_field_id('neuersensor'); ?>" name="<?php echo $this->get_field_name('neuersensor'); ?>" type="text" value="<?php echo $instance['neuersensor']; ?>" /> 249 </p> 250 <?php 251 252 foreach ($instance as $key => $value) { 253 if(strcmp($key,'title')&&strcmp($key,'neuersensor')){ 254 $this->printBackend($value->items, $value->id); 255 } 256 } 257 ?> 258 259 <?php 260 } 261 262 // widget update 263 function update($new_instance, $old_instance) { 264 $instance = $old_instance; 265 266 foreach ($new_instance as $key => $value) { 267 //$instance[$key] = strip_tags($value); 268 $expl = explode("_", $key ); 269 if(count($expl)>1){ 270 $instance[$expl[0]]->updateItem(trim($expl[1]),trim($value)); 271 }else{ 272 $instance[$key] = strip_tags($value); 273 } 274 } 275 276 //wenn id für neuen Sensor, dann anlegen 277 if($new_instance['neuersensor']!=$old_instance['neuersensor']){ 278 $instance = array(); 279 $instance['title']=$new_instance['title']; 280 $instance['neuersensor']=$new_instance['neuersensor']; 281 $comma_separated = explode(",", $new_instance['neuersensor'] ); 282 283 foreach ($comma_separated as $value) { 284 $instance[trim($value)] = new Sensor( trim($value) ); 285 } 286 287 } 288 return $instance; 289 } 290 291 292 function widget($args, $instance) { 293 extract( $args ); 294 echo $before_widget; 295 $title = apply_filters('widget_title', $instance['title']); 296 if ( $title ) { 297 echo $before_title . $title . $after_title; 298 } 299 300 foreach ($instance as $key => $value) { 301 if(strcmp($key,'title')&&strcmp($key,'neuersensor')){ 302 $this->printData($value); 303 } 304 } 305 306 echo $after_widget; 307 308 } 309 310 function printBackend($items,$id){ 311 foreach ($items as $key => $value) { 312 ?> 313 314 <p> 315 <label for="<?php echo $this->get_field_id($id.'_'.$key); ?>"><?php _e('Label '.$key, 'LuftdatenWidget'); ?></label> 316 <input class="widefat" id="<?php echo $this->get_field_id($id.'_'.$key); ?>" name="<?php echo $this->get_field_name($id.'_'.$key); ?>" type="text" value="<?php echo $value; ?>" /> 317 </p> 318 319 <?php 320 } 321 } 322 323 function printData($sensor){ 324 $v = array(); 325 326 $results = $sensor->fetchJson(); 327 328 $count = 0; 329 330 if($results!=null){ 331 332 foreach ($results as $item) { 333 334 foreach($item->sensordatavalues as $values){ 335 $count++; 336 if (!array_key_exists($values->value_type, $v)) { 337 $v[$values->value_type]=$values->value; 338 }else{ 339 $v[$values->value_type]=$v[$values->value_type]+($values->value-$v[$values->value_type])/$count; 340 } 341 } 342 343 } 344 } 345 129 346 echo '<div id="'.$sensor->id.'-name">'.$sensor->items['name'].'</div><ul>'; 130 347 … … 157 374 } 158 375 376 //// 377 159 378 class Sensor{ 160 379 public $id = 260; … … 201 420 // register widget 202 421 add_action('widgets_init', create_function('', 'return register_widget("LuftdatenWidget");')); 422 add_action('widgets_init', create_function('', 'return register_widget("LuftdatenAmpel");')); 203 423 204 424 -
live-widget-luftdaten/trunk/readme.txt
r1632200 r1649150 20 20 *Bugfix: Error if whitespaces between sensor ids 21 21 *Bugfix: Error if sensor offline 22 23 = 1.1 = 24 *new Widget: colorful widget for showing the current PM values. Turns red if over EU threshold.
Note: See TracChangeset
for help on using the changeset viewer.