-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketController.js
executable file
·175 lines (151 loc) · 4.35 KB
/
socketController.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const fs = require("fs");
// const characteristicPM5 = require('./characteristicPM531');
class SocketController {
constructor() {
this.socket = undefined;
}
setSocket(socket) {
this.socket = socket;
}
emit(event,data){
if(this.socket)
this.socket.emit(event,data);
else
console.log("Socket has not been initialized")
}
}
let socketUnity = [];
let unityUserNames = ["a","b","c","d"];
let unityUserLogFiles = [undefined,undefined,undefined,undefined];
let unityUserOldDistance =[0,0,0,0];
const date = new Date();
//{flags:'w'}
function init(characteristicPM5){
let socketController = new SocketController();
let string
io.on('connection', function(socket){
console.log('a Mackiz connected');
socketController.setSocket(socket);
socket.on('unityUser', function (data){
console.log("unity User Socket connected!");
socketUnity.push(socket);
socket.emit('unityUserConnected',{status:'OK'});
console.log("emitted unityUserConnected");
// socketUnity = socket;
})
socket.on('name', function(data){
// who is the erg from?
/*
let dir = "./data/"+data.name+"/";
console.log(date.getMinutes())
let name = dir+data.name+"_"+date.getHours() +":"+date.getMinutes()+"_"+date.getDate()+"_"+date.getMonth()+"_"+date.getFullYear()+".txt";
if(!fs.existsSync("./data")){
fs.mkdirSync("./data");
}
if(!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
console.log("Got name ",name);
unityUserNames[data.id] = data.name;
unityUserLogFiles[data.id] = fs.createWriteStream(name);
*/
});
socket.on('ergData', function(data){
// console.log("ergData," ,data);
/*
let i = Number(data.i);
if (i == NaN)i=0;
i=0;
characteristicPM5.characteristicPM5ErgCallback(i,data);*/
if(socketUnity.length){
for (var i = 0; i < socketUnity.length; i++) {
console.log(data);
socketUnity[i].emit('ergData',data);
}
// for (var i = 0; i < socketUnity.length; i++) {
// console.log(data);
// socketUnity[i].emit('ergData',data);
// }
/*
// only emit if different
if(unityUserOldDistance[data.i]!=data.distance){
for (var i = 0; i < socketUnity.length; i++) {
console.log(data);
socketUnity[i].emit('ergData',data);
}
// socketUnity.emit('ergData',data);
// console.log(data.i)
if(unityUserLogFiles[data.i]){
// console.log(data);
unityUserLogFiles[data.i].write(JSON.stringify(data));
unityUserLogFiles[data.i].write("\n");
}
}*/
// unityUserOldDistance[data.i]=data.distance;
/*
power: 6,
spm: 22,
distance: 7150.499999999058,
calhr: 320.6496,
calories: 0,
pace: 87.8277952417603,
time: 3972.5
*/
}else{
// console.log("No user");
// console.log(data)
}
// packageErgEntry(data)
// console.log('ergData: ' , data);
// console.log('ergData: ' , data,data.i);
// characteristicPM5.characteristicPM5ErgCallback(data);
// console.log("Send BLE data ",data);
// console.log("ErgData",data);
// if(data.i==0){
// characteristicPM5.characteristicPM5ErgCallback(data.i,data);
// }
/*
switch(data.i){
case 0:
characteristicPM5.characteristicPM5ErgCallback(data);
break;
case 1:
characteristicPM5.characteristicPM51ErgCallback(data);
break;
}// Telcel call for credit *135#
*/
// characteristicPM5.characteristicPM51ErgCallback(data);
socketController.emit('ergData',data);
});
socket.on('strokeData', function(data){
let i = Number(data.i);
console.log("strokeData : ",data);
// if (i == NaN)
// i=0;
// console.log("Clean i : ",i);
// if(socketUnity.length){
// for (var i = 0; i < socketUnity.length; i++) {
// socketUnity[i].emit('strokeData',data);
// }
// // socketUnity.emit('ergData',data);
// // console.log(data);
// }
// console.log('strokeData:', data);
// characteristicPM5.characteristicPM5StrokeCallback(i,data);
socketController.emit('strokeData',data);
});
});
http.listen(8080, function(){
console.log('listening on *:8080');
});
// http.listen(8081, function(){
// console.log('listening on *:8081');
// });
return socketController;
}
module.exports = init;
// export default socketController;