initial add
[profile/ivi/GhostCluster.git] / nobdy.js
1 function NobdyStream(type) {
2         this.type = type;
3
4         this.propName = type.charAt(0).toLowerCase() + type.slice(1);
5
6         this[this.propName] = "";
7         this.val="";
8 }
9
10 NobdyStream.prototype.changed = function (value) { }
11 NobdyStream.prototype.type = undefined;
12 NobdyStream.prototype.propName = "";
13 NobdyStream.prototype.setValue = function(value) {
14         this[this.propName] = value;
15         this.changed(value);
16 }
17
18 var nobdy = new Nobdy();
19
20 function Nobdy() {
21         this.websocket = undefined;
22 }
23
24 Nobdy.prototype.connect = function(address) {
25
26         var that = this
27
28     if(typeof MozWebSocket != "undefined")
29                 this.websocket = new MozWebSocket("ws://" + address);
30         else this.websocket = new WebSocket("ws://" + address);
31
32         this.websocket.onopen = function(evt) {
33
34     }
35
36         this.websocket.onclose = function(evt) {
37                 simulateData = true;
38                 that.setSupported(["Velocity","EngineRPM","EngineCoolantTemp","ThrottlePosition",
39                                                    "IntakeAirTemp","EngineLoad","MassAirFlow"]);
40                 that.produceSimulatedData();
41     }
42
43         this.websocket.onmessage = function(evt) {
44                 var nobdy = that;
45         eval(evt.data);
46     }
47
48         this.websocket.onerror = function(evt) {
49
50     }
51
52         this.setSupported = function(supportedList) {
53                 for(var i=0;i<supportedList.length; i++) {
54                         this.supported[supportedList[i]] = supportedList[i];
55                 }
56                 if(!this.isConnected) {
57                         this.connected();
58                         this.isConnected = true;
59                 }
60         }
61
62         this.streams = [];
63 }
64
65 Nobdy.prototype.supported = [];
66
67 Nobdy.prototype.isConnected = false;
68
69 Nobdy.prototype.simulateData = false;
70
71 Nobdy.prototype.requestReceived = function(code, value, time) {
72         for(var i=0;i<this.streams.length;i++) {
73                 if(this.streams[i].type == code) {
74                         this.streams[i].setValue(value);
75                 }
76         }
77
78 }
79
80 Nobdy.prototype.produceSimulatedData = function () {
81
82         if(!simulateData) return;
83
84         if(nobdy.streams.length)
85         {
86                 for(var i=0;i<nobdy.streams.length;i++) {
87                         var value=0;
88
89                         if(nobdy.streams[i].type == nobdy.supported.Velocity)
90                         {
91                                 value = Math.random()*250;
92                         }
93                         else if(nobdy.streams[i].type == nobdy.supported.EngineRPM)
94                         {
95                                 value = Math.random()*10000;
96                         }
97                         else if(nobdy.streams[i].type == nobdy.supported.EngineCoolantTemp)
98                         {
99                                 value = Math.random()*180;
100                         }
101                         else if(nobdy.streams[i].type == nobdy.supported.ThrottlePosition)
102                         {
103                                 value = Math.random()*100;
104                         }
105                         else if(nobdy.streams[i].type == nobdy.supported.IntakeAirTemp)
106                         {
107                                 value = Math.random()*200;
108                         }
109                         else if(nobdy.streams[i].type == nobdy.supported.EngineLoad)
110                         {
111                                 value = Math.random()*100;
112                         }
113                         else if(nobdy.streams[i].type == nobdy.supported.MassAirFlow)
114                         {
115                                 value = Math.random()*2550;
116                         }
117
118                         nobdy.streams[i].setValue(value);
119                 }
120         }
121
122         setTimeout(nobdy.produceSimulatedData,1000);
123 }
124
125
126 Nobdy.prototype.connected = function () { }
127
128 Nobdy.prototype.createStream = function(code) {
129         this.websocket.send("nobdy.addRequest('"+code+"');");
130
131         var stream = new NobdyStream(code);
132
133         this.streams.push(stream);
134
135         return stream;
136 }
137
138 Nobdy.prototype.issueCommand = function(command) {
139         this.websocket.send("nobdy.issueCommand('"+command+"');");
140 }
141
142
143