Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from camphor-/develop
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
ymyzk committed Feb 18, 2016
2 parents fcf8c41 + 1f0faf2 commit f1d9cfc
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["es2015"]
"presets": ["es2015-node5"]
}
4 changes: 2 additions & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ rules:
- always
no-unused-vars:
- 1
ecmaFeatures:
modules: true
parserOptions:
sourceType: module
env:
es6: true
node: true
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
# now-client-camera

[![Build Status](https://travis-ci.org/camphor-/now-client-camera.svg?branch=master)](https://travis-ci.org/camphor-/now-client-camera)
[![Dependency Status](https://david-dm.org/camphor-/now-client-camera.svg)](https://david-dm.org/camphor-/now-client-camera)
[![devDependency Status](https://david-dm.org/camphor-/now-client-camera/dev-status.svg)](https://david-dm.org/camphor-/now-client-camera#info=devDependencies)

## Run
Examples:
- `npm start -- --mode=raspistill http://192.168.1.128:3000`
- `DEBUG=now-client-camera npm start -- --mode=sample http://192.168.1.128:3000`
- `npm start -- --mode=sample --authorization='Basic <token>' http://192.168.1.128:3000`
- `npm start -- --driver=raspistill http://192.168.1.128:3000`
- `DEBUG=now-client-camera npm start -- --driver=sample http://192.168.1.128:3000`
- `npm start -- --driver=sample --authorization='Basic <token>' http://192.168.1.128:3000`

### Drivers
Specify a driver with `--driver=` option. (default: `sample`)

#### raspistill
Raspberry Pi Camera Module

Environment variables:
- `RASPISTILL_WIDTH` - Width of a picture (default: 320)
- `RASPISTILL_HEIGHT` - Height of a picture (default: 240)

#### sample
Sample JPEG image
87 changes: 34 additions & 53 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import {argv} from 'yargs';
import debugLogger from 'debug';
import {execSync} from 'child_process';
import {readFile} from 'fs';
import io from 'socket.io-client';

import raspistill from './lib/drivers/raspistill';
import sampleFile from './lib/drivers/sample';

// Constants
const NAMESPACE = '/camera';

// Logger
const debug = debugLogger('now-client-camera');

// Parse command line arguments
let parseMode = (mode) => {
const _mode = (mode || '').toLowerCase();
switch (_mode) {
let parseDriver = (driver) => {
const _driver = (driver || '').toLowerCase();
switch (_driver) {
case 'raspistill':
return _mode;
return _driver;
case 'sample':
return _mode;
return _driver;
default:
return 'sample';
}
};

const url = argv._[0] || 'http://localhost:3000';
const mode = parseMode(argv.mode);
const driver = parseDriver(argv.driver);
const authorization = argv.authorization;

// Start
debug(`Server URL: ${url}`);
debug(`Namespace: ${NAMESPACE}`);
debug(`Mode: ${mode}`);
debug(`Driver: ${driver}`);

const extraHeaders = {};
if (authorization) {
Expand All @@ -49,52 +50,32 @@ socket.on('disconnect', () => {
debug('disconnected');
});

let raspistill = (responseEvent) => {
try {
const picture = execSync('raspistill -w 320 -h 240 -n -e jpg -o -');
debug('success');
socket.emit(responseEvent, {
success: true,
data: picture
});
} catch (e) {
const message = e.toString();
debug(`error: ${message}`);
socket.emit(responseEvent, {
success: false,
message: message
});
}
};

let sampleFile = (responseEvent) => {
readFile('./test.jpg', (err, data) => {
if (err) {
socket.on('take picture', (data) => {
debug('take picture');
const responseEvent = data.responseEvent;
(() => {
switch (driver) {
case 'sample':
return sampleFile;
case 'raspistill':
return raspistill;
default:
return sampleFile;
}
})()().then(
(pictureData) => {
debug('success');
socket.emit(responseEvent, {
success: true,
data: pictureData
});
},
(errorMessage) => {
debug(`error: ${errorMessage}`);
socket.emit(responseEvent, {
success: false,
message: err.toString()
message: errorMessage
});
return;
}
socket.emit(responseEvent, {
success: true,
data: data
});
});
};

socket.on('take picture', (data) => {
debug('take picture');
const responseEvent = data.responseEvent;
switch (mode) {
case 'sample':
sampleFile(responseEvent);
break;
case 'raspistill':
raspistill(responseEvent);
break;
default:
sampleFile(responseEvent);
break;
}
);
});
28 changes: 28 additions & 0 deletions lib/drivers/raspistill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {exec} from 'child_process';

const MAX_BUFFER = 1 * 1024 * 1024; // 1MiB

const parseInteger = (string, defaultValue) => {
const number = Number(string);
if (Number.isInteger(number)) {
return number;
} else {
return defaultValue;
}
};

const WIDTH = parseInteger(process.env.RASPISTILL_WIDTH, 320);
const HEIGHT = parseInteger(process.env.RASPISTILL_HEIGHT, 240);

export default () => new Promise((resolve, reject) => {
exec(`raspistill -w ${WIDTH} -h ${HEIGHT} -n -e jpg -o -`, {
encoding: 'buffer',
maxBuffer: MAX_BUFFER
}, (err, stdout, stderr) => {
if (err === null) {
resolve(stdout);
} else {
reject(err.toString());
}
});
});
11 changes: 11 additions & 0 deletions lib/drivers/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {readFile} from 'fs';

export default () => new Promise((resolve, reject) => {
readFile('./test.jpg', (err, data) => {
if (err) {
reject(err.toString());
} else {
resolve(data);
}
});
});
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "now-client-camera",
"version": "0.1.0",
"version": "0.2.0",
"private": true,
"scripts": {
"eslint": "./node_modules/eslint/bin/eslint.js .",
Expand All @@ -9,12 +9,12 @@
"dependencies": {
"babel": "~6.5.1",
"babel-cli": "~6.5.1",
"babel-preset-es2015": "~6.5.0",
"babel-preset-es2015-node5": "~1.1.2",
"debug": "~2.2.0",
"socket.io-client": "~1.4.5",
"yargs": "~3.32.0"
"yargs": "~4.1.0"
},
"devDependencies": {
"eslint": "~1.10.3"
"eslint": "~2.1.0"
}
}

0 comments on commit f1d9cfc

Please sign in to comment.