This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nobdy.js
143 lines (107 loc) · 2.99 KB
/
nobdy.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
function NobdyStream(type) {
this.type = type;
var blah;
this.propName = type.charAt(0).toLowerCase() + type.slice(1);
this[this.propName] = "";
this.val="";
}
NobdyStream.prototype.changed = function (value) { }
NobdyStream.prototype.type = undefined;
NobdyStream.prototype.propName = "";
NobdyStream.prototype.setValue = function(value) {
this[this.propName] = value;
this.changed(value);
}
var nobdy = new Nobdy();
function Nobdy() {
this.websocket = undefined;
}
Nobdy.prototype.connect = function(address) {
var that = this
if(typeof MozWebSocket != "undefined")
this.websocket = new MozWebSocket("ws://" + address);
else this.websocket = new WebSocket("ws://" + address);
this.websocket.onopen = function(evt) {
}
this.websocket.onclose = function(evt) {
simulateData = true;
that.setSupported(["Velocity","EngineRPM","EngineCoolantTemp","ThrottlePosition",
"IntakeAirTemp","EngineLoad","MassAirFlow"]);
that.produceSimulatedData();
}
this.websocket.onmessage = function(evt) {
var nobdy = that;
eval(evt.data);
}
this.websocket.onerror = function(evt) {
}
this.setSupported = function(supportedList) {
for(var i=0;i<supportedList.length; i++) {
this.supported[supportedList[i]] = supportedList[i];
}
if(!this.isConnected) {
this.connected();
this.isConnected = true;
}
}
this.streams = [];
}
Nobdy.prototype.supported = [];
Nobdy.prototype.isConnected = false;
Nobdy.prototype.simulateData = false;
Nobdy.prototype.requestReceived = function(code, value, time) {
for(var i=0;i<this.streams.length;i++) {
if(this.streams[i].type == code) {
this.streams[i].setValue(value);
}
}
}
Nobdy.prototype.produceSimulatedData = function () {
if(!simulateData) return;
if(nobdy.streams.length)
{
for(var i=0;i<nobdy.streams.length;i++) {
var value=0;
if(nobdy.streams[i].type == nobdy.supported.Velocity)
{
value = Math.random()*250;
}
else if(nobdy.streams[i].type == nobdy.supported.EngineRPM)
{
value = Math.random()*10000;
}
else if(nobdy.streams[i].type == nobdy.supported.EngineCoolantTemp)
{
value = Math.random()*180;
}
else if(nobdy.streams[i].type == nobdy.supported.ThrottlePosition)
{
value = Math.random()*100;
}
else if(nobdy.streams[i].type == nobdy.supported.IntakeAirTemp)
{
value = Math.random()*200;
}
else if(nobdy.streams[i].type == nobdy.supported.EngineLoad)
{
value = Math.random()*100;
}
else if(nobdy.streams[i].type == nobdy.supported.MassAirFlow)
{
value = Math.random()*2550;
}
nobdy.streams[i].setValue(value);
}
}
setTimeout(nobdy.produceSimulatedData,1000);
}
Nobdy.prototype.connected = function () { }
Nobdy.prototype.createStream = function(code) {
this.websocket.send("nobdy.addRequest('"+code+"');");
var stream = new NobdyStream(code);
this.streams.push(stream);
return stream;
}
Nobdy.prototype.issueCommand = function(command) {
this.websocket.send("nobdy.issueCommand('"+command+"');");
}