Extending Piece functionality

Contents

This page explains the process of adding functionality to Pieces made with Lambda.


Current location data

While making a Piece, you may want to utilize a user’s current location data. If that’s the case, use the following procedure to request the user’s location in a simple way.

Amending PieceJSON

Add the parameters line to the PieceJSON in the same manner as below:

"serviceProxy":{
"service":"citizenSample",
"parameters":["location"]
},

Use the parameters shown in the table below to select an "accuracy" level, or the degree of detail you want to utilize user location. The more accuracy you want, the more time will be needed to obtain the location information.

ParametersAccuracy (measurement error)
"location_rough"Around ±10 km
"location_normal"Around ±100 m
"location_accurate" or "location"Highest achievable accuracy
Acquiring current location data with ProxyCore

Once you’ve added parameters to PieceJSON, you can obtain latitudinal and longitudinal information in ProxyCore using the method displayed below.

exports.handler = async event =>{
     const latitude = event.properties.parameters.location.latitude;
     const longitude = event.properties.parameters.location.longitude;
     // Execute processing using location information
}

Controlling the watch hands

When you create an S Piece that you want to output information that will move the hands on a watch, there are two possible choices: You can choose to connect to a "3 Hand Action" A Piece that treats each watch hand as its own value, or you can choose to connect to a "Multi Hand <Time>" A Piece that will visualize your result as a time. Here, we will explain how to create S Pieces that connect with each of these two A Pieces.


3 Hand Action

Below, you’ll learn how to move the watch hands to desired "3 Hand Action" positions to indicate the result of your Service Piece.

Amending PieceJSON

In order to match the input values of a "3 Hand Action" A Piece, which is expecting up to 3 numbers from your Service Piece, the PieceJSON output values should be set as follows.

exports.handler = async event => {
  const date = event.userData.date;
  
  // Code here using the execution date and time information
  
}
ProxyCore output processing

ProxyCore output processing, based on PieceJSON output, is seen in the next code block. This example shows how to set each of the watch hands to a value of 10.

"output":{
  "type":"object",
  "properties":{
    "SpecificPositionArray":{
      "type":"array",
      "items":{
        "type":"object",
        "properties":{
          "Hour":{
            "type":"number",
            "minimum":0,
            "maximum":359
          },
          "Minute":{
            "type":"number",
            "minimum":0,
            "maximum":359       
          },
          "Second":{
            "type":"number",
            "minimum":0,
            "maximum":59       
          }
        }
      }
    }
  }
}
exports.handler = async event =>{

// The second hand is set to 10 seconds and the hour and minute hands are set to 10°. (For the second hand, specify a value in the range of 0 to 59. For the hour and minute hands, specify a value in the range of 0 to 359.)

  let responseData = {
    status:200, 
    body:{
      "SpecificPositionArray":[{
        "Second":10,
        "Minute":10,
        "Hour":10
      }]
    }
  };
  
  return responseData;

}

Multi Hand <Time>

Below, you’ll learn how to move the watch hands to desired "Multi Hand <Time>" positions to indicate the result of your Service Piece.

Amending PieceJSON

In order to match the input values of a "Multi Hand <Time>" A Piece, which is expecting a time output from your Service Piece, the PieceJSON output values should be set as follows.

"output":{
  "type":"object",
  "properties":{
    "returnTime":{
      "type":"string",
      "format":"time"
    }
  }
}
ProxyCore output processing

ProxyCore output processing, based on PieceJSON output, is seen here:

exports.handler = async event =>{
  let hour = 2;
  let min = 30;
  let sec = 10;
// The output format must be 'hh:mm:ss'. Zero padding is required when the value is between 0 to 9. 
  let output = hour.toString().padStart(2,'0') + ':' +
  min.toString().padStart(2,'0') + ':' + 
  sec.toString().padStart(2,'0'); 
  let responseData = {
    status: 200,
    body: {
      "returnTime": output
    }
  };
  return responseData;
}