Skip to content

Device Registry API - @energyweb/origin-device-registry-api

Source code on GitHub:

Overview

The Device Registry API is a NestJS package that provides restful endpoints for fetching and creating/persisting Devices.

The below gives an overview the of the package architecture, however the NestJS documentation provides further detail into the fundamentals of NestJS Architecture that may help to understand the elements of this application:

Device Registry API Architecture

The Device Registry API exports one module, which is the Device Registry Module. The module contains:

  • A controller that manages requests and responses to the client
  • An entity file that maps an entity to a database repository
  • A service file that provides methods to fetch and transform data
  • Data Transfer Object (DTO) file(s) that provide Data Transfer Objects, which are representations of the data that are exposed to the endpoint consumer
  • A module class that is used by NestJS to structure the application

Service Endpoints

The services file provides public methods to fetch and create new devices:

    public async find(options?: FindManyOptions<OriginDevice>): Promise<OriginDevice[]> {
        return this.repository.find(options);
    }

source

    public async register(user: ILoggedInUser, newDevice: NewDeviceDTO): Promise<string> {
        await this.validateRegistration(user, newDevice);

        const deviceToStore = new OriginDevice({
            ...NewDeviceDTO.sanitize(newDevice),
            owner: user.ownerId
        });

        const storedDevice = await this.repository.save(deviceToStore);

        return storedDevice.id;
    }

source

Persistence

Device information is persisted in the Origin Device Repository. The Origin Device entity file maps the Device data to a strongly typed entity in the repository.

Repositories are injected into services or command handlers so they are available to use in methods:

@Injectable()
export class DeviceRegistryService {
    constructor(
        @InjectRepository(OriginDevice) private readonly repository: Repository<OriginDevice>,
        private readonly queryBus: QueryBus
    ) {}

    public async find(options?: FindManyOptions<OriginDevice>): Promise<OriginDevice[]> {
        return this.repository.find(options);
    }
...
}

source

Note that the device entity fields are dependent on implementation needs and the registry that the application will integrate with. If, for example, the registry will integrate with I-REC, different or additional fields will be required to conform to I-REC's device registration standards.

The External Registry Id is the Id of this device in another external (e.g. I-REC) registry. Note that the Smart Meter Id and External Registry Id can only be tied to one device.

Reference Implentation