|
|
@@ -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 |
|
|
|
} |