Add mode column back, add fbos_version to devices table + TS interfaces.

pull/664/head
Rick Carlino 2018-02-15 15:43:08 -06:00
parent 2d41a18b60
commit 34e74bf8f4
10 changed files with 54 additions and 28 deletions

View File

@ -195,7 +195,8 @@ private
# We update this column every time an FBOS device talks to the API.
def mark_as_seen(entity = (current_user && current_user.device))
when_farmbot_os do
entity.update_attributes(last_saw_api: Time.now) if entity
entity.update_attributes(last_saw_api: Time.now,
fbos_version: fbos_version.to_s) if entity
end
end
end

View File

@ -12,7 +12,8 @@ class ApplicationRecord < ActiveRecord::Base
"sign_in_count",
"updated_at",
"current_sign_in_ip",
"current_sign_in_at" ]
"current_sign_in_at",
"fbos_version" ]
def the_changes
self.saved_changes.except(*self.class::DONT_BROADCAST)
end

View File

@ -15,7 +15,7 @@ class Device < ApplicationRecord
has_many :tools, dependent: :destroy
has_many :images, dependent: :destroy
has_many :webcam_feeds, dependent: :destroy
has_many :sensor_reading, dependent: :destroy
has_many :sensor_readings, dependent: :destroy
validates :timezone, inclusion: { in: TIMEZONES,
message: BAD_TZ,
allow_nil: true }
@ -30,22 +30,6 @@ class Device < ApplicationRecord
logs.all.last(max_log_count || DEFAULT_MAX_LOGS)
end
# def auth_token
# SessionToken.as_json(self.users.first)[:token].encoded
# end
# # Send a realtime message to a logged in user.
# def tell(message, chan = "toast")
# log = Log.new({ device: self,
# message: message,
# created_at: Time.now,
# channels: [chan],
# meta: { type: "info" } })
# json = LogSerializer.new(log).as_json.to_json
# Transport.amqp_send(json, self.id, "logs")
# end
def self.current
RequestStore.store[:device]
end

View File

@ -1,6 +1,6 @@
class DeviceSerializer < ActiveModel::Serializer
attributes :id, :name, :timezone, :last_saw_api, :last_saw_mq,
:last_seen, :tz_offset_hrs
attributes :id, :name, :timezone, :last_saw_api, :last_saw_mq, :last_seen,
:tz_offset_hrs, :fbos_version
def last_seen
# TODO: Remove this by December 2017.

View File

@ -1,7 +1,3 @@
class PeripheralSerializer < ActiveModel::Serializer
attributes :id, :pin, :label, :mode
def mode
-1 # DEPRECATED. Still here for legacy reasons. Don't use it. - RC
end
end

View File

@ -0,0 +1,6 @@
class UpdateDevicesAndPeripherals < ActiveRecord::Migration[5.1]
def change
add_column :devices, :fbos_version, :string, limit: 15 # "99.99.99-rc99"
add_column :peripherals, :mode, :integer
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180215171709) do
ActiveRecord::Schema.define(version: 20180215205625) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -38,6 +38,7 @@ ActiveRecord::Schema.define(version: 20180215171709) do
t.string "timezone", limit: 280
t.datetime "last_saw_api"
t.datetime "last_saw_mq"
t.string "fbos_version", limit: 15
t.index ["timezone"], name: "index_devices_on_timezone"
end
@ -224,6 +225,7 @@ ActiveRecord::Schema.define(version: 20180215171709) do
t.string "label", limit: 280
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "mode"
t.index ["device_id"], name: "index_peripherals_on_device_id"
end

View File

@ -29,7 +29,7 @@ class LogService
if(major_version >= 6)
device_id = delivery_info.routing_key.split(".")[1].gsub("device_", "").to_i
if save?(log)
device = Device.find(device_id)
device = Device.find(device_id)
db_log = Logs::Create.run!(log, device: device)
db_log.save!
maybe_clear_logs(device)

View File

@ -14,7 +14,7 @@ describe Api::SensorReadingsController do
body: { pin: 13, value: 128, x: nil, y: 1, z: 2 }.to_json,
params: { format: :json }
expect(response.status).to eq(200)
expect(response.status).to eq(200)
expect(json[:id]).to be_kind_of(Integer)
expect(json[:value]).to eq(128)
expect(json[:device_id]).to eq(nil) # Use the serializer, not as_json.
@ -25,6 +25,41 @@ describe Api::SensorReadingsController do
expect(before < SensorReading.count).to be_truthy
end
it 'shows one reading' do
sign_in user
SensorReading.destroy_all
id = reading.id
get :show, params: { format: :json, id: id }
expect(json).to be_kind_of(Hash)
reading.reload
[ :id, :value, :x, :y, :z, :pin ].map do |attr|
expect(json[attr]).to eq(reading.send(attr))
end
end
it 'shows all readings' do
sign_in user
SensorReading.destroy_all
id = reading.id
get :index, params: { format: :json }
expect(json).to be_kind_of(Array)
expect(json.length).to eq(user.device.sensor_readings.length)
keys = json.first.keys
expect(json.map{|x| x[:id] }).to include(id)
expect(keys).to include(:x, :y, :z, :value, :pin)
end
it 'destroys a reading' do
sign_in user
SensorReading.destroy_all
id = reading.id
before = SensorReading.count
expect(before).to eq(1)
delete :destroy, params: { format: :json, id: id }
expect(SensorReading.where(id: id).count).to eq(0)
expect(before).to be > SensorReading.count
end
it 'requires logged in user' do
post :create, params: {}
expect(response.status).to eq(401)

View File

@ -46,6 +46,7 @@ export interface DeviceAccountSettings {
name: string;
timezone?: string | undefined;
tz_offset_hrs: number;
fbos_version?: string | undefined;
last_saw_api?: string | undefined;
last_saw_mq?: string | undefined;
}