예제 #1
0
        // RackConfig handler

        /// <summary>
        /// Save RackConfig in Application SharedPreferences
        /// </summary>
        /// <param name="config"></param>
        public static void SaveConfig(RackConfig config)
        {
            var MyRackConfig     = Application.Context.GetSharedPreferences("MyRackConfig", FileCreationMode.Private);
            var MyRackConfigEdit = MyRackConfig.Edit();

            MyRackConfigEdit.PutString("Width", Convert.ToString(config.GetWidth()));
            MyRackConfigEdit.PutString("LayersCount", Convert.ToString(config.GetLayersCount()));
            MyRackConfigEdit.PutString("LayersInfo", config.GetLayersInfo());
            MyRackConfigEdit.Apply();
        }
예제 #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

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

            //fill view object;
            buttonSave          = FindViewById <Button>(Resource.Id.buttonSave);
            buttonCancel        = FindViewById <Button>(Resource.Id.buttonCancel);
            buttonIp            = FindViewById <Button>(Resource.Id.buttonIp);
            editTextWidth       = FindViewById <EditText>(Resource.Id.editTextWidth);
            textViewLayersValue = FindViewById <TextView>(Resource.Id.textViewLayersValue);
            buttonPlus          = FindViewById <Button>(Resource.Id.buttonPlus);
            buttonMin           = FindViewById <Button>(Resource.Id.buttonMin);
            listViewConfigure   = FindViewById <ListView>(Resource.Id.listViewConfigure);

            config = DataHandler.GetConfig();
            if (config == null)
            {
                config = new RackConfig(0, 1, "0:"); //|6:Bier|3:S
            }

            //set start point view
            editTextWidth.Text       = Convert.ToString(config.GetWidth());
            textViewLayersValue.Text = Convert.ToString(config.GetLayersCount());
            UpdateListViewConfig();

            //change the amount of Width
            editTextWidth.TextChanged += (slender, e) => {
                string widthText = editTextWidth.Text;
                if (widthText != "")
                {
                    config.SetWidth(Convert.ToInt32(widthText));
                }
            };

            //change the amount of Layers
            buttonPlus.Click += (slender, e) =>
            {
                int LayersCount = config.GetLayersCount();
                if (LayersCount < 99)
                {
                    LayersCount++;
                    config.SetLayersCount(LayersCount);
                    UpdateTextViewLayersCount(LayersCount);
                    config.Layers.Add(new Layer(0, ""));
                    //update list view
                    UpdateListViewConfig();
                }
            };
            buttonMin.Click += (slender, e) =>
            {
                int LayersCount = config.GetLayersCount();
                if (LayersCount > 1)
                {
                    LayersCount--;
                    config.SetLayersCount(LayersCount);
                    UpdateTextViewLayersCount(LayersCount);
                    config.Layers.RemoveAt(LayersCount);
                    //update list view
                    UpdateListViewConfig();
                }
            };

            //save the new config if correct and complete
            buttonSave.Click += (slender, e) =>
            {
                //check if the current config data complete and valid is,
                //else show a toast with fail message
                if (CheckNewConfig())
                {
                    //overwrite the config and empty the currently saved data
                    DataHandler.SaveConfig(config);
                    DataHandler.ResetData();

                    //go back to main activity
                    Intent newActivityMain = new Intent(this, typeof(MainActivity));
                    StartActivity(newActivityMain);
                }
            };

            //cancel by going back to main and saving nothing (the original config will be unchanged)
            buttonCancel.Click += (slender, e) =>
            {
                GoBackToMain();
            };

            //go to change ip activity
            buttonIp.Click += (sender, e) =>
            {
                if (!warning_saveconfig_first)
                {
                    warning_saveconfig_first = true;
                    Toast.MakeText(this, "Make sure that the changes to the config are saved before going to conection configere", ToastLength.Long).Show();
                }
                else
                {
                    GoToIp();
                }
            };
        }
예제 #3
0
        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);
            };
        }