Skip to content

Commit

Permalink
Creates MotionApi class and related README
Browse files Browse the repository at this point in the history
  • Loading branch information
baudev committed Mar 19, 2020
1 parent 34a789e commit 3491b13
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 9 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Motion API

This library aims to expose a simplistic API for the https://motion-project.github.io/ project.

# Installation

```
npm i motion-project-api
```

# Usage

Example creating a snapshot with the camera 1.

```typescript
let motionApi = new MotionApi("http://localhost:7999"); // URL of the motion web server
motionApi.getCamera(1).createSnapshot(); // creates a snapshot for the camera 1
```

## API

### Class MotionApi

**MotionApi(apiUrl)**

+ apiUrl - (_string_) The motion web server url.

**getCamera(idCamera): Camera**

+ idCamera - (_number_) The camera number to control.

Returns an instance of `Camera`.

### Class Camera

**Camera(id)**

+ id - (_number_) The camera number to control.

**startMotionDetection(): Promise<void>**

Start or resume motion detection.

**pauseMotionDetection(): Promise<void>**

Pause the motion detection.

**createSnapshot(): Promise<void>**

Create a snapshot

**getCurrentStatus(): Promise<MotionDetectionStatus>**

Return the current status of the camera. `MotionDetectionStatus.ENABLE` is enable or `MotionDetectionStatus.DISABLE` if disabled.

**getConnectionStatus(): Promise<MotionConnectionStatus>**

Return the connection status of the camera. `MotionConnectionStatus.OK` if well connected otherwise `MotionConnectionStatus.DISCONNECTED`.
5 changes: 5 additions & 0 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
"name": "motion-project-api",
"version": "1.0.0",
"description": "API for https://motion-project.github.io/",
"main": "build/index.js",
"types": "build/index.d.ts",
"main": "build/MotionApi.js",
"types": "build/MotionApi.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rm -rf build/ && tsc",
"install": "npm run build",
"start": "npm run build && node build/index.js"
"install": "npm run build"
},
"repository": {
"type": "git",
Expand All @@ -27,6 +26,7 @@
"homepage": "https://github.com/baudev/motion-project-api#readme",
"dependencies": {
"@types/node": "^13.9.2",
"lodash": "^4.17.15",
"node-fetch": "^2.6.0",
"typescript": "^3.8.3"
}
Expand Down
2 changes: 1 addition & 1 deletion src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Camera {
/**
* Return the current status of the camera.
*/
public getCurrentStatus() {
public getCurrentStatus(): Promise<MotionDetectionStatus> {
return WebControl.getDetectionStatus(this.id)
.then((message) => {
return new RegExp(/Detection status ACTIVE/).test(message) ? MotionDetectionStatus.ENABLE : MotionDetectionStatus.DISABLE;
Expand Down
34 changes: 34 additions & 0 deletions src/MotionApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {WebControl} from "./WebControl";
import {Camera} from "./Camera";

const _ = require('lodash');

export class MotionApi {

private cameras: Array<Camera> = [];

/**
* Creates MotionApi object
* @param apiUrl The base url of the HTTP motion server
*/
constructor(apiUrl: string) {
WebControl.url = apiUrl;
}

/**
* Returns an unique instance of the camera
* @param idCamera
*/
public getCamera(idCamera: number): Camera{
let camera = _.find(this.cameras, function (camera: Camera) {
return camera.id === idCamera;
});
if (camera !== undefined) {
return camera;
} else {
let newCamera = new Camera(idCamera);
this.cameras.push(newCamera);
return newCamera;
}
}
}
14 changes: 10 additions & 4 deletions src/WebControl.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {throws} from "assert";

const fetch = require("node-fetch");

export class WebControl {

public static url = "http://192.168.1.68:7999";
private static _url: string;

private static request(route: string){
return fetch(WebControl.url + "/" + route)
return fetch(WebControl._url + "/" + route)
.then((res: Response) => res.text());
}

Expand Down Expand Up @@ -51,4 +49,12 @@ export class WebControl {
return WebControl.request(cameraId + "/action/snapshot");
}

/**
* Setter for server URL
* @param value
*/
static set url(value: string) {
this._url = value;
}

}

0 comments on commit 3491b13

Please sign in to comment.