Browse Source

changed to capture system mac address to set user status to online / offline

master
kj1352 10 months ago
parent
commit
a0537bb1d4
3 changed files with 55 additions and 200 deletions
  1. +6
    -137
      index.html
  2. +48
    -59
      main.js
  3. +1
    -4
      preload.js

+ 6
- 137
index.html View File

@@ -1,143 +1,12 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Monitoring Tool</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}

table {
width: 100%;
max-width: 600px;
margin: 20px auto;
border-collapse: collapse;
}

td {
padding: 10px;
}

label {
font-weight: bold;
}

button {
display: block;
margin: 20px auto;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
}

button:hover {
background-color: #0056b3;
}

textarea {
width: 100%;
min-height: 60px;
}

input, select {
width: 100%;
}

h1 {
text-align: center;
color: #333;
}

.section {
margin: 40px 0;
}
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Project Deep Water </title>
</head>

<body>
<!-- Token Saving Section -->
<div class="section">
<h1>Save Token</h1>
<form id="tokenForm">
<table>
<tr>
<td>
<label for="token">Token:</label>
</td>
<td>
<input type="text" id="token" placeholder="Enter your token" required />
</td>
</tr>
</table>
<button type="submit">Save Token</button>
</form>
</div>

<!-- Check-Out Form Section -->
<div class="section">
<h1>Check Out Form</h1>
<form id="taskForm">
<table>
<tr>
<td>
<label for="tasks">Tasks:</label>
</td>
<td>
<textarea id="tasks" placeholder="Describe your tasks..." required></textarea>
</td>
</tr>
</table>
<button type="submit">Submit Check-Out</button>
</form>
</div>

<script>
// Handle token saving
const tokenForm = document.getElementById('tokenForm');
const tokenInput = document.getElementById('token');
if (localStorage.token) {
tokenInput.value = localStorage.token;
}

tokenForm.addEventListener('submit', (e) => {
e.preventDefault();
const token = tokenInput.value.trim();
if (token) {
localStorage.token = token;
alert('Token saved successfully!');
} else {
alert('Please enter a valid token.');
}
});

// Handle check-out form submission
const taskForm = document.getElementById('taskForm');
taskForm.addEventListener('submit', (e) => {
e.preventDefault();
const project = document.getElementById('project').value;
const tasks = document.getElementById('tasks').value;
const token = localStorage.token || '';

if (!token) {
alert('Please save your token first.');
return;
}

// Send data to Electron main process
window.electronAPI.submitCheckout({ project, tasks, token });

// Display success message
alert('Check-Out submitted successfully!');
});
</script>
<h1> Project Deep water is Up & Running! </h1>
<p> Please ensure the application is active </p>
</body>

</html>
</html>

+ 48
- 59
main.js View File

@@ -1,44 +1,45 @@
const { app, BrowserWindow, ipcMain, powerMonitor } = require('electron');
const { app, BrowserWindow, powerMonitor } = require('electron');
const path = require('path');
const os = require('os');

let mainWindow;
const endpoint = "https://workx.webtrigon.com/api/v1/device/presence/";

app.on('ready', () => {
createMainWindow();

// Automatically check in on app startup
const token = getToken();
if (token) {
console.log('Automatically checking in...');
sendAPIRequest('https://workx.webtrigon.com/api/v1/slack/check-in/', { token });
} else {
console.warn('No token found for automatic check-in.');
}
const macAddress = getMacAddress();
console.log('MAC Address:', macAddress);

// Automatically check-in when app starts
sendAPIRequest({ mac_address: macAddress, is_online: true });

// Handle sleep or lid close event
// Handle system events
powerMonitor.on('suspend', () => {
console.log('System is going to sleep.');
const token = getToken();
if (token) {
sendAPIRequest('http://workx.webtrigon.com/api/v1/slack/check-out/', { token });
} else {
console.warn('No token found for check-out.');
}
sendAPIRequest({ mac_address: macAddress, is_online: false });
});

powerMonitor.on('shutdown', () => {
console.log('System is shutting down.');
sendAPIRequest({ mac_address: macAddress, is_online: false });
});

powerMonitor.on('lock-screen', () => {
console.log('System is locking.');
sendAPIRequest({ mac_address: macAddress, is_online: false });
});

powerMonitor.on('resume', () => {
console.log('System is resuming.');
const token = getToken();
if (token) {
sendAPIRequest('https://workx.webtrigon.com/api/v1/slack/check-in/', { token });
} else {
console.warn('No token found for check-in.');
}
createMainWindow(); // Reopen the window on resume
sendAPIRequest({ mac_address: macAddress, is_online: true });
mainWindow.show();
});
});

function createMainWindow() {
const userDataPath = path.join(app.getPath('userData'), 'Cache');
mainWindow = new BrowserWindow({
width: 800,
height: 600,
@@ -49,57 +50,45 @@ function createMainWindow() {
},
});

app.setPath('userData', userDataPath);
mainWindow.loadFile('index.html');
}

// Handle manual check-out
ipcMain.on('submit-checkout', (event, taskData) => {
const { token, project, tasks } = taskData;
if (!token) {
console.error('Token is missing for check-out.');
return;
}
sendAPIRequest('http://workx.webtrigon.com/api/v1/slack/check-out/', { project, tasks, token });
});


// API request function
async function sendAPIRequest(endpoint, data = {}) {
const token = data.token || getToken(); // Retrieve token
if (!token) {
console.error('Token is required for API calls.');
return;
}

async function sendAPIRequest(data = {}) {
try {
const response = await fetch(endpoint, {
method: 'POST', // Assuming POST; modify if needed
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
});

if (!response.ok) {
console.error(`API call failed with status: ${response.status}`);
} else {
const result = await response.json();
console.log('API call succeeded:', result);
const textResponse = await response.text();
try {
const result = JSON.parse(textResponse.toString());
if (!response.ok) {
console.error(`API call failed with is_online: ${response.is_online}`, result);
} else {
console.log('API call succeeded:', result.toString());
}
} catch (jsonError) {
console.error('Error parsing JSON response:', textResponse.toString());
}
} catch (error) {
console.error('Error during API call:', error);
console.error('Error during API call:', error.toString());
}
}

// Helper function to get token from local storage
function getToken() {
// Assuming `localStorage` is used to persist token via preload script
try {
const token = require('electron').ipcRenderer.sendSync('get-token');
return token || null;
} catch (error) {
// console.error('Error retrieving token:', error);
return '2c920ac10c960614a6c73ed15d11c79529910d24';
function getMacAddress() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
// Skip over non-IPv4 and internal (127.0.0.1) addresses
if (iface.family === 'IPv4' && !iface.internal) {
return iface.mac;
}
}
}
return null; // If no MAC address found
}

+ 1
- 4
preload.js View File

@@ -1,6 +1,3 @@
const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
submitCheckout: (taskData) => ipcRenderer.send('submit-checkout', taskData),
getToken: () => localStorage.token || '2c920ac10c960614a6c73ed15d11c79529910d24', // Fetch token securely
});
contextBridge.exposeInMainWorld('electronAPI', { });

Loading…
Cancel
Save