Skip to content

Commit

Permalink
Add functionality to add and remove target AP and
Browse files Browse the repository at this point in the history
BT addresses from config
  • Loading branch information
infinitel8p committed Nov 18, 2023
1 parent ce36efb commit f876a44
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 25 deletions.
8 changes: 8 additions & 0 deletions server/public/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ button {
border-radius: 30px;
padding: 5px 20px;
cursor: pointer;
}

.mac-address {
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 10px;
display: flex;
width: -moz-fit-content;
width: fit-content;
}
73 changes: 60 additions & 13 deletions server/public/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,71 @@ document.addEventListener('DOMContentLoaded', () => {
})
.catch(error => console.error('Error fetching config:', error));

var form = document.querySelector('form');
form.addEventListener('submit', function (e) {
e.preventDefault();
const addApButton = document.getElementById('add-ap');
addApButton.addEventListener('click', () => {
const input = addApButton.parentElement.querySelector('input').value;
fetch('/add-config-item', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ key: 'TARGET_AP_MAC_ADDRESSES', value: input })
})
.then(response => response.json())
.then(content => {
document.getElementById('target_ap_mac_addresses').value = content.TARGET_AP_MAC_ADDRESSES;
})
.catch(error => console.error('Error adding config item:', error));
});

const removeApButton = document.getElementById('remove-ap');
removeApButton.addEventListener('click', () => {
const input = removeApButton.parentElement.querySelector('input').value;
fetch('/remove-config-item', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ key: 'TARGET_AP_MAC_ADDRESSES', value: input })
})
.then(response => response.json())
.then(content => {
document.getElementById('target_ap_mac_addresses').value = content.TARGET_AP_MAC_ADDRESSES;
})
.catch(error => console.error('Error removing config item:', error));
});

// Create a FormData object, passing in the form
var formData = new FormData(form);
const addBtButton = document.getElementById('add-bt');
addBtButton.addEventListener('click', () => {
const input = addBtButton.parentElement.querySelector('input').value;
fetch('/add-config-item', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ key: 'TARGET_BT_ADDRESSES', value: input })
})
.then(response => response.json())
.then(content => {
document.getElementById('target_bt_addresses').value = content.TARGET_BT_ADDRESSES;
})
.catch(error => console.error('Error adding config item:', error));
});

fetch('/update-config', {
const removeBtButton = document.getElementById('remove-bt');
removeBtButton.addEventListener('click', () => {
const input = removeBtButton.parentElement.querySelector('input').value;
fetch('/remove-config-item', {
method: 'POST',
body: formData
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ key: 'TARGET_BT_ADDRESSES', value: input })
})
.then(response => response.json())
.then(data => {
window.alert("Success");
console.log('Success:', data);
.then(content => {
document.getElementById('target_bt_addresses').value = content.TARGET_BT_ADDRESSES;
})
.catch((error) => {
console.error('Error:', error);
});
.catch(error => console.error('Error removing config item:', error));
});
});
35 changes: 28 additions & 7 deletions server/public/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,34 @@ <h1>Check the documentation</h1>

<section class="feed">
<h1>Settings</h1>
<form>
<label for="target_ap_mac_addresses">Target AP MAC addresses</label>
<input type="text" id="target_ap_mac_addresses">
<label for="target_bt_addresses">Target BT addresses</label>
<input type="text" id="target_bt_addresses">
<button type="submit">Save</button>
</form>
<label for="target_ap_mac_addresses">Target AP MAC addresses</label>
<input type="text" id="target_ap_mac_addresses" name="target_ap_mac_addresses">
<label for="target_bt_addresses">Target BT addresses</label>
<input type="text" id="target_bt_addresses" name="target_bt_addresses">

<div class="mac-address">
<label for="test_add_ap_mac">Target AP address to add</label>
<input id="test_add_ap_mac" value="XX:XX:XX:XX:XX:XX">
<button id="add-ap">Add</button>
</div>

<div class="mac-address">
<label for="test_remove_ap_mac">Target AP address to add</label>
<input id="test_remove_ap_mac" value="XX:XX:XX:XX:XX:XX">
<button id="remove-ap">Remove</button>
</div>

<div class="mac-address">
<label for="test_add_bt_mac">Target BT address to add</label>
<input id="test_add_bt_mac" value="XX:XX:XX:XX:XX:XX">
<button id="add-bt">Add</button>
</div>

<div class="mac-address">
<label for="test_remove_bt_mac">Target BT address to add</label>
<input id="test_remove_bt_mac" value="XX:XX:XX:XX:XX:XX">
<button id="remove-bt">Remove</button>
</div>
</section>

<section class="trending">
Expand Down
45 changes: 40 additions & 5 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const app = express();
const port = 3000;

app.use(express.static('public'));
app.use(express.json());
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

Expand Down Expand Up @@ -148,11 +149,45 @@ app.get('/config', (req, res) => {
});
});

app.post('/update-config', (req, res) => {
console.log(req);
var data = 'Success'
res.json(data);
// TODO Update json, restart or update python
app.post('/remove-config-item', (req, res) => {
const { key, value } = req.body;
fs.readFile('../config.json', (err, data) => {
if (err) {
res.status(500).send('Error reading config file');
return;
}
const config = JSON.parse(data);
const index = config[key].indexOf(value);
if (index !== -1) {
config[key].splice(index, 1);
}
fs.writeFile('../config.json', JSON.stringify(config), err => {
if (err) {
res.status(500).send('Error writing config file');
return;
}
res.json(config);
});
});
});

app.post('/add-config-item', (req, res) => {
const { key, value } = req.body;
fs.readFile('../config.json', (err, data) => {
if (err) {
res.status(500).send('Error reading config file');
return;
}
const config = JSON.parse(data);
config[key].push(value);
fs.writeFile('../config.json', JSON.stringify(config), err => {
if (err) {
res.status(500).send('Error writing config file');
return;
}
res.json(config);
});
});
});

app.get('/version', (req, res) => {
Expand Down

0 comments on commit f876a44

Please sign in to comment.