This repository was archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
200 lines (175 loc) · 6.36 KB
/
main.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
196
197
198
199
200
/*
Express Modul eingebunden (stellt für den Webserver statische Dateien bereit )
*/
var express = require("express")
, app = express();
/*
HTTP Modul eingebunden (stellt Webserverdienste bereit, Zur deklaration eines Ports verwendet)
*/
var server = require("http").createServer(app);
/*
Socket Modul eingebunden (ermöglicht Kommunikation zwischen Webinterface und main.js. Informationsübermittlung via Socket Event)
*/
var io = require("socket.io")(server);
/*
MIP Modul eingebunden (SDK beinhaltet Funktionen und Definitionen zur Bluetoothansteuerung des MIP und seiner Bestandteile)
*/
var mip = require("wowweemip")
, mipFinder = new mip.Finder()
, MiPRobot = mip.Robot;
/*
Definitionen
*/
const PORT = 5000; //Der Port wurde als 5000 definiert, kann jedoch geändert werden
const DEBUG = true; //Sollen Zusaetzliche Informationen in der Shellconsole angezeigt werden?
/*
Object: Bot
*/
function Bot(bot) //Funktion zur Abfrage von verfügbaren MIPs
{
this.bot = bot;
this.getBotId = bot._peripheral.id; //Ließt MIP Identitätsnummer aus
this.getBotName = bot.name; //Ließt MIP Name aus
this.isConnected = false; //Setzt Verbindung auf false, MIP nicht verbunden
this.connectErrorMsg = "";
};
/*
Funktion: Funktion zum erstellen eines Arrays, welches die Daten der gefundenen MIPs beinhaltet
*/
function getRobotlist()
{
var returnArray = new Array();
for(var bot of Robots)
{
infoArray = {};
infoArray['id'] = bot.getBotId; //MIP ID
infoArray['name'] = bot.getBotName; //MIP Name
returnArray.push(infoArray);
};
return JSON.stringify(returnArray);
};
/*
Funktion: Funktion zur Abfrage des Verbindungsstatus des MIP
*/
function getConnectStatus(bot)
{
var returnArray = {};
returnArray['success'] = bot.isConnected; //gibt 'success' aus, wenn MIP erfolgreich verbunden
returnArray['msg'] = bot.connectErrorMsg; //gibt 'msg' (Fehlermeldung) aus wenn Verbindung fehlgeschlagen
return JSON.stringify(returnArray);
};
/*
Variabeln
*/
var Robots = [], ConnectedBot;
/*
Konfigurationen
*/
server.listen(PORT);
app.use(express.static(__dirname + '/web'));
/*
Socket (für die Kommunikation zwischen Webinterface und main.js)
*/
io.sockets.on('connection', function(socket) {
// Socket um Farbe zu ändern
socket.on('setLed', function(r, g, b) {
MiPRobot.prototype.setMipChestLedWithColor.call(ConnectedBot.bot, r, g, b, 0x00); //setzt LED standardgemäß auf aus. Farbeinstellung erfolgt über Webinterface
});
// Socket um Sound abzuspielen
socket.on('playSound', function(soundNumber) {
MiPRobot.prototype.playMipSound.call(ConnectedBot.bot, soundNumber, 0); //Ruft je nach Wahl den entsprechenden Sound auf
});
// Socket zum Fahren
socket.on('drive', function(cm) {
MiPRobot.prototype.driveDistanceByCm.call(ConnectedBot.bot, cm, 0); //Fahrdistanz standardgemäß auf 0. Je nach Wahl wird die gewünschte Distanz übergeben und ausgeführt
});
// Socket für Fahrrichtung
socket.on('direction', function(degree) {
MiPRobot.prototype.driveDistanceByCm.call(ConnectedBot.bot, 3, degree); //Fahrtrischtung in Grad. Auch über Webinterface einstellbar.
});
// Socket mit vorhandenen Botnamen
socket.emit('getBotconnections', getRobotlist());
// Socket um mit Bot zu verbinden
socket.on('botConnect', function(id) {
for(var bot of Robots)
{
if(DEBUG)
{
console.log("--> Try to connect to: "+bot.getBotId); //schreibt ins Log, mit welchem MIP sich derzeit versucht wird zu verbinden
};
if(bot.getBotId == id)
{
mipFinder.connect(bot.bot, function(err) {
if (err != null) //Tritt ein Verbindungsfehler auf, wird folgende Anweisung aufgerufen
{
if(DEBUG)
{
console.log("--> Connect failed: "+err); //Konsole gibt aus: "--> Connect failed: " und Fehlercode
};
bot.connectErrorMsg = err;
socket.emit('getConnectStatus', getConnectStatus(bot));
return;
};
if(DEBUG) //War die Verbindung erfolgreich, wird folgende If Anweisung aufgerufen
{
console.log("--> Connect was successful!"); //Konsolenlogeintrag "--> Connect was successful!"
console.log('----------------------------------------------------------------');
};
bot.isConnected = true; //Setzt Verbindungsstatus auf true (Verbunden)
ConnectedBot = bot; //übergibt verbundenen Bot an Variable bot
ConnectedBot.bot.enableBTReceiveDataNotification(true, function(err, data) { //Prüft ob gesendete Daten empfangen wurden
if (err) //Nicht empfangen -> Fehler
{
console.log(err);
return;
};
ConnectedBot.bot.convertMiPResponse(data, function(command, arr)
{
if(DEBUG) //Empfangen -> Gibt aus, was empfangen wurde
{
console.log("--> "+command+": "+arr);
};
var returnArray = {};
returnArray['comand'] = command;
returnArray['value'] = arr;
returnArray['id'] = ConnectedBot.getBotId;
socket.emit('receiveMessage', JSON.stringify(returnArray));
});
});
MiPRobot.prototype.readMipHardwareVersion.call(ConnectedBot.bot);
MiPRobot.prototype.readMipFirmwareVersion.call(ConnectedBot.bot);
socket.emit('getConnectStatus', getConnectStatus(bot));
});
};
};
});
});
//gibt den folgenden Text in Konsole aus
console.log('----------------------------------------------------------------');
console.log('---> Studienprojekt MIP Control');
console.log('----------------------------------------------------------------');
console.log('---> Entwickler: Benjamin Hoheisel');
console.log('---> Entwickler: Lukas Gundermann');
console.log('----------------------------------------------------------------');
console.log('---> Server: http://localhost:'+PORT+'/');
console.log('----------------------------------------------------------------');
//Funktion zum scannen nach verfügbaren Bluetoothverbindungen von MIPs
mipFinder.scan(function(err, robots) {
if (err != null)
{
console.log(err);
return;
};
for (var i in robots)
{
Robots.push(new Bot(robots[i]));
if(DEBUG)
{
console.log('---> Found bot: '+robots[i].name);
};
};
if(DEBUG)
{
console.log('----------------------------------------------------------------');
};
});