npm package: @openhps/protobuf
Description: OpenHPS Protocol Buffer Generator
Author: Maxim Van de Wynckel
Latest version: 1.0.2

<h1 align="center">
  <img alt="OpenHPS" src="https://openhps.org/images/logo_text-512.png" width="40%" /><br />
  @openhps/protobuf
</h1>
<p align="center">
    <a href="https://github.com/OpenHPS/openhps-protobuf/actions/workflows/main.yml" target="_blank">
        <img alt="Build Status" src="https://github.com/OpenHPS/openhps-protobuf/actions/workflows/main.yml/badge.svg">
    </a>
    <a href="https://codecov.io/gh/OpenHPS/openhps-protobuf">
        <img src="https://codecov.io/gh/OpenHPS/openhps-protobuf/branch/master/graph/badge.svg?token=U896HUBDCZ"/>
    </a>
    <a href="https://codeclimate.com/github/OpenHPS/openhps-protobuf/" target="_blank">
        <img alt="Maintainability" src="https://img.shields.io/codeclimate/maintainability/OpenHPS/openhps-protobuf">
    </a>
    <a href="https://badge.fury.io/js/@openhps%2Fprotobuf">
        <img src="https://badge.fury.io/js/@openhps%2Fprotobuf.svg" alt="npm version" height="18">
    </a>
</p>

<h3 align="center">
    <a href="https://github.com/OpenHPS/openhps-core">@openhps/core</a> &mdash; <a href="https://openhps.org/docs/protobuf">API</a>
</h3>

<br />

When serializing data generated by OpenHPS, the framework will use a modified serialization strategy of [TypedJSON](https://github.com/JohnWeisz/TypedJSON/) to convert the data to JSON format. This serialized JSON data can be used for socket, MQTT and REST communication when transmitting data from client to server. In real-time situations, JSON messages might be too large to transmit from embedded devices to a server. 

[Protocol buffers](https://protobuf.dev/) are language-neutral and platform-neutral mechanisms for serializing structured data to lightweight buffers. Instead of encoding the structure of your data within the JSON data that is being serialized, a structured *message type* is defined beforehand that can be used by the serializer and deserializer. This structured *message type* will ensure that the data adheres to a specific structure while also prevent the need to encode this structure within the serialized data, lowering the size of the serialized data drastically.

This OpenHPS module includes the serialization and deserialization engine for Protocol Buffer Messages to OpenHPS classes and object instances. In addition, this module provides a CLI tool for generating protocol message types of all OpenHPS modules installed in your project. Combined, these two features ensure that you do not have to manually create message types and do not manually have to create a custom deserialization strategy for each class you might need to serialize.

## Features

- Automatic protocol buffer message type generator
- Serialization and deserialization of all serializable OpenHPS classes

## Known Limitations/Workarounds

- JavaScript only uses `Number` as a primitive. Protocol buffers use integers, floats, ... OpenHPS includes the `NumberType` enum option for `SerializableMember` decorators. When no number type is specified a number will be serialized to a string to ensure compatibility.
- Polymorphism and inheritance is not possible in protocol buffer v3. In order to ensure maintainable messages, any serializable object with known types will be treated as a `google.protobuf.Any` type with an included `type_url` that can be used for deserialization. 
- Objects that use generic types will also make use of `google.protobuf.Any`. However, generic types that include primitives will be serialized using a primitive wrapper. 

## Usage

### CLI Generator
This module can generate the protocol files (*.proto) automatically. 

1. Install the module using `npm install @openhps/protobuf@latest`
2. Open the root directory of your project containing your `package.json` file
3. Execute the CLI command using `openhps-protobuf`

#### Parameters

`-d <directory>`        Output directory\
`-v`                    Verbose logging

#### Output Data
The output directory will include a subdirectory for each OpenHPS package.

### Serialization and Deserialization
First the protocol buffer serializer has to be initialized with the messages.
```typescript
import { ProtobufSerializer } from '@openhps/protobuf';

// Initialize the protocol buffer serializer with the protocol files
ProtobufSerializer.initialize("/home/openhps/protobuf/");

// Or

// Initialize the protocol buffer serialization by creating protocol messages in the ./tmp directory
ProtobufSerializer.initialize();
```

An example protocol buffer message type can be seen below for a `DataObject`. The absolute and relative positions are set to an `Any` type.

```proto
package openhps.core;
syntax = "proto3";
import "google/protobuf/any.proto";
message DataObject {
	string displayName = 1;
	int64 createdTimestamp = 2;
	string uid = 3;
	string parentUID = 4;
	google.protobuf.Any position = 5;
	repeated google.protobuf.Any relativePositions = 6;
}
```
```proto
package openhps.core;
syntax = "proto3";
import "Velocity.proto";
import "Orientation.proto";
import "google/protobuf/any.proto";
message Absolute3DPosition {
	string timestamp = 1;
	Velocity velocity = 2;
	Orientation orientation = 3;
	google.protobuf.Any unit = 4;
	string referenceSpaceUID = 5;
	google.protobuf.Any accuracy = 6;
	string probability = 7;
	double x = 8;
	double y = 9;
	double z = 10;
}

```

### @openhps/socket
```ts
ModelBuilder.create()
	.addService(new SocketServer({
		srv: server,
		path: "/api/v1"
	}))
	.from(new SocketServerSource({
		uid: "source",
		// Override serializer and deserializer with protocol buffer
		serialize: (obj) => ProtobufSerializer.serialize(obj),
		deserialize: (obj) => ProtobufSerializer.deserialize(obj)
	}))
	.to()
	.build();
```

### @openhps/mqtt
```ts
ModelBuilder.create()
	.addService(new MQTTServer({
		port: 1443,
	}))
	.from(new MQTTSourceNode({
		uid: "source",
		// Override frame serializer (not the options)
		serialize: (obj, options) => ({
			frame: ProtobufSerializer.serialize(obj),
			options
		}),
		deserialize: (obj) => ProtobufSerializer.deserialize(obj.frame)
	}))
	.to()
	.build();
```

## Getting Started
If you have [npm installed](https://www.npmjs.com/get-npm), start using @openhps/protobuf with the following command.
```bash
npm install @openhps/protobuf --save
```

## Contributors
The framework is open source and is mainly developed by PhD Student Maxim Van de Wynckel as part of his research towards *Hybrid Positioning and Implicit Human-Computer Interaction* under the supervision of Prof. Dr. Beat Signer.

## Contributing
Use of OpenHPS, contributions and feedback is highly appreciated. Please read our [contributing guidelines](CONTRIBUTING.md) for more information.

## License
Copyright (C) 2019-2024 Maxim Van de Wynckel & Vrije Universiteit Brussel

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.