mirror of
https://github.com/Retropex/apolloapi-v2.git
synced 2025-05-28 13:02:35 +02:00
Merge branch 'master' of https://github.com/CryptofyCH/apolloapi
This commit is contained in:
commit
ef22cb77c6
55
scripts/wifi_scan
Executable file
55
scripts/wifi_scan
Executable 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
|
22
src/graphql/graphqlModules/Mcu/McuWifiConnect.js
Normal file
22
src/graphql/graphqlModules/Mcu/McuWifiConnect.js
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
32
src/graphql/graphqlModules/Mcu/McuWifiScan.js
Normal file
32
src/graphql/graphqlModules/Mcu/McuWifiScan.js
Normal 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')
|
||||
}
|
||||
}
|
||||
}
|
34
src/store/api/mcu/mcuWifiConnect.js
Normal file
34
src/store/api/mcu/mcuWifiConnect.js
Normal 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()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
29
src/store/api/mcu/mcuWifiScan.js
Normal file
29
src/store/api/mcu/mcuWifiScan.js
Normal 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)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user