Skip to content

Commit

Permalink
Command-line scripting dxdc#45
Browse files Browse the repository at this point in the history
  • Loading branch information
dxdc committed Dec 27, 2020
1 parent 2bdc661 commit b8c682a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
56 changes: 45 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ const CERT_DAYS = 365;
const CERT_VERSION = 2;
const packageJSON = require('./package.json');

let request = require('requestretry');
const request = require('requestretry');

let auth = require('http-auth');
let fs = require('fs');
let http = require('http');
let https = require('https');
let jsonata = require('jsonata');
let url = require('url');
const auth = require('http-auth');
const fs = require('fs');
const http = require('http');
const https = require('https');
const jsonata = require('jsonata');

const exec = require('child_process').exec;
const url = require('url');

let Service, Characteristic, UUIDGen, HomebridgeAPI;

Expand Down Expand Up @@ -140,9 +142,7 @@ BlindsHTTPAccessory.prototype.configureWebhook = function () {

// Webhook callback
let createServerCallback = function (request, response) {
const theUrl = request.url;
const theUrlParts = url.parse(theUrl, true);
const theUrlParams = theUrlParts.query;
const q = url.parse(request.url, true);
let body = [];

request
Expand All @@ -165,7 +165,7 @@ BlindsHTTPAccessory.prototype.configureWebhook = function () {
});

response.setHeader('Content-Type', 'application/json');
const pos = theUrlParams.pos ? parseInt(theUrlParams.pos, 10) : NaN;
const pos = q.query.pos ? parseInt(q.query.pos, 10) : NaN;

if (isNaN(pos) || pos < 0 || pos > 100) {
this.log.error('Invalid position specified in request.');
Expand Down Expand Up @@ -634,7 +634,41 @@ BlindsHTTPAccessory.prototype.httpRequest = function (url, methods, callback) {
}.bind(this);

const startTimestamp = Date.now();
const cmdMatch = options.url.match(/(?:file:\/\/)(.*)/i);

// handling for file
if (cmdMatch !== null) {
exec(
cmdMatch[1],
function (err, stdout, stderr) {
const requestTime = Date.now() - startTimestamp;

if (!err) {
if (this.verbose) {
this.log.info(`Command succeeded in ${requestTime} ms`);
}

if (this.verbose) {
this.log.info(`Stdout: ${stdout}`);
if (stderr) {
this.log.info(`Stderr: ${stderr}`);
}
}

return callback(stdout, requestTime, null);
} else {
this.log.error(`Error running command: ${stderr}`);
this.log.info(`Stdout: ${stdout}`);

return callback(stdout, requestTime, err);
}
}.bind(this),
);

return;
}

// handling for http
request(
options(),
function (err, response, body) {
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-blinds",
"version": "1.3.22",
"version": "1.3.23-beta.0",
"description": "A homebridge plugin to control my blinds",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,16 +32,16 @@
"license": "ISC",
"dependencies": {
"http-auth": "^4.1.2",
"jsonata": "^1.8.3",
"jsonata": "^1.8.4",
"node-persist": "^2.1.0",
"request": "^2.88.2",
"requestretry": "^4.1.1",
"selfsigned": "^1.10.7"
"requestretry": "^4.1.2",
"selfsigned": "^1.10.8"
},
"devDependencies": {
"eslint": "^7.4.0",
"eslint-plugin-json": "^2.1.1",
"prettier": "^2.0.5",
"eslint": "^7.16.0",
"eslint-plugin-json": "^2.1.2",
"prettier": "^2.2.1",
"rimraf": "^3.0.2"
}
}

0 comments on commit b8c682a

Please sign in to comment.