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

abstract-things

v0.9.0

Published

Base for building libraries that interact with physical things, such as IoT-devices

Downloads

9,689

Readme

abstract-things

abstract-things is a JavaScript library that provides a simple base for building libraries that interact with physical things, such as IoT-devices, and virtual things.

This library provides a base class named Thing that supports mixins of various types. Things are described using two types of tags, one describing the type of the thing and one describing its capabilities. Things are also expected to describe their public API, to make remote use easier.

Types and capabilities are designed to be stable and to be combined. When combined they describe a thing and what it can do.

Detailed documentation is available at: http://abstract-things.readthedocs.io/en/latest/

Building a simple thing

npm install abstract-things

A basic thing might look a bit like this:

const { Thing } = require('abstract-things');

class SimpleThing extends Thing {
  // Define the type of the thing
  static get type() {
    return 'simple-thing';
  }

  // Quick way to define actions available for this thing
  static get availableAPI() {
    return [ 'hello' ];
  }

  constructor() {
    super();

    // An identifier of the thing should always be set - with a namespace
    this.id = 'thing:example-1';
  }

  hello() {
    return 'Cookies are tasty';
  }
}

console.log(new SimpleThing());

Identifiers and namespaces

Identifiers are required when bulding a thing. They should uniquelly identify the thing and if possible remain stable over time. In addition that that a thing should also belong to a namespace. Namespaces are used to make ids unique when several thing-libraries are used. Namespaces should be short and related to the type of thing, such as hue for Philips Hue lights or bluetooth for Bluetooth peripherals.

Types and capabilities

Types are used to describe what a thing is and capabilities describe what a thing can do. A thing can have many capabilities but usually its only a few types.

This library includes a set of capabilities and types that are intended to cover many common appliances and devices. Information about these can be found in the detailed documentation.

Using a capability

Pre-defined capabilities can simply be mixed in when creating a thing:

const { Thing, State } = require('abstract-things');

class MyThing extends Thing.with(State) {
  constructor() {
    super();

    this.updateState('key', 'value');
  }
}

Defining a capability

To create a reusable capability you can create a mixin using the Thing.capability function:

const { Thing } = require('abstract-things');

module.exports = Thing.capability(BaseThing => class extends BaseThing {
  
  // Tell clients about this capability
  static get capability() {
    return 'cookie-monster';
  }

  // Tell clients about our API
  static availableAPI(builder) {
    builder.action('nom')
      .description('Eat a cookie')
      .argument('string', false, 'The type of cookie to eat')
      .done();
  }
  
  constructor(...args) {
    super(...args);
  }

  nom(cookieType) {
    return 'Ate cookie of type ' + cookieType;
  }
});