The Real Blacks Projekte ConnectionUtil

From Tinkerunity
Jump to: navigation, search

Verwendung: Im Konstruktor:

new BrickConnectionUtil(this);

Zeitversetzt oder per Button:

// fügt alle per Enumerate gelesenen Bricks der Connection hinzu.
BrickConnectionUtil.GetInstance().AddAllBricks();

// alle Bricks durchgehen -> und für aktuelle Anwendung speichern:
foreach(DataBucketDevice t in EnumerateUtil.GetInstance().dataBucketDeviceList)
{...

Der Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tinkerforge;

namespace TRB_TF_Utils
{
	/// <summary>
	/// Diese Klasse speichert alle Werte aus der enumeration.
	/// </summary>
	public class DataBucket_Triple
	{
		public String Name = "";
		public String Uid = "";
		public byte StackID = 0;
		public bool NewFlag = false;
		public BrickType Typ = BrickType.None;
	
		/// <summary>
		/// Der Konstruktor
		/// </summary>
		/// <param name="Name">Brickname</param>
		/// <param name="Uid">Brick ID</param>
		/// <param name="StackID">Stack ID</param>
		/// <param name="NewFlag">Flag</param>
		public DataBucket_Triple(String Name, String Uid,
			byte StackID, bool NewFlag)
		{
			this.Name = Name;
			this.Uid = Uid;
			this.StackID = StackID;
			this.NewFlag = NewFlag;
			this.Typ = Util.ParseBrickName(Name);
		}


		/// <summary>
		/// Wird durch die Util Klasse gesetzt.
		/// </summary>
		internal bool AddedByUtilToIPConnection = false;
		/// <summary>
		/// Wird durch den DataBucketDevice Konstruktor gesetzt
		/// </summary>
		internal DataBucketDevice backRef = null;
	}

	/// <summary>
	/// Speichert das Device und hat eine
	/// Referenz zum Tripe an informationen.
	/// </summary>
	public class DataBucketDevice
	{
		public Device d = null;
		public DataBucket_Triple informations = null;
		public IPConnection conn = null;

		/// <summary>
		/// Konstruktor
		/// </summary>
		/// <param name="device">Das Device</param>
		/// <param name="info">Der Info Link</param>
		/// <param name="ipconn">Die Connection</param>
		public DataBucketDevice(Device device, DataBucket_Triple info, IPConnection ipconn)
		{
			d = device;
			informations = info;
			conn = ipconn;
			info.backRef = this;
		}
	}


	/// <summary>
	/// Diese Klasse kümmert sich um alle Enumerations Bedürfnisse.
	/// Am Ende gibt es eine Liste mit allen Bricks.
	/// </summary>
	public class EnumerateUtil
	{
		/// <summary>
		/// Singelton Instanz
		/// </summary>
		static EnumerateUtil Instance = null;

		/// <summary>
		/// Gibt die Instanz zurück oder wirft eine InvalidOperationException.
		/// </summary>
		/// <returns>Die Hilfsklasse</returns>
		public static EnumerateUtil GetInstance(){
			if(Instance != null)
			{
				return Instance;
			}
			else
			{
				throw new InvalidOperationException("Instanz ist nicht initialisiert!");
			}
		}

		/// <summary>
		/// Liste der Bricks.
		/// </summary>
		public List<DataBucket_Triple> dataBucketList = new List<DataBucket_Triple>();
		/// <summary>
		/// Alle devices nochmal in einer eigenen Liste.
		/// </summary>
		public List<Device> deviceList = new List<Device>();
		/// <summary>
		/// Alle DataBucketDevice nochmal in einer eigenen Liste.
		/// </summary>
		public List<DataBucketDevice> dataBucketDeviceList = new List<DataBucketDevice>();

		/// <summary>
		/// erstellt eine neue Instanz und speihert diese 
		/// als Singelton ab.
		/// </summary>
		/// <param name="ipcon">eine geöffnete IP Connection</param>
		public EnumerateUtil(IPConnection ipcon)
		{
			if(ipcon == null) {
				throw new ArgumentNullException("IP Connection darf nicht null sein!");
			}
			EnumerateUtil.Instance = this;
			ipcon.Enumerate(new IPConnection.EnumerateCallback(EnumerateCB));
		}

		/// <summary>
		/// Callback 
		/// </summary>
		/// <param name="uid">Die Brick ID</param>
		/// <param name="name">Der Brickname</param>
		/// <param name="stackID">Die Stack ID</param>
		/// <param name="isNew">new Flag</param>
		void EnumerateCB(string uid, string name, byte stackID, bool isNew)
		{
			DataBucket_Triple tmp 
				= new DataBucket_Triple(name,uid,stackID,isNew);

			//tmp.Name = name;
			//tmp.Uid = uid;
			//tmp.StackID = stackID;
			//tmp.NewFlag = isNew;

			dataBucketList.Add(tmp);

			#region off - Debugausgaben
			//*
			if(isNew)
			{
				System.Console.WriteLine("New Device:");
			}
			else
			{
				System.Console.WriteLine("Device Removed:");
			}
			System.Console.WriteLine(" name:    " + name);
			System.Console.WriteLine(" uid:     " + uid);
			System.Console.WriteLine(" stackID: " + stackID);
			// */
			#endregion
		}

		/// <summary>
		/// Fügt alle Bricks der übergebenen Connection 
		/// hinzu und setzt alles in die Listen.
		/// </summary>
		/// <param name="ipconn"></param>
		/// <returns>true alle Bricks wurden übergeben</returns>
		public static bool AddAllBricksToConnection(IPConnection ipconn)
		{
			bool ret = false;

			if(ipconn != null &&
				EnumerateUtil.Instance != null)
			{
				ret = true;
				// Add alle Enumeration items
				foreach(DataBucket_Triple t in EnumerateUtil.Instance.dataBucketList)
				{
					Device tmp = null;
					switch(t.Typ)
					{
						case BrickType.AmbientLight_Bricklet:
							tmp = new BrickletAmbientLight(t.Uid);
							break;
						case BrickType.AnalogIn_Bricklet:
							tmp = new BrickletAnalogIn(t.Uid);
							break;
						case BrickType.AnalogOut_Bricklet:
							tmp = new BrickletAnalogOut(t.Uid);
							break;
						case BrickType.Buzzer_Bricklet:
							tmp = new BrickletPiezoBuzzer(t.Uid);
							break;
						case BrickType.Current12_Bricklet:
							tmp = new BrickletCurrent12(t.Uid);
							break;
						case BrickType.Current25_Bricklet:
							tmp = new BrickletCurrent25(t.Uid);
							break;
						case BrickType.DC_Brick:
							tmp = new BrickDC(t.Uid);
							break;
						case BrickType.DistantzIR_Bricklet:
							tmp = new BrickletDistanceIR(t.Uid);
							break;
						case BrickType.Humidity_Bricklet:
							tmp = new BrickletHumidity(t.Uid);
							break;
						case BrickType.IMU_Brick:
							tmp = new BrickIMU(t.Uid);
							break;
						case BrickType.IO16_Bricklet:
							tmp = new BrickletIO16(t.Uid);
							break;
						case BrickType.IO4_Bricklet:
							tmp = new BrickletIO4(t.Uid);
							break;
						case BrickType.Joystick_Bricklet:
							tmp = new BrickletJoystick(t.Uid);
							break;
						case BrickType.LCD16x2_Bricklet:
							tmp = new BrickletLCD16x2(t.Uid);
							break;
						case BrickType.LCD20x4_Bricklet:
							tmp = new BrickletLCD20x4(t.Uid);
							break;
						case BrickType.Master_Brick:
							tmp = new BrickMaster(t.Uid);
							break;
						case BrickType.Poti_Linear_Bricklet:
							tmp = new BrickletLinearPoti(t.Uid);
							break;
						case BrickType.Poti_Rotary_Bricklet:
							tmp = new BrickletPiezoBuzzer(t.Uid);
							break;
						case BrickType.Relay_Dual_Bricklet:
							tmp = new BrickletDualRelay(t.Uid);
							break;
						case BrickType.Servo_Brick:
							tmp = new BrickServo(t.Uid);
							break;
						case BrickType.Stepper_Brick:
							tmp = new BrickStepper(t.Uid);
							break;
						case BrickType.Temperature_Ambient_Bricklet:
							tmp = new BrickletTemperature(t.Uid);
							break;
						case BrickType.Temperature_IR_Bricklet:
							tmp = new BrickletTemperatureIR(t.Uid);
							break;
						case BrickType.Voltage_Bricklet:
							tmp = new BrickletVoltage(t.Uid);
							break;
						case BrickType.Chibi:
						case BrickType.StepDown_Brick:
						case BrickType.RS485:
						case BrickType.None:
						default:
							ret = false;
							break;
					} // switch

					if(tmp != null)
					{
						t.AddedByUtilToIPConnection = true;
						// EnumerateUtil.Instance.list
						EnumerateUtil.Instance.deviceList.Add(tmp);
						EnumerateUtil.Instance.dataBucketDeviceList.Add(
							new DataBucketDevice(tmp, t, ipconn));

						ipconn.AddDevice(tmp);
						
					}
				}

			}
			else
			{
				ret = false;
			}
			return ret;
		}
		
	}
}