npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vorto-metamorph

v0.11.0

Published

Maps arbitrary device json payload to vorto compliant json using mapping specification.

Downloads

4

Readme

Mapping arbitrary JSON to Vorto JSON Spec

Vorto allows application developers to create IoT solutions without having to customize their code to deal with the variety of json sent by different kinds of IoT devices. So a temperature reading from a toaster or an aircon will always have the same structure. Vorto achieves this by defining a semantic which abstracts device data using Function Blocks.

Sounds interesting, check out the Eclipse Vorto project here.

The package is an effort to making it easier for developers to map arbitrary device json into vorto compliant json structure.

Installation

npm install vorto-metamorph

Example Scenario

I want to create a cool mobile app which shows room temperature. The challenge is there are a wide range of connectable temperature sensors available in the market and my app should be able to work with most of them. A perfect scenario for using Vorto.

To test my setup I bought a connectable temperature sensor, unfortunately it only reads in Fahrenheit. I am not going to change my application code to adjust to this json format, I will just map it to the desired json format.

Device JSON:

{
  "temperature" : "82.4f"
}

Desired Vorto compliant JSON structure:

{
  "temperature": {
    "status": {
      "sensorValue": "28"
    }
  }
}

Prerequisite

To work through this tutorial, you will need:

Step 1: Create Mapping Specification

Mapping adds device specific information to an Information Model. Since the representation of data can vary from one device to another, we will create a mapping specification to standardize the data.

To create a mapping go to your newly created model and press the Create Mapping Spec Button.

create mapping spec button

Now add a Mapping key to uniquely identify your mapping to the device it belongs to, like acme_temperature_monitor (connected temperature sensor from Acme Corporation).

platform key

Click Create and the web editor opens allowing you to add mapping expression for the Function Blocks you added. You can write XPath 3.1 like notation. Behind the scenes the engine uses fotonxpath to apply XPath expressions on the DOM created using the device json. To add functionality that may not be possible using xpath alone, you can also use/create custom JavaScript functions (courtesy fotonxpath) . Once you have written your xpath expressions, press Save.

xpath

Custom function to convert Fahrenheit to Celsius and round-off to two decimal places.

xpath

Step 2: Test the Mapping Specification

On the right hand-side, define the device payload(in JSON format) and click Map:

mapping editor test

Satisfied with the results, save it.

Step 3: Download & Execute Mapping Specification

Download it to use with our nodejs library:

download json spec

Usage

const VortoMapper = require("vorto-metamorph")

const vortoMapper = new VortoMapper();

const rawPayload = {
    "temperature": "82.4f"
};

const mappingSpec = {}; // copy the downloaded mapping spec

vortoMapper.setMappingSpec(mappingSpec);
vortoMapper.transform(rawPayload);

Registering Custom Functions

To register user defined functions for the mapping, we will leverage on the functionality provided by fontoxpath. Note the additional parameter 'dynamicContext' which needs to be added as the first parameter of the function.

vortoMapper.registerCustomXPathFunction(
    'temperature:fToC',
    ['xs:string'], 'xs:string',
    function fToC(dynamicContext, fahrenheit) 
    {
          var fTemp = fahrenheit.substr(0, fahrenheit.length -1);
          var cel = (fTemp - 32) * 5 / 9;
          return Math.round(cel * 100) /100;
    }
);

Debug Logs

The library internally uses loglevel npm package. To enable debug level logs set the corresponding log level.

vortoMapper.setLogLevel("debug");