Co-authored-by: David Westgate <david@dwestgate.us> Reviewed-on: https://git.dwestgate.us/david/grow/pulls/1
96 lines
2.6 KiB
TypeScript
96 lines
2.6 KiB
TypeScript
import HttpServer from './http';
|
|
import IO, { ISensors } from './io';
|
|
import VideoSocket from './ws';
|
|
import * as programs from './programs.json'
|
|
|
|
const HTTP_PORT = process.env.HTTP_PORT ? parseInt(process.env.HTTP_PORT, 10) : 8080;
|
|
const WS_PORT = process.env.WS_PORT ? parseInt(process.env.WS_PORT, 10) : 3003;
|
|
const STATIC_ROOT = process.cwd() + "/dist/static";
|
|
const TV_DEV_0 = process.env.TV_DEV_0 ?? '/dev/video0'
|
|
const GPIO_LIGHTS = parseInt(process.env.GPIO_LIGHTS) ?? 17;
|
|
const GPIO_HEAT = parseInt(process.env.GPIO_HEAT) ?? 22;
|
|
const START_HOUR = parseInt(process.env.START_HOUR) ?? 8;
|
|
|
|
interface IProgram {
|
|
name:string,
|
|
daylightHours: number,
|
|
soilMoisture: number
|
|
}
|
|
|
|
const io = new IO();
|
|
const getSensors: () => ISensors = io.getSensors.bind(io);
|
|
|
|
const test = (device: string) => {
|
|
const gpio = device == "lights" ? GPIO_LIGHTS : device == "heat" ? GPIO_HEAT : NaN
|
|
if (!isNaN(gpio)) {
|
|
const state = io[device];
|
|
io.setPower(!state, gpio)
|
|
setTimeout(() => {
|
|
io.setPower(state, gpio)
|
|
}, 4000)
|
|
}
|
|
|
|
}
|
|
|
|
const runProgram = async (ID = 0) =>{
|
|
let state = false;
|
|
const program: IProgram = programs[ID];
|
|
const {daylightHours, soilMoisture} = program;
|
|
const now = new Date();
|
|
const startTime = new Date();
|
|
startTime.setHours(START_HOUR, 0, 0, 0);
|
|
const endTime = new Date(startTime.getTime());
|
|
endTime.setHours(startTime.getHours() + daylightHours);
|
|
if(now >= startTime && now <= endTime){
|
|
state = true;
|
|
}
|
|
io.setPower(state,GPIO_LIGHTS);
|
|
io.setPower(state,GPIO_HEAT);
|
|
}
|
|
runProgram();
|
|
|
|
const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT, test);
|
|
const videoSocket = new VideoSocket(WS_PORT, TV_DEV_0, getSensors);
|
|
|
|
httpServer.start();
|
|
|
|
|
|
process.stdin.setEncoding("utf8");
|
|
process.stdin.resume();
|
|
|
|
console.log("Menu:\n1)Lights Power off\n2) Lights Power on\n3)Heat Power Off\n4) Heat power on\n5)Read Sensors");
|
|
|
|
process.stdin.on("data", async (data: string) => {
|
|
const input = data.trim();
|
|
console.log(`Received: "${input}"`);
|
|
const val = parseInt(input);
|
|
switch (val) {
|
|
case 0:
|
|
|
|
break;
|
|
case 1:
|
|
io.setPower(false, GPIO_LIGHTS);
|
|
break;
|
|
case 2:
|
|
io.setPower(true, GPIO_LIGHTS);
|
|
break;
|
|
case 3:
|
|
io.setPower(false, GPIO_HEAT);
|
|
break;
|
|
case 4:
|
|
io.setPower(true, GPIO_HEAT);
|
|
break;
|
|
case 5:
|
|
console.log(getSensors())
|
|
break;
|
|
default:
|
|
console.log("No option for " + input)
|
|
}
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|