Add Settings and Devices classes TypeScript source.

This commit is contained in:
Matt McWilliams 2024-05-22 23:37:21 -04:00
parent 8e8fac92d1
commit 7f8a9d4289
3 changed files with 76 additions and 19 deletions

46
scad/cable_protector.scad Normal file
View File

@ -0,0 +1,46 @@
use <./common/common.scad>;
module m3_bolt(pos = [0, 0, 0], rot = [0, 0, 0], cap = 10, bolt = 20) {
$fn = 40;
translate(pos) rotate(rot) {
translate([0, 0, (cap / 2) - 0.1]) cylinder(r = R(6), h = cap, center = true);
translate([0, 0, -bolt / 2]) cylinder(r = R(3), h = bolt, center = true);
}
}
module tube(pos = [0, 0, 0], rot = [0, 0, 0]) {
translate(pos) rotate(rot) {
translate([0, 0, 2.501]) cylinder(r = R(7), h = 5, center = true, $fn = 40);
}
}
module cable_protector_body(pos = [0, 0, 0], rot = [0, 0, 0]) {
translate(pos) rotate(rot) {
difference () {
union () {
minkowski() {
cylinder(r = R(18), h = 35, center = true, $fn = 80);
sphere(r = R(6), $fn = 20);
}
tube([0, 9, 0], [0, 90, 0]);
tube([0, -9, 0], [0, -90, 0]);
}
cylinder(r = R(7), h = 50, center = true, $fn = 80);
cylinder(r = R(16), h = 33, center = true, $fn = 80);
m3_bolt([5, 9, 0], [0, 90, 0]);
m3_bolt([-5, -9, 0], [0, -90, 0]);
}
}
}
module cable_protector (top = true) {
difference () {
cable_protector_body();
if (top) {
translate([25, 0, 0]) cube([50, 50, 50], center = true);
} else {
translate([-25, 0, 0]) cube([50, 50, 50], center = true);
}
}
}
cable_protector(top = false);

View File

@ -3,6 +3,7 @@
import { delay } from 'delay';
import { Log } from 'log';
import type { Logger } from 'winston';
import type { Settings } from '../settings';
/**
* class representing the device discovery features
@ -12,7 +13,7 @@ import type { Logger } from 'winston';
class Devices {
public settings : any;
public settings : Settings;
public connected : any = {}
private arduino : Arduino;
@ -26,7 +27,7 @@ class Devices {
* Constructor assigns arduino, settings, UI browser window and cam objects
* locally to this class for reference.
**/
constructor (arduino : Arduino, settings : any, mainWindow : any, cam : any) {
constructor (arduino : Arduino, settings : Settings, mainWindow : any, cam : any) {
this.arduino = arduino;
this.settings = settings;
this.mainWindow = mainWindow;

View File

@ -1,11 +1,12 @@
'use strict'
import os = require('os');
import path = require('path');
import fs = require('fs-extra');
import { homedir } from 'os';
import { join } from 'path';
import { mkdir, writeFile, readFile, unlink, access } from 'fs/promises';
import type { Stats } from 'fs';
class Settings {
private file : string= path.join(os.homedir(), `/.mcopy/settings.json`);
export class Settings {
private file : string = join(homedir(), `/.mcopy/settings.json`);
private defaultState : any = {
server : {
port : 1111,
@ -27,6 +28,15 @@ class Settings {
this.state = this.freshState();
}
private async exists (path : string) : Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}
private freshState () {
return JSON.parse(JSON.stringify(this.defaultState));
}
@ -34,11 +44,11 @@ class Settings {
*
**/
private async checkDir () {
const dir : string = path.join(os.homedir(), '.mcopy/');
const exists : boolean = await fs.exists(dir)
const dir : string = join(homedir(), '.mcopy/');
const exists : boolean = await this.exists(dir)
if (!exists) {
try {
await fs.mkdir(dir);
await mkdir(dir);
} catch (err) {
if (err.code === 'EEXIST') return true
console.error(err);
@ -50,10 +60,10 @@ class Settings {
*
**/
public async save () {
const str = JSON.stringify(this.state, null, '\t');
const str : string = JSON.stringify(this.state, null, '\t');
this.checkDir();
try {
await fs.writeFile(this.file, str, 'utf8');
await writeFile(this.file, str, 'utf8');
} catch (err) {
console.error(err);
}
@ -80,14 +90,14 @@ class Settings {
*
**/
public async restore () {
let exists;
let str;
let exists : boolean = false;
let str : string;
this.checkDir();
exists = await fs.exists(this.file);
exists = await this.exists(this.file);
if (exists) {
str = await fs.readFile(this.file, 'utf8');
str = await readFile(this.file, 'utf8');
this.state = JSON.parse(str);
//console.dir(this.state)
} else {
@ -98,10 +108,10 @@ class Settings {
*
**/
public async reset () {
const exists = await fs.exists(this.file);
const exists : boolean = await this.exists(this.file);
if (exists) {
try {
await fs.unlink(this.file);
await unlink(this.file);
} catch (err) {
console.error(err);
}
@ -111,4 +121,4 @@ class Settings {
};
}
module.exports = new Settings()
module.exports = new Settings();