Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

From now on programs started manually can be stopped only manually an…

…d programs started from the app can be stopped only from the app.
  • Loading branch information...
commit 305229fedfacf6bff6c664c142a5c0241c1f2c07 1 parent e9ee268
@morkai authored
Showing with 256 additions and 61 deletions.
  1. +20 −0 app/controllers/Controller.js
  2. +22 −4 app/controllers/Zone.js
  3. +18 −35 app/controllers/zoneStates/connected.js
  4. +2 −3 app/controllers/zoneStates/programErrored.js
  5. +2 −3 app/controllers/zoneStates/programFinished.js
  6. +8 −0 app/controllers/zoneStates/programRunning.js
  7. +2 −3 app/controllers/zoneStates/programStopped.js
  8. +1 −1  app/models/ActiveZone.js
  9. +16 −0 app/models/ControllerProcess.js
  10. +5 −0 app/models/Zone.js
  11. +12 −0 app/models/controllerProcesses.js
  12. +31 −0 app/routers/zones.js
  13. +1 −1  public/app/templates/dashboard/connectedState.html
  14. +1 −0  public/app/templates/dashboard/programErroredState.html
  15. +1 −0  public/app/templates/dashboard/programFinishedState.html
  16. +1 −1  public/app/templates/dashboard/programRunningState.html
  17. +1 −0  public/app/templates/dashboard/programStoppedState.html
  18. +18 −2 public/app/views/dashboard/ProgramErroredStateView.js
  19. +18 −2 public/app/views/dashboard/ProgramFinishedStateView.js
  20. +20 −3 public/app/views/dashboard/ProgramRunningStateView.js
  21. +14 −3 public/app/views/dashboard/ProgramStoppedStateView.js
  22. +40 −0 public/app/views/dashboard/ZoneNeedsResetMixin.js
  23. +2 −0  public/assets/css/dashboard.css
View
20 app/controllers/Controller.js
@@ -166,6 +166,22 @@ Controller.prototype.stopZone = function(zoneId, done)
};
/**
+ * @param {String} zoneId
+ * @param {Function} done
+ */
+Controller.prototype.resetZone = function(zoneId, done)
+{
+ var zone = this.zones[zoneId];
+
+ if (!zone)
+ {
+ return done();
+ }
+
+ zone.reset(done);
+};
+
+/**
* @param {Object} data
* @param {Function} done
*/
@@ -320,6 +336,10 @@ var messageHandlers = {
{
this.stopZone(req, res);
},
+ resetZone: function(req, res)
+ {
+ this.resetZone(req, res);
+ },
startProgram: function(req, res)
{
this.startProgram(req, res);
View
26 app/controllers/Zone.js
@@ -49,13 +49,28 @@ Zone.prototype.finalize = function(stopReason, done)
};
/**
+ * @param {Function} done
+ */
+Zone.prototype.reset = function(done)
+{
+ done();
+
+ this.connected();
+
+ if (this.doesNeedReset)
+ {
+ this.wasReset();
+ }
+};
+
+/**
* @param {Object} data
* @param {Function} done
*/
Zone.prototype.startProgram = function(data, done)
{
var options = {
- manual: data.user ? true : false,
+ manual: data.user ? false : true,
program: data.program
};
@@ -473,7 +488,7 @@ Zone.prototype.programStopped = function()
* @param {Number} remainingTime
* @param {String} state
* @param {Number} stepIndex
- * @param {NumbeR} stepIteration
+ * @param {Number} stepIteration
*/
Zone.prototype.updateProgress = function(
remainingTime, state, stepIndex, stepIteration)
@@ -490,8 +505,6 @@ Zone.prototype.updateProgress = function(
Zone.prototype.startAssignedProgram = function()
{
- this.needsReset();
-
this.controller.sendMessage('startAssignedProgram', {
zoneId: this.zone._id
});
@@ -513,6 +526,11 @@ Zone.prototype.onInputChange = function(input, newValue, oldValue)
newValue
);
+ if (input === 'stopButton')
+ {
+ return newValue === 1 ? this.wasReset() : this.needsReset();
+ }
+
if (input === 'connected')
{
return newValue === 1 ? this.wasPlugIn() : this.needsPlugIn();
View
53 app/controllers/zoneStates/connected.js
@@ -57,13 +57,6 @@ function onInputChange(input, newValue, oldValue)
*/
function handleStopButtonChange(zone, newValue, oldValue)
{
- // If somehow stop button is changed while the zone cart is not plugged in
- // to a power supply then the zone needs to be reset
- if (zone.doesNeedPlugIn)
- {
- return zone.needsReset();
- }
-
// If the stop button is released (newValue=0) right after
// connect (oldValue=-1) then the zone needs to be reset
if (newValue === 0 && oldValue === -1)
@@ -71,42 +64,32 @@ function handleStopButtonChange(zone, newValue, oldValue)
return zone.needsReset();
}
- // If the stop button is pressed (newValue=1) and the zone needs a reset
- // (doesNeedReset=true) then consider the zone to be reset
- if (zone.doesNeedReset && newValue === 1)
- {
- return zone.wasReset();
- }
+ var timers = zone.timers;
- if (!zone.doesNeedReset)
+ // If the stop button is released (newValue=0 and oldValue=1)
+ // then start the start assigned program timer
+ if (newValue === 0 && oldValue === 1)
{
- var timers = zone.timers;
+ clearTimeout(timers.startAssignedProgram);
- // If the stop button is released (newValue=0 and oldValue=1)
- // then start the start assigned program timer
- if (newValue === 0 && oldValue === 1)
+ timers.startAssignedProgram = setTimeout(function()
{
- clearTimeout(timers.startAssignedProgram);
-
- timers.startAssignedProgram = setTimeout(function()
- {
- delete timers.startAssignedProgram;
+ delete timers.startAssignedProgram;
- zone.startAssignedProgram();
- }, START_ASSIGNED_PROGRAM_TIMEOUT);
+ zone.startAssignedProgram();
+ }, START_ASSIGNED_PROGRAM_TIMEOUT);
- return;
- }
+ return;
+ }
- // If the stop button is pressed (newValue=1 and oldValue=0) and the start
- // assigned program timer is running then stop it
- if (newValue === 1 && oldValue === 0)
- {
- clearTimeout(timers.startAssignedProgram);
+ // If the stop button is pressed (newValue=1 and oldValue=0) and the start
+ // assigned program timer is running then stop it
+ if (newValue === 1 && oldValue === 0)
+ {
+ clearTimeout(timers.startAssignedProgram);
- delete timers.startAssignedProgram;
+ delete timers.startAssignedProgram;
- return;
- }
+ return;
}
}
View
5 app/controllers/zoneStates/programErrored.js
@@ -35,14 +35,13 @@ function onInputChange(input, newValue, oldValue)
// reset and restore it to the connected state
if (input === 'stopButton' && newValue === 1)
{
- this.connected();
- this.wasReset();
+ return this.connected();
}
// If the zone cart was plugged off a power supply then switch to
// the connected state
if (input === 'connected' && newValue === 0)
{
- this.connected();
+ return this.connected();
}
}
View
5 app/controllers/zoneStates/programFinished.js
@@ -35,14 +35,13 @@ function onInputChange(input, newValue, oldValue)
// reset and restore it to the connected state
if (input === 'stopButton' && newValue === 1)
{
- this.connected();
- this.wasReset();
+ return this.connected();
}
// If the zone cart was plugged off a power supply then switch to
// the connected state
if (input === 'connected' && newValue === 0)
{
- this.connected();
+ return this.connected();
}
}
View
8 app/controllers/zoneStates/programRunning.js
@@ -17,6 +17,7 @@ exports.enter = function(oldState, options, done)
var zone = this;
zone.program = _.clone(options.program);
+ zone.program.manual = options.manual;
zone.program.remainingTime = zone.program.totalTime;
zone.program.running = true;
@@ -86,6 +87,13 @@ function onInputChange(input, newValue, oldValue)
*/
function handleStopButtonChange(zone, newValue, oldValue)
{
+ // Ignore the stop button input changes if the program was not started
+ // manually
+ if (!zone.program.manual)
+ {
+ return;
+ }
+
var timers = zone.timers;
// If the stop button is pressed (newValue=1)
View
5 app/controllers/zoneStates/programStopped.js
@@ -33,14 +33,13 @@ function onInputChange(input, newValue, oldValue)
// reset and restore it to the connected state
if (input === 'stopButton' && newValue === 1)
{
- this.connected();
- this.wasReset();
+ return this.connected();
}
// If the zone cart was plugged off a power supply then switch to
// the connected state
if (input === 'connected' && newValue === 0)
{
- this.connected();
+ return this.connected();
}
}
View
2  app/models/ActiveZone.js
@@ -149,7 +149,7 @@ ActiveZone.prototype.programStopped = function(user)
'Program [%s] on zone [%s] was stopped by a user%s.',
this.program.name,
this.zone.name,
- user ? (': ' + user.name) : 'manually'
+ user ? (': ' + user.name) : ' manually'
);
};
View
16 app/models/ControllerProcess.js
@@ -128,6 +128,22 @@ _.extend(ControllerProcess.prototype, {
},
/**
+ * @param {String} zoneId
+ * @param {Function} done
+ */
+ resetZone: function(zoneId, done)
+ {
+ var activeZone = this.zones[zoneId];
+
+ if (!activeZone)
+ {
+ return done();
+ }
+
+ this.sendMessage('resetZone', zoneId, done);
+ },
+
+ /**
* @param {Program} program
* @param {String} zoneId
* @param {?Object} user
View
5 app/models/Zone.js
@@ -81,6 +81,11 @@ Zone.methods.stop = function(done)
controllerProcesses.stopZone(this._id, done);
};
+Zone.methods.reset = function(done)
+{
+ controllerProcesses.resetZone(this._id, done);
+};
+
Zone.methods.startProgram = function(programId, user, done)
{
var zone = this;
View
12 app/models/controllerProcesses.js
@@ -125,6 +125,18 @@ exports.stopZone = function(zoneId, done)
});
};
+exports.resetZone = function(zoneId, done)
+{
+ var controllerId = zoneToControllerMap[zoneId];
+
+ if (!controllerId)
+ {
+ return done();
+ }
+
+ controllerProcesses[controllerId].resetZone(zoneId, done);
+};
+
exports.startProgram = function(program, zoneId, user, done)
{
var controllerId = zoneToControllerMap[zoneId];
View
31 app/routers/zones.js
@@ -140,6 +140,7 @@ app.post('/zones/:id', function(req, res, next)
var actionPrivileges = {
'startProgram': 'startStop',
'stopProgram': 'startStop',
+ 'reset': 'startStop',
'start': 'diag',
'stop': 'diag'
};
@@ -176,6 +177,10 @@ app.post('/zones/:id', function(req, res, next)
stopProgram(req, res, next, zone, user);
break;
+ case 'reset':
+ resetZone(res, next, zone);
+ break;
+
case 'start':
startZone(res, next, zone);
break;
@@ -595,6 +600,32 @@ function stopZone(res, next, zone)
});
}
+function resetZone(res, next, zone)
+{
+ zone.reset(function(err)
+ {
+ if (err)
+ {
+ console.debug(
+ 'Resetting zone <%s> failed: %s', zone.name, err.message || err
+ );
+
+ if (err instanceof Error)
+ {
+ next(err);
+ }
+ else
+ {
+ res.send(err, 400);
+ }
+ }
+ else
+ {
+ res.send(201);
+ }
+ });
+}
+
function emitNewZoneProgram(zoneId, programId)
{
var activeZone = controllerProcesses.getStartedZone(zoneId);
View
2  public/app/templates/dashboard/connectedState.html
@@ -21,4 +21,4 @@
podłączenie<br>
wózka
</p>
-<input class="startProgram action green" type="button" value="Start">
+<a class="startProgram action green">Start</a>
View
1  public/app/templates/dashboard/programErroredState.html
@@ -10,6 +10,7 @@
ręczny<br>
reset
</p>
+<input class="resetZone action blue" type="button" value="Reset">
<p class="info">
Program <em><%- program.name %></em> uruchomiony
<% if (program.startUser) { %>
View
1  public/app/templates/dashboard/programFinishedState.html
@@ -10,6 +10,7 @@
ręczny<br>
reset
</p>
+<input class="resetZone action blue" type="button" value="Reset">
<p class="info">
Program <em><%- program.name %></em> uruchomiony
<% if (program.startUser) { %>
View
2  public/app/templates/dashboard/programRunningState.html
@@ -5,7 +5,7 @@
<a class="programPreview" href="#programs/<%- program._id %>" data-programProperty="program"><%- program.name %></a>
</span>
</h1>
-<input class="stopProgram action red" type="button" value="Stop">
+<a class="stopProgram action red">Stop</a>
<div class="progressBar">
<p class="timeToEnd">&nbsp;</p>
<div class="progressValue">&nbsp;</div>
View
1  public/app/templates/dashboard/programStoppedState.html
@@ -10,6 +10,7 @@
ręczny<br>
reset
</p>
+<a class="resetZone action blue">Reset</a>
<p class="info">
Program <em><%- program.name %></em> uruchomiony
<% if (program.startUser) { %>
View
20 public/app/views/dashboard/ProgramErroredStateView.js
@@ -5,6 +5,7 @@ define(
'Backbone',
'app/models/Program',
+ 'app/views/dashboard/ZoneNeedsResetMixin',
'text!app/templates/dashboard/programErroredState.html'
],
@@ -13,9 +14,10 @@ define(
* @param {Underscore} _
* @param {Backbone} Backbone
* @param {function(new:Program)} Program
+ * @param {Object} ZoneNeedsResetMixin
* @param {String} programErroredStateTpl
*/
-function($, _, Backbone, Program, programErroredStateTpl)
+function($, _, Backbone, Program, ZoneNeedsResetMixin, programErroredStateTpl)
{
/**
* @class ProgramErroredStateView
@@ -26,11 +28,23 @@ function($, _, Backbone, Program, programErroredStateTpl)
var ProgramErroredStateView = Backbone.View.extend({
tagName: 'div',
className: 'programErrored',
- template: _.template(programErroredStateTpl)
+ template: _.template(programErroredStateTpl),
+ events: {
+ 'click .resetZone': 'resetZone'
+ }
});
+ _.extend(ProgramErroredStateView.prototype, ZoneNeedsResetMixin);
+
+ ProgramErroredStateView.prototype.initialize = function()
+ {
+ this.model.bind('change:needsReset', this.toggleResetAction, this);
+ };
+
ProgramErroredStateView.prototype.destroy = function()
{
+ this.model.unbind('change:needsReset', this.toggleResetAction, this);
+
this.remove();
};
@@ -47,6 +61,8 @@ function($, _, Backbone, Program, programErroredStateTpl)
this.el.innerHTML = this.template(data);
+ this.toggleResetAction();
+
return this;
};
View
20 public/app/views/dashboard/ProgramFinishedStateView.js
@@ -5,6 +5,7 @@ define(
'Backbone',
'app/models/Program',
+ 'app/views/dashboard/ZoneNeedsResetMixin',
'text!app/templates/dashboard/programFinishedState.html'
],
@@ -13,9 +14,10 @@ define(
* @param {Underscore} _
* @param {Backbone} Backbone
* @param {function(new:Program)} Program
+ * @param {Object} ZoneNeedsResetMixin
* @param {String} programFinishedStateTpl
*/
-function($, _, Backbone, Program, programFinishedStateTpl)
+function($, _, Backbone, Program, ZoneNeedsResetMixin, programFinishedStateTpl)
{
/**
* @class ProgramFinishedStateView
@@ -26,11 +28,23 @@ function($, _, Backbone, Program, programFinishedStateTpl)
var ProgramFinishedStateView = Backbone.View.extend({
tagName: 'div',
className: 'programFinished',
- template: _.template(programFinishedStateTpl)
+ template: _.template(programFinishedStateTpl),
+ events: {
+ 'click .resetZone': 'resetZone'
+ }
});
+ _.extend(ProgramFinishedStateView.prototype, ZoneNeedsResetMixin);
+
+ ProgramFinishedStateView.prototype.initialize = function()
+ {
+ this.model.bind('change:needsReset', this.toggleResetAction, this);
+ };
+
ProgramFinishedStateView.prototype.destroy = function()
{
+ this.model.unbind('change:needsReset', this.toggleResetAction, this);
+
this.remove();
};
@@ -47,6 +61,8 @@ function($, _, Backbone, Program, programFinishedStateTpl)
this.el.innerHTML = this.template(data);
+ this.toggleResetAction();
+
return this;
};
View
23 public/app/views/dashboard/ProgramRunningStateView.js
@@ -84,12 +84,29 @@ function(
{
this.el.innerHTML = this.template(this.model.toJSON());
- if (!user.isAllowedTo('startStop'))
+ var program = this.model.get('program');
+
+ if (!user.isAllowedTo('startStop') || !program.startUser)
{
- this.$('.stopProgram').hide();
+ var zoneName = this.model.get('name');
+
+ this.$('.stopProgram').addClass('disabled', true).click(function()
+ {
+ console.log(this);
+
+ viewport.msg.show({
+ type: 'error',
+ time: 5000,
+ text: 'Program &lt;' + program.name + '&gt; na strefie '
+ + '&lt;' + zoneName + '&gt; może być zatrzymany tylko '
+ + 'z pulpitu sterującego wózkiem!'
+ });
+
+ return false;
+ });
}
- this.totalTime = this.model.get('program').totalTime;
+ this.totalTime = program.totalTime;
this.remainingTime = this.calcRemainingTime();
this.updateProgressBar();
View
17 public/app/views/dashboard/ProgramStoppedStateView.js
@@ -5,6 +5,7 @@ define(
'Backbone',
'app/models/Program',
+ 'app/views/dashboard/ZoneNeedsResetMixin',
'text!app/templates/dashboard/programStoppedState.html'
],
@@ -13,9 +14,10 @@ define(
* @param {Underscore} _
* @param {Backbone} Backbone
* @param {function(new:Program)} Program
+ * @param {Object} ZoneNeedsResetMixin
* @param {String} programStoppedStateTpl
*/
-function($, _, Backbone, Program, programStoppedStateTpl)
+function($, _, Backbone, Program, ZoneNeedsResetMixin, programStoppedStateTpl)
{
/**
* @class ProgramStoppedStateView
@@ -26,16 +28,23 @@ function($, _, Backbone, Program, programStoppedStateTpl)
var ProgramStoppedStateView = Backbone.View.extend({
tagName: 'div',
className: 'programStopped',
- template: _.template(programStoppedStateTpl)
+ template: _.template(programStoppedStateTpl),
+ events: {
+ 'click .resetZone': 'resetZone'
+ }
});
+ _.extend(ProgramStoppedStateView.prototype, ZoneNeedsResetMixin);
+
ProgramStoppedStateView.prototype.initialize = function()
{
-
+ this.model.bind('change:needsReset', this.toggleResetAction, this);
};
ProgramStoppedStateView.prototype.destroy = function()
{
+ this.model.unbind('change:needsReset', this.toggleResetAction, this);
+
this.remove();
};
@@ -52,6 +61,8 @@ function($, _, Backbone, Program, programStoppedStateTpl)
this.el.innerHTML = this.template(data);
+ this.toggleResetAction();
+
return this;
};
View
40 public/app/views/dashboard/ZoneNeedsResetMixin.js
@@ -0,0 +1,40 @@
+define(
+[
+ 'jQuery',
+
+ 'app/user'
+],
+/**
+ * @param {jQuery} $
+ * @param {Object} user
+ */
+function($, user)
+{
+ return {
+ toggleResetAction: function()
+ {
+ if (!this.model.get('needsReset'))
+ {
+ this.$('.needsReset').hide();
+
+ if (user.isAllowedTo('startStop'))
+ {
+ this.$('.resetZone').show();
+ }
+ }
+ else
+ {
+ this.$('.resetZone').hide();
+ this.$('.needsReset').show();
+ }
+ },
+ resetZone: function()
+ {
+ $.ajax({
+ type: 'POST',
+ url: '/zones/' + this.model.id,
+ data: {action: 'reset'}
+ });
+ }
+ };
+});
View
2  public/assets/css/dashboard.css
@@ -50,6 +50,8 @@
top: .25em;
right: .25em;
font-size: 2em;
+ padding-top: .5em;
+ text-align: center;
}
.activeZone .actionMessage {
box-sizing: border-box;
Please sign in to comment.
Something went wrong with that request. Please try again.