Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "assets/js/EventEmitter"]
path = assets/js/EventEmitter
url = git://github.com/Wolfy87/EventEmitter.git
[submodule "assets/js/Heir"]
path = assets/js/Heir
url = git://github.com/Wolfy87/Heir.git
34 changes: 0 additions & 34 deletions Contributing.md

This file was deleted.

140 changes: 140 additions & 0 deletions First_server/lights.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<script type='text/javascript' src="../assets/js/EventEmitter/EventEmitter.js"></script>
<script type='text/javascript' src="../assets/js/Heir/heir.js"></script>

<script>
var json = '{"green":4000,"yellow":1000,"red":6000}';

function Traffic_lights(config_file)
{
this.state = null
this.switchTime = null;

this.tram_mode = false;
this.wait_for_tram = 3000;
this.green_for_tram = 10000;
this.percent_of_curr_color = 0.9;

this._timeout = null
this._config = config_file || null;
if (this._config)
this._config = JSON.parse(this._config)

Traffic_lights.prototype.toYellow = function () {
if (this._timeout) {
clearTimeout(this._timeout)
}
this.switchTime = new Date();
this.state = 'yellow'
this._timeout = setTimeout(function () {
this.toRed();
}.bind(this), this._config[this.state])
}

Traffic_lights.prototype.toGreen = function () {
if (this._timeout) {
clearTimeout(this._timeout)
}
this.switchTime = new Date();
this.state = 'green'
this._timeout = setTimeout(function () {
this.toYellow();
}.bind(this), this._config[this.state])
}

Traffic_lights.prototype.toRed = function () {
if (this._timeout) {
clearTimeout(this._timeout)
}
this.switchTime = new Date();
this.state = 'red'
this._timeout = setTimeout(function () {
this.toGreen();
}.bind(this), this._config[this.state])
}

Traffic_lights.prototype.getState = function() { // current state
if (this.tram_mode)
return 'green'
else
return this.state
};

Traffic_lights.prototype.stopIt = function() { // Stop switching lights
if (this._timeout) {
clearTimeout(this._timeout)
}
}

Traffic_lights.prototype.passTram = function() {
console.log("Трамвай на подходе");
setTimeout(function() {
this.tram_mode = true;
console.log("Трамвай пошел");
setTimeout(function() {
this.tram_mode = false; // Трамвай как-будто не проезжал, таймер все это время шел, как обычно.
// Можно, конечно, было понимать, что надо вернуться в старое состояние до трамвая
// Проверим, так ли много осталось от текущего цвета, стоит ли в него переходить
if (this.switchTime - new Date() > self.percent_of_curr_color * this._config[this.state])
switch(this.state) {
case 'green':
this.toYellow();
break;
case 'yellow':
this.toRed();
break;
case 'red':
this.toGreen();
break;
}
}.bind(this), this.green_for_tram)
}.bind(this), this.wait_for_tram)
}

// Traffic_lights.prototype.tramPassed = function() {

// }

Traffic_lights.prototype.onEventEmmiter = function(event_emmiter) {
event_emmiter.addListener('Tram is comming', function() {this.passTram();}.bind(this))
}

}

function Tram(){}
heir.inherit(Tram, EventEmitter); // Tram is a subclass of EventEmmiter

// Creating Objects
var my_Lights = new Traffic_lights(json);
my_Lights.toGreen();

var my_tram = new Tram();
my_Lights.onEventEmmiter(my_tram);

//Asking server for tram state every 7 seconds
//CORS решен с помощью добавления "Access-Control-Allow-Origin", "*"
setInterval(function() {
var sURL = "http://" + "localhost:3000" + "/tram_api";
var xhr = new XMLHttpRequest();
xhr.open("GET", sURL, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var resp = JSON.parse(xhr.responseText);
//console.log(resp.is_tram_comming);
if (resp.is_tram_comming)
my_tram.emit("Tram is comming");
}
}
xhr.send();
}, 7000);

// Tests
// Check state once in a sec
intervalID = setInterval(function () {
console.log(this.getState());
}.bind(my_Lights), 1000);

</script>

<button onclick='my_tram.emit("Tram is comming")'>Ручное выпускание трамвая</button>
<button onclick='my_Lights.stopIt()'>Выключить светофор</button>
<button onclick='clearInterval(intervalID)'>Закончить проверку состояний</button>
9 changes: 9 additions & 0 deletions First_server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Tram-server",
"description": "Second task CRI",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "4.x"
}
}
25 changes: 25 additions & 0 deletions First_server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var express = require('express');
var app = express();

var is_tram_comming = 0

setInterval(function () {
is_tram_comming = (is_tram_comming += 1) % 2
console.log("Changing tram state to: ", is_tram_comming);
}, 20000);

app.get('/', function(req, res){
res.setHeader("Access-Control-Allow-Origin", "*");
res.send('The state of tram is: ' + is_tram_comming);
});

app.get('/tram_api', function(req, res){
res.setHeader("Access-Control-Allow-Origin", "*");
res.json({ is_tram_comming: is_tram_comming,
"permissions": ["http://www.google.com/", "http://*/"],
})
});

var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
115 changes: 0 additions & 115 deletions Gruntfile.js

This file was deleted.

21 changes: 0 additions & 21 deletions License.md

This file was deleted.

1 change: 1 addition & 0 deletions assets/js/EventEmitter
Submodule EventEmitter added at 19afbb
1 change: 1 addition & 0 deletions assets/js/Heir
Submodule Heir added at 2a5e40
24 changes: 0 additions & 24 deletions bower.json

This file was deleted.

1 change: 0 additions & 1 deletion css/default.min.css

This file was deleted.

1 change: 0 additions & 1 deletion css/idea.min.css

This file was deleted.

Loading