-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
153 lines (149 loc) · 5.28 KB
/
controller.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"use strict"
var _ = require('lodash');
var Q = require('q');
var Config = require('./config')
var getlogs=function(date){
var deferred = Q.defer();
var logs = [];
// fs.createReadStream('data/2016-07-27/part-00000')
fs.createReadStream('data/'+date+'/part-00000')
.pipe(split())
.on('data', function (line) {
if(line.length>5){
var d=line.replace(/\]/, '').replace(/\[/, '').split(',');
var user=d.shift()
var hashtags=d.shift()
var date=d.pop()
var location=d.join(',')
hashtags && hashtags.split(" ").forEach(h=>{
if(h.length>3)
logs.push({
hashtag: h,
date: new Date(date)
});
});
}else{
Log(line)
}
})
.on('end', ()=>{
deferred.resolve(logs)
})
.on('finish', ()=>{
Log("finish")
})
.on("error", err=>{
deferred.reject(new Error(err));
});
return deferred.promise
}
class Controller{
constructor(Network){
this.plays=[]
this.playingTweet = false
this.playingRegular = false
this.regularOption={
duration: 1000,
sequential: false,
sequential_delta: 10,
randomize: false,
randomize_range: 400,
drift: false,
drift_delta: 10,
msg:"255,0,0,1" //rgbRelay
}
this.Network = Network
this.duration = 100
this.heartMonitorId = 'all'
}
day_to_id(i_day, i_hashtag){
return i_day*Config.HashtagPerDay + i_hashtag
}
id_to_day(id){
var i_day = Math.floor(id/Config.HashtagPerDay)
var i_hashtag = id - i_day*Config.HashtagPerDay
return {i_day: i_day, i_hashtag: i_hashtag}
}
heart_beat_received(id, ip){
// Log(id, ip);
if(this.plays[id]!==undefined){
this.plays[id].add(ip)
}
}
init(){
return _.range(Config.NumDays).reduce((soFar, i_day)=>{
return soFar.then(()=>{return getlogs(Hashtags[i_day].date)
.then(logs=>{
global.L = logs;
Log(logs.length, "logs loaded")
_.range(Config.HashtagPerDay).forEach(i_hashtag=>{
var date = Hashtags[i_day].date
var hashtag = Hashtags[i_day].hashtags[i_hashtag].hashtag
var stream = _.map(logs.filter(l=>{return l.hashtag == hashtag}), 'date')
this.plays.push( new PlayStream(date, hashtag, stream, this.Network))
})
})
})
}, Q([]))
}
tick(id){
if(this.heartMonitorId=='all' || this.heartMonitorId == id)
this.plays.forEach(play=>{play.tick()})
}
startTweet(duration){
if(duration !== undefined) this.duration = duration
Log("start playing tweets at duration:", this.duration);
this.plays.forEach(play=>{play.startTweet()})
this.playingTweet = true
this.renderTweet()
}
stopTweet(){
this.playingTweet = false
}
renderTweet(){
if(this.playingTweet){
setTimeout(()=>{this.renderTweet()}, this.duration)
this.plays.forEach(play=>{play.renderTweet()})
}
}
startRegular(option){
if(option!==undefined){
this.regularOption.duration = option.duration;
this.regularOption.sequential = option.sequential;
this.regularOption.sequential_delta = option.sequential_delta;
this.regularOption.randomize = option.randomize;
this.regularOption.randomize_range = option.randomize_range;
this.regularOption.drift = option.drift;
this.regularOption.drift_delta = option.drift_delta;
this.regularOption.msg = option.msg;
}
this.playingRegular = true
this.renderRegular()
}
updateRegular(option){
if(option!==undefined){
this.regularOption.duration = option.duration;
this.regularOption.sequential = option.sequential;
this.regularOption.sequential_delta = option.sequential_delta;
this.regularOption.randomize = option.randomize;
this.regularOption.randomize_range = option.randomize_range;
this.regularOption.drift = option.drift;
this.regularOption.drift_delta = option.drift_delta;
this.regularOption.msg = option.msg;
}
}
stopRegular(){
this.playingRegular = false
this.plays.forEach(play=>{play.stopRegular()})
}
renderRegular(){
if(this.playingRegular){
setTimeout(()=>{this.renderRegular()}, this.regularOption.duration)
this.plays.forEach((play, idx)=>{play.renderRegular(this.regularOption, idx)})
if(this.regularOption.drift){
this.regularOption.sequential_delta += this.regularOption.drift_delta
}
}
}
}
module.exports=Controller;