Skip to content

Commit

Permalink
restore devices script
Browse files Browse the repository at this point in the history
  • Loading branch information
dexif committed May 23, 2024
1 parent a40af36 commit 10111ce
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
68 changes: 68 additions & 0 deletions restore-devices/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const axios = require('axios');

// URL for GET request
const getUrl = 'https://flespi.io/platform/deleted/all/logs?data=%7B%22count%22%3A3000%2C%22reverse%22%3Atrue%2C%22filter%22%3A%22origin_type%3D11%22%2C%22fields%22%3A%22old%22%7D';

// Headers for requests
const headers = {
'Authorization': 'FlespiToken YOUR_TOKEN_HERE',
'Content-Type': 'application/json'
};

// URL for POST request
const postUrl = 'https://flespi.io/gw/devices';

// Function to perform GET request and fetch data
async function fetchData() {
try {
const response = await axios.get(getUrl, { headers });
const data = response.data;

// Transforming data
const transformedData = data.result.map(item => {
let a = item.old
delete a.protocol_id
delete a.cid
delete a.id
return a
});

console.log('response: ', data);

console.log('Transformed data:', transformedData);

// Returning transformed data
return transformedData;
} catch (error) {
console.error('Error performing GET request:', error);
}
}

// Function to split array into chunks
function chunkArray(array, chunkSize) {
const chunks = [];
for (let i = 0; i < array.length; i += chunkSize) {
chunks.push(array.slice(i, i + chunkSize));
}
return chunks;
}

// Function to perform POST request
async function sendData(dataChunks) {
try {
for (const chunk of dataChunks) {
const response = await axios.post(postUrl, JSON.stringify(chunk), { headers });
console.log('Response to POST request:', response.data);
}
} catch (error) {
console.error('Error performing POST request:', JSON.stringify(error.response.data, null, ' '));
}
}

// Calling functions to fetch and send data
fetchData().then(transformedData => {
if (transformedData) {
const dataChunks = chunkArray(transformedData, 100);
sendData(dataChunks);
}
});
102 changes: 102 additions & 0 deletions restore-devices/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions restore-devices/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"axios": "^1.7.2"
}
}

0 comments on commit 10111ce

Please sign in to comment.