// Ip handler

        /// <summary>
        /// Save RackData in Application SharedPreferences
        /// </summary>
        /// <param name="data"></param>
        public static void SaveIp(IpData ipData)
        {
            var MyIpData     = Application.Context.GetSharedPreferences("MyRackData", FileCreationMode.Private);
            var MyIpDataEdit = MyIpData.Edit();

            MyIpDataEdit.PutString("Ip", ipData.ip);
            MyIpDataEdit.PutString("Poort", ipData.poort);
            MyIpDataEdit.Apply();
        }
Beispiel #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.Ip);

            // Get current ip
            ipData = DataHandler.GetIp();

            // Get view elements
            editTextIp     = FindViewById <EditText>(Resource.Id.editTextIp);
            editTextPoort  = FindViewById <EditText>(Resource.Id.editTextpoort);
            buttonCancelIp = FindViewById <Button>(Resource.Id.buttonCancelIp);
            buttonSaveIp   = FindViewById <Button>(Resource.Id.buttonSaveIp);

            // Set default values view
            editTextIp.Text    = ipData.ip;
            editTextPoort.Text = ipData.poort;

            // Set button click liseners:
            buttonCancelIp.Click += (sender, e) =>
            {
                GoBackToMain();
            };

            buttonSaveIp.Click += (sender, e) =>
            {
                if (!DataHandler.CheckValidIpAddress(editTextIp.Text))
                {
                    //invalide ip
                    Toast.MakeText(this, "Invalide ip format", ToastLength.Long).Show();
                }
                else if (!DataHandler.CheckValidPort(editTextPoort.Text))
                {
                    //invalide poort
                    Toast.MakeText(this, "Invalide poort format", ToastLength.Long).Show();
                }
                else
                {
                    //valide connection data
                    DataHandler.SaveIp(new IpData(editTextIp.Text, editTextPoort.Text));
                    GoBackToMain();
                }
            };
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get view items
            buttonConnect     = FindViewById <Button>(Resource.Id.buttonConnect);
            buttonUpdate      = FindViewById <Button>(Resource.Id.buttonUpdate);
            buttonExtra       = FindViewById <Button>(Resource.Id.buttonExtra);
            buttonOptions     = FindViewById <Button>(Resource.Id.buttonOptions);
            listViewResults   = FindViewById <ListView>(Resource.Id.listViewResults);
            textViewConnectie = FindViewById <TextView>(Resource.Id.textViewConnectie);
            textViewUpdated   = FindViewById <TextView>(Resource.Id.textViewUpdated);

            // Get config, data en ip
            rackConfig = DataHandler.GetConfig();
            rackData   = DataHandler.GetData();
            ipData     = DataHandler.GetIp();

            // Update ListViewResults and Buttons Enabled default state
            if (rackConfig == null)
            {
                ArrayAdapter noConfigAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, new string[] { "There is no Config, Go to Options." });
                listViewResults.Adapter = noConfigAdapter;
                buttonConnect.Enabled   = false;
            }
            else
            {
                ListViewResults_Adapter ConfigAdapter = new ListViewResults_Adapter(this, new Rack(rackConfig.Layers, rackData == null ? null : rackData.amounts));
                listViewResults.Adapter = ConfigAdapter;
                if (rackData != null)
                {
                    textViewUpdated.Text = rackData.updated.ToString("h:mm:ss");
                }
            }
            buttonUpdate.Enabled = false;
            textViewConnectie.SetTextColor(Android.Graphics.Color.Red);

            // Go to options to set config
            buttonOptions.Click += (slender, e) =>
            {
                //if connected, stop connection
                if (socket != null)
                {
                    socket.Close();
                    socket = null;
                }
                UpdateConnectionState(4);
                //go to Options Acivity
                Intent nextActivityOptions = new Intent(this, typeof(OptionsActivity));
                StartActivity(nextActivityOptions);
            };

            //timerSockets = new System.Timers.Timer() { Interval = 10000, Enabled = false }; // Interval >= 750
            //timerSockets.Elapsed += (obj, args) =>
            //{
            //    //RunOnUiThread(() =>
            //    //{
            //    if (socket != null) // only if socket exists
            //    {

            //    }
            //    else timerSockets.Enabled = false;  // If socket broken -> disable timer
            //    //});
            //};

            //If connected ask the amount on the layers
            buttonUpdate.Click += (sender, e) =>
            {
                string result = executeSend(3 + rackConfig.GetLayersCount() * 3, "a");                 // Send toggle-command to the Arduino
                if (result != "err")
                {
                    try
                    {
                        rackData = DataHandler.SetData(result, rackConfig.GetLayersCount());
                        DataHandler.SaveData(rackData);
                        ListViewResults_Adapter ConfigAdapter = new ListViewResults_Adapter(this, new Rack(rackConfig.Layers, rackData.amounts));
                        listViewResults.Adapter = ConfigAdapter;
                        textViewUpdated.Text    = rackData.updated.ToString("h:mm:ss");
                    }
                    catch
                    {
                        Toast.MakeText(this, "Updating failed", ToastLength.Long).Show();
                    }
                }
                else
                {
                    Toast.MakeText(this, "Updating failed", ToastLength.Long).Show();
                }
            };

            //If the connection data (Ip and Port) that are given are valid, then we will try to connect
            buttonConnect.Click += (sender, e) =>
            {
                //Validate the user input (IP address and port)
                if (ipData != null)
                {
                    ConnectSocket(ipData.ip, ipData.poort);
                }
                else
                {
                    UpdateConnectionState(3);
                }
            };
            buttonConnect.LongClick += (sender, e) =>
            {
                Disconnect();
                //go to SetIp Acivity
                Intent nextActivitySetIp = new Intent(this, typeof(SetIpActivity));
                StartActivity(nextActivitySetIp);
            };

            buttonExtra.Click += (slender, e) =>
            {
                Disconnect();
                //go to extra Acivity
                Intent nextActivityExtra = new Intent(this, typeof(ExtraActivity));
                StartActivity(nextActivityExtra);
            };
        }