-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor listener data streams to be basic data streams with a separa…
…te API endpoint.
- Loading branch information
Showing
13 changed files
with
116 additions
and
1,338 deletions.
There are no files selected for viewing
3 changes: 1 addition & 2 deletions
3
...es/listener/farm_sensor_listener.info.yml → ...es/listener/farm_sensor_listener.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
name: Sensor listener | ||
description: Provides a Listener (Legacy) data stream type to support sensors created in farmOS 1.x. | ||
description: Provides a Listener (Legacy) API endpoint to support sensors created in farmOS 1.x. | ||
type: module | ||
package: farmOS (Legacy) | ||
core_version_requirement: ^9 | ||
dependencies: | ||
- farm:farm_sensor | ||
- farm:data_stream | ||
- fraction:fraction |
32 changes: 32 additions & 0 deletions
32
modules/asset/sensor/modules/listener/farm_sensor_listener.module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* The farm_sensor_listener module. | ||
*/ | ||
|
||
use Drupal\Core\Entity\EntityTypeInterface; | ||
use Drupal\data_stream\Entity\DataStream; | ||
|
||
/** | ||
* Implements hook_farm_entity_bundle_field_info(). | ||
*/ | ||
function farm_sensor_listener_farm_entity_bundle_field_info(EntityTypeInterface $entity_type, string $bundle) { | ||
$fields = []; | ||
|
||
// Add a public_key reference field to sensor assets. | ||
if ($entity_type->id() === 'asset' && $bundle === 'sensor') { | ||
$options = [ | ||
'type' => 'string', | ||
'label' => t('Public key (legacy)'), | ||
'description' => t('Public key (legacy) for the sensor.'), | ||
'default_value_callback' => DataStream::class . '::createUniqueKey', | ||
'weight' => [ | ||
'form' => 3, | ||
], | ||
]; | ||
$fields['public_key'] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options); | ||
} | ||
|
||
return $fields; | ||
} |
4 changes: 2 additions & 2 deletions
4
...listener/farm_sensor_listener.routing.yml → ...listener/farm_sensor_listener.routing.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
modules/asset/sensor/modules/listener/src/Controller/LegacyListenerController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Drupal\farm_sensor_listener\Controller; | ||
|
||
use Drupal\farm_sensor\Controller\SensorDataController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
/** | ||
* Route callbacks for the LegacyListener controller. | ||
*/ | ||
class LegacyListenerController extends SensorDataController { | ||
|
||
/** | ||
* Respond to GET or POST requests referencing sensor assets by public_key. | ||
* | ||
* @param \Symfony\Component\HttpFoundation\Request $request | ||
* The request. | ||
* @param string $public_key | ||
* The sensor asset public_key. | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
* The response. | ||
*/ | ||
public function publicKey(Request $request, string $public_key) { | ||
|
||
// Load the sensor asset. | ||
$sensor_assets = $this->entityTypeManager() | ||
->getStorage('asset') | ||
->loadByProperties([ | ||
'type' => 'sensor', | ||
'public_key' => $public_key, | ||
]); | ||
|
||
// Bail if the public_key is not found. | ||
if (empty($sensor_assets)) { | ||
throw new NotFoundHttpException(); | ||
} | ||
|
||
/** @var \Drupal\asset\Entity\AssetInterface $asset */ | ||
$asset = reset($sensor_assets); | ||
return $this->handleAssetRequest($asset, $request); | ||
} | ||
|
||
} |
File renamed without changes.
36 changes: 36 additions & 0 deletions
36
modules/asset/sensor/modules/listener/tests/src/Functional/LegacyApiTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\farm_sensor_listener\Functional; | ||
|
||
use Drupal\asset\Entity\AssetInterface; | ||
use Drupal\Tests\farm_sensor\Functional\SensorDataApiTest; | ||
|
||
/** | ||
* Test the Listener (Legacy) sensor API. | ||
* | ||
* @group farm | ||
*/ | ||
class LegacyApiTest extends SensorDataApiTest { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = [ | ||
'farm_sensor_listener', | ||
]; | ||
|
||
/** | ||
* Helper function to build the path to the sensor API. | ||
* | ||
* @param \Drupal\asset\Entity\AssetInterface $asset | ||
* The asset. | ||
* | ||
* @return string | ||
* The path. | ||
*/ | ||
protected function buildPath(AssetInterface $asset) { | ||
$public_key = $asset->get('public_key')->value; | ||
return "base://farm/sensor/listener/{$public_key}"; | ||
} | ||
|
||
} |
9 changes: 0 additions & 9 deletions
9
...les/core/data_stream/modules/listener/config/install/data_stream.type.legacy_listener.yml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.