Skip to content
Snippets Groups Projects
CommsBehaviours.h 2.34 KiB
Newer Older
Joe Revans's avatar
Joe Revans committed
#ifndef COMMS_BEHAVIOUR_h
#define COMMS_BEHAVIOUR_h
#include "Arduino.h"
#include "Behaviours.h"
Joe Revans's avatar
Joe Revans committed
#include <VizBlocks.h>
Joe Revans's avatar
Joe Revans committed

/*
 * --------------------------------------------------
 * ---------------- SendCapabilities ----------------
 * --------------------------------------------------
 */

Matthew's avatar
Matthew committed
class SendCapabilities : public Behaviour
{
	VizBlocks* _node;
Joe Revans's avatar
Joe Revans committed

public:
Matthew's avatar
Matthew committed
	SendCapabilities(VizBlocks* node, String name = "SendCapabilities") :
		Behaviour(name), _node(node)
	{ }
Joe Revans's avatar
Joe Revans committed

Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	String start(String args)
	{
		 //This is where you do your stuff for a simple behaviour
		_node->announce_capabilities();

		return "SendCapabilities behaviour " + _name;
	}
Joe Revans's avatar
Joe Revans committed

};

/*
 * --------------------------------------------------
 * ---------------------- Link ----------------------
 * --------------------------------------------------
 */

Matthew's avatar
Matthew committed
class Link : public Behaviour
{
	VizBlocks* _node;
	String _peerId;
	const int _timeoutInterval = 5000;
	unsigned long _t = 0;
Joe Revans's avatar
Joe Revans committed

public:
Matthew's avatar
Matthew committed
	Link(VizBlocks* node, String name = "Link") : Behaviour(name), _node(node)
	{
		_background = true;
	}

Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	char* args()
	{
		return "<String peerId>";
	};

Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	String start(String args)
	{
		_running = true;

		if (args == name() || args.indexOf(" ")>0)
		{
			return "Invalid args (" + args + ") in behaviour " + name();
		}

		_t = millis();

		if (args == _peerId)
		{
			return "Link ping from (" + _peerId + ")";
		}

		_peerId = args;

		String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Link\":{\"peerId\":\"" + _peerId + "\"}}";
		_node->announce(str);

		return "New link with (" + _peerId + ")";
	}

Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	void update()
	{
		if (millis() > (_t+_timeoutInterval))
		{
			String str = "{\"id\":\"" + String(_node->getId()) + "\",\"Unlink\":{\"peerId\":\"" + _peerId + "\"}}";
			_node->announce(str);
			_peerId = "";
			_running = false;
		}
	}
Joe Revans's avatar
Joe Revans committed

};

/*
 * --------------------------------------------------
 * ------------------- PingServer -------------------
 * --------------------------------------------------
 */

Matthew's avatar
Matthew committed
class PingServer : public Behaviour
{
	VizBlocks* _node;
	String str;
	const int _interval = 4000;
	unsigned long _t = 0;
Joe Revans's avatar
Joe Revans committed

public:
Matthew's avatar
Matthew committed
	PingServer(VizBlocks* node, String name = "PingServer");
	String start(String args);
	void update();
Joe Revans's avatar
Joe Revans committed

};

#endif