From 1811d3c58b8a12dd4689aac5f52645675b42360c Mon Sep 17 00:00:00 2001 From: chensivan Date: Tue, 8 Dec 2015 17:50:54 -0500 Subject: [PATCH] create local json to store questionID and responses --- lib/voice-assist.js | 84 +++++++++++++++++++++++++++++++++++++++------ package.json | 1 + 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/lib/voice-assist.js b/lib/voice-assist.js index bdce48d..fe03f4f 100644 --- a/lib/voice-assist.js +++ b/lib/voice-assist.js @@ -1,24 +1,66 @@ var EventEmitter = require('events').EventEmitter; var fse = require('fs-extra'); var fsp = require('fs-plus'); +var fs = require('fs'); +var jsonfile = require('jsonfile'); +var util = require('util'); +var path = require('path'); var RecordingBarView = require('./RecordingBarView'); -var STORAGE_DIRECTORY = __dirname + '/recordings/'; +var STORAGE_DIRECTORY = path.join(__dirname,'recordings'); var CompositeDisposable, Notification, Notifications, ref; ref = require('atom'), Notification = ref.Notification, CompositeDisposable = ref.CompositeDisposable; -// var io = require('socket.io')(3000); -// -// io.sockets.emit('an event sent to all connected clients'); -// io.emit('an event sent to all connected clients'); -// - var client = require('socket.io-client'); var socket = client.connect('http://localhost:3000'); -socket.on('responses', function(msg) { - console.log("client connected"+msg); - atom.notifications.addSuccess("Success: "+msg); +// +//TODO: 2 conditions logic + +// if (this is the first time trigger this package) +// 1) check any updated responses +// 2) set socket on addNotification +// else if (this is not the first time trigger this package) +// 1) listen as always + + +// if (there is a data.json file) +// open file +// load all the question objectsID, and their last response timestamp +// send the object ID and + +var file = path.join(STORAGE_DIRECTORY,'data.json'); + + + +socket.on('addNotification', function(data) { + console.log("client connected: "); + console.log(data); + //read file and see if the q-id is here + + var questionID; + var message; + console.log(" fewafew"); + var obj = new Object(); + fs.stat(file, function(err, stat){ + if (err == null){ + obj = JSON.parse(fs.readFileSync(file, 'utf8')); + obj[uid].response = data.msg; + console.log(" hi fewafew"); + jsonfile.writeFile(file, obj, function (err) { + console.error(err) + }) + atom.notifications.addSuccess("You received one response for this question:\n "+data.id + +"\n the response is: "+data.msg); + + } + }); + + + + // var question = msg[0]; + // var answer = msg[1]; + }); @@ -117,6 +159,28 @@ var cwd; recorder.on('start', function() { uid = guid(); + + var obj = new Object(); + fs.stat(file, function(err, stat){ + console.log(err); + if (err == null){ + obj = JSON.parse(fs.readFileSync(file, 'utf8')); + obj[uid] = true; + jsonfile.writeFile(file, obj, function (err) { + console.error(err) + }) + }else if(err.code === 'ENOENT') { + obj[uid] = true; + console.log("ok"); + jsonfile.writeFile(file, obj, function (err) { + console.error(err) + }) + }else{ + console.log(" some thing wrong"); + } + }) + + cwd = workspaceSnapshot.start(uid); voiceRecorder.start(uid); editorRecorder.start(uid, cwd); diff --git a/package.json b/package.json index a878c46..d43abf8 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "fs-extra": "latest", "fs-plus": "^2.6.0", "jquery": "latest", + "jsonfile": "^2.2.3", "marked": "^0.3.5", "recordrtc": "5.1.0", "request": "latest",