-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle-listener.coffee
53 lines (48 loc) · 1.67 KB
/
particle-listener.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Description
# A script that listens for a specific event posted to the particle cloud and logs it into a google spreadsheet. Can be extended to post info to a chat client.
#
# Configuration:
#
# Commands:
#
# Notes:
# Requires a file name es-config.json to be in the base directory (same dir as 'package.json'). See README for how to format file.
#
# Author:
# <john.paul.quicksall@gmail.com>
EventSource = require 'eventsource'
http = require 'http'
QS = require 'querystring'
url = require 'url'
fs = require 'fs'
es_config = {}
data = ""
fs.readFile './es-config.json', (err, contents) ->
if err
console.log "Encountered an error: #{err}"
else
es_config = JSON.parse(contents.toString())
addr = es_config['particle-url']
event_name = es_config['event']
token = es_config['auth-token']
module.exports = (robot) ->
eventSourceInitDict =
rejectUnauthorized: false
headers: 'Authorization': 'Bearer #{token}'
es = new EventSource("#{addr}", eventSourceInitDict)
es.addEventListener "#{event_name}", ((event) ->
#Function code goes here
data = JSON.parse(event.data)
robot.logger.info "This is the data we got...#{data}"
robot.logger.info "This is the event type...#{event.type}"
robot.logger.info "The core ID is -#{data.coreid}- and it's data is -#{data.data}"
robot.http(es_config['google-sheet-url'])
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("Core_ID=#{data.coreid}&Core_Data=#{data.data}") (err, res, body) ->
if err
robot.logger.info "Encountered an error: #{err}"
return
else
robot.logger.info "We got back success!"
return
), false