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
21
21
Take a look at the implementation example in the directory ` example ` .
22
22
23
23
``` php
24
- $client = \Moay\OpensensemapApiClient\ OpensensemapApiClientFactory::create();
24
+ $client = OpensensemapApiClientFactory::create();
25
25
26
26
// Change senseBox id
27
27
$senseBoxData = $client->getSenseBoxData('someSenseBoxId');
28
28
29
- foreach ($senseBoxData->getSensorValues() as $sensorValue) {
29
+ foreach ($senseBoxData as $sensorValue) {
30
30
// $sensorValue->getValueType()
31
31
// $sensorValue->getValue()
32
32
// $sensorValue->getUnit()
@@ -35,6 +35,20 @@ foreach ($senseBoxData->getSensorValues() as $sensorValue) {
35
35
}
36
36
```
37
37
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
+
38
52
## Features
39
53
40
54
The client gives you the latest data for a specific senseBox from the OpenSenseMap Api.
Original file line number Diff line number Diff line change 2
2
3
3
require __DIR__ .'/../vendor/autoload.php ' ;
4
4
5
- $ client = \Moay \OpensensemapApiClient \OpensensemapApiClientFactory::create ();
5
+ use \Moay \OpensensemapApiClient \OpensensemapApiClientFactory ;
6
+ use \Moay \OpensensemapApiClient \SensorValue \SensorValue ;
7
+
8
+ $ client = OpensensemapApiClientFactory::create ();
6
9
$ senseBoxData = $ client ->getSenseBoxData ('5a0c2cc89fd3c200111118f0 ' );
7
10
8
- foreach ($ senseBoxData-> getSensorValues () as $ sensorValue ) {
11
+ foreach ($ senseBoxData as $ sensorValue ) {
9
12
echo sprintf (
10
- '%s: %s %s (Sensor: %s, %s)<br/> ' ,
13
+ '%s: %s %s (Sensor: %s, %s) ' . "\n" ,
11
14
$ sensorValue ->getValueType (),
12
15
$ sensorValue ->getValue (),
13
16
$ sensorValue ->getUnit (),
14
17
$ sensorValue ->getSensorType (),
15
18
$ sensorValue ->getMeasurementTime ()->format ('Y-m-d H:i:s ' )
16
19
);
17
20
}
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