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.

115 changes: 0 additions & 115 deletions Gruntfile.js

This file was deleted.

21 changes: 0 additions & 21 deletions License.md

This file was deleted.

98 changes: 98 additions & 0 deletions Second_server/Traffic_lights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
function Traffic_lights(config_file)
{
this.state = null
this.switchTime = null;

this.tram_mode = false;
this.wait_for_tram = 3000;
this.green_for_tram = 15000;
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() > this.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))
}

}
// Экспортируем
module.exports = Traffic_lights;
//exports.Traffic_lights = Traffic_lights;
9 changes: 9 additions & 0 deletions Second_server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "TrafficLightsServer",
"description": "Second task CRI",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "4.x"
}
}
23 changes: 23 additions & 0 deletions Second_server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var trafficlight = require('./Traffic_lights');
var events = require('events');
var express = require('express');

var app = express();
var emitter = new events.EventEmitter();

var param = '{"green":4000,"yellow":1000,"red":6000}';
var my_trafficlight = new trafficlight(param);
my_trafficlight.toGreen(); // Start TrafficLights
my_trafficlight.onEventEmmiter(emitter);

setInterval(function() {
emitter.emit("Tram is comming");
}, 20000);

app.get('/', function(req, res){
res.send('The state of tram is: ' + my_trafficlight.getState());
});

var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
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