Merge pull request #1758 from FarmBot/mark_as

Phase 0: Ability to pass variables to MARK AS step
pull/1759/head
Rick Carlino 2020-04-16 13:27:52 -05:00 committed by GitHub
commit 643bcb1a37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 166 additions and 133 deletions

View File

@ -38,7 +38,7 @@ module CeleryScriptSettingsBag
factory_reset find_home flash_firmware home
install_farmware install_first_party_farmware _if
move_absolute move_relative power_off read_pin
read_status reboot remove_farmware resource_update
read_status reboot remove_farmware update_resource
send_message set_servo_angle set_user_env sync
take_photo toggle_pin update_farmware wait
write_pin zero)
@ -74,7 +74,6 @@ module CeleryScriptSettingsBag
"as input. Please change your selection to a single" \
" location."
PLANT_STAGES = %w(planned planted harvested sprouted)
RESOURCE_UPDATE_ARGS = [:resource_type, :resource_id, :label, :value]
SCOPE_DECLARATIONS = [:variable_declaration, :parameter_declaration]
MISC_ENUM_ERR = '"%s" is not valid. Allowed values: %s'
MAX_WAIT_MS = 1000 * 60 * 3 # Three Minutes
@ -82,6 +81,13 @@ module CeleryScriptSettingsBag
"A single wait node cannot exceed #{MAX_WAIT_MS / 1000 / 60} minutes. " +
"Consider lowering the wait time or using multiple WAIT blocks."
Corpus = CeleryScript::Corpus.new
THIS_IS_DEPRECATED = {
args: [:resource_type, :resource_id, :label, :value],
tags: [:function, :api_writer, :network_user],
blk: ->(n) do
n.invalidate!("Deprecated `mark_as` detected. Delete it and re-add")
end,
}
CORPUS_VALUES = {
boolean: [TrueClass, FalseClass],
@ -278,6 +284,9 @@ module CeleryScriptSettingsBag
lua: {
defn: [v(:string)],
},
resource: {
defn: [n(:identifier), n(:resource)],
},
}.map do |(name, conf)|
blk = conf[:blk]
defn = conf.fetch(:defn)
@ -513,15 +522,22 @@ module CeleryScriptSettingsBag
tags: [:function, :firmware_user, :rpi_user],
blk: ->(n) { no_rpi_analog(n) },
},
resource_update: {
args: RESOURCE_UPDATE_ARGS,
tags: [:function, :api_writer, :network_user],
# DEPRECATED- Get rid of this node ASAP -RC 15 APR 2020
resource_update: THIS_IS_DEPRECATED,
resource: {
args: [:resource_type, :resource_id],
tags: [:network_user],
blk: ->(n) do
resource_type = n.args.fetch(:resource_type).value
resource_id = n.args.fetch(:resource_id).value
check_resource_type(n, resource_type, resource_id, Device.current)
end,
},
update_resource: {
args: [:resource],
body: [:pair],
tags: [:function, :api_writer, :network_user],
},
point_group: {
args: [:point_group_id],
tags: [:data, :list_like],
@ -529,7 +545,7 @@ module CeleryScriptSettingsBag
resource_id = n.args.fetch(:point_group_id).value
check_resource_type(n, "PointGroup", resource_id, Device.current)
end,
}
},
}.map { |(name, list)| Corpus.node(name, **list) }
HASH = Corpus.as_json

View File

@ -148,19 +148,36 @@ describe CeleryScript::Corpus do
expect(checker.error.message).to include("Tool #0 does not exist.")
end
it "Validates resource_update nodes" do
ast = { "kind": "resource_update",
"args": { "resource_type" => "Device",
it "Validates update_resource nodes" do
ast = {
kind: "update_resource",
args: {
"resource" => {
kind: "resource",
args: {
"resource_type" => "Device",
"resource_id" => 23, # Mutated to "0" later..
},
},
},
body: [
{
kind: "pair",
args: {
"label" => "mounted_tool_id",
"value" => 1 } }
"value" => 1,
},
},
],
}
checker = CeleryScript::Checker
.new(CeleryScript::AstNode.new(**ast), corpus, device)
expect(checker.valid?).to be(true)
expect(checker.tree.args[:resource_id].value).to eq(device.id)
device_id = checker.tree.args[:resource].args[:resource_id].value
expect(device_id).to eq(device.id)
end
it "rejects bogus resource_updates" do
it "deprecates resource_updates" do
fake_id = FactoryBot.create(:plant).id + 1
expect(Plant.exists?(fake_id)).to be(false)
ast = { "kind": "resource_update",
@ -172,20 +189,8 @@ describe CeleryScript::Corpus do
expect(hmm.args.fetch(:resource_id).value).to eq(fake_id)
checker = CeleryScript::Checker.new(hmm, corpus, device)
expect(checker.valid?).to be(false)
expect(checker.error.message).to eq("Can't find Plant with id of #{fake_id}")
end
it "rejects bogus resource_types" do
ast = { "kind": "resource_update",
"args": { "resource_type" => "CanOpener",
"resource_id" => 0,
"label" => "foo",
"value" => "Should Fail" } }
checker = CeleryScript::Checker.new(CeleryScript::AstNode.new(**ast),
corpus,
device)
expect(checker.valid?).to be(false)
expect(checker.error.message).to include('"CanOpener" is not a valid resource_type.')
msg = "Deprecated `mark_as` detected. Delete it and re-add"
expect(checker.error.message).to eq(msg)
end
it "has enums" do

View File

@ -167,13 +167,25 @@ describe Points::Destroy do
def mark_as(resource)
{
kind: "resource_update",
kind: "update_resource",
args: {
resource: {
kind: "resource",
args: {
resource_type: resource.class.to_s,
resource_id: resource.id,
},
},
},
body: [
{
kind: "pair",
args: {
label: "foo",
value: "bar",
},
},
],
}
end