From 26d94159498891b4cded2ad3fcdce635a56a6c88 Mon Sep 17 00:00:00 2001 From: Amanda Ghassaei <amandaghassaei@gmail.com> Date: Fri, 11 Sep 2015 18:04:14 -0400 Subject: [PATCH] is streaming on node server --- .../PositionControlPanelView.html | 2 +- js/SerialMonitor/PositionControlPanelView.js | 7 +++-- js/SerialMonitor/SerialMonitorView.js | 29 ++++++++++++------- js/cam/Cam.js | 1 - js/menus/SendMenuView.js | 17 ++++------- js/menus/templates/SendMenuView.html | 2 +- js/models/SerialComm.js | 20 ++++++++++++- node/nodeServer.js | 26 ++++++++++++++++- 8 files changed, 75 insertions(+), 29 deletions(-) diff --git a/js/SerialMonitor/PositionControlPanelView.html b/js/SerialMonitor/PositionControlPanelView.html index 15f398bb..2d2e2df1 100644 --- a/js/SerialMonitor/PositionControlPanelView.html +++ b/js/SerialMonitor/PositionControlPanelView.html @@ -1,5 +1,5 @@ <div id="positionData"> </div><br/> <a href="#" id="askForPosition" class="btn btn-block btn-lg btn-default">Refresh</a> -<a href="#" id="askForPosition" class="btn btn-block btn-lg btn-warning">Pause</a> +<a href="#" id="pauseOutput" class="btn btn-block btn-lg btn-warning">Pause</a> <a href="#" id="stopMachine" class="btn pull-right btn-block btn-lg btn-danger">Stop</a> \ No newline at end of file diff --git a/js/SerialMonitor/PositionControlPanelView.js b/js/SerialMonitor/PositionControlPanelView.js index 511318a5..60775b90 100644 --- a/js/SerialMonitor/PositionControlPanelView.js +++ b/js/SerialMonitor/PositionControlPanelView.js @@ -11,7 +11,9 @@ define(['jquery', 'underscore', 'backbone', 'text!PositionControlPanelView.html' el: "#positionPanel", events: { - + "click #stopMachine": "_stopMachine", + "click #pauseOutput": "_pause", + "click #askForPosition": "_askForPosition" }, @@ -21,7 +23,6 @@ define(['jquery', 'underscore', 'backbone', 'text!PositionControlPanelView.html' _renderPositionData: function(){ var machineState = this.model.getMachineState(); - console.log(machineState); if (machineState === null) { $("#positionData").html("<div id='positionDataError'>no position data available</div>"); return; @@ -38,7 +39,7 @@ define(['jquery', 'underscore', 'backbone', 'text!PositionControlPanelView.html' this._renderPositionData(); }, - template: _.template(template), + template: _.template(template) }); diff --git a/js/SerialMonitor/SerialMonitorView.js b/js/SerialMonitor/SerialMonitorView.js index 40f3ddab..fcebe94f 100644 --- a/js/SerialMonitor/SerialMonitorView.js +++ b/js/SerialMonitor/SerialMonitorView.js @@ -3,27 +3,24 @@ */ -define(['jquery', 'underscore', 'commParentMenu', 'serialComm', 'text!SerialMonitorView.html'], - function($, _, CommParentMenu, serialComm, template){ +define(['jquery', 'underscore', 'backbone', 'serialComm', 'text!SerialMonitorView.html'], + function($, _, Backbone, serialComm, template){ - return CommParentMenu.extend({ + return Backbone.View.extend({ el: "#serialMonitorView", - parentEvents: { - }, - events: { "click #clearMonitor": "_clear", "change input:checkbox": "_clickCheckbox" }, - __initialize: function(){ + initialize: function(){ _.bindAll(this, "_onKeyUp"); $(document).bind('keyup', {}, this._onKeyUp); - + this.listenTo(serialComm, "change:lastMessageReceived", this._updateIncomingMessage); this.listenTo(serialComm, "change:lastMessageSent", this._updateOutgoingMessage); this.listenTo(serialComm, "change:baudRate change:portName", this.render); this.listenTo(serialComm, "change:connected", function(){ @@ -79,6 +76,14 @@ define(['jquery', 'underscore', 'commParentMenu', 'serialComm', 'text!SerialMoni } }, + _sendMessage: function(e){ + e.preventDefault(); + var message = $("#sendSerialMessage").val(); + $("#sendSerialMessage").val(""); + if (message == "") return; + serialComm.send(message); + }, + _makeTemplateJSON: function(){ return this.model.toJSON(); }, @@ -101,14 +106,18 @@ define(['jquery', 'underscore', 'commParentMenu', 'serialComm', 'text!SerialMoni _clear: function(e){ e.preventDefault(); - this.render(); + $("#serialMonitorOutput").html(""); }, _close: function(){ - this.userInitedReload = false; window.close(); }, + render: function(){ + if ($("input[type=text]").is(":focus")) return; + this.$el.html(this.template(this._makeTemplateJSON())); + }, + template: _.template(template) }); diff --git a/js/cam/Cam.js b/js/cam/Cam.js index 23fe5737..a6563ab6 100644 --- a/js/cam/Cam.js +++ b/js/cam/Cam.js @@ -299,7 +299,6 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel scaledSettings, function(){ currentLine++; self.set("simLineNumber", currentLine); - console.log(self); self._stockSimulation(); }); } else { diff --git a/js/menus/SendMenuView.js b/js/menus/SendMenuView.js index a4d1e807..feea3546 100644 --- a/js/menus/SendMenuView.js +++ b/js/menus/SendMenuView.js @@ -18,32 +18,28 @@ define(['jquery', 'underscore', 'menuParent', 'serialComm', 'commPlist', 'text!s }, - __initialize: function(){ - this.isStreaming = false; + _initialize: function(){ this.listenTo(this.model, "change:stockSimulationPlaying", this.render); + this.listenTo(serialComm, "change:isStreaming", this.render); this.listenTo(cam, "change:simLineNumber", this._drawGcodeHighlighter); }, _startStream: function(e){ e.preventDefault(); this.model.set("stockSimulationPlaying", true); - this.isStreaming = true; - this.render(); + serialComm.startStream(); }, _pauseStream: function(e){ e.preventDefault(); this.model.set("stockSimulationPlaying", false); - this.isStreaming = false; - this.render(); + serialComm.pauseStream(); }, _stopMachine: function(e){ e.preventDefault(); this.model.set("stockSimulationPlaying", false); - this.isStreaming = false; - serialComm.send("!"); - this.render(); + serialComm.stopStream(); }, _decrementLineNum: function(e){ @@ -63,7 +59,6 @@ define(['jquery', 'underscore', 'menuParent', 'serialComm', 'commPlist', 'text!s _drawGcodeHighlighter: function(){ var lineNum = cam.get("simLineNumber"); - console.log(lineNum); if (lineNum == 0) return; var code = cam.get("dataOut").split("\n"); code[lineNum] = "<span id='gcodeHighlighter'>" + code[lineNum] + " </span>"; @@ -78,7 +73,7 @@ define(['jquery', 'underscore', 'menuParent', 'serialComm', 'commPlist', 'text!s }, _makeTemplateJSON: function(){ - return _.extend(serialComm.toJSON(), commPlist, cam.toJSON(), camPlist, {streaming: this.isStreaming}); + return _.extend(serialComm.toJSON(), commPlist, cam.toJSON(), camPlist); }, _render: function(){ diff --git a/js/menus/templates/SendMenuView.html b/js/menus/templates/SendMenuView.html index 010546f9..c7f271af 100644 --- a/js/menus/templates/SendMenuView.html +++ b/js/menus/templates/SendMenuView.html @@ -2,7 +2,7 @@ <% if(dataOut==0){ %> You need to go through the Assemble workflow to setup your machine and create <%= allCamProcesses[camProcess] %> first. <% }else{ %> - <% if(streaming){ %> + <% if(isStreaming){ %> <a href="#" id="pauseStream" class="btn btn-halfWidth btn-lg btn-warning">Pause</a> <a href="#" id="stopMachine" class="btn pull-right btn-halfWidth btn-lg btn-danger">Stop</a><br/> <% }else{ %> diff --git a/js/models/SerialComm.js b/js/models/SerialComm.js index 806fe8c2..4bd02eea 100644 --- a/js/models/SerialComm.js +++ b/js/models/SerialComm.js @@ -14,7 +14,8 @@ define(['underscore', 'backbone', 'socketio', 'machineState'], function(_, Backb baudRate: 200, error: null, lastMessageReceived: null, - lastMessageSent: "" + lastMessageSent: "", + isStreaming: false }, initialize: function(){ @@ -47,6 +48,18 @@ define(['underscore', 'backbone', 'socketio', 'machineState'], function(_, Backb this.socket.emit("flush"); }, + startStream: function(){ + this.socket.emit("startStream"); + }, + + pauseStream: function(){ + this.socket.emit("pauseStream"); + }, + + stopStream: function(){ + this.socket.emit("stopStream"); + }, + refreshMachineState: function(){//when updating connection, create a new instance of machine state if (this.machineState) this.machineState.destroy(); this.machineState = new MachineState(); @@ -97,6 +110,11 @@ define(['underscore', 'backbone', 'socketio', 'machineState'], function(_, Backb serialComm.trigger("change:lastMessageSent"); }); + socket.on('isStreaming', function(data){ + console.log(data); + serialComm.set("isStreaming", data); + }); + socket.on('portConnected', function(data){ console.log("connected port " + data.portName + " at " + data.baudRate); serialComm.set("baudRate", data.baudRate); diff --git a/node/nodeServer.js b/node/nodeServer.js index 2e2f4696..ef7ce053 100644 --- a/node/nodeServer.js +++ b/node/nodeServer.js @@ -19,6 +19,7 @@ var io = require('socket.io').listen(8080); var portName = null; var currentPort = null; var baudRate = 115200; +var isStreaming = false; //io.sockets.on('connection', function (socket) { // io.emit('baudRate', baudRate); @@ -50,6 +51,10 @@ io.on('connection', function(socket){ }); socket.on('dataOut', function(data){ + outputData(data); + }); + + function outputData(data){ io.emit('dataSent', data); data += '\n'; console.log("Sending data: " + data); @@ -57,7 +62,7 @@ io.on('connection', function(socket){ if (err) onPortError(err); }); // currentPort.write(new Buffer([parseInt(data)]));//write byte - }); + } socket.on('flush', function(){ if (currentPort) currentPort.flush(function(){ @@ -70,6 +75,25 @@ io.on('connection', function(socket){ allPorts = refreshAvailablePorts(); }); + socket.on('stopStream', function(){ + outputData("!"); + setIsStreaming(false); + }); + + socket.on('startStream', function(){ + setIsStreaming(true); + }); + + socket.on('pauseStream', function(){ + setIsStreaming(false); + }); + + function setIsStreaming(state){ + if (state == isStreaming) return; + isStreaming = state; + io.emit('isStreaming', isStreaming); + } + function checkThatPortExists(_portName){ if (allPorts.indexOf(_portName) < 0) { onPortError("no available port called " + _portName); -- GitLab