-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
195 lines (161 loc) · 4.7 KB
/
index.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var WebSocketServer = require("ws").Server
var http = require("http")
var express = require("express")
var fs = require("fs");
var app = express()
var clientSock;
var makePwm;
var debugLevel = 1;
function log(params){
if(!debugLevel)return;
var str="";
for (var i = 0; i < arguments.length; i++)str+=arguments[i];
if(clientSock)
clientSock.send(str);
console.log(str);
}
var i2c;
var sensorLink;
try{
i2c = require("i2c");
sensorLink = new i2c(0x08, {device: '/dev/i2c-1'});
sensorLink.on('data', function(data) {
log("Got data from sensor:",JSON.stringify(data));
});
}
catch(err){
log("Couldn't load i2c library.")
sensorLink=function(){return {writeBytes:function(){},readBytes:function(){}}}
}
try{
makePwm = require( "adafruit-pca9685" );
}
catch(err){
log("Couldn't load pwm library.")
makePwm=function(){return {stop:function(){},setPwm:function(){}}}
}
var correctionFactor;//=1.118;
var pwm = makePwm({"freq": 60});//, "correctionFactor": 1.118});
function parseConfig(cfg){
log(cfg)
if(cfg.freq||cfg.correctionFactor){
if(pwm)
pwm.stop();
pwm = makePwm({"freq": cfg.freq?cfg.freq:60, "correctionFactor":cfg.correctionFactor?cfg.correctionFactor:0});
}
log("Parsed config.");
}
parseConfig({servoMin:200,servoMax:700,freq:60,correctionFactor:undefined});
//var pwm = makePwm({"freq": 160, "correctionFactor": 1.118});
// pwm.setPwm(ch, 0, v);
// pwm.setPulse(ch, v);
// pwm.stop();
//pwm.setFrequency(60);
//pwm.setPwm(0, 0, servoMid);
//pwm.setPulse(channel, pulse);
pwm.stop();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
process.argv.forEach(function (val, index, array) {
log(index + ': ' + val);
});
var serveRoot = "";
var servePort;
var args = process.argv;
if(args.length>2){
serveRoot = args[2];
}
if(args.length>3){
servePort = args[3];
}
var port = process.env.PORT || (servePort?servePort:80)
//var webroot = __dirname;
var webroot = __dirname + '/' + serveRoot;
app.use(express.static(webroot))
var server = http.createServer(app)
server.listen(port)
log("http serving:"+webroot+" on port %d", port)
//var playerIdBase=0;
var connections = [];
var wss = new WebSocketServer({server: server})
log("websocket server created")
function strToAsciiArray(str){var arr=[]; for(var i=0;i<str.length;i++)arr.push(str.charCodeAt(i));return arr;}
wss.on("connection", function(ws) {
clientSock = ws;
//ws.playerId = playerIdBase++;
log("websocket connection opened..");//,ws.playerId)
//GameServer.processEvent({cmd:'connected',obj:ws.playerId},ws); //Broadcast to all
ws.on("close", function() {
clientSock = undefined;
//GameServer.processEvent({cmd:'disconnected',obj:ws.playerId},ws);
log("websocket connection closed:",ws.playerId)
})
ws.on("message",function(msg){
try{
var data = JSON.parse(msg);
//log("Got message from:",ws.playerId," : ",msg)
//data.obj = ws.playerId; //slam playerID
//GameServer.processEvent(data,ws);
if(data.config!=undefined){
parseConfig(data.config);
}
if(data.bones!=undefined){
var sstr ="";
for(var i=0;i<data.bones.length;i++){
var bone = data.bones[i];
var sval = bone.v|0;
sstr+="c:"+bone.c+" v:"+sval;
pwm.setPwm(bone.c, 0, sval);
}
log(sstr);
}
if(data.stop){
pwm.stop();
}
if(data.restartServer)
setTimeout(function(){
//Cause the server to restart....
log("Restarting server....");
process.exit(0);
},100);
if(data.killServer)
setTimeout(function(){
//Cause the server to restart....
log("Killing server....");
process.exit(-1);
},100);
if(data.burnToBot){
var stream = fs.createWriteStream(data.burnToBot.path);
stream.once('open', function(fd) {
stream.write(data.burnToBot.data);
stream.end();
});
}
if(data.sensor){
if(data.sensor.send){
if(typeof data.sensor.data === 'string'){//Speak
sensorLink.writeBytes(data.sensor.cmd,strToAsciiArray(data.sensor.data),function err(e,d){
//log("SensorLink write callback:"+JSON.stringify(e)+":"+JSON.stringify(d));
});
}else{
sensorLink.writeBytes(data.sensor.cmd,data.sensor.data,function err(e,d){
//log("SensorLink write callback:"+JSON.stringify(e)+":"+JSON.stringify(d));
});
}
}else if(data.sensor.request){
sensorLink.readBytes(8, 4, function(err, res) {
// result contains a buffer of bytes
//log("SensorLink read callback!"+JSON.stringify(err)+":"+JSON.stringify(res));
ws.send(JSON.stringify({rsp:res}));
});
}
}
}
catch(e){
log("Got malformed data from client:",ws.playerId,JSON.stringify(msg));
}
});
})