Added wifi disconnect

This commit is contained in:
Michele Marcucci 2018-11-09 08:36:45 +01:00
parent a06ddd7f23
commit 86d265d585
6 changed files with 56 additions and 1 deletions

View File

@ -30,6 +30,7 @@ UPTIME=$(uptime -s)
LOADAVG=$(cat /proc/loadavg)
ARCHITECTURE=$(uname -m)
TEMP=$(cat /sys/class/thermal/thermal_zone*/temp)
ACTIVE_WIFI=$(nmcli -t c show --active | grep wireless | cut -d":" -f1)
# Memory
memTotal=$(egrep '^MemTotal:' /proc/meminfo | awk '{print $2}')
@ -59,6 +60,7 @@ JSON="{
\"loadAverage\": \"$LOADAVG\",
\"architecture\": \"$ARCHITECTURE\",
\"temperature\": \"$TEMP\",
\"activeWifi\": \"$ACTIVE_WIFI\",
\"memory\":
{
\"total\": $memTotal,

View File

@ -8,6 +8,7 @@ cat << EOF
"loadAverage": "0.00 0.00 0.00 1/120 2859",
"architecture": "armv7l",
"temperature": "40656",
"activeWifi": "befree",
"memory":
{
"total": 245760,

View File

@ -10,4 +10,8 @@ app.use('/api/graphql', graphqlApp)
if (process.env.NODE_ENV === 'production') app.use(express.static(buildPath));
app.get('*', function (req, res) {
res.sendFile(buildPath + '/index.html');
});
module.exports = app

View File

@ -0,0 +1,17 @@
module.exports.typeDefs = `
type McuActions {
wifiDisconnect: McuWifiDisconnectOutput!
}
type McuWifiDisconnectOutput {
error: Error
}
`
module.exports.resolvers = {
McuActions: {
wifiDisconnect (root, args, { dispatch }) {
return dispatch('api/mcu/wifiDisconnect')
}
}
}

View File

@ -14,7 +14,7 @@ function wifiConnect(ssid, passphrase, errors) {
return new Promise((resolve, reject) => {
let command = 'sudo nmcli dev wifi connect ' + ssid;
if (passphrase) command += ' password ' + passphrase;
if (process.env.NODE_ENV !== 'production') command = 'echo true';
if (process.env.NODE_ENV !== 'production') command = 'sleep 2 && nmcli dev wifi connect ' + ssid;
exec(command, {}, (err, stdout) => {
if (err) {

View File

@ -0,0 +1,31 @@
const { exec } = require('child_process')
module.exports = ({ define }) => {
define('wifiDisconnect', async (payload, { knex, errors, utils }) => {
await wifiDisconnect(errors);
return;
}), {
auth: true
}
}
function wifiDisconnect(ssid, passphrase, errors) {
return new Promise((resolve, reject) => {
let command = 'for i in $(nmcli -t c show |grep wireless | cut -d":" -f2); do nmcli c delete $i; done';
if (process.env.NODE_ENV !== 'production') command = 'sleep 2 && echo true';
exec(command, {}, (err, stdout) => {
if (err) {
reject(err)
} else {
if (stdout.includes('Error')) {
errMsg = stdout.trim().replace(/^.+\(\d+\)\ /g, "").replace(/\.$/g, "");
reject(new errors.ValidationError(errMsg));
}
else {
resolve()
}
}
})
})
}