diff --git a/src/http.ts b/src/http.ts index ce699fd..4239e35 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1,6 +1,7 @@ import * as http from "http"; import * as fs from "fs"; import * as path from "path"; +import { ISensors } from "./io"; export default class HttpServer { @@ -10,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) { + public constructor(port: number, root: string, getSensors: ()=>ISensors, setPower: (power: boolean) => void) { this.port = port; this.root = root; this.httpServer = http.createServer((req, res) => { @@ -25,8 +26,8 @@ export default class HttpServer { switch (req.method) { case "GET": switch (api) { - case "list": - // body = JSON.stringify(getChannels()); + case "sensors": + body = JSON.stringify(getSensors()); status = 200; break; case "signal": @@ -39,7 +40,7 @@ export default class HttpServer { break; case "PUT": switch (api) { - case "tune": + case "power": const channel = decodeURIComponent(query[3]); const adapter = parseInt(url.searchParams.get('adapter')); // tune(channel, adapter); diff --git a/src/io.ts b/src/io.ts new file mode 100644 index 0000000..e605905 --- /dev/null +++ b/src/io.ts @@ -0,0 +1,50 @@ +import Serial from "./serial"; +import * as onoff from 'onoff' + +const ON = onoff.Gpio.HIGH; +const OFF = onoff.Gpio.LOW; + +export interface ISensors { + temperature: number; + power: boolean; + moisture: number; + humidity: number +} + +export default class IO { + private temp: number + private serial: Serial; + // private power: boolean; + private gpio: onoff.Gpio + + public constructor(POWER_GPIO = 17) { + this.serial = new Serial(); + this.gpio = new onoff.Gpio(POWER_GPIO, 'out'); + } + + public getPower() { + return this.gpio.readSync() + } + + public setPower(power: boolean) { + this.gpio.writeSync(power ? ON : OFF); + } + + public togglePower() { + const cur: onoff.BinaryValue = this.getPower() ; + this.gpio.writeSync(cur ? OFF : ON); + } + + public getSensors (): ISensors { + return { + power: this.getPower() ? true : false, + moisture: 0, + temperature: 0, + humidity: 0 + } + } + + public getMoisture() { + return this.serial.getMoisture() + } +} \ No newline at end of file diff --git a/src/serial.ts b/src/serial.ts new file mode 100644 index 0000000..e550bb4 --- /dev/null +++ b/src/serial.ts @@ -0,0 +1,34 @@ +import * as serialport from 'serialport'; + +// interface ISerial { +// moisture: number; +// parser: serialport.ReadlineParser +// } + +export default class Serial { + private moisture: number; + + public constructor(path = '/dev/ttyUSB0', baudRate = 9600, delimiter = '\n') { + const port = new serialport.SerialPort({ path, baudRate }); + const readline = new serialport.ReadlineParser({ delimiter }); + const parser = port.pipe(readline); + parser.on('data', line => { + const moisture = parseInt(line.trim(), 10); + if (!isNaN(moisture)) { + this.moisture = moisture + } + }); + port.on('error', err => { + console.error('Serial Error:', err.message); + }); + } + public getMoisture() { + return this.moisture; + } + + + + + + +} \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index 18705c7..4ef217a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,4 +1,5 @@ import HttpServer from './http'; +import IO from './io'; import VideoSocket from './ws'; const HTTP_PORT = process.env.HTTP_PORT ? parseInt(process.env.HTTP_PORT, 10) : 8080; @@ -24,26 +25,44 @@ const TV_DEV_0 = process.env.TV_DEV_0 ?? '/dev/video0' // const getSignal = (adapter: number) => // zap.getSignal(adapter) -const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT); +const io = new IO(); + +const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT, io.getSensors, io.setPower); const videoSocket = new VideoSocket(WS_PORT, TV_DEV_0); + + + httpServer.start(); process.stdin.setEncoding("utf8"); process.stdin.resume(); -console.log("Menu:"); +console.log("Menu:\n1) Power off\n2)Power on\n3) Power flop\n4)Read moisture"); process.stdin.on("data", async (data: string) => { const input = data.trim(); console.log(`Received: "${input}"`); - // await zap.zapTo(input).then((zap: IZap) => { - // console.log(`Tuned ${zap.adapter} to ${zap.channel}`) + const val = parseInt(input); + switch(val) { + case 0: - // }).catch((err: Error) => { - // console.error(err.message); - // }); + break; + case 1: + io.setPower(false); + break; + case 2: + io.setPower(true); + break; + case 3: + io.setPower(io.getPower()? true:false) + break; + case 4: + console.log(io.getMoisture()); + default: + console.log("No option for "+input) + } }); diff --git a/src/static/index.html b/src/static/index.html index 215da3a..7ceffb1 100644 --- a/src/static/index.html +++ b/src/static/index.html @@ -18,12 +18,14 @@
- + + +