Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = class API{
constructor() {
this.link = "http://192.168.1.78:8080/"
}

async getBuilding(){
return (await fetch(`${this.link}building`, {
method: "GET"
})).json()
}

async getFloors(id){
return (await fetch(`${this.link}building/${id}/floors`, {
method: "GET"
})).json()
}

}
15 changes: 15 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="../styles.css" rel="stylesheet">
<link href="../node_modules/bootstrap/dist/css/bootstrap.css" rel="stylesheet">
<title>Interface</title>
</head>
<body>

<script src="../renderer.js"></script>
</body>
</html>
26 changes: 0 additions & 26 deletions index.html

This file was deleted.

31 changes: 13 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const { app, BrowserWindow, ipcMain} = require('electron')
const path = require('node:path')
const apiModule = require("./api.js")

const API = new apiModule()
let mainWindow

function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile(path.join(__dirname, '/html/index.html'))

// and load the index.html of the app.
mainWindow.loadFile('index.html')
mainWindow.webContents.openDevTools()
}

// Open the DevTools.
// mainWindow.webContents.openDevTools()
async function getBuildings(){
const resp = await API.getBuilding()
return resp
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
ipcMain.handle('api:getBuildings', getBuildings)

app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
23 changes: 15 additions & 8 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
*
* https://www.electronjs.org/docs/latest/tutorial/sandbox
*/
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
// window.addEventListener('DOMContentLoaded', () => {
// const replaceText = (selector, text) => {
// const element = document.getElementById(selector)
// if (element) element.innerText = text
// }
//
// for (const type of ['chrome', 'node', 'electron']) {
// replaceText(`${type}-version`, process.versions[type])
// }
// })

for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('electronAPI', {
getBuildings: () => ipcRenderer.invoke('api:getBuildings'),
})

20 changes: 13 additions & 7 deletions renderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
const btn = document.getElementsByClassName("btn")
const p = document.getElementById("1")

// window.electronAPI.

// window.addEventListener("DOMContentLoaded", async () => {
// const buildings = await window.electronAPI.getBuildings()
// })

window.addEventListener("DOMContentLoaded", async () => {
const buildings = await window.electronAPI.getBuildings()
console.log(buildings)
})