-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
110 lines (94 loc) · 3.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*** RaspberryCam Z-Way HA module *******************************************
Version: 1.02
(c) Maroš Kollár, 2015-2017
-----------------------------------------------------------------------------
Author: Maroš Kollár <maros@k-1.com>
Description:
Displays stills from the local Raspberry PI camera module.
******************************************************************************/
// ----------------------------------------------------------------------------
// --- Class definition, inheritance and setup
// ----------------------------------------------------------------------------
function RaspberryCam (id, controller) {
// Call superconstructor first (AutomationModule)
RaspberryCam.super_.call(this, id, controller);
}
inherits(RaspberryCam, AutomationModule);
_module = RaspberryCam;
// ----------------------------------------------------------------------------
// --- Module RaspberryCam initialized
// ----------------------------------------------------------------------------
RaspberryCam.prototype.init = function (config) {
RaspberryCam.super_.prototype.init.call(this, config);
var self = this;
var vDevId = "CameraDevice_" + this.id;
// Setup global http callback
RaspberryCamHandler = function() {
// TODO create tmp/
// Aspect ration 1 1/3
var options = {
width: 500,
height: 375,
quality: 90,
//encoding: 'jpeg',
output: '/opt/z-way-server/automation/tmp/current.jpg'
};
if (config.flip === true) {
options.hflip = true;
options.vflip = true;
}
// TODO --exposure night for late hours
var command = '/usr/bin/raspistill';
command = command + ' ' + _.map(options, function(value,key) {
return '--' + key + (typeof(value) === 'boolean' ? '':' '+value);
}).join(' ');
console.log('[RaspberryCam] Run command '+command);
system(command);
var image = fs.load('tmp/current.jpg');
if (typeof image !== 'string') {
console.error('[RaspberryCam] Could not find image');
image = fs.load('modules/RaspberryCam/noimage.jpg');
return {
status: 404,
headers: {
'Content-Type': 'image/png'
},
body: image
};
}
return {
status: 200,
headers: {
'Content-Type': 'image/jpeg',
'cache-control': 'public, max-age=0, no-cache'
},
body: image
};
};
ws.allowExternalAccess('RaspberryCamHandler',self.controller.auth.ROLE.LOCAL);
self.vDev = this.controller.devices.create({
deviceId: vDevId,
defaults: {
deviceType: "camera",
metrics: {
icon: 'camera',
title: 'RaspberryCam ' + this.id
}
},
overlay: {
metrics: {
autoRefresh: true,
url: "/RaspberryCamHandler",
}
},
moduleId: this.id
});
};
RaspberryCam.prototype.stop = function () {
RaspberryCam.super_.prototype.stop.call(this);
var self = this;
if (self.vDev) {
self.controller.devices.remove(self.vDev.id);
self.vDev = undefined;
}
};