Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mysql = require('mysql');
- var con = mysql.createConnection({
- host: "localhost",
- user: "root",
- password: "prueba1",
- database: "Instrumentacion"
- });
- const actualizar = (tag, val, tipo, chip) => {
- con.query('SELECT * FROM `firebase` WHERE `tag` = ?', [tag], function (err, result, fields) {
- if (err) throw err;
- if (result.lenght > 0) {
- if (tipo && chip){
- con.query('UPDATE firebase SET value = ?, tipo = ?, chip = ?, WHERE tag = ?', [val, tipo, chip, tag])
- }else{
- con.query('UPDATE firebase SET value = ? WHERE tag = ?', [val, tag])
- }
- } else {
- con.query('INSERT INTO firebase SET tag=?, value=?, tipo=?, chip=?', [tag, val, tipo ? tipo : result[0].tipo, chip ? chip : result[0].chip ])
- }
- });
- }
- var osc = require("osc")
- var admin = require("firebase-admin");
- var serviceAccount = require("./serviceAccountKey.json");
- admin.initializeApp({
- credential: admin.credential.cert(serviceAccount),
- databaseURL: "https://maqueta-firebase-simple.firebaseio.com"
- });
- var udpPort = new osc.UDPPort({
- localAddress: "0.0.0.0",
- localPort: 8085,
- metadata: true
- });
- udpPort.on("message", function (oscMsg) {
- console.log("mensaje recibido", oscMsg);
- try {
- if (oscMsg && oscMsg.address) {
- const ref = oscMsg.address.substring(1)
- // let type = ""
- let value = ""
- if (oscMsg.args && oscMsg.args.length > 0) {
- type = oscMsg.args[0].type
- value = oscMsg.args[0].value
- actualizar(ref, value)
- admin.database().ref().child(ref).update({
- value
- });
- }
- }
- } catch (e) {
- console.log("error", e)
- }
- });
- udpPort.open();
- udpPort.on("ready", function () {
- con.connect(function (err) {
- if (err) throw err;
- con.query("SELECT * FROM firebase", function (err, result, fields) {
- if (err) throw err;
- result.map(res => {
- enviarConfiguracion(res.tag, res.chip, res.etiqueta, res.value, res.tipo)
- })
- });
- });
- admin.database().ref().on("value", function (snapshot) {
- snapshot.forEach(function (data) {
- let datos = data.val()
- try {
- enviar(`/${data.key}`, "s", datos.value)
- actualizar(data.key, datos.value, datos.tipo, datos.chip)
- } catch (e) {
- console.log("Error: ", e)
- }
- });
- });
- function enviarConfiguracion(tag, chipid, etiqueta, value, tipo) {
- udpPort.send({
- address: `/${tag}`,
- args: [
- {
- type: 's',
- value: `304 ${tag} ${chipid} ${etiqueta} ${tipo} ${value}`
- }
- ]
- }, "127.0.0.1", 8084);
- }
- function enviar(path, type, value) {
- udpPort.send({
- address: path,
- args: [
- {
- type: type,
- value: value
- }
- ]
- }, "127.0.0.1", 8084);
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement