diff --git a/server/config.json b/server/config.json new file mode 100644 index 0000000..a87706a --- /dev/null +++ b/server/config.json @@ -0,0 +1,9 @@ +{ + "mqtt": { + "url": "mqtt://192.168.188.11", + "username": "mqtt-user", + "password": "hivi1234hivi", + "port": 1883 + }, + "camUrl": "http://192.168.188.60/capture" +} \ No newline at end of file diff --git a/server/src/index.ts b/server/src/index.ts index 56a7cb1..13eaca9 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -6,16 +6,23 @@ import { connect } from "mqtt"; import { getSync } from '@andreekeberg/imagedata' import axios from 'axios' +if(!fs.existsSync('./config.json')) { + console.log("Config file not found") + process.exit(1) +} + +const config = JSON.parse(fs.readFileSync('./config.json').toString()) as Types.Config + type states = "close" | "open" let state: states = "close"; (async () => { console.log("Starting...") - const client = connect('mqtt://192.168.188.11', { - username: 'mqtt-user', - password: 'hivi1234hivi', - port: 1883, - connectTimeout: 1000, + const client = connect(config.mqtt.url, { + username: config.mqtt.username, + password: config.mqtt.password, + port: config.mqtt.port, + connectTimeout: 5000, manualConnect: true }) @@ -61,7 +68,7 @@ let state: states = "close"; async function detectFace() { process.stdout.write("\n") process.stdout.write("Run : ") - const req = await axios.get("http://192.168.188.60/capture", { + const req = await axios.get(config.camUrl, { responseType: 'arraybuffer' }) // returns a 800x600 jpeg img diff --git a/server/src/types.d.ts b/server/src/types.d.ts index 8c7dd5f..5b70e2e 100644 --- a/server/src/types.d.ts +++ b/server/src/types.d.ts @@ -1 +1,14 @@ -declare module '@andreekeberg/imagedata'; \ No newline at end of file +declare module '@andreekeberg/imagedata'; + + +declare namespace Types { + interface Config { + mqtt: { + url: string; + username: string; + password: string; + port: number; + }; + camUrl: string; + } +} \ No newline at end of file