Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
killroyboy committed Jun 9, 2018
2 parents 2e0dc2c + d9551aa commit 4c99aef
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 26 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ npm install
node index.js
```

The first command will install all the prerequisites and the second starts *ambient2pwsweather*. You will need to keep this window and process running in order to allow it to continue to retrieve data from AmbientWeather.net. There are tools available (like [forever](https://www.npmjs.com/package/forever)) to ensure the process runs in the background.

The first command will install all the prerequisites and the second starts *ambient2pwsweather*. You will need to keep this window and process running in order to allow it to continue to retrieve data from AmbientWeather.net.

Configuration
---------------
Expand All @@ -54,6 +53,31 @@ Alternatively, the following environment variables can be used to override confi
- PWD_PASSWORD - (From your PWS Weather account. NOTE: Your password cannot have any punctuation or special characters)
- LOG_LEVEL - (debug, info, warn, error)

Install as Service
---------------
To install as a service, you must first install a service tool for your platform.

```js
// for macOS
npm install node-mac

// for Windows
npm install node-windows

// for Linux
npm install node-linux
```

Then install as a service:
```js
npm run service install
```

You can view logs through the standard/system logging for your platform.

For more details, visit the respective module npm page: [macOS](https://www.npmjs.com/package/node-mac), [Windows](https://www.npmjs.com/package/node-windows), [Linux](https://www.npmjs.com/package/node-linux)


Author
---------------
Dan Wilson ([@killroyboy](https://twitter.com/killroyboy) / [Web](https://www.codeality.com))
Expand All @@ -64,4 +88,4 @@ MIT

Contributing
---------------
Code contributions are greatly appreciated, please submit a new [pull request](https://github.com/killroyboy/ambient2pwsweather/pull/new/master)!
Code contributions are greatly appreciated, please submit a new [pull request](https://github.com/killroyboy/ambient2pwsweather/pull/new/master)!
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* * Currently only supports 1 device from AmbientWeather
*/

process.env['NODE_CONFIG_DIR'] = __dirname + '/config/';

const got = require('got'),
pkg = require('./package.json'),
config = require('config'),
Expand Down
69 changes: 47 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "ambient2pwsweather",
"version": "0.0.6",
"version": "1.0.0",
"description": "Pull weather data from AmbientWeather.net and push to PWSWeather",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"service": "sudo node service.js",
"start dev": "nodemon index.js",
"build": "mkdir -p build && zip -x 'node_modules/*' -x 'build/*' -x '.git/*' -x '.idea/*' -x 'config/development.json' -r build/ambient2pwsweather-$(npm run version --silent).zip *",
"version": "echo $npm_package_version"
Expand All @@ -30,6 +31,7 @@
"config": "^1.30.0",
"eazy-logger": "^3.0.2",
"got": "^8.3.1",
"is-elevated": "^2.0.1",
"lodash": "^4.17.10",
"moment": "^2.22.1"
},
Expand Down
109 changes: 109 additions & 0 deletions service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* ambient2pwsweather installation
* * Manager ambient2pwsweather as a service on macOS, Windows or Linux
* * You must manually install the platform service yourself: node-mac, node-windows, node-linux
*/

// determine which service helper we should require
var service_mod = '';
switch (process.platform) {
case 'darwin':
service_mod = 'node-mac';
break;
case 'win32':
service_mod = 'node-windows';
break;
case 'linux':
service_mod = 'node-linux';
break;
}

if (!service_mod) {
console.log('ERROR: Undetected platform. Running as a service is only available on macOS, Windows and Linux.');
process.exit();
}

const isElevated = require('is-elevated');

isElevated().then(elevated => {
if (!elevated) {
console.log('ERROR: Must be run as root/Administrator.')
process.exit();
} else {
const Service = require(service_mod).Service;

// Create a new service object
var svc = new Service({
name:'ambient2pwsweather',
description: 'Pull weather data from an AmbientWeather.net Station and push to a PWSweather.com Station',
script: require('path').join(__dirname, 'index.js')
});

svc.on('start', function () {
setTimeout(function () {
// console.log('Service running:', svc.exists);
}, 300);
});

svc.on('stop', function () {
setTimeout(function () {
// console.log('Service running:', svc.exists);
}, 300);
});

svc.on('error', function (e) {
console.log('Error occurred:', e);
});

svc.on('alreadyinstalled', function () {
console.log('Service already installed.');
console.log('Service installed:', svc.exists);
});

svc.on('install', function() {
svc.start();
});

svc.on('uninstall', function() {
console.log('Uninstall complete.');
console.log('Service installed:', svc.exists);
});

var command = process.argv[2];

var usage = function () {
console.log('Usage: service [install|uninstall|status|start|stop|restart]');
process.exit(0);
};

if (!command) {
usage();
}

switch (command) {
case 'install':
// Install the service
svc.install();
break;
case 'uninstall':
// Uninstall the service.
svc.uninstall();
break;
case 'status':
console.log('Service installed:', svc.exists);
// console.log('Service running:', svc.exists);
break;
case 'start':
svc.start();
break;
case 'stop':
svc.stop();
break;
case 'restart':
svc.restart();
break;
default:
usage();
}
}
});

0 comments on commit 4c99aef

Please sign in to comment.