yealink-bridge/index.js
2025-02-14 20:36:59 +01:00

77 lines
No EOL
2.1 KiB
JavaScript

// Require the framework and instantiate it
import axios from 'axios';
import 'dotenv/config'
let yealinkbaseurl = "http://"+process.env.yealink_ip+"/cgi-bin/ConfigManApp.com"
function send_request(params) {
axios.get(yealinkbaseurl, {
auth: {
username: process.env.yealink_username,
password: process.env.yealink_password
},
params: params, // Query parameters
})
}
// ESM
import Fastify from 'fastify'
const fastify = Fastify()
console.log(process.env.mqtt_broker)
import mqtt from 'mqtt';
const client = mqtt.connect({
host: process.env.mqtt_broker,
port: 1883,
username: process.env.mqtt_username,
password: process.env.mqtt_password,
reconnectPeriod: 5000,
});
client.on("connect", () => {
console.log("Connected to broker")
if (process.env.enable_dial) {
client.subscribe(process.env.mqtt_topic+"/dial", (err) => {
});
}
if (process.env.enable_control) {
client.subscribe(process.env.mqtt_topic+"/key", (err) => {
});
}
});
client.on("reconnect", () => {
console.log("Retrying connection")
})
client.on('message', function (topic, message) {
// message is Buffer
switch (topic) {
case process.env.mqtt_topic+"/dial":
console.log("Dialing "+message)
send_request({"number": message, "outgoing_uri": 'URI'})
break
case process.env.mqtt_topic+"/key":
console.log("Pressing "+message)
send_request({"key": message})
break
default:
break
}
})
// Declare a route
fastify.get('/', function (request, reply) {
if (request.ip == process.env.yealink_ip) {
client.publish(process.env.mqtt_topic+"/event/"+request.query.event, JSON.stringify(request.query));
reply.send({ ok: true })
} else {
reply.status(403).send({ ok: false })
}
//client.send(request.)
})
// Run the server!
fastify.listen({ port: 80, host: '0.0.0.0' }, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
// Server is now listening on ${address}
})