Exemplo n.º 1
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource (strings are loaded from Recources -> values -> Strings.xml)
            SetContentView(Resource.Layout.Main);

            // find and set the controls, so it can be used in the code
            buttonConnect = FindViewById<Button>(Resource.Id.buttonConnect);
			buttonSwitch1 = FindViewById<Button> (Resource.Id.buttonSwitch1);
			buttonSwitch2 = FindViewById<Button> (Resource.Id.buttonSwitch2);
            kakuOne = FindViewById<Button>(Resource.Id.buttonOnOff1);
            kakuTwo = FindViewById<Button>(Resource.Id.buttonOnOff2);
            kakuThree = FindViewById<Button>(Resource.Id.buttonOnOff3);
            valOne = FindViewById<TextView>(Resource.Id.textViewValue1);
            valTwo = FindViewById<TextView>(Resource.Id.textViewValue2);
            valThree = FindViewById<TextView>(Resource.Id.textViewValue3);
            thresholdOne = FindViewById<EditText>(Resource.Id.editTextThreshold1);
            thresholdTwo = FindViewById<EditText>(Resource.Id.editTextThreshold2);
            thresholdThree = FindViewById<EditText>(Resource.Id.editTextThreshold3);
            thresholdFour = FindViewById<EditText>(Resource.Id.editTextThreshold4);
			updateSpeed = FindViewById<EditText> (Resource.Id.editTextTimerSpeed);
			updateSpeedText =FindViewById<TextView>(Resource.Id.textViewTimerSpeedText);
            toggleOne = FindViewById<Button>(Resource.Id.buttonToggle1);
            toggleTwo = FindViewById<Button>(Resource.Id.buttonToggle2);
            toggleThree = FindViewById<Button>(Resource.Id.buttonToggle3);
            textViewTimerStateValue = FindViewById<TextView>(Resource.Id.textViewTimerStateValue);
            textViewServerConnect = FindViewById<TextView>(Resource.Id.textViewServerConnect);
            editTextIPAddress = FindViewById<EditText>(Resource.Id.editTextIPAddress);
            editTextIPPort = FindViewById<EditText>(Resource.Id.editTextIPPort);
			kakuOne.SetTextColor (Color.Red);
			kakuTwo.SetTextColor (Color.Red);
			kakuThree.SetTextColor (Color.Red);
			toggleOne.SetTextColor (Color.Red);
			toggleTwo.SetTextColor (Color.Red);
			toggleThree.SetTextColor (Color.Red);

            UpdateConnectionState(4, "Disconnected");

            // Init commandlist, scheduled by socket timer
            commandList.Add(new Tuple<string, TextView>("a", valOne));
            commandList.Add(new Tuple<string, TextView>("b", valTwo));

            // activation of connector -> threaded sockets otherwise -> simple sockets 
            connector = new Connector(this);

            this.Title = (connector == null) ? this.Title + " (simple sockets)" : this.Title + " (thread sockets)";

            // timer object, running clock
            timerClock = new System.Timers.Timer() { Interval = 2000, Enabled = true }; // Interval >= 1000
            timerClock.Elapsed += (obj, args) =>
            {
                RunOnUiThread(() => { textViewTimerStateValue.Text = DateTime.Now.ToString("h:mm:ss"); });
				RunOnUiThread(() => { valThree.Text = DateTime.Now.ToString("HH:mm"); });
				UpdateSpeed();
            };

            // timer object, check Arduino state
            // Only one command can be serviced in an timer tick, schedule from list
			timerSockets = new System.Timers.Timer() { Interval = 1000, Enabled = true }; // Interval >= 750
            timerSockets.Elapsed += (obj, args) =>
            { RunOnUiThread(
				() =>
                {
					if (connector.socket != null) // only if socket exists
                    {
                    // Send a command to the Arduino server on every tick (loop though list)
						if (timerSpeedCounter == timerSpeed)
						{
							UpdateValue();
							updateOrder++;
							if (updateOrder > 2)
							{
								updateOrder = 0;
							}
						}
						timerSpeedCounter++;
						if (timerSpeedCounter> timerSpeed)
						{
							timerSpeedCounter = 1;
						}
                    }
                });
            };

			//All the buttons go here.
			//Add the "Connect" button handler.
            if (buttonConnect != null)  // if button exists
            {
                buttonConnect.Click += (sender, e) =>
                {
                    //Validate the user input (IP address and port)
                    if (CheckValidIpAddress(editTextIPAddress.Text) && CheckValidPort(editTextIPPort.Text))
                    {
                        if (connector == null) // -> simple sockets
                        {
                            ConnectSocket(editTextIPAddress.Text, editTextIPPort.Text);
                        }
                        else // -> threaded sockets
                        {
                            //Stop the thread If the Connector thread is already started.
                            if (connector.CheckStarted()) connector.StopConnector();
                               connector.StartConnector(editTextIPAddress.Text, editTextIPPort.Text);
                        }
                    }
                    else UpdateConnectionState(3, "Please check IP");
                };
            }

            //Add the "Change pin state" button handler.
			if (buttonSwitch1 != null)
			{
				buttonSwitch1.Click += (sender, e) =>
				{
					if (connector == null) // -> simple sockets
					{
						socket.Send(Encoding.ASCII.GetBytes("o"));                 // Send toggle-command to the Arduino
					}
					else // -> threaded sockets
					{
						if (connector.CheckStarted()) connector.SendMessage("o");  // Send toggle-command to the Arduino
					}
				};
			}
			if (buttonSwitch2 != null)
			{
				buttonSwitch2.Click += (sender, e) =>
				{
					if (connector == null) // -> simple sockets
					{
						socket.Send(Encoding.ASCII.GetBytes("p"));                 // Send toggle-command to the Arduino
					}
					else // -> threaded sockets
					{
						if (connector.CheckStarted()) connector.SendMessage("p");  // Send toggle-command to the Arduino
					}
				};
			}
            if (kakuOne != null)
            {
                kakuOne.Click += (sender, e) =>
                {
                    if (connector == null) // -> simple sockets
                    {
                        socket.Send(Encoding.ASCII.GetBytes("1"));                 // Send toggle-command to the Arduino
						UpdateGUITime(kakuOne);
					}
                    else // -> threaded sockets
                    {
                        if (connector.CheckStarted()) connector.SendMessage("1");  // Send toggle-command to the Arduino
						UpdateGUITime(kakuOne);
                    }
                };
            }
            if (kakuTwo != null)
            {
                kakuTwo.Click += (sender, e) =>
                {
                    if (connector == null) // -> simple sockets
                    {
                        socket.Send(Encoding.ASCII.GetBytes("2"));                 // Send toggle-command to the Arduino
						UpdateGUITime(kakuTwo);
					}
                    else // -> threaded sockets
                    {
                        if (connector.CheckStarted()) connector.SendMessage("2");  // Send toggle-command to the Arduino
						UpdateGUITime(kakuTwo);
                    }
                };
            }
            if (kakuThree != null)
            {
                kakuThree.Click += (sender, e) =>
                {
                    if (connector == null) // -> simple sockets
                    {
                        socket.Send(Encoding.ASCII.GetBytes("3"));                 // Send toggle-command to the Arduino
						UpdateGUITime(kakuThree);
					}
                    else // -> threaded sockets
                    {
                        if (connector.CheckStarted()) connector.SendMessage("3");  // Send toggle-command to the Arduino
						UpdateGUITime(kakuThree);
                    }
                };
            }
            if (toggleOne != null)
            {
                toggleOne.Click += (sender, e) =>
                {
					UpdateGUITime(toggleOne);
                };
            }
            if (toggleTwo != null)
            {
                toggleTwo.Click += (sender, e) =>
                {
					UpdateGUITime(toggleTwo);
                };
            }

            if (toggleThree != null)
            {
                toggleThree.Click += (sender, e) =>
                {
                    UpdateGUITime(toggleThree);
                };
            }


		}
Exemplo n.º 2
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource (strings are loaded from Recources -> values -> Strings.xml)
            SetContentView(Resource.Layout.Main);

            // find and set the controls, so it can be used in the code
            buttonConnect = FindViewById<Button>(Resource.Id.buttonConnect);
            buttonChangePinState = FindViewById<Button>(Resource.Id.buttonChangePinState);
            textViewTimerStateValue = FindViewById<TextView>(Resource.Id.textViewTimerStateValue);
            textViewServerConnect = FindViewById<TextView>(Resource.Id.textViewServerConnect);
            textViewChangePinStateValue = FindViewById<TextView>(Resource.Id.textViewChangePinStateValue);
            textViewSensorValue = FindViewById<TextView>(Resource.Id.textViewSensorValue);
            textViewDebugValue = FindViewById<TextView>(Resource.Id.textViewDebugValue);
            editTextIPAddress = FindViewById<EditText>(Resource.Id.editTextIPAddress);
            editTextIPPort = FindViewById<EditText>(Resource.Id.editTextIPPort);

            UpdateConnectionState(4, "Disconnected");

            // Init commandlist, scheduled by socket timer
            commandList.Add(new Tuple<string, TextView>("s", textViewChangePinStateValue));
            commandList.Add(new Tuple<string, TextView>("a", textViewSensorValue));

            // activation of connector -> threaded sockets otherwise -> simple sockets
            connector = new Connector(this);

            this.Title = (connector == null) ? this.Title + " (simple sockets)" : this.Title + " (thread sockets)";

            // timer object, running clock
            timerClock = new System.Timers.Timer() { Interval = 1000, Enabled = true }; // Interval >= 1000
            timerClock.Elapsed += (obj, args) =>
            {
                RunOnUiThread(() => { textViewTimerStateValue.Text = DateTime.Now.ToString("h:mm:ss"); });
            };

            // timer object, check Arduino state
            // Only one command can be serviced in an timer tick, schedule from list
            timerSockets = new System.Timers.Timer() { Interval = 1000, Enabled = false }; // Interval >= 750
            timerSockets.Elapsed += (obj, args) =>
            {
                //RunOnUiThread(() =>
                //{
                    if (socket != null) // only if socket exists
                    {
                        // Send a command to the Arduino server on every tick (loop though list)
                        UpdateGUI(executeCommand(commandList[listIndex].Item1), commandList[listIndex].Item2);  //e.g. UpdateGUI(executeCommand("s"), textViewChangePinStateValue);
                        if (++listIndex >= commandList.Count) listIndex = 0;
                    }
                    else timerSockets.Enabled = false;  // If socket broken -> disable timer
                //});
            };

            //Add the "Connect" button handler.
            if (buttonConnect != null)  // if button exists
            {
                buttonConnect.Click += (sender, e) =>
                {
                    //Validate the user input (IP address and port)
                    if (CheckValidIpAddress(editTextIPAddress.Text) && CheckValidPort(editTextIPPort.Text))
                    {
                        if (connector == null) // -> simple sockets
                        {
                            ConnectSocket(editTextIPAddress.Text, editTextIPPort.Text);
                        }
                        else // -> threaded sockets
                        {
                            //Stop the thread If the Connector thread is already started.
                            if (connector.CheckStarted()) connector.StopConnector();
                               connector.StartConnector(editTextIPAddress.Text, editTextIPPort.Text);
                        }
                    }
                    else UpdateConnectionState(3, "Please check IP");
                };
            }

            //Add the "Change pin state" button handler.
            if (buttonChangePinState != null)
            {
                buttonChangePinState.Click += (sender, e) =>
                {
                    if (connector == null) // -> simple sockets
                    {
                        socket.Send(Encoding.ASCII.GetBytes("t"));                 // Send toggle-command to the Arduino
                    }
                    else // -> threaded sockets
                    {
                        if (connector.CheckStarted()) connector.SendMessage("t");  // Send toggle-command to the Arduino
                    }
                };
            }
        }