Skip to content

Commit

Permalink
Merge pull request #1 from javierbrea/release-1.0.0
Browse files Browse the repository at this point in the history
Release 1.0.0
  • Loading branch information
javierbrea authored Dec 15, 2018
2 parents f4a741b + 6f2e8e8 commit 257e631
Show file tree
Hide file tree
Showing 21 changed files with 700 additions and 16 deletions.
93 changes: 93 additions & 0 deletions .narval.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
schemas:
bind: &bind
- lib
- test
- server.js
- bin
coverage: &disable-coverage
enabled: false
envs:
local-service: &local-service-env
service_port: 3000
service_host_name: localhost
domapic_path: .test
service_extra_options: --gpio=18 --debounce=3000
docker-service: &docker-service-env
service_port: 3000
service_host_name: module-container
domapic_path: .shared
service_extra_options: --gpio=18 --debounce=3000
clean: &clean
local:
command: test/functional/commands/local-clean.sh
docker:
container: service-container
command: test/functional/commands/clean.sh
down-volumes: true
services:
local: &local-service
command: test/functional/commands/start-module.sh
env: *local-service-env
docker: &docker-service
container: module-container
command: test/functional/commands/start-module.sh
env: *docker-service-env
local-cli: &local-service-cli
<<: *local-service
command: test/functional/commands/start-module-cli.sh
docker-cli: &docker-service-cli
<<: *docker-service
command: test/functional/commands/start-module-cli.sh
test: &functional-test
local:
wait-on: tcp:localhost:3000
env: *local-service-env
docker:
container: test-container
wait-on: tcp:module-container:3000
env: *docker-service-env
docker-images:
- name: node-image
from: node:8.11.1
expose:
- 3000
add:
- package.json
- npm-shrinkwrap.json
install: test/functional/commands/install.sh
docker-containers:
- name: module-container
build: node-image
bind: *bind
- name: test-container
build: node-image
bind: *bind
suites:
unit:
- name: unit
Expand All @@ -6,3 +70,32 @@ suites:
coverage:
config:
dir: .coverage
functional:
- name: api-and-config
describe: should init the contact sensor with provided configuration, and api should work as expected
before: *clean
services:
- name: module
abort-on-error: true
local: *local-service
docker: *docker-service
test:
<<: *functional-test
specs:
- test/functional/specs/api.specs.js
- test/functional/specs/config.specs.js
coverage: *disable-coverage
- name: api-and-config-cli
describe: should init the contact sensor with provided configuration, and api should work as expected when started using cli
before: *clean
services:
- name: module
abort-on-error: true
local: *local-service-cli
docker: *docker-service-cli
test:
<<: *functional-test
specs:
- test/functional/specs/api.specs.js
- test/functional/specs/config.specs.js
coverage: *disable-coverage
124 changes: 122 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,123 @@
# contact-sensor-domapic-module
# Contact Sensor Domapic Module

> Domapic module for handling a contact sensor
[![Build status][travisci-image]][travisci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Quality Gate][quality-gate-image]][quality-gate-url] [![js-standard-style][standard-image]][standard-url]

[![NPM dependencies][npm-dependencies-image]][npm-dependencies-url] [![Last commit][last-commit-image]][last-commit-url] [![Last release][release-image]][release-url]

[![NPM downloads][npm-downloads-image]][npm-downloads-url] [![License][license-image]][license-url]

---

## Intro

This package starts a Domapic Module with an ability called "contactSensor" that handles a GPIO in "in" mode. It is intended to be used in a Raspberry Pi or any other system supporting the [onoff][onoff-url] library, such as C.H.I.P. or BeagleBone.

It can be used alone, but also can be connected to a [Domapic Controller][domapic-controller-url] to get the most out of it.

![Contact sensor connection schema][contact-sensor-schema-image]

## Installation

```bash
npm i contact-sensor-domapic-module -g
```

## Start the server

```bash
domapic-contact-sensor start --gpio=12 --debounce=3000 --save
```

The server will be started in background using [pm2][pm2-url].

To display logs, type:

```bash
domapic-contact-sensor logs #--lines=300
```

## Options

The module, apart of all common [domapic services options][domapic-service-options-url], provides custom options for configuring the sensor:

* `gpio` - Number defining the Gpio where the contact sensor to be controlled is connected.
* `debounce` - Time in miliseconds to wait for before notifying about a change in the status of the contact sensor.

## Connection with Domapic Controller

Connect the module with a Domapic Controller providing the Controller url and connection token (you'll find it the Controller logs when it is started):

```bash
domapic-contact-sensor start --controller=http://192.168.1.110:3000 --controllerApiKey=fo--controller-api-key
```

Now, the module can be controlled through the Controller interface, or installed plugins.

## Stand alone usage

Domapic modules are intended to be used through Domapic Controller, but can be used as an stand-alone service as well. Follow next instructions to use the built-in api by your own:

### Rest API

When the server is started, you can browse to the provided Swagger interface to get all the info about the api resources. Apart of all api methods common to all [Domapic Services][domapic-service-url], the server provides one [_Domapic Ability_][domapic-service-abilities-url] for getting the state of the sendor, which generates one extra API resource:

* `/api/abilities/contact-sensor/state` - Returns the current state of the sensor.

### Authentication

The server includes the [Domapic Services][domapic-service-url] authentication method, which is disabled by default for `127.0.0.1`.
You can disable the authentication using the `--authDisabled` option (not recommended if your server is being exposed to the Internet). Read more about [available options in the domapic services documentation][domapic-service-options-url].

If you want to authenticate when requesting from another IPs, look for the api key automatically generated and intended to be used by Domapic Controller when the server is started. You'll find it in the server logs:

```
-----------------------------------------------------------------
Try adding connection from Controller, using the next service Api Key: HMl6GHWr7foowxM40CB6tQPuXt3zc7zE
-----------------------------------------------------------------
```

To make your own requests to the api, provide this token using the `X-Api-Key` header.

Use the mentioned api key also for authenticating when using the Swagger interface.

## Alternative command line methods

### Not global installation

If the package is not installed globally, you can replace the `domapic-contact-sensor` command in examples above by `npm run domapic-contact-sensor --` (commands must be executed inside the package folder in that case)

### Not background mode

If you don't want to use the built-in background runner, you can start the server directly, attaching logs to current `stdout`. Move to the package folder and replace the `domapic-contact-sensor` command of examples above by `node server.js`. Press `CTRL+C` to stop the server.


[coveralls-image]: https://coveralls.io/repos/github/javierbrea/contact-sensor-domapic-module/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/javierbrea/contact-sensor-domapic-module
[travisci-image]: https://travis-ci.com/javierbrea/contact-sensor-domapic-module.svg?branch=master
[travisci-url]: https://travis-ci.com/javierbrea/contact-sensor-domapic-module
[last-commit-image]: https://img.shields.io/github/last-commit/javierbrea/contact-sensor-domapic-module.svg
[last-commit-url]: https://github.com/javierbrea/contact-sensor-domapic-module/commits
[license-image]: https://img.shields.io/npm/l/contact-sensor-domapic-module.svg
[license-url]: https://github.com/javierbrea/contact-sensor-domapic-module/blob/master/LICENSE
[npm-downloads-image]: https://img.shields.io/npm/dm/contact-sensor-domapic-module.svg
[npm-downloads-url]: https://www.npmjs.com/package/contact-sensor-domapic-module
[npm-dependencies-image]: https://img.shields.io/david/javierbrea/contact-sensor-domapic-module.svg
[npm-dependencies-url]: https://david-dm.org/javierbrea/contact-sensor-domapic-module
[quality-gate-image]: https://sonarcloud.io/api/project_badges/measure?project=contact-sensor-domapic-module&metric=alert_status
[quality-gate-url]: https://sonarcloud.io/dashboard?id=contact-sensor-domapic-module
[release-image]: https://img.shields.io/github/release-date/javierbrea/contact-sensor-domapic-module.svg
[release-url]: https://github.com/javierbrea/contact-sensor-domapic-module/releases
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
[standard-url]: http://standardjs.com/

[onoff-url]: https://www.npmjs.com/package/onoff
[domapic-controller-url]: https://www.npmjs.com/package/domapic-controller
[domapic-service-options-url]: https://github.com/domapic/domapic-service#options
[domapic-service-abilities-url]: https://github.com/domapic/domapic-service#abilities
[domapic-service-url]: https://github.com/domapic/domapic-service
[pm2-url]: http://pm2.keymetrics.io/

[contact-sensor-schema-image]: http://domapic.com/assets/contact-sensor/fritz_schema.png

Domapic module for handling a contact sensor
12 changes: 12 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const path = require('path')
const domapic = require('domapic-service')

const options = require('./options')

domapic.cli({
packagePath: path.resolve(__dirname, '..'),
script: path.resolve(__dirname, '..', 'server.js'),
customConfig: options
})
15 changes: 15 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const { GPIO_OPTION, DEBOUNCE_OPTION } = require('./statics')

module.exports = {
[GPIO_OPTION]: {
type: 'number',
describe: 'Set gpio number for the sensor'
},
[DEBOUNCE_OPTION]: {
type: 'number',
describe: 'Set debounce timeout for the sensor',
default: 1000
}
}
19 changes: 19 additions & 0 deletions lib/statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const ABILITY_NAME = 'contactSensor'
const DEBOUNCE_OPTION = 'debounce'
const GPIO_OPTION = 'gpio'

const ABILITY_DESCRIPTION = 'Contact sensor status'
const ABILITY_STATE_DESCRIPTION = 'Returns current contact sensor status'
const ABILITY_EVENT_DESCRIPTION = 'Contact sensor status has changed'

module.exports = {
ABILITY_NAME,
DEBOUNCE_OPTION,
GPIO_OPTION,

ABILITY_DESCRIPTION,
ABILITY_STATE_DESCRIPTION,
ABILITY_EVENT_DESCRIPTION
}
51 changes: 42 additions & 9 deletions package-lock.json → npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 257e631

Please sign in to comment.