Dev prototype #1
@ -1,7 +1,7 @@
|
||||
import * as http from "http";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import { ISensors } from "./io";
|
||||
import IO, { ISensors } from "./io";
|
||||
|
||||
|
||||
export default class HttpServer {
|
||||
@ -11,7 +11,7 @@ export default class HttpServer {
|
||||
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, getSensors: ()=>ISensors, setPower: (power: boolean) => void) {
|
||||
public constructor(port: number, root: string, io: IO) {
|
||||
this.port = port;
|
||||
this.root = root;
|
||||
this.httpServer = http.createServer((req, res) => {
|
||||
@ -27,7 +27,7 @@ export default class HttpServer {
|
||||
case "GET":
|
||||
switch (api) {
|
||||
case "sensors":
|
||||
body = JSON.stringify(getSensors());
|
||||
body = JSON.stringify(io.getSensors());
|
||||
status = 200;
|
||||
break;
|
||||
case "signal":
|
||||
|
71
src/io.ts
71
src/io.ts
@ -1,50 +1,71 @@
|
||||
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 {
|
||||
temperature: number;
|
||||
power: boolean;
|
||||
moisture: number;
|
||||
humidity: number
|
||||
humidity: number;
|
||||
}
|
||||
|
||||
export default class IO {
|
||||
private temp: number
|
||||
private serial: Serial;
|
||||
// private power: boolean;
|
||||
private gpio: onoff.Gpio
|
||||
|
||||
export default class IO implements ISensors {
|
||||
temperature: number;
|
||||
moisture: number;
|
||||
humidity: number;
|
||||
gpio: pigpio.Gpio
|
||||
serial: Serial;
|
||||
power: boolean;
|
||||
public constructor(POWER_GPIO = 17) {
|
||||
this.serial = new Serial();
|
||||
this.gpio = new onoff.Gpio(POWER_GPIO, 'out');
|
||||
const serial = new Serial()
|
||||
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() {
|
||||
return this.gpio.readSync()
|
||||
getMoisture() {
|
||||
return
|
||||
}
|
||||
|
||||
// getPower() {
|
||||
// // Read the current state of the GPIO pin
|
||||
// return this.gpio.digitalRead() === ON;
|
||||
// }
|
||||
|
||||
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() {
|
||||
const cur: onoff.BinaryValue = this.getPower() ;
|
||||
this.gpio.writeSync(cur ? OFF : ON);
|
||||
// Toggle the power state (turn the pin on or off)
|
||||
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 {
|
||||
power: this.getPower() ? true : false,
|
||||
power: this.power,
|
||||
moisture: 0,
|
||||
temperature: 0,
|
||||
humidity: 0
|
||||
temperature: this.temperature,
|
||||
humidity: this.humidity
|
||||
}
|
||||
}
|
||||
|
||||
public getMoisture() {
|
||||
return this.serial.getMoisture()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,13 +26,16 @@ const TV_DEV_0 = process.env.TV_DEV_0 ?? '/dev/video0'
|
||||
// zap.getSignal(adapter)
|
||||
|
||||
const io = new IO();
|
||||
|
||||
const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT, io.getSensors, io.setPower);
|
||||
// const getSensors = io.getSensors.bind(this);
|
||||
// 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);
|
||||
|
||||
|
||||
|
||||
|
||||
httpServer.start();
|
||||
|
||||
|
||||
@ -50,16 +53,17 @@ process.stdin.on("data", async (data: string) => {
|
||||
|
||||
break;
|
||||
case 1:
|
||||
io.setPower(false);
|
||||
io.setPower(false);// console.log(io.getMoisture());
|
||||
break;
|
||||
case 2:
|
||||
io.setPower(true);
|
||||
break;
|
||||
case 3:
|
||||
io.setPower(io.getPower()? true:false)
|
||||
io.togglePower();
|
||||
break;
|
||||
case 4:
|
||||
console.log(io.getMoisture());
|
||||
io.getSensors()
|
||||
break;
|
||||
default:
|
||||
console.log("No option for "+input)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user