A IoT simulator for Nostr written in TypeScript and using deno.
- Copy config.json.example to config.json and modify the values as needed.
- Run the following command:
deno run --allow-net --allow-read src/main.tsThe simulator supports a plugin architecture.
To add a new plugin to the system, follow these steps:
-
Plugin Directory Structure: Each plugin should reside in its own directory inside the
pluginsfolder. The directory should contain:- The plugin's main TypeScript file (e.g.,
plugin.ts). - An optional
config.jsonfile, if the plugin requires configuration parameters like units, thresholds, etc.
Example:
/plugins /myPlugin - myPlugin.ts - config.json - The plugin's main TypeScript file (e.g.,
-
Plugin Interface: The plugin must implement a few basic methods to integrate smoothly with the system:
getCapability(): string: Returns a string describing the capability of the plugin. plugin provides.execute(params: (string | number)[]): string: Executes the functionality of the plugin based on input parameters and returns a result (as a string).
In the plugins/myPlugin/ directory, create a TypeScript file for your plugin
(e.g., myPlugin.ts). Below is an example of what your plugin should look like.
// plugins/myPlugin/myPlugin.ts
import { PluginConfig } from "../../types.ts"; // Adjust path if necessary
export default class MyPlugin {
config: PluginConfig;
constructor(config: PluginConfig) {
this.config = config;
}
getCapability(): string {
return "myCustomCapability";
}
execute(params: (string | number)[] = []): string {
// Perform some action based on params and return a result
return `MyPlugin executed with params: ${JSON.stringify(params)}`;
}
}If your plugin requires additional configuration (e.g., units, thresholds),
create a config.json file in the plugin directory. This file will be loaded
automatically by the system when the plugin is initialized.
Example config.json:
{
"unit": "exampleUnit",
"threshold": 10
}To tell the system to load your plugin, you need to update the main
configuration file (config.json) located in the root directory of the project.
Add a new entry for your plugin, specifying its name and path.
Example update to config.json:
{
"plugins": [
{ "name": "myPlugin", "path": "./plugins/myPlugin/myPlugin.ts" }
]
}name: The name of your plugin (used for identification purposes).path: The relative path to the main TypeScript file of your plugin.
When the system loads your plugin, it will call the getCapability() method.
Define the capability that your plugin provides, which will be used to call the
correct functionality.
For example, if your plugin provides a motor control capability:
getCapability(): string {
return "runMotor";
}You can then handle the logic in the execute() method for the runMotor
capability when invoked.
Once the plugin is added and properly configured, it will be dynamically loaded by the application based on the main configuration file. When the application runs, it will:
- Detect the plugin's capabilities.
- Execute the plugin's functionality based on those capabilities.
For example, if your plugin has the runMotor capability, it can be invoked by
the main application like so:
const motorParams = ["value", 20, "unit", "rpm"];
plugin.execute(motorParams);- Plugin Config (Optional): If your plugin does not require configuration,
you can omit the
config.jsonfile, and the system will simply pass an empty object as the config to your plugin. - Flexible Parameters: The
execute()method accepts an array of parameters that can be strings or numbers. Customize the behavior of your plugin based on the input you receive.
- Ensure the path in the main
config.jsonfile points correctly to the plugin's main TypeScript file. - If your plugin requires configuration, ensure the
config.jsonin the plugin directory contains valid JSON.
Run the deno test suite with:
deno test --allow-write --allow-read