Skip to content

Commit

Permalink
Refactor listener data streams to be basic data streams with a separa…
Browse files Browse the repository at this point in the history
…te API endpoint.
  • Loading branch information
paul121 authored and mstenta committed Sep 20, 2021
1 parent fd74fa2 commit e528813
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 1,338 deletions.
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 modules/asset/sensor/modules/listener/farm_sensor_listener.module
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;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
farm_sensor_listener.data_stream:
path: '/farm/sensor/listener/{pub_key}'
path: '/farm/sensor/listener/{public_key}'
defaults:
_controller: '\Drupal\farm_sensor_listener\Controller\LegacyListenerController::handle'
_controller: '\Drupal\farm_sensor_listener\Controller\LegacyListenerController::publicKey'
requirements:
# There is no access restriction to this endpoint.
_access: 'TRUE'
Expand Down
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);
}

}
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}";
}

}

This file was deleted.

0 comments on commit e528813

Please sign in to comment.