Skip to content

Commit

Permalink
Switch to websockets (UnchartedBull#1438)
Browse files Browse the repository at this point in the history
* remove api from host

* websocket connected

* wip

* move config to electron

* finish moving config part to electron

* temperatures on main screen using socket

* defer filament manager from abstract class

* replaced printer temperature + status observable

* PrinterService behind abstract service

* Create system service

* merge profile & printer service

* add lottie loading animation for files

* enclosure service created

* starting to refactor files service

* adjust sorting

* fix M600 not being sent

* Fix long-pressing directive

* replace subject with replaySubject

* Move PSUControl + TPLinkSmartPlug

* get fanspeed from websocket

* rework OctoDash model

* remove DLP service and replace with socket

* wip

* event socket done

* update standby screen

* fix state issue

* socket reauth

* Finish job service transition

* squish the bugs

* replace checkmark animation

* start transitioning file service

* transition files service

* cleanup
  • Loading branch information
UnchartedBull authored and kantlivelong committed May 5, 2021
1 parent c6d8fb8 commit f946abc
Show file tree
Hide file tree
Showing 117 changed files with 3,654 additions and 3,519 deletions.
1 change: 0 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"builder": "@angular-devkit/build-angular:browser",
"options": {
"aot": true,
"allowedCommonJsDependencies": ["angular-svg-round-progressbar", "lodash", "ajv"],
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
Expand Down
56 changes: 56 additions & 0 deletions helper/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable @typescript-eslint/no-var-requires */

const Store = require('electron-store');
const Ajv = require('ajv');
const configSchema = require('./config.schema');

let store;
const ajv = new Ajv({ allErrors: true });
const validate = ajv.compile(configSchema);

function readConfig(window) {
try {
if (!store) {
store = new Store();
}
const config = store.get('config');
window.webContents.send('configRead', config);
} catch {
window.webContents.send('configError', "Can't read config file.");
}
}

function saveConfig(window, config) {
if (validate(config)) {
try {
store.set('config', config);
window.webContents.send('configSaved', config);
} catch {
window.webContents.send('configError', "Can't save config file.");
}
} else {
window.webContents.send('configSaveFail', getConfigErrors());
}
}

function checkConfig(window, config) {
if (!validate(config)) {
window.webContents.send('configFail', getConfigErrors());
} else {
window.webContents.send('configPass');
}
}

function getConfigErrors() {
const errors = [];
validate.errors?.forEach(error => {
if (error.keyword === 'type') {
errors.push(`${error.dataPath} ${error.message}`);
} else {
errors.push(`${error.dataPath === '' ? '.' : error.dataPath} ${error.message}`);
}
});
return errors;
}

module.exports = { readConfig, saveConfig, checkConfig };
4 changes: 3 additions & 1 deletion src/app/config/config.schema.ts → helper/config.schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const configSchema = {
const configSchema = {
definitions: {},
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'http://example.com/root.json',
Expand Down Expand Up @@ -322,3 +322,5 @@ export const configSchema = {
},
},
};

module.exports = configSchema;
2 changes: 1 addition & 1 deletion helper/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function discoverNodes(window) {
id: service.addresses[0] + service.port,
name: service.name,
version: service.txt.version,
url: `http://${service.host.replace(/\.$/, '')}:${service.port}${service.txt.path}api/`,
url: `http://${service.host.replace(/\.$/, '')}:${service.port}${service.txt.path}`,
disable: compareVersions(minimumVersion, service.txt.version) === -1,
});
sendNodes(window);
Expand Down
43 changes: 11 additions & 32 deletions helper/listener.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable import/no-commonjs */
const exec = require('child_process').exec;
const waitPort = require('wait-port');

const sendCustomStyles = require('./styles');
const { downloadUpdate, sendVersionInfo } = require('./update');
const { discoverNodes, stopDiscovery } = require('./discover');
const { readConfig, saveConfig, checkConfig } = require('./config');

function activateScreenSleepListener(ipcMain) {
ipcMain.on('screenControl', (_, screenCommand) => {
exec(screenCommand.command);
});
ipcMain.on('screenControl', (_, screenCommand) => exec(screenCommand.command));
}

function activateReloadListener(ipcMain, window, dev) {
ipcMain.on('reload', () => {
if (dev) {
window.reload();
window.loadURL('http://localhost:4200');
} else {
window.loadURL('app://.');
}
Expand All @@ -31,42 +29,23 @@ function activateAppInfoListener(ipcMain, window, app) {
}

function activateUpdateListener(ipcMain, window) {
ipcMain.on('update', (_, updateInfo) => {
downloadUpdate(updateInfo, window);
});
ipcMain.on('update', (_, updateInfo) => downloadUpdate(updateInfo, window));
}

function activateDiscoverListener(ipcMain, window) {
ipcMain.on('discover', () => {
discoverNodes(window);
});
ipcMain.on('discover', () => discoverNodes(window));

ipcMain.on('stopDiscover', () => {
stopDiscovery();
});
ipcMain.on('stopDiscover', () => stopDiscovery());
}

function activatePortListener(ipcMain, window) {
ipcMain.on('checkOctoPrintPort', (_, hostInfo) => {
const waitPortParams = {
host: hostInfo.host,
port: hostInfo.port,
output: 'silent',
timeout: 60000,
};

waitPort(waitPortParams)
.then(open => {
window.webContents.send('octoprintReady', open);
})
.catch(error => {
window.webContents.send('waitPortError', error);
});
});
function activateConfigListener(ipcMain, window) {
ipcMain.on('readConfig', () => readConfig(window));
ipcMain.on('saveConfig', (_, config) => saveConfig(window, config));
ipcMain.on('checkConfig', (_, config) => checkConfig(window, config));
}

function activateListeners(ipcMain, window, app, dev) {
activatePortListener(ipcMain, window);
activateConfigListener(ipcMain, window);
activateAppInfoListener(ipcMain, window, app);
activateScreenSleepListener(ipcMain);
activateReloadListener(ipcMain, window, dev);
Expand Down
15 changes: 9 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ const Store = require('electron-store');
const args = process.argv.slice(1);
const big = args.some(val => val === '--big');
const dev = args.some(val => val === '--serve');
const scheme = 'app';

const activateListeners = require('./helper/listener');
const createProtocol = require('./helper/protocol');

protocol.registerSchemesAsPrivileged([{ scheme: scheme, privileges: { standard: true } }]);
createProtocol(scheme, path.join(__dirname, 'dist'));
if (!dev) {
const createProtocol = require('./helper/protocol');
const scheme = 'app';

app.commandLine.appendSwitch('touch-events', 'enabled');
protocol.registerSchemesAsPrivileged([{ scheme: scheme, privileges: { standard: true } }]);
createProtocol(scheme, path.join(__dirname, 'dist'));
}

const store = new Store();
app.commandLine.appendSwitch('touch-events', 'enabled');

let window;

function createWindow() {
const _store = new Store();

if (!dev) {
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
callback({
Expand Down
Loading

0 comments on commit f946abc

Please sign in to comment.