public void Start ()
		{
			foreach (CustomNotification notification in Notifications) {
				notification.InitializeServices ();
			}

			if (monitorWorker == null) {
				monitorWorker = new BackgroundWorker ();
				monitorWorker.DoWork += new DoWorkEventHandler (monitorWorker_DoWork);
				monitorWorker.WorkerSupportsCancellation = true;
				monitorWorker.RunWorkerAsync ();
			}

			if (animationWorker == null) {
				animationWorker = new BackgroundWorker ();
				animationWorker.DoWork += new DoWorkEventHandler (animationWorker_DoWork);
				animationWorker.WorkerSupportsCancellation = true;
				animationWorker.RunWorkerAsync ();
			}

			if (ambilightWorker == null) {
				ambilightWorker = new BackgroundWorker();
				ambilightWorker.DoWork += new DoWorkEventHandler (ambilightWorker_DoWork);
				ambilightWorker.WorkerSupportsCancellation = true;
			}

			if (client == null) {
				client = new BayeuxClient (CurrentApiAccessAddress);

				client.DataReceived += (object sender, BayeuxClient.DataReceivedEventArgs e) => {
					Regex r = new Regex ("/([^/]+)$");
					Match m = r.Match (e.Channel);
					if (m.Success) {
						BlinkstickService notification = FindBlinkstickNotificaitonByAccessCode (m.Groups [1].Value);
						if (notification != null)
						{
							if (e.Data.ContainsKey ("status")) {
								if ((String)e.Data ["status"] == "off") {
									log.InfoFormat("Blinkstick device {0} turned off", notification.Name);
									notification.Active = false;
									LedController controller = FindControllerBySerialNumber(notification.Device);
									if (controller != null)
									{
										controller.MorphToColor(RgbColor.FromRgb(0, 0, 0));
									}
									return;
								}
							}

							// Handle the message
							String color = (String)e.Data ["color"];

							if (IsValidColor (color)) {
								log.InfoFormat ("New color received for Blinkstick device {0} - {1}", color, notification.Name);
								notification.Color = RgbColor.FromString (color);
								notification.Active = true;
								OnNotificationUpdated(notification);
							}
						}
						else
						{
							log.WarnFormat("Notification for access code {0} not found...", m.Groups[1].Value);
						}

					}
				};

				client.ChannelUnsubscribed += (object sender, EventArgs e) => {
					if (!client.HasSubscribedChannels) {
						client.Disconnect ();
					}
				};

				client.Disconnected += (object sender, EventArgs e) => {
					client.CloseThread();
				};
			}

			Boolean enabledDevicesExist = false;

			Boolean ambilightExist = false;

			foreach (CustomNotification notification in Notifications) {
				if (notification is BlinkstickService) {
					if (notification.Enabled) {
						client.Subscribe ("/devices/" + ((BlinkstickService)notification).AccessCode);
						enabledDevicesExist = true;
					}
				}
				else if (notification is AmbiLightNotification && notification.Enabled)
				{
					ambilightExist = true;
				}

			}

			if (enabledDevicesExist) {
				client.Connect ();
			}

			if (ambilightExist)
				ambilightWorker.RunWorkerAsync();
		}
Beispiel #2
0
		public static void Main (string[] args)
		{
			log4net.Config.XmlConfigurator.Configure();

			Console.WriteLine ("1 - subscribe to channel");
			Console.WriteLine ("q - unsubscribe from channel");
			Console.WriteLine ("2 - subscribe to channel");
			Console.WriteLine ("w - unsubscribe from channel");
			Console.WriteLine ("x - exit");
			Console.WriteLine ("");
			Console.WriteLine ("");

			ServerEndpoint = new Uri (args [0]);

			BayeuxClient client = new BayeuxClient (ServerEndpoint);

			client.DataReceived += (object sender, BayeuxClient.DataReceivedEventArgs e) => {
				if (e.Data.ContainsKey("color"))
				{
					Console.WriteLine ("Received color : " + e.Data ["color"]);
				}
				else if (e.Data.ContainsKey("status"))
				{
					Console.WriteLine ("Received status : " + e.Data ["status"]);
				}
				else
				{
					Console.WriteLine("Received unknown message");
				}
			};

			client.Subscribe ("/devices/51251481d988b43c45000002");
			client.Connect ();

			ConsoleKeyInfo key = Console.ReadKey ();

			while (key.KeyChar != 'x') {
				if (key.KeyChar == '1') {
					client.Subscribe ("/devices/51251481d988b43c45000002");
				}

				if (key.KeyChar == 'q') {
					client.Unsubscribe ("/devices/51251481d988b43c45000002");
				}

				if (key.KeyChar == '2') {
					client.Subscribe ("/devices/512518fcd988b43c45000003");
				}

				if (key.KeyChar == 'w') {
					client.Unsubscribe ("/devices/512518fcd988b43c45000003");
				}

				if (key.KeyChar == '*') {
					client.Subscribe ("/dees/*");
				}

				if (key.KeyChar == 'i') {
					client.Unsubscribe ("/devices/*");
				}

				key = Console.ReadKey ();
			}

			client.Disconnect ();
			Console.WriteLine ("Waiting to disconnect");
			client.WaitFor (BayeuxClient.ClientStateEnum.Disconnected);
			Console.WriteLine ("Press any key to exit...");
			Console.ReadKey();
		}