From 8b423c16677adeabaac8197f3dd1789d21856211 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Thu, 30 Jul 2015 12:29:45 -0500 Subject: [PATCH 1/6] Text case for device sharing --- .../api/devices/devices_controller_create_spec.rb | 11 +++++++++++ .../api/devices/devices_controller_update_spec.rb | 3 +++ spec/features/angular_app/device_page_spec.rb | 6 ------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/spec/controllers/api/devices/devices_controller_create_spec.rb b/spec/controllers/api/devices/devices_controller_create_spec.rb index e5dcde683..556cb5d26 100644 --- a/spec/controllers/api/devices/devices_controller_create_spec.rb +++ b/spec/controllers/api/devices/devices_controller_create_spec.rb @@ -7,6 +7,7 @@ describe Api::DevicesController do describe '#create' do let(:user) { FactoryGirl.create(:user) } + let(:user2) { FactoryGirl.create(:user) } it 'creates a new device for a user' do sign_in user @@ -18,5 +19,15 @@ describe Api::DevicesController do expect(user.device).to eq(new_device) expect(response.status).to eq(200) end + + it 'shares devices between two users' do + bot = user.device + sign_in user2 + params = {name: 'Frank', uuid: bot.uuid, token: bot.token} + post :create, params + user.reload + user2.reload + expect(user.device._id).to eq(user2.device._id) + end end end diff --git a/spec/controllers/api/devices/devices_controller_update_spec.rb b/spec/controllers/api/devices/devices_controller_update_spec.rb index e534250dd..6f715bf2d 100644 --- a/spec/controllers/api/devices/devices_controller_update_spec.rb +++ b/spec/controllers/api/devices/devices_controller_update_spec.rb @@ -32,5 +32,8 @@ describe Api::DevicesController do expect(user.device[key]).to eq('Not set.') end end + + it 'shares devices between two users' + end end diff --git a/spec/features/angular_app/device_page_spec.rb b/spec/features/angular_app/device_page_spec.rb index 9bdd00822..84ef574bd 100644 --- a/spec/features/angular_app/device_page_spec.rb +++ b/spec/features/angular_app/device_page_spec.rb @@ -4,10 +4,4 @@ describe 'Device Management' do include Capybara::Angular::DSL let(:user) { FactoryGirl.create(:user) } - - # it 'adds a device', js: true do - # pending - # visit 'dashboard#/devices' - # fill_in 'botname', with: 'LOL!@' - # end end From e4cf26abb92595aad18c35a27e95e8c633591ef9 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Thu, 30 Jul 2015 14:47:45 -0500 Subject: [PATCH 2/6] Fix sharing of devices --- app/controllers/api/devices_controller.rb | 8 +++---- app/mutations/devices/create.rb | 8 ++++--- .../devices/devices_controller_update_spec.rb | 23 ++++++++++++------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/controllers/api/devices_controller.rb b/app/controllers/api/devices_controller.rb index f1617673f..99bb9b4c3 100644 --- a/app/controllers/api/devices_controller.rb +++ b/app/controllers/api/devices_controller.rb @@ -17,11 +17,9 @@ module Api # PATCH/PUT /api/device def update - current_device - .if_null { create } - .if_not_null do - render json: current_device.update_attributes(device_params) - end + # Because of the way bots are shared, there is no true 'update' action. + # Just a creation/reasignment of bots based on UUID / Token. + create end # DELETE /api/devices/1 diff --git a/app/mutations/devices/create.rb b/app/mutations/devices/create.rb index 8d2328700..fc336637e 100644 --- a/app/mutations/devices/create.rb +++ b/app/mutations/devices/create.rb @@ -4,18 +4,20 @@ module Devices required do model :user, class: User + string :uuid + string :token end optional do string :name, default: 'Not set.' - string :uuid, default: 'Not set.' - string :token, default: 'Not set.' end def execute - create Device, inputs.except(:user) do |dev| + dev = Device.find_or_initialize_by(uuid: uuid, token: token) + if update_attributes(dev, inputs.except(:user)) user.update_attributes(device: dev) end + dev end end end diff --git a/spec/controllers/api/devices/devices_controller_update_spec.rb b/spec/controllers/api/devices/devices_controller_update_spec.rb index 6f715bf2d..e34044a1c 100644 --- a/spec/controllers/api/devices/devices_controller_update_spec.rb +++ b/spec/controllers/api/devices/devices_controller_update_spec.rb @@ -9,14 +9,15 @@ describe Api::DevicesController do describe '#update' do let(:user) { FactoryGirl.create(:user) } + let(:user2) { FactoryGirl.create(:user) } it 'updates a Device' do sign_in user fake_name = Faker::Name.name - put :update, {id: user.device.id, name: fake_name}, format: :json + put :update, {id: user.device.id, name: fake_name, uuid: 1, token: 2}, format: :json # put path, params, options user.reload - device = user.device + device = user.reload.device.reload expect(device.name).to eq(fake_name) expect(response.status).to eq(200) end @@ -25,15 +26,21 @@ describe Api::DevicesController do user.update_attributes(device: nil) sign_in user fake_name = Faker::Name.name - put :update, {}, format: :json + put :update, {uuid: 1, token: 2}, format: :json user.reload expect(user.device).to be_kind_of(Device) - [:uuid, :token, :name].each do |key| - expect(user.device[key]).to eq('Not set.') - end + expect(user.device['name']).to eq('Not set.') + expect(user.device['uuid']).to eq('1') + expect(user.device['token']).to eq('2') end - it 'shares devices between two users' - + it 'shares devices between two users' do + bot = user.device + sign_in user2 + post :update, { name: 'Frank', uuid: bot.uuid, token: bot.token } + user.reload + user2.reload + expect(user.device._id).to eq(user2.device._id) + end end end From c9cdd09b07d97030924fa2a9df52f95c45b5f4d9 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Tue, 11 Aug 2015 11:51:08 -0500 Subject: [PATCH 3/6] WIP inversion button directive --- .../controllers/devices_controller.js.coffee | 5 +- .../directives/calibrationbutton.js.coffee | 17 ++++++ app/controllers/api/abstract_controller.rb | 1 + .../ng-partials/widgets/_devices.html.haml | 57 +++++++++++++++---- 4 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee diff --git a/app/assets/javascripts/farmbot_app/controllers/devices_controller.js.coffee b/app/assets/javascripts/farmbot_app/controllers/devices_controller.js.coffee index 0771f2427..cc23bb964 100644 --- a/app/assets/javascripts/farmbot_app/controllers/devices_controller.js.coffee +++ b/app/assets/javascripts/farmbot_app/controllers/devices_controller.js.coffee @@ -8,11 +8,12 @@ ctrl = [ 'Info' ($scope, Data, Devices, Info) -> nope = (e) -> alert 'Doh!'; console.error e - $scope.logs = Info.logs - $scope.form = Devices + $scope.logs = Info.logs + $scope.device = Devices Devices.socket.on 'ready',-> Devices.fetchLogs (d) -> Info.logs.push(data) for data in (d.data || []) $scope.createDevice = -> Devices.save().error(nope) + $scope.debug = -> debugger ] controller = diff --git a/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee b/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee new file mode 100644 index 000000000..8e1dbe66c --- /dev/null +++ b/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee @@ -0,0 +1,17 @@ +# A button used to set integers +ctrl = [ + '$scope', + 'Devices', + ($scope, Devices) -> + $scope.toggle = -> debugger +] +directive = + restrict: 'AEC' + template: '' + scope: + toggle_val: '@' + link: ($scope, el, attr) -> + el.on 'click', => $scope.toggle() + controller: ctrl + +angular.module("FarmBot").directive 'calibrationbutton', [() -> directive] diff --git a/app/controllers/api/abstract_controller.rb b/app/controllers/api/abstract_controller.rb index 12c09ef6f..24f56869c 100644 --- a/app/controllers/api/abstract_controller.rb +++ b/app/controllers/api/abstract_controller.rb @@ -36,6 +36,7 @@ private if auth.success? @current_device = auth.result else + binding.pry sorry("""You failed to authenticate with the API. Ensure that you have provided a `bot_token` and `bot_uuid` header in the HTTP request. """.squish, 401) diff --git a/app/views/dashboard/ng-partials/widgets/_devices.html.haml b/app/views/dashboard/ng-partials/widgets/_devices.html.haml index 4bb77a9e9..124c57afc 100644 --- a/app/views/dashboard/ng-partials/widgets/_devices.html.haml +++ b/app/views/dashboard/ng-partials/widgets/_devices.html.haml @@ -40,19 +40,19 @@ .row .large-12.columns %label FarmBot Name * - %input{id: 'botname', placeholder: "Brocolli Overlord", type: "text", ng_model: 'form.name', required: true}/ + %input{id: 'botname', placeholder: "Brocolli Overlord", type: "text", ng_model: 'device.name', required: true}/ .row .large-6.columns %label UUID * - %input{placeholder: "ad698900-2546-11e3-87fb-c560cb0ca47b", type: "text", ng_model: 'form.uuid', required: true}/ + %input{placeholder: "ad698900-2546-11e3-87fb-c560cb0ca47b", type: "text", ng_model: 'device.uuid', required: true}/ .large-6.columns %label Security Token * - %input{placeholder: "4bbd2jm242dl5wmimbwz4rvlu77m0a4i", type: "text", ng_model: 'form.token', required: true}/ + %input{placeholder: "4bbd2jm242dl5wmimbwz4rvlu77m0a4i", type: "text", ng_model: 'device.token', required: true}/ .row .large-12.columns %button.green.button-like - {{ !!form._id ? "Update" : "Add" }} FarmBot - %button.gray.button-like{ng_if: '!!form._id', ng_click: 'refreshLogs()', type: 'button'} + {{ !!device._id ? "Update" : "Add" }} FarmBot + %button.gray.button-like{ng_if: '!!device._id', ng_click: 'refreshLogs()', type: 'button'} Fetch Logs / sorry %br @@ -69,11 +69,9 @@ %thead %tr %th.row-axis Axis - %th.row-length Length %th.row-input Max Speed %th.row-input Accelerate For %th.row-input Timeout After - %th.row-input Steps Per mm %th.row-invert Invert %br @@ -86,24 +84,59 @@ %tbody %tr %td X - %td 1.23 m %td - %input + %input{ng_model: "device.MOVEMENT_MAX_SPD_X"} %br mm/s %td - %input + %input{ng_model: "device.MOVEMENT_STEPS_ACC_DEC_X"} %br steps %td - %input + %input{ng_model: "device.MOVEMENT_TIMEOUT_X"} %br seconds + %td{ng_click: "debug()"} + %calibrationbutton{toggle_val: "'MOVEMENT_TIMEOUT_X'"} %td - %input + %button.xx-small.green YES + %td + %button.xx-small.red NO + %tr + %td Y + %td + %input{ng_model: "device.MOVEMENT_MAX_SPD_Y"} + %br + mm/s + %td + %input{ng_model: "device.MOVEMENT_STEPS_ACC_DEC_Y"} %br steps %td + %input{ng_model: "device.MOVEMENT_TIMEOUT_Y"} + %br + seconds + %td{ng_click: "debug()"} + %button.xx-small.green YES + %td + %button.xx-small.green YES + %td + %button.xx-small.red NO + %tr + %td Z + %td + %input{ng_model: "device.MOVEMENT_MAX_SPD_Z"} + %br + mm/s + %td + %input{ng_model: "device.MOVEMENT_STEPS_ACC_DEC_Z"} + %br + steps + %td + %input{ng_model: "device.MOVEMENT_TIMEOUT_Z"} + %br + seconds + %td{ng_click: "debug()"} %button.xx-small.green YES %td %button.xx-small.green YES From 0451b1eae6b6ce33313f71aa89e97aac18908757 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Tue, 11 Aug 2015 18:53:09 -0500 Subject: [PATCH 4/6] Reading of calibration values works! Hooray! --- .../directives/calibrationbutton.js | 35 +++++++++++++++++++ .../directives/calibrationbutton.js.coffee | 17 --------- .../ng-partials/widgets/_devices.html.haml | 19 ++++------ 3 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 app/assets/javascripts/farmbot_app/directives/calibrationbutton.js delete mode 100644 app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee diff --git a/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js b/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js new file mode 100644 index 000000000..b630dcf5f --- /dev/null +++ b/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js @@ -0,0 +1,35 @@ +var directive; + +angular.module("FarmBot").directive('calibrationbutton', [ + + function() { + return { + restrict: 'AEC', + template: '', + scope: { + toggleval: '@' + }, + link: function($scope, el, attr) { + el.on('click', function() { + $scope.toggle(); + }); + }, + controller: [ + '$scope', 'Devices', + function($scope, Devices) { + $scope.label = function() { + if (Devices[this.toggleval]) { + return 'YES' + } else { + return 'NO' + }; + }; + + $scope.toggle = function() { + debugger; + }; + } + ] + }; + } +]); diff --git a/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee b/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee deleted file mode 100644 index 8e1dbe66c..000000000 --- a/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee +++ /dev/null @@ -1,17 +0,0 @@ -# A button used to set integers -ctrl = [ - '$scope', - 'Devices', - ($scope, Devices) -> - $scope.toggle = -> debugger -] -directive = - restrict: 'AEC' - template: '' - scope: - toggle_val: '@' - link: ($scope, el, attr) -> - el.on 'click', => $scope.toggle() - controller: ctrl - -angular.module("FarmBot").directive 'calibrationbutton', [() -> directive] diff --git a/app/views/dashboard/ng-partials/widgets/_devices.html.haml b/app/views/dashboard/ng-partials/widgets/_devices.html.haml index 124c57afc..fc0bd666d 100644 --- a/app/views/dashboard/ng-partials/widgets/_devices.html.haml +++ b/app/views/dashboard/ng-partials/widgets/_devices.html.haml @@ -80,7 +80,6 @@ Invert %br Motor - %th.row-invert Negatives %tbody %tr %td X @@ -96,12 +95,10 @@ %input{ng_model: "device.MOVEMENT_TIMEOUT_X"} %br seconds - %td{ng_click: "debug()"} - %calibrationbutton{toggle_val: "'MOVEMENT_TIMEOUT_X'"} %td - %button.xx-small.green YES + %calibrationbutton{toggleval: "MOVEMENT_INVERT_ENDPOINTS_X"} %td - %button.xx-small.red NO + %calibrationbutton{toggleval: "MOVEMENT_INVERT_MOTOR_Y"} %tr %td Y %td @@ -116,12 +113,10 @@ %input{ng_model: "device.MOVEMENT_TIMEOUT_Y"} %br seconds - %td{ng_click: "debug()"} - %button.xx-small.green YES %td - %button.xx-small.green YES + %calibrationbutton{toggleval: "MOVEMENT_INVERT_ENDPOINTS_Y"} %td - %button.xx-small.red NO + %calibrationbutton{toggleval: "MOVEMENT_INVERT_MOTOR_Y"} %tr %td Z %td @@ -136,12 +131,10 @@ %input{ng_model: "device.MOVEMENT_TIMEOUT_Z"} %br seconds - %td{ng_click: "debug()"} - %button.xx-small.green YES %td - %button.xx-small.green YES + %calibrationbutton{toggleval: "MOVEMENT_INVERT_ENDPOINTS_Z"} %td - %button.xx-small.red NO + %calibrationbutton{toggleval: "MOVEMENT_INVERT_MOTOR_Z"} %p %button.xx-small.yellow.pull-right calibrate From e83cff41c5c269c05ac96927a7ea842d9ed7f33c Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Wed, 12 Aug 2015 11:49:47 -0500 Subject: [PATCH 5/6] Rough draft of working calibration menu --- .../controllers/devices_controller.js.coffee | 4 ++- .../directives/calibrationbutton.js | 35 ------------------- .../farmbot_app/factories/command.js.coffee | 7 ++++ .../ng-partials/widgets/_devices.html.haml | 3 +- 4 files changed, 12 insertions(+), 37 deletions(-) delete mode 100644 app/assets/javascripts/farmbot_app/directives/calibrationbutton.js diff --git a/app/assets/javascripts/farmbot_app/controllers/devices_controller.js.coffee b/app/assets/javascripts/farmbot_app/controllers/devices_controller.js.coffee index cc23bb964..f44c42f29 100644 --- a/app/assets/javascripts/farmbot_app/controllers/devices_controller.js.coffee +++ b/app/assets/javascripts/farmbot_app/controllers/devices_controller.js.coffee @@ -13,7 +13,9 @@ ctrl = [ Devices.socket.on 'ready',-> Devices.fetchLogs (d) -> Info.logs.push(data) for data in (d.data || []) $scope.createDevice = -> Devices.save().error(nope) - $scope.debug = -> debugger + $scope.updateCalibration = -> + console.log 'wut' + Devices.send("update_calibration", Devices) ] controller = diff --git a/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js b/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js deleted file mode 100644 index b630dcf5f..000000000 --- a/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js +++ /dev/null @@ -1,35 +0,0 @@ -var directive; - -angular.module("FarmBot").directive('calibrationbutton', [ - - function() { - return { - restrict: 'AEC', - template: '', - scope: { - toggleval: '@' - }, - link: function($scope, el, attr) { - el.on('click', function() { - $scope.toggle(); - }); - }, - controller: [ - '$scope', 'Devices', - function($scope, Devices) { - $scope.label = function() { - if (Devices[this.toggleval]) { - return 'YES' - } else { - return 'NO' - }; - }; - - $scope.toggle = function() { - debugger; - }; - } - ] - }; - } -]); diff --git a/app/assets/javascripts/farmbot_app/factories/command.js.coffee b/app/assets/javascripts/farmbot_app/factories/command.js.coffee index 6863e2c53..45d718225 100644 --- a/app/assets/javascripts/farmbot_app/factories/command.js.coffee +++ b/app/assets/javascripts/farmbot_app/factories/command.js.coffee @@ -13,6 +13,13 @@ class Command return Command.all[type](args) @all: + update_calibration: (the_whole_friggin_bot) -> + { + message_type: 'update_calibration' + command: the_whole_friggin_bot + } + + read_status: (values) -> message_type: 'read_status' diff --git a/app/views/dashboard/ng-partials/widgets/_devices.html.haml b/app/views/dashboard/ng-partials/widgets/_devices.html.haml index fc0bd666d..262d6d5ee 100644 --- a/app/views/dashboard/ng-partials/widgets/_devices.html.haml +++ b/app/views/dashboard/ng-partials/widgets/_devices.html.haml @@ -136,8 +136,9 @@ %td %calibrationbutton{toggleval: "MOVEMENT_INVERT_MOTOR_Z"} %p - %button.xx-small.yellow.pull-right + %button.xx-small.yellow.pull-right{ng_click: "updateCalibration()"} calibrate + .row .small-12.columns .row From 3ca8d34ba833be3d484908b22e3ef3d6b9f9a431 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Wed, 12 Aug 2015 11:49:53 -0500 Subject: [PATCH 6/6] Rough draft of working calibration menu --- .../directives/calibrationbutton.js.coffee | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee diff --git a/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee b/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee new file mode 100644 index 000000000..0428fb975 --- /dev/null +++ b/app/assets/javascripts/farmbot_app/directives/calibrationbutton.js.coffee @@ -0,0 +1,25 @@ +angular.module('FarmBot').directive 'calibrationbutton', [ -> + { + restrict: 'AEC' + template: '' + scope: toggleval: '@' + link: ($scope, el, attr) -> + el.on 'click', -> $scope.toggle() + controller: [ + '$scope' + 'Devices' + ($scope, Devices) -> + $scope.isTrue = -> if Devices[@toggleval] then yes else no + $scope.label = -> + if Devices[@toggleval] then 'YES' else 'NO' + $scope.toggle = -> + Devices[this.toggleval] = if Devices[this.toggleval] then 0 else 1 + Devices.send("update_calibration", _.pick(Devices, this.toggleval)) + ] + } + ]