(Classic) Creating a SquareTag Application

Creating a SquareTag Application

After you have created a CloudOS Hello World App, creating a SquareTag application is quite simple. I'll be using the final result from our Hello World app as the starting point for our SquareTag app. If you have not followed the CloudOS Hello World Tutorial, go do that now.

At the end of the CloudOS tutorial, you should have a ruleset similar to:

ruleset a41x216 {
  meta {
    name "Hello World"
    description <<
      Hello World
    >>
    author ""
    logging off
    use module a169x701 alias CloudRain
  }
  dispatch {
  }
  global {
  }
  rule HelloWorld {
    select when web cloudAppSelected
    pre {
      my_html = <<
        <h5>Hello, world!</h5>
      >>;
    }
    {
      CloudRain:createLoadPanel("Hello World!", {}, my_html);
    }
  }
}

Getting Ready for SquareTag

To get our application ready for SquareTag, we're going to 

  1. Load the SquareTag module
  2. Add an action to the HelloWorld rule to inject SquareTag specific styling. 

We load the module just as we did the CloudRain module by adding a use statement to the meta section of the ruleset:

use module a41x196 alias SquareTag

We add the styling by adding an action to the rule:

SquareTag:inject_styling();

When you're done, the ruleset should look like this:

ruleset a41x216 {
  meta {
    name "Hello World"
    description <<
      Hello World
    >>
    author ""
    logging off
    use module a169x701 alias CloudRain
    use module a41x196 alias SquareTag
  }
  dispatch {
  }
  global {
  }
  rule HelloWorld {
    select when web cloudAppSelected
    pre {
      my_html = <<
        <h5>Hello, world!</h5>
      >>;
    }
    {
      SquareTag:inject_styling();
      CloudRain:createLoadPanel("Hello World!", {}, my_html);
    }
  }
} 

Once your ruleset is loading the SquareTag module, there are two steps to making this action fire when a tag is scanned:

  1. Adding events to the rule that select on scans.
  2. Making this application the default app for a tag so that it sees the scan. 

We'll cover each of these in turn in the following sections. 

Firing on SquareTag Scans

When a tag is scanned, the SquareTag service in the cloud associated with tag raises specific events. Your HelloWorld rule must be modified so that it is listening for those events.  Looking at the documentation of the SquareTag Service, we can see that there are three different events that we can respond to if we so choose. Let's modify our select statement to fire on all three:

rule appSelected {
	select when web cloudAppSelected
	         or explicit isOwner
	         or explicit isNotOwner
	         or explicit isAnonymous

By adding those events to your select statement, you have made it possible for your app to run on a SquareTag Scan!

Note

You can have more than one rule in your ruleset so that you respond to the explicit:isOwner, explicit:isNotOwner, and explicit:isAnonymous events differently. Further, more than one rule can listen to any given event.

Making the App the Default

The next step to have your SquareTag Application run is to set your application as the SquareTag default. This can be done with a combination of the SquareTag Service and a rule in your ruleset.

In the rule which gets run when the application is selected (HelloWorld in the example), add a call to the get_default_app_html() function. Then insert the returned HTML into your displayed HTML using a beesting. Your rule's prelude should now look like this:

pre {
	defaultAppHtml = SquareTag:get_default_app_html();
	
	my_html = <<
		#{defaultAppHtml}
		<h5>Hello, world!</h5>
	>>;
}

Next we need to create a rule that handles the web:cloudAppAction event when the action attribute has the value makeDefault. The rule will raise an event into the thing's (Classic) PDS to set the defaultOwnerApp and defaultNonOwnerApp. It is possible to have different apps run based on whether the scanner is the owner or if they are a non-owner of the thing. To change the default of both the owner and the non-owner, create the following rule:

rule makeDefault {
	select when web cloudAppAction action re/makeDefault/
	{
		replace_html("#makeDefaultSquareTagApp", "");
		CloudRain:hideSpinner();
	}
	fired {
		raise pds event new_settings_attribute
			with _api = "sky"
			 and setRID = SquareTagRID
			 and setAttr = "defaultOwnerApp"
			 and setValue = meta:rid();
		raise pds event new_settings_attribute
			with _api = "sky"
			 and setRID = SquareTagRID
			 and setAttr = "defaultNonOwnerApp"
			 and setValue = meta:rid();
		}
	}
}

Raising these events into the PDS will cause your application to become the default whenever a tag associated with this cloud is scanned. We also need to set SquareTagRID in the global. Add the following code to the global section:

global {
	...
	SquareTagRID = "a41x178";
}

Your app should now look like:

ruleset a41x216 {
  meta {
    name "Hello World"
    description <<
      Hello World
    >>
    author ""
    logging off
    use module a169x701 alias CloudRain
    use module a41x196 alias SquareTag
  }
  dispatch {
  }
  global {
    SquareTagRID = "a41x178";
  }
  rule appSelected {
    select when web cloudAppSelected
             or explicit isOwner
             or explicit isNotOwner
             or explicit isAnonymous
    pre {
      defaultAppHtml = SquareTag:get_default_app_html();
      my_html = <<
        #{defaultAppHtml}
        <h5>Hello, world!</h5>
      >>;
    }
    {
      SquareTag:inject_styling();
      CloudRain:createLoadPanel("Hello World!", {}, my_html);
    }
  }
  rule makeDefault {
    select when web cloudAppAction action re/makeDefault/
    {
      replace_html("#makeDefaultSquareTagApp", "");
      CloudRain:hideSpinner();
    }
    fired {
      raise pds event new_settings_attribute
        with _api = "sky"
        and setRID = SquareTagRID
        and setAttr = "defaultOwnerApp"
        and setValue = meta:rid();
      raise pds event new_settings_attribute
        with _api = "sky"
        and setRID = SquareTagRID
        and setAttr = "defaultNotOwnerApp"
        and setValue = meta:rid();
    }
  }
}

Final Steps

If you already have a tag and have already associated it with something, all that's left is installing, running, and setting the app as the default.

  1. Log in as your thing (Go to myThings, click the gear menu when hovering over one of your things, select "Login As...").
  2. Once you are logged in as your thing, go through the same steps for installing the app as you did in the CloudOS Hello World Tutorial. You need to change the cloudType from cloudTypeThing to cloudTypeDeveloper.
  3. Install your application as a devApp and save it.
  4. Go back to myApps and run the application.
  5. Select the "Make Default" button.
  6. Scan your tag! You should see "Hello World" displayed!

What's Next?

Once you've got a basic hello world app working, why not use all of the other CloudOS modules and services at your disposal to build a more complex app?

Copyright Picolabs | Licensed under Creative Commons.