Last active
April 28, 2016 11:59
-
-
Save harrisonhjones/c22c300f6458c54d8c24 to your computer and use it in GitHub Desktop.
NodeJS SparkCore Event Recorder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Includes | |
var EventSource = require('eventsource'); | |
var mysql = require('mysql'); | |
// Variables | |
var deviceID = "***FILL THIS IN***"; | |
var accessToken = "***FILL THIS IN***"; | |
// Mysql Connection | |
var connection = mysql.createConnection({ | |
host : '***FILL THIS IN***', | |
user : '***FILL THIS IN***', | |
password : '***FILL THIS IN***', | |
database : '***FILL THIS IN***' | |
}); | |
connection.connect(function(err) { | |
if (err) { | |
console.error('[MYSQL] error connecting: ' + err.stack); | |
return; | |
} | |
console.log('[MYSQL] connected as id ' + connection.threadId); | |
}); | |
// Sparkcore Connection | |
var url = "https://api.spark.io/v1/devices/" + deviceID + "/events?access_token=" + accessToken; | |
var es = new EventSource(url); | |
console.log("Listening for events"); | |
es.addEventListener('***FILL THIS IN (Event Name)***', function(e){ | |
console.log("[SPARKCORE] SYSTEM EVENT:"); | |
evt = JSON.parse(e.data); | |
console.log(evt); | |
console.log("Name: " + "SYSTEM"); | |
console.log("Data: " + evt['data']); | |
console.log("TTL: " + evt['ttl']); | |
console.log("Published_At: " + evt['published_at']); | |
console.log("Core_ID: " + evt['coreid']); | |
var post = {name: 'SYSTEM',data: evt['data'], ttl: evt['ttl'], published_at: evt['published_at'],core_id: evt['coreid']}; | |
var query = connection.query('INSERT INTO `SparkCoreEvents` SET ?', post, function(err, result) { | |
if(err) | |
console.log("[MYSQL] There was an error inserting the event into the database"); | |
else | |
console.log("[[MYSQL] Row inserted"); | |
}); | |
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- phpMyAdmin SQL Dump | |
-- version 4.1.8 | |
-- http://www.phpmyadmin.net | |
-- | |
-- Host: localhost | |
-- Generation Time: Oct 16, 2014 at 01:01 AM | |
-- Server version: 5.5.32-cll-lve | |
-- PHP Version: 5.4.23 | |
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
SET time_zone = "+00:00"; | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8 */; | |
-- | |
-- Database: `jiavap_dev` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `SparkCoreEvents` | |
-- | |
CREATE TABLE IF NOT EXISTS `SparkCoreEvents` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`name` varchar(25) NOT NULL, | |
`data` text NOT NULL, | |
`ttl` int(11) NOT NULL, | |
`published_at` datetime NOT NULL, | |
`core_id` varchar(25) NOT NULL, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `id` (`id`) | |
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment