Merge pull request #6 from daaanny90/battery-level

Battery level
This commit is contained in:
Nicco 2020-03-07 16:16:59 +01:00 committed by GitHub
commit a44773188d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 21 deletions

View File

@ -16,7 +16,7 @@
}, },
"dependencies": { "dependencies": {
"auto-launch": "^5", "auto-launch": "^5",
"battery-level": "^3", "child_process": "^1.0.2",
"dark-mode": "^3.0.0", "dark-mode": "^3.0.0",
"electron-settings": "^3" "electron-settings": "^3"
} }

View File

@ -23,10 +23,10 @@ body,
#app { #app {
color: #3B252C; color: #3B252C;
font-family: 'Source Sans Pro', sans-serif; font-family: 'Source Sans Pro', sans-serif;
height: 100%;
width: 100%; width: 100%;
padding: 0; padding: 0;
margin: 0; margin: 0;
border-radius: 8px;
} }
#app.dark { #app.dark {
@ -54,9 +54,7 @@ hr {
#app .link { #app .link {
color: #3F8EFC; color: #3F8EFC;
/* color: inherit; */
font-size: 1em; font-size: 1em;
/* font-weight: normal; */
cursor: pointer; cursor: pointer;
} }

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Weather</title> <title>Volta</title>
<link rel="stylesheet" href="./index.css" charset="utf-8"> <link rel="stylesheet" href="./index.css" charset="utf-8">
<link rel="stylesheet" href="./vendor/nouislider.min.css" charset="utf-8"> <link rel="stylesheet" href="./vendor/nouislider.min.css" charset="utf-8">
<script charset="utf-8" src="./vendor/nouislider.min.js"> <script charset="utf-8" src="./vendor/nouislider.min.js">
@ -20,7 +20,6 @@
<div class="title">Current: <div class="title">Current:
<span id="currentBattery"></span> <span id="currentBattery"></span>
</div> </div>
<small>Note: Due to limits in the implementation the actual battery value may differ from the one given by the OS</small>
<hr/> <hr/>
<div id="settings"> <div id="settings">
@ -56,4 +55,4 @@
</script> </script>
</body> </body>
</html> </html>

View File

@ -11,7 +11,7 @@ const {
const settings = require('electron-settings') const settings = require('electron-settings')
const path = require('path') const path = require('path')
const AutoLaunch = require('auto-launch') const AutoLaunch = require('auto-launch')
const batteryLevel = require('battery-level') const exec = require('child_process').exec
const al = new AutoLaunch({ const al = new AutoLaunch({
name: 'Volta' name: 'Volta'
@ -94,6 +94,7 @@ const createWindow = () => {
} }
}) })
window.loadURL(`file://${path.join(__dirname, 'index.html')}`) window.loadURL(`file://${path.join(__dirname, 'index.html')}`)
window.setVisibleOnAllWorkspaces(true)
window.on('blur', () => { window.on('blur', () => {
if (!window.webContents.isDevToolsOpened()) window.hide() if (!window.webContents.isDevToolsOpened()) window.hide()
@ -148,21 +149,25 @@ function sendMax() {
}).show() }).show()
} }
let level = '--';
setInterval(() => { setInterval(() => {
batteryLevel().then(level => { exec('pmset -g batt | egrep "([0-9]+\%)" -o', function(err, stdout, stderr) {
level = parseInt(level * 100) if (err) {
console.log(error.stack)
console.log('Error code: ' + error.code)
console.log('Signal received: ' + error.signal)
}
level = parseInt(stdout)
})
if (level > lastBattery) charging = true
else charging = false
lastBattery = level
if (level === lastBattery) return const limits = settings.get('values', defaultValues)
if (level > lastBattery) charging = true
else charging = false
lastBattery = level
const limits = settings.get('values', defaultValues) if (level <= limits.min) sendMin()
else if (level >= limits.max) sendMax()
else numMax = numMin = 0
if (level <= limits.min) sendMin() window.webContents.send('battery', level)
else if (level >= limits.max) sendMax()
else numMax = numMin = 0
window.webContents.send('battery', level)
}).catch(() => console.log('Could not get Battery level'))
}, 3000) }, 3000)