This commit is contained in:
Michele Marcucci 2018-11-06 19:04:53 +01:00
commit ef22cb77c6
5 changed files with 172 additions and 0 deletions

55
scripts/wifi_scan Executable file
View File

@ -0,0 +1,55 @@
#!/bin/bash
# Collects wifi networks informations such as SSID, rate,
# signal etc.
# EXAMPLE USAGE:
# ./wifi_scan.sh
set -e
# Debugging options:
# set -x
# set -v
# always try to rescan
nmcli device wifi rescan || true
# list connections
nmcli_output=$(nmcli -f ssid,mode,chan,rate,signal,security,in-use -t dev wifi)
# generate json
echo "[" > /tmp/.nmcli_gen_json
# make newlines the only separator
IFS=$'\n'
for line in $nmcli_output ; do
IFS=':' read -r -a array <<< "$line"
# remove "Mbit/s"
rate=$(echo "${array[3]}" | sed "s/ Mbit\/s$//")
# change in-use string to Boolean
if [ "${array[6]}" = "*" ]; then
inuse="true"
else
inuse="false"
fi
# parse into json
json="{
\"ssid\": \"${array[0]}\",
\"mode\": \"${array[1]}\",
\"channel\": ${array[2]},
\"rate\": ${rate},
\"signal\": ${array[4]},
\"security\": \"${array[5]}\",
\"inuse\": ${inuse}
}"
echo "$json", >> /tmp/.nmcli_gen_json
done
# remove last comma, change later?
perl -0777 -pi -e 's/(.*),(.*?)/\1\2/s' /tmp/.nmcli_gen_json
echo "]" >> /tmp/.nmcli_gen_json
# output json
cat /tmp/.nmcli_gen_json
rm /tmp/.nmcli_gen_json

View File

@ -0,0 +1,22 @@
module.exports.typeDefs = `
type McuActions {
wifiConnect (input: McuWifiConnectInput!): McuWifiConnectOutput!
}
input McuWifiConnectInput {
ssid: String!
passphrase: String
}
type McuWifiConnectOutput {
error: Error
}
`
module.exports.resolvers = {
McuActions: {
wifiConnect (root, args, { dispatch }) {
return dispatch('api/mcu/wifiConnect', args.input)
}
}
}

View File

@ -0,0 +1,32 @@
module.exports.typeDefs = `
type McuActions {
wifiScan: McuWifiScanOutput!
}
type McuWifiScanOutput {
result: McuWifiScanResult
error: Error
}
type McuWifiScanResult {
wifiScan: [McuWifiScan]
}
type McuWifiScan {
ssid: String
mode: String
channel: Int
rate: Int
signal: Int
security: String
inuse: Boolean
}
`
module.exports.resolvers = {
McuActions: {
wifiScan (root, args, { dispatch }) {
return dispatch('api/mcu/wifiScan')
}
}
}

View File

@ -0,0 +1,34 @@
const { exec } = require('child_process')
module.exports = ({ define }) => {
define('wifiConnect', async ({ssid, passphrase}, { knex, errors, utils }) => {
await wifiConnect(ssid, passphrase, errors)
}), {
auth: true
}
}
function wifiConnect(ssid, passphrase, errors) {
return new Promise((resolve, reject) => {
if (passphrase) {
command = 'sudo nmcli dev wifi connect ' + ssid + ' password ' + passphrase
}
else {
command = 'sudo nmcli dev wifi connect ' + ssid
}
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()
}
}
})
})
}

View File

@ -0,0 +1,29 @@
const { join } = require('path')
const { exec } = require('child_process')
module.exports = ({ define }) => {
define('wifiScan', async (payload, { knex, errors, utils }) => {
const wifiScan = await getWifiScan()
return { wifiScan }
}, {
auth: true
})
}
function getWifiScan() {
return new Promise((resolve, reject) => {
const scriptPath = join(__dirname, '..', '..', '..', '..', 'scripts', 'wifi_scan')
exec(scriptPath, {}, (err, stdout) => {
if (err) {
reject(err)
} else {
try {
const result = JSON.parse(stdout.toString())
resolve(result)
} catch (err) {
reject(err)
}
}
})
})
}