File tree Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -21,12 +21,12 @@ composer require moay/opensensemap-api-php-client
2121Take a look at the implementation example in the directory ` example ` .
2222
2323``` php
24- $client = \Moay\OpensensemapApiClient\ OpensensemapApiClientFactory::create();
24+ $client = OpensensemapApiClientFactory::create();
2525
2626// Change senseBox id
2727$senseBoxData = $client->getSenseBoxData('someSenseBoxId');
2828
29- foreach ($senseBoxData->getSensorValues() as $sensorValue) {
29+ foreach ($senseBoxData as $sensorValue) {
3030 // $sensorValue->getValueType()
3131 // $sensorValue->getValue()
3232 // $sensorValue->getUnit()
@@ -35,6 +35,20 @@ foreach ($senseBoxData->getSensorValues() as $sensorValue) {
3535}
3636```
3737
38+ You don't have to iterate over all values, just use this handy function:
39+
40+ ``` php
41+ $temperature = $senseBoxData->getValueByType(SensorValue::TYPE_TEMPERATURE);
42+ ```
43+
44+ Outputting values can be done directly by casting the values to strings:
45+
46+ ``` php
47+ echo 'Temperature: '.$temperature; // Output: Temperature: 12 °C
48+ echo $temperature; // Output: 12 °C
49+ $temperatureString = (string) $temperature // $temperature = '12 °C'
50+ ```
51+
3852## Features
3953
4054The client gives you the latest data for a specific senseBox from the OpenSenseMap Api.
Original file line number Diff line number Diff line change 22
33require __DIR__ .'/../vendor/autoload.php ' ;
44
5- $ client = \Moay \OpensensemapApiClient \OpensensemapApiClientFactory::create ();
5+ use \Moay \OpensensemapApiClient \OpensensemapApiClientFactory ;
6+ use \Moay \OpensensemapApiClient \SensorValue \SensorValue ;
7+
8+ $ client = OpensensemapApiClientFactory::create ();
69$ senseBoxData = $ client ->getSenseBoxData ('5a0c2cc89fd3c200111118f0 ' );
710
8- foreach ($ senseBoxData-> getSensorValues () as $ sensorValue ) {
11+ foreach ($ senseBoxData as $ sensorValue ) {
912 echo sprintf (
10- '%s: %s %s (Sensor: %s, %s)<br/> ' ,
13+ '%s: %s %s (Sensor: %s, %s) ' . "\n" ,
1114 $ sensorValue ->getValueType (),
1215 $ sensorValue ->getValue (),
1316 $ sensorValue ->getUnit (),
1417 $ sensorValue ->getSensorType (),
1518 $ sensorValue ->getMeasurementTime ()->format ('Y-m-d H:i:s ' )
1619 );
1720}
21+
22+ $ temperature = $ senseBoxData ->getValueByType (SensorValue::TYPE_TEMPERATURE );
23+ echo 'Temperature by type: ' .$ temperature ->getValue ().' ' .$ temperature ->getUnit ()."\n" ;
24+
25+ // Shorthand string output
26+ echo 'Temperature by type shorthand: ' .$ temperature ."\n" ;
You can’t perform that action at this time.
0 commit comments