Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. var mysql = require('mysql');
  2.  
  3. var con = mysql.createConnection({
  4. host: "localhost",
  5. user: "root",
  6. password: "prueba1",
  7. database: "Instrumentacion"
  8. });
  9.  
  10. const actualizar = (tag, val, tipo, chip) => {
  11. con.query('SELECT * FROM `firebase` WHERE `tag` = ?', [tag], function (err, result, fields) {
  12. if (err) throw err;
  13. if (result.lenght > 0) {
  14. if (tipo && chip){
  15. con.query('UPDATE firebase SET value = ?, tipo = ?, chip = ?, WHERE tag = ?', [val, tipo, chip, tag])
  16. }else{
  17. con.query('UPDATE firebase SET value = ? WHERE tag = ?', [val, tag])
  18. }
  19. } else {
  20. con.query('INSERT INTO firebase SET tag=?, value=?, tipo=?, chip=?', [tag, val, tipo ? tipo : result[0].tipo, chip ? chip : result[0].chip ])
  21. }
  22. });
  23. }
  24.  
  25. var osc = require("osc")
  26. var admin = require("firebase-admin");
  27. var serviceAccount = require("./serviceAccountKey.json");
  28. admin.initializeApp({
  29. credential: admin.credential.cert(serviceAccount),
  30. databaseURL: "https://maqueta-firebase-simple.firebaseio.com"
  31. });
  32.  
  33. var udpPort = new osc.UDPPort({
  34. localAddress: "0.0.0.0",
  35. localPort: 8085,
  36. metadata: true
  37. });
  38.  
  39. udpPort.on("message", function (oscMsg) {
  40. console.log("mensaje recibido", oscMsg);
  41. try {
  42. if (oscMsg && oscMsg.address) {
  43. const ref = oscMsg.address.substring(1)
  44. // let type = ""
  45. let value = ""
  46. if (oscMsg.args && oscMsg.args.length > 0) {
  47. type = oscMsg.args[0].type
  48. value = oscMsg.args[0].value
  49. actualizar(ref, value)
  50. admin.database().ref().child(ref).update({
  51. value
  52. });
  53. }
  54. }
  55. } catch (e) {
  56. console.log("error", e)
  57. }
  58. });
  59.  
  60. udpPort.open();
  61.  
  62. udpPort.on("ready", function () {
  63.  
  64. con.connect(function (err) {
  65. if (err) throw err;
  66. con.query("SELECT * FROM firebase", function (err, result, fields) {
  67. if (err) throw err;
  68. result.map(res => {
  69. enviarConfiguracion(res.tag, res.chip, res.etiqueta, res.value, res.tipo)
  70. })
  71. });
  72. });
  73.  
  74.  
  75. admin.database().ref().on("value", function (snapshot) {
  76. snapshot.forEach(function (data) {
  77. let datos = data.val()
  78. try {
  79. enviar(`/${data.key}`, "s", datos.value)
  80. actualizar(data.key, datos.value, datos.tipo, datos.chip)
  81. } catch (e) {
  82. console.log("Error: ", e)
  83. }
  84. });
  85. });
  86.  
  87.  
  88. function enviarConfiguracion(tag, chipid, etiqueta, value, tipo) {
  89. udpPort.send({
  90. address: `/${tag}`,
  91. args: [
  92. {
  93. type: 's',
  94. value: `304 ${tag} ${chipid} ${etiqueta} ${tipo} ${value}`
  95. }
  96. ]
  97. }, "127.0.0.1", 8084);
  98. }
  99.  
  100. function enviar(path, type, value) {
  101. udpPort.send({
  102. address: path,
  103. args: [
  104. {
  105. type: type,
  106. value: value
  107. }
  108. ]
  109. }, "127.0.0.1", 8084);
  110. }
  111. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement