Close

Plugin-API demonstration

A project log for SmartRoom

A Raspberry Pi and Arduino based voice enabled smart room system that controls lighting and music and responses to questions.

bennetBennet 03/22/2016 at 16:580 Comments

Here is a little example plugin that uses the CloudFileManager, Properties, RaspiCam, Logging and MQTT functionality provided by the plugin API.

When you publish a string representing the desired file name into the topic home/room/picam/upload, this plugin takes a photo with dimensions and quality as specified in the PiCamUpload.properties and upload it to dropbox. If "current_time" is sent , the current date and time will be used as file name.

public class PiCamUpload extends BasePlugin {

 @Override
 public void onStart() {

  // get CloudFileManager implementation from plugins (dropbox in this case)
  CloudFileManager cfm = getSystemManager().getCloudFileManager();
		
  Objects.requireNonNull(cfm, "A CloudFileManager implementation is needed for this plugin to work.");

  // get properties from this plugins's .properties file
  int width = Integer.parseInt(getProperties().getProperty("width"));
  int height = Integer.parseInt(getProperties().getProperty("height"));
  int quality = Integer.parseInt(getProperties().getProperty("quality"));

  // subscribe to a fitting topic
  MQTT.get().subscribe("home/room/picam/upload", (MqttMessage m) -> {

  // convert binary payload to ascii string
  String filename = new String(m.getPayload(), StandardCharsets.US_ASCII);
  if (filename.equals("current_time"))
    filename = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date());
			
  File img = new File("./" + filename + ".jpg");
			
  try {
    img.createNewFile();
  } catch (IOException e) {
    getLog().error("Exception", e);
  }
  RaspiCamera.take(img, width, height, quality);
  try {
    cfm.uploadFile(img, null);
  } catch (FileNotFoundException e) {
    getLog().error("Exception", e);
  }
 });
}
}

Discussions