UNPKG

gopro-js

Version:

A Promise based JavaScript GoPro Controller/API Wrapper (Unofficial)

130 lines (108 loc) 4.78 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _GpControlAPI = require('./GpControlAPI'); var _GpControlAPI2 = _interopRequireDefault(_GpControlAPI); var _utils = require('./utils'); var _errors = require('./errors'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var REGEX = { gpControl: /HERO4|HERO5|HERO\+/, auth: /HERO2|HERO3/, interface: (0, _utils.multilineRegExp)([/^delay|status|mode|set|shutter|powerOff|powerOn|listMedia/, /|deleteAll|deleteLast|deleteFile$/]) }; var APIs = { gpControl: _GpControlAPI2.default }; var GoPro = function () { function GoPro() { var _this = this; var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, GoPro); this.lastResult = props.lastResult; this.apiProps = { ip: props.ip || '10.5.5.9', mac: props.mac, model: props.model }; this._promise = this._discover().then(function (api) { if (api === null) return new _errors.NotFoundError();else if (api === -1) return new _errors.UnsupportedError();else _this.api = new APIs[api](_this.apiProps); }); // Delegate functions to API var proxy = new Proxy(this, { get: function get(target, property) { var match = property.match(/^catch|then$/); if (target[property] !== undefined && !match) return target[property]; return function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } if (match) { var newProps = function newProps(lastResult) { return Object.assign(props, _this.apiProps, { lastResult: lastResult }); }; var instance = function instance(lastResult, index) { if (!args[index]) return Promise.reject(lastResult);else return args[index](new GoPro(newProps(lastResult))); }; _this._promise = _this._promise[property](function (v) { return instance(v, 0); }, function (e) { return instance(e, 1); }); } else { var resolve = function resolve(lastResult) { _this.lastResult = lastResult; if (target.api === undefined) return new _errors.NotFoundError();else if (props.strict && !property.match(REGEX.interface)) { return new _errors.ForbiddenMethodError(property); } else if (target.api[property]) return target.api[property].apply(target.api, args);else return new _errors.UndefinedMethodError(property); }; _this._promise = _this._promise.then(resolve); } return proxy; }; } }); return proxy; } _createClass(GoPro, [{ key: '_selectAPI', value: function _selectAPI(model) { return model && (model.match(REGEX.gpControl) && 'gpControl' || model.match(REGEX.auth) && -1) || undefined; } }, { key: '_discover', value: function _discover() { var _this2 = this; var selection = this._selectAPI(this.apiProps.model); if (selection !== undefined) return Promise.resolve(selection); var api = new _GpControlAPI2.default(this.apiProps); return api.discover().then(function (data) { if (!data.model.match(REGEX.gpControl)) return -1; Object.assign(_this2.apiProps, data); return 'gpControl'; }).catch(function () { // Handle other types later return null; }); } /* === Interface === * discover() * delay(milliseconds:integer) * set(path:string) * status(path:string) * mode(mode:string, submode:string) * shutter(delay:integer, duration:integer) * powerOff() * powerOn() * listMedia() * deleteLast(number:integer) * deleteFile(folder:string, file:string) * deleteAll() */ }]); return GoPro; }(); exports.default = GoPro;