├── LICENSE
├── README.md
├── index.js
└── package.json
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # homebridge-foscamcamera [](https://badge.fury.io/js/homebridge-foscamcamera)
2 | Foscam Plugin (Camera, Security System, Motion Sensor) for [HomeBridge](https://github.com/nfarina/homebridge) (API 2.1)
3 |
4 | Older verion using API 1.0: [homebridge-foscam](https://github.com/rooi/homebridge-foscam)
5 | Older verion using API 2.0: [homebridge-foscam2](https://github.com/luisiam/homebridge-foscam2) (deprecated)
6 |
7 | **Due to protocol limitation, users will need to pair with the camera in a HomeKit app separately.
**
8 | **Pairing PIN is the same as the HomeBridge pairing PIN.**
9 |
10 | # Prerequisites
11 | 1. Node.js **v6.6.0** or above
12 | 2. HomeBridge **v0.4.6** or above
13 | 3. FFmpeg
14 | 4. Only H.264 cameras are supported.
15 |
16 | # Installation
17 | 1. Install homebridge using `npm install -g homebridge`.
18 | 2. Install this plugin using `npm install -g homebridge-foscamcamera`.
19 | 3. Update your configuration file. See configuration sample below.
20 |
21 | # Configuration
22 | Edit your `config.json` accordingly. Configuration sample:
23 | ```
24 | "platforms": [{
25 | "platform": "FoscamCamera",
26 | "name": "Foscam",
27 | "cameras": [{
28 | "username": "admin",
29 | "password": "password",
30 | "host": "192.168.1.10",
31 | "port": 88,
32 | "stay": 13,
33 | "away": 15,
34 | "night": 14,
35 | "armPreset": "Start",
36 | "disarmPreset": "LeftMost",
37 | "sensitivity": 2,
38 | "triggerInterval": 5,
39 | "videoConfig": {
40 | "source": "-re -i rtsp://myfancy_rtsp_stream",
41 | "stillImageSource": "-i http://faster_still_image_grab_url/this_is_optional.jpg",
42 | "maxStreams": 2,
43 | "maxWidth": 1280,
44 | "maxHeight": 720,
45 | "maxFPS": 30
46 | }
47 | }]
48 | }]
49 |
50 | ```
51 |
52 | | Fields | Description | Default | Required |
53 | |----------------------|---------------------------------------------------------------|---------------|----------|
54 | | platform | Must always be `FoscamCamera`. | | Yes |
55 | | name | For logging purposes. | | No |
56 | | cameras | Array of camera config (multiple cameras supported). | | Yes |
57 | | \|- username | Your camera login username. | admin | No |
58 | | \|- password | Your camera login password. | | Yes |
59 | | \|- host | Your camera IP address. | | Yes |
60 | | \|- port | Your camera port. | 88 | No |
61 | | \|- stay\* | Configuration for Stay Arm. | 0 | No |
62 | | \|- away\* | Configuration for Away Arm. | 0 | No |
63 | | \|- night\* | Configuration for Night Arm. | 0 | No |
64 | | \|- armPreset | Preset point name for arm state. | `undefined` | No |
65 | | \|- disarmPreset | Preset point name for disarm state. | `undefined` | No |
66 | | \|- sensitivity | Motion sensor sensitivity from 0 (lowest) to 4 (high). | Camera Config | No |
67 | | \|- triggerInterval | Time in `s` (5-15) of which motion sensor can be retriggered. | Camera Config | No |
68 | | \|- videoConfig\** | Array of video config for streaming. | | Yes |
69 |
70 | \*`stay`, `away`, `night` define configuration for different ARMED state.
71 | \*\*reference [homebridge-camera-ffmpeg](https://github.com/KhaosT/homebridge-camera-ffmpeg) for configuration instructions.
72 |
73 | The supported configurations depend on your device. The Foscam public CGI defines the following:
74 | bit 3 | bit 2 | bit 1 | bit 0
75 | bit 0 = Ring
76 | bit 1 = Send email
77 | bit 2 = Snap picture
78 | bit 3 = Record
79 |
80 | The following seems to be valid for the C2 as well (not found in any documentation)
81 | bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0
82 | bit 0 = Ring
83 | bit 1 = Send email
84 | bit 2 = Snap picture
85 | bit 3 = Record
86 | bit 7 = Push notification
87 |
88 | Note: The configuration is defined as int, thus the followings are valid, e.g. 0 (Do Nothing), 1 (Ring), 2 (Email), 3 (Ring + Email), 4 (Picture), 12 (Picture and Record), 13 (Ring, Picture and Record), etc.
89 |
90 | P.S.: Any ARMED state will activate motion detection by default.
91 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | var FFMPEG = require("homebridge-camera-ffmpeg/ffmpeg").FFMPEG;
2 | var Foscam = require("foscam-client");
3 | var Accessory, Service, Characteristic, UUIDGen, hap;
4 |
5 | module.exports = function (homebridge) {
6 | Accessory = homebridge.platformAccessory;
7 | Service = homebridge.hap.Service;
8 | Characteristic = homebridge.hap.Characteristic;
9 | UUIDGen = homebridge.hap.uuid;
10 | hap = homebridge.hap;
11 |
12 | homebridge.registerPlatform("homebridge-foscamcamera", "FoscamCamera", FoscamPlatform, true);
13 | }
14 |
15 | function FoscamPlatform(log, config, api) {
16 | this.log = log;
17 | this.config = config || {"platform": "FoscamCamera"};
18 | this.cameras = this.config.cameras || [];
19 |
20 | // HomeKit Current State: 0 (STAY_ARM), 1 (AWAY_ARM), 2 (NIGHT_ARM), 3 (DISARMED), 4 (ALARM_TRIGGERED)
21 | this.armState = ["armed (stay).", "armed (away).", "armed (night).", "disarmed.", "alarm triggered."];
22 |
23 | // Camera motion sensor sensitivity
24 | this.sensitivity = [4, 3, 0, 1, 2];
25 |
26 | // Global cache
27 | this.accessories = {};
28 | this.foscamAPI = {};
29 | this.cameraInfo = {};
30 |
31 | if (api) {
32 | this.api = api;
33 | if (api.version < 2.1) throw new Error("Unexpected API version.");
34 | this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
35 | }
36 | }
37 |
38 | FoscamPlatform.prototype.configureAccessory = function (accessory) {
39 | // Won't be invoked
40 | }
41 |
42 | // Method to setup accessories from config.json
43 | FoscamPlatform.prototype.didFinishLaunching = function () {
44 | var self = this;
45 |
46 | if (this.cameras) {
47 | this.cameras.forEach(function (cameraConfig) {
48 | if (cameraConfig.password && cameraConfig.host) {
49 | self.getInfo(cameraConfig, function (mac, error) {
50 | if (!error) {
51 | self.configureCamera(mac);
52 | } else {
53 | self.log(error);
54 | }
55 | });
56 | } else {
57 | self.log("Missing Required Information!");
58 | }
59 | });
60 | }
61 | }
62 |
63 | // Method to detect Foscam camera info and API version
64 | FoscamPlatform.prototype.getInfo = function (cameraConfig, callback) {
65 | var self = this;
66 |
67 | // Setup for foscam-client
68 | var thisFoscamAPI = new Foscam({
69 | username: cameraConfig.username,
70 | password: cameraConfig.password,
71 | host: cameraConfig.host,
72 | port: cameraConfig.port,
73 | protocol: cameraConfig.protocol || 'http',
74 | rejectUnauthorizedCerts: true
75 | });
76 |
77 | // Retrieve camera info
78 | Promise.all([thisFoscamAPI.getDevInfo(), thisFoscamAPI.getMotionDetectConfig(), thisFoscamAPI.getMotionDetectConfig1()]).then(function (output) {
79 | var info = output[0];
80 |
81 | if (info.result === 0) {
82 | // Create a copy of config
83 | var thisCamera = JSON.parse(JSON.stringify(cameraConfig));
84 | var config, linkageMask;
85 |
86 | if (output[1].result === 0) {
87 | // Older API
88 | config = output[1];
89 | linkageMask = 0x0f;
90 | thisCamera.version = 0;
91 | } else if (output[2].result === 0) {
92 | // Newer API
93 | config = output[2];
94 | linkageMask = 0xff;
95 | thisCamera.version = 1;
96 | }
97 |
98 | // Initialize default config
99 | thisCamera.username = cameraConfig.username || "admin";
100 | thisCamera.port = cameraConfig.port || 88;
101 | thisCamera.linkage = [cameraConfig.stay || 0, cameraConfig.away || 0, cameraConfig.night || 0];
102 | thisCamera.linkage = thisCamera.linkage.map(function (k) {return (k & linkageMask)});
103 |
104 | // Compute sensivity
105 | if (thisCamera.sensitivity < 0 || thisCamera.sensitivity > 4) {
106 | throw new Error("Sensitivity " + thisCamera.sensitivity + " is out of range.");
107 | } else if (thisCamera.sensitivity === undefined) {
108 | thisCamera.sensitivity = self.sensitivity.indexOf(config.sensitivity);
109 | }
110 |
111 | // Compute triggerInterval
112 | if (thisCamera.triggerInterval < 5 || thisCamera.triggerInterval > 15) {
113 | throw new Error("Trigger interval " + thisCamera.triggerInterval + " is out of range.");
114 | } else if (thisCamera.triggerInterval === undefined) {
115 | thisCamera.triggerInterval = config.triggerInterval + 5;
116 | }
117 |
118 | // Remove unnecessary config
119 | delete thisCamera.stay;
120 | delete thisCamera.away;
121 | delete thisCamera.night;
122 |
123 | // Store camera information
124 | thisCamera.name = info.devName.toString();
125 | thisCamera.model = info.productName.toString();
126 | thisCamera.serial = info.serialNo.toString();
127 | thisCamera.fw = info.firmwareVer.toString();
128 | thisCamera.hw = info.hardwareVer.toString();
129 |
130 | // Initialize global cache
131 | thisCamera.currentState = Characteristic.SecuritySystemCurrentState.DISARMED;
132 | thisCamera.motionAlarm = false;
133 | thisCamera.statusActive = 0;
134 |
135 | // Workaround for empty serial number
136 | if (thisCamera.serial === "") thisCamera.serial = "Default-SerialNumber";
137 |
138 | // Store information to global
139 | self.foscamAPI[info.mac] = thisFoscamAPI;
140 | self.cameraInfo[info.mac] = thisCamera;
141 | callback(info.mac);
142 | } else {
143 | callback(null, "Failed to retrieve camera information!");
144 | }
145 | });
146 | }
147 |
148 | // Method to configure camera info for HomeKit
149 | FoscamPlatform.prototype.configureCamera = function (mac) {
150 | var self = this;
151 | var thisCamera = this.cameraInfo[mac];
152 | var name = "Foscam " + thisCamera.name;
153 | var uuid = UUIDGen.generate(mac);
154 |
155 | this.log("Initializing platform accessory '" + name + "'...");
156 |
157 | // Setup for FoscamAccessory
158 | var videoProcessor = self.config.videoProcessor || 'ffmpeg';
159 | var cameraSource = new FFMPEG(hap, thisCamera, self.log, videoProcessor);
160 | var newAccessory = new Accessory(name, uuid, hap.Accessory.Categories.CAMERA);
161 | newAccessory.configureCameraSource(cameraSource);
162 |
163 | // Add HomeKit Security System Service
164 | newAccessory.addService(Service.SecuritySystem, name + " Motion Detection");
165 |
166 | // Add HomeKit Motion Sensor Service
167 | newAccessory.addService(Service.MotionSensor, name + " Motion Sensor");
168 |
169 | // Setup listeners for different events
170 | self.setService(newAccessory, mac);
171 |
172 | // Publish accessories to HomeKit
173 | self.api.publishCameraAccessories("FoscamCamera", [newAccessory]);
174 |
175 | // Store accessory in cache
176 | self.accessories[mac] = newAccessory;
177 |
178 | // Retrieve initial state
179 | self.getInitState(newAccessory, thisCamera);
180 | }
181 |
182 | // Method to setup listeners for different events
183 | FoscamPlatform.prototype.setService = function (accessory, mac) {
184 | // Setup listeners for Security System events
185 | accessory.getService(Service.SecuritySystem)
186 | .getCharacteristic(Characteristic.SecuritySystemCurrentState)
187 | .on('get', this.getCurrentState.bind(this, mac));
188 |
189 | accessory.getService(Service.SecuritySystem)
190 | .getCharacteristic(Characteristic.SecuritySystemTargetState)
191 | .on('get', this.getTargetState.bind(this, mac))
192 | .on('set', this.setTargetState.bind(this, mac));
193 |
194 | accessory.getService(Service.SecuritySystem)
195 | .getCharacteristic(Characteristic.StatusFault);
196 |
197 | // Setup listeners for Motion Sensor events
198 | accessory.getService(Service.MotionSensor)
199 | .getCharacteristic(Characteristic.MotionDetected)
200 | .on('get', this.getMotionDetected.bind(this, mac));
201 |
202 | accessory.getService(Service.MotionSensor)
203 | .getCharacteristic(Characteristic.StatusActive);
204 |
205 | accessory.getService(Service.MotionSensor)
206 | .getCharacteristic(Characteristic.StatusFault);
207 |
208 | // Setup additional Accessory Information
209 | accessory.getService(Service.AccessoryInformation)
210 | .getCharacteristic(Characteristic.FirmwareRevision);
211 |
212 | accessory.getService(Service.AccessoryInformation)
213 | .getCharacteristic(Characteristic.HardwareRevision);
214 |
215 | accessory.on('identify', this.identify.bind(this, mac));
216 | }
217 |
218 | // Method to retrieve initial state
219 | FoscamPlatform.prototype.getInitState = function (accessory, info) {
220 | // Update HomeKit accessory information
221 | accessory.getService(Service.AccessoryInformation)
222 | .setCharacteristic(Characteristic.Manufacturer, "Foscam Digital Technologies LLC")
223 | .setCharacteristic(Characteristic.Model, info.model)
224 | .setCharacteristic(Characteristic.SerialNumber, info.serial)
225 | .setCharacteristic(Characteristic.FirmwareRevision, info.fw)
226 | .setCharacteristic(Characteristic.HardwareRevision, info.hw);
227 |
228 | // Retrieve initial state
229 | accessory.getService(Service.SecuritySystem)
230 | .getCharacteristic(Characteristic.SecuritySystemCurrentState)
231 | .getValue();
232 |
233 | accessory.getService(Service.SecuritySystem)
234 | .getCharacteristic(Characteristic.SecuritySystemTargetState)
235 | .getValue();
236 |
237 | accessory.getService(Service.MotionSensor)
238 | .getCharacteristic(Characteristic.MotionDetected)
239 | .getValue();
240 | }
241 |
242 | // Method to get the security system current state
243 | FoscamPlatform.prototype.getCurrentState = function (mac, callback) {
244 | var self = this;
245 | var thisFoscamAPI = this.foscamAPI[mac];
246 | var thisCamera = this.cameraInfo[mac];
247 | var thisAccessory = this.accessories[mac];
248 |
249 | if (thisCamera.version === 0) {
250 | var getConfig = thisFoscamAPI.getMotionDetectConfig();
251 | } else if (thisCamera.version === 1) {
252 | var getConfig = thisFoscamAPI.getMotionDetectConfig1();
253 | }
254 |
255 | getConfig.then(function (config) {
256 | if (config.result === 0) {
257 | // Compute current state and target state
258 | if (config.isEnable === 0) {
259 | thisCamera.currentState = Characteristic.SecuritySystemCurrentState.DISARMED;
260 | } else {
261 | if (thisCamera.linkage.indexOf(config.linkage) >= 0) {
262 | thisCamera.currentState = thisCamera.linkage.indexOf(config.linkage);
263 | } else {
264 | thisCamera.currentState = Characteristic.SecuritySystemCurrentState.STAY_ARM;
265 | }
266 | }
267 |
268 | // Configre motion polling
269 | self.startMotionPolling(mac);
270 |
271 | // Set motion sensor status active
272 | thisAccessory.getService(Service.MotionSensor)
273 | .setCharacteristic(Characteristic.StatusActive, config.isEnable ? true : false);
274 |
275 | // Set security system status fault
276 | thisAccessory.getService(Service.SecuritySystem)
277 | .setCharacteristic(Characteristic.StatusFault, 0);
278 |
279 | self.log(thisCamera.name + " is " + self.armState[thisCamera.currentState]);
280 | callback(null, thisCamera.currentState);
281 | } else {
282 | var error = "Failed to retrieve " + thisCamera.name + " state!";
283 |
284 | // Set security system status fault to 1 in case of error
285 | thisAccessory.getService(Service.SecuritySystem)
286 | .setCharacteristic(Characteristic.StatusFault, 1);
287 |
288 | self.log(error);
289 | callback(new Error(error));
290 | }
291 | });
292 | }
293 |
294 | // Method to get the security system target state
295 | FoscamPlatform.prototype.getTargetState = function (mac, callback) {
296 | var self = this;
297 |
298 | setTimeout(function () {
299 | callback(null, self.cameraInfo[mac].currentState);
300 | }, 1000);
301 | }
302 |
303 | // Method to set the security system target state
304 | FoscamPlatform.prototype.setTargetState = function (mac, state, callback) {
305 | var self = this;
306 | var thisFoscamAPI = this.foscamAPI[mac];
307 | var thisCamera = this.cameraInfo[mac];
308 | var thisAccessory = this.accessories[mac];
309 |
310 | // Convert target state to isEnable
311 | var enable = state < 3 ? 1 : 0;
312 |
313 | if (thisCamera.version === 0) {
314 | var getConfig = thisFoscamAPI.getMotionDetectConfig();
315 | var setConfig = function (config) {thisFoscamAPI.setMotionDetectConfig(config);};
316 | } else if (thisCamera.version === 1) {
317 | var getConfig = thisFoscamAPI.getMotionDetectConfig1();
318 | var setConfig = function (config) {thisFoscamAPI.setMotionDetectConfig1(config);};
319 | }
320 |
321 | // Set PTZ Preset
322 | var ptzPreset = enable ? thisCamera.armPreset : thisCamera.disarmPreset;
323 | if (ptzPreset !== undefined) {
324 | thisFoscamAPI.ptzGotoPresetPoint(ptzPreset);
325 | self.log(thisCamera.name + " is moved to preset " + ptzPreset + ".");
326 | }
327 |
328 | // Get current config
329 | getConfig.then(function (config) {
330 | if (config.result === 0) {
331 | // Change isEnable, linkage, sensitivity, triggerInterval to requested state
332 | config.isEnable = enable;
333 | if (enable) config.linkage = thisCamera.linkage[state];
334 | config.sensitivity = self.sensitivity[thisCamera.sensitivity];
335 | config.triggerInterval = thisCamera.triggerInterval - 5;
336 |
337 | // Update config with requested state
338 | setConfig(config);
339 |
340 | // Set motion sensor status
341 | thisAccessory.getService(Service.MotionSensor)
342 | .setCharacteristic(Characteristic.StatusActive, enable ? true : false);
343 |
344 | // Set security system current state
345 | thisCamera.currentState = state;
346 | thisAccessory.getService(Service.SecuritySystem)
347 | .setCharacteristic(Characteristic.SecuritySystemCurrentState, state);
348 |
349 | // Configure motion polling
350 | self.startMotionPolling(mac);
351 |
352 | // Set status fault
353 | thisAccessory.getService(Service.SecuritySystem)
354 | .setCharacteristic(Characteristic.StatusFault, 0);
355 |
356 | self.log(thisCamera.name + " is set to " + self.armState[state]);
357 | callback(null);
358 | } else {
359 | var error = "Failed to set " + thisCamera.name + " state!";
360 |
361 | // Set status fault to 1 in case of error
362 | thisAccessory.getService(Service.SecuritySystem)
363 | .setCharacteristic(Characteristic.StatusFault, 1);
364 |
365 | self.log(error);
366 | callback(new Error(error));
367 | }
368 | });
369 | }
370 |
371 | // Method to get the motion sensor motion detected
372 | FoscamPlatform.prototype.getMotionDetected = function (mac, callback) {
373 | callback(null, this.cameraInfo[mac].motionAlarm);
374 | }
375 |
376 | // Method to handle identify request
377 | FoscamPlatform.prototype.identify = function (mac, paired, callback) {
378 | this.log(this.cameraInfo[mac].name + " identify requested!");
379 | callback();
380 | }
381 |
382 | // Method to start polling for motion
383 | FoscamPlatform.prototype.startMotionPolling = function (mac) {
384 | var self = this;
385 | var thisFoscamAPI = this.foscamAPI[mac];
386 | var thisCamera = this.cameraInfo[mac];
387 |
388 | // Clear polling
389 | clearTimeout(thisCamera.polling);
390 |
391 | // Start polling if armed
392 | if (thisCamera.currentState !== 3) {
393 | thisFoscamAPI.getDevState().then(function (state) {
394 | if (state.motionDetectAlarm === 2) self.motionDetected(mac);
395 | });
396 |
397 | // Setup next polling
398 | thisCamera.polling = setTimeout(this.startMotionPolling.bind(this, mac), 1000);
399 | }
400 | }
401 |
402 | // Method to configure motion sensor when motion is detected
403 | FoscamPlatform.prototype.motionDetected = function (mac) {
404 | var thisCamera = this.cameraInfo[mac];
405 | var thisAccessory = this.accessories[mac];
406 |
407 | // Clear motion reset
408 | clearTimeout(thisCamera.resetMotion);
409 |
410 | // Set motion detected
411 | if (thisCamera.motionAlarm === false) {
412 | this.log(thisCamera.name + " Motion Detected!");
413 | thisCamera.motionAlarm = true;
414 | thisAccessory.getService(Service.MotionSensor)
415 | .setCharacteristic(Characteristic.MotionDetected, thisCamera.motionAlarm);
416 | }
417 |
418 | // Reset motion detected after trigger interval
419 | thisCamera.resetMotion = setTimeout(function () {
420 | thisCamera.motionAlarm = false;
421 | thisAccessory.getService(Service.MotionSensor)
422 | .setCharacteristic(Characteristic.MotionDetected, thisCamera.motionAlarm);
423 | }, (thisCamera.triggerInterval - 1) * 1000);
424 | }
425 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "homebridge-foscamcamera",
3 | "description": "Foscam Plugin for HomeBridge (API 2.1): https://github.com/nfarina/homebridge",
4 | "version": "0.3.1",
5 | "repository": {
6 | "type": "git",
7 | "url": "https://github.com/luisiam/homebridge-foscamcamera.git"
8 | },
9 | "license": "ISC",
10 | "preferGlobal": true,
11 | "keywords": [
12 | "homebridge-plugin"
13 | ],
14 | "engines": {
15 | "node": ">=6.6.0",
16 | "homebridge": ">=0.4.6"
17 | },
18 | "dependencies": {
19 | "foscam-client": "^0.4.0",
20 | "homebridge-camera-ffmpeg": "^0.1.8"
21 | }
22 | }
23 |
--------------------------------------------------------------------------------