diff --git a/scripts/wifi_scan b/scripts/wifi_scan new file mode 100755 index 0000000..6b3b928 --- /dev/null +++ b/scripts/wifi_scan @@ -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 diff --git a/src/graphql/graphqlModules/Mcu/McuWifiConnect.js b/src/graphql/graphqlModules/Mcu/McuWifiConnect.js new file mode 100644 index 0000000..fd57e43 --- /dev/null +++ b/src/graphql/graphqlModules/Mcu/McuWifiConnect.js @@ -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) + } + } +} \ No newline at end of file diff --git a/src/graphql/graphqlModules/Mcu/McuWifiScan.js b/src/graphql/graphqlModules/Mcu/McuWifiScan.js new file mode 100644 index 0000000..7f8ce99 --- /dev/null +++ b/src/graphql/graphqlModules/Mcu/McuWifiScan.js @@ -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') + } + } +} \ No newline at end of file diff --git a/src/store/api/mcu/mcuWifiConnect.js b/src/store/api/mcu/mcuWifiConnect.js new file mode 100644 index 0000000..dd4d484 --- /dev/null +++ b/src/store/api/mcu/mcuWifiConnect.js @@ -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() + } + } + }) + }) +} diff --git a/src/store/api/mcu/mcuWifiScan.js b/src/store/api/mcu/mcuWifiScan.js new file mode 100644 index 0000000..6cb44db --- /dev/null +++ b/src/store/api/mcu/mcuWifiScan.js @@ -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) + } + } + }) + }) +} \ No newline at end of file