power flipping

This commit is contained in:
David Westgate 2025-04-17 23:08:17 +01:00
parent e6b72c597a
commit fb5301c9fe
3 changed files with 61 additions and 36 deletions

View File

@ -1,7 +1,7 @@
import * as http from "http"; import * as http from "http";
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import { ISensors } from "./io"; import IO, { ISensors } from "./io";
export default class HttpServer { export default class HttpServer {
@ -11,7 +11,7 @@ export default class HttpServer {
private root: string; private root: string;
// public constructor(port: number, root: string, tune: (ch: string, adp?: number) => void, getChannels: ()=>string[], getSignal: (adapter:number)=>object) { // public constructor(port: number, root: string, tune: (ch: string, adp?: number) => void, getChannels: ()=>string[], getSignal: (adapter:number)=>object) {
public constructor(port: number, root: string, getSensors: ()=>ISensors, setPower: (power: boolean) => void) { public constructor(port: number, root: string, io: IO) {
this.port = port; this.port = port;
this.root = root; this.root = root;
this.httpServer = http.createServer((req, res) => { this.httpServer = http.createServer((req, res) => {
@ -27,7 +27,7 @@ export default class HttpServer {
case "GET": case "GET":
switch (api) { switch (api) {
case "sensors": case "sensors":
body = JSON.stringify(getSensors()); body = JSON.stringify(io.getSensors());
status = 200; status = 200;
break; break;
case "signal": case "signal":

View File

@ -1,50 +1,71 @@
import Serial from "./serial"; import Serial from "./serial";
import * as onoff from 'onoff' import * as pigpio from 'pigpio';
type ONOFF = 0 | 1;
const ON: ONOFF = 0
const OFF: ONOFF = 1
const ON = onoff.Gpio.HIGH;
const OFF = onoff.Gpio.LOW;
export interface ISensors { export interface ISensors {
temperature: number; temperature: number;
power: boolean; power: boolean;
moisture: number; moisture: number;
humidity: number humidity: number;
} }
export default class IO { export default class IO implements ISensors {
private temp: number temperature: number;
private serial: Serial; moisture: number;
// private power: boolean; humidity: number;
private gpio: onoff.Gpio gpio: pigpio.Gpio
serial: Serial;
power: boolean;
public constructor(POWER_GPIO = 17) { public constructor(POWER_GPIO = 17) {
this.serial = new Serial(); const serial = new Serial()
this.gpio = new onoff.Gpio(POWER_GPIO, 'out'); serial.getMoisture.bind(this);
this.serial = serial;
// Initialize pigpio and the GPIO pin
const read = new pigpio.Gpio(POWER_GPIO, {mode: pigpio.Gpio.INPUT});
this.power = read.digitalRead() == ON ? true : false
this.gpio = new pigpio.Gpio(POWER_GPIO, { mode: pigpio.Gpio.OUTPUT });
this.humidity = 0
this.temperature = 0;
// this.serial.getMoisture.bind(this)
} }
public getPower() { getMoisture() {
return this.gpio.readSync() return
} }
// getPower() {
// // Read the current state of the GPIO pin
// return this.gpio.digitalRead() === ON;
// }
public setPower(power: boolean) { public setPower(power: boolean) {
this.gpio.writeSync(power ? ON : OFF); // Set the GPIO pin to high (1) or low (0)
this.power = power;
this.gpio.digitalWrite(this.power ? ON : OFF);
} }
public togglePower() { public togglePower() {
const cur: onoff.BinaryValue = this.getPower() ; // Toggle the power state (turn the pin on or off)
this.gpio.writeSync(cur ? OFF : ON); console.log("toggle")
this.power = !this.power;
this.gpio.digitalWrite(this.power ? ON : OFF);
} }
public getSensors(): ISensors { public getSensors(): ISensors {
console.log("serial ", this.serial)
return { return {
power: this.getPower() ? true : false, power: this.power,
moisture: 0, moisture: 0,
temperature: 0, temperature: this.temperature,
humidity: 0 humidity: this.humidity
} }
} }
public getMoisture() {
return this.serial.getMoisture()
}
} }

View File

@ -26,13 +26,16 @@ const TV_DEV_0 = process.env.TV_DEV_0 ?? '/dev/video0'
// zap.getSignal(adapter) // zap.getSignal(adapter)
const io = new IO(); const io = new IO();
// const getSensors = io.getSensors.bind(this);
const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT, io.getSensors, io.setPower); // setTimeout(()=>{
// console.log("sen ",io.getSensors())
// console.log("sen2 ",getSensors())
// },200)
// const setPower = io.setPower.bind(this, true)
// const togglePower = io.togglePower.bind(this)
const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT, io);
const videoSocket = new VideoSocket(WS_PORT, TV_DEV_0); const videoSocket = new VideoSocket(WS_PORT, TV_DEV_0);
httpServer.start(); httpServer.start();
@ -50,16 +53,17 @@ process.stdin.on("data", async (data: string) => {
break; break;
case 1: case 1:
io.setPower(false); io.setPower(false);// console.log(io.getMoisture());
break; break;
case 2: case 2:
io.setPower(true); io.setPower(true);
break; break;
case 3: case 3:
io.setPower(io.getPower()? true:false) io.togglePower();
break; break;
case 4: case 4:
console.log(io.getMoisture()); io.getSensors()
break;
default: default:
console.log("No option for "+input) console.log("No option for "+input)
} }