示例#1
0
        /// <summary>
        ///     Remove an alarm from the alarms list.
        /// </summary>
        private async void DeleteAlarm()
        {
            //Copy over the alarms we want to keep.
            List <Alarm> tempAlarms = new List <Alarm>();

            foreach (var a in alarms)
            {
                if (a.Id != currentAlarm.Id)
                {
                    tempAlarms.Add(a);
                }
            }

            //Assign the modified list.
            alarms = tempAlarms;

            //Send the command to the arduino.
            SendRemoveAlarm(currentAlarm);

            //Save the alarms
            await IOWorker.SaveFile(AppFiles.Alarm, AppFileExtension.JSON, alarms);

            //Return to main screen.
            StartActivity(typeof(AlarmActivity));
        }
示例#2
0
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            base.SetContentView(Resource.Layout.Alarm);

            //Get elements from view.
            lv_alarms      = FindViewById <ListView>(Resource.Id.lv_alarms);
            btnAlarmCreate = FindViewById <Button>(Resource.Id.btn_alarm_create);
            btnAlarmCancel = FindViewById <Button>(Resource.Id.btnAlarmCancel);

            //Grab the alarms file from disk and add it to the list we want to display.
            List <Alarm> a = await IOWorker.ReadFile <List <Alarm> >(AppFiles.Alarm);

            if (a != null)
            {
                alarms = a;
            }

            //Add event listeners for various events.
            btnAlarmCreate.Click += (o, s) => GotoAddEditAlarmsActivity(null);     //Goto add alarm.
            btnAlarmCancel.Click += (o, s) => StartActivity(typeof(MainActivity)); //Go back to the main activity.
            lv_alarms.ItemClick  += OnClickAlarm;                                  //Edit an alarm.


            //Set the adapter for the alarms list.
            AlarmAdapter alarmAdapter = new AlarmAdapter(this, alarms);

            lv_alarms.Adapter = alarmAdapter;
        }
示例#3
0
        /// <summary>
        ///     Save any new alarm that we added or modified.
        ///     Save this to disk.
        /// </summary>
        private async void SaveAlarms()
        {
            List <Alarm> tempAlarmList = new List <Alarm>();

            //Validate if the user input is valid.
            if (ValidAlarmInputs(out string name, out DateTime time))
            {
                //We are modifying an existing alarm
                if (currentAlarm != null)
                {
                    //Apply changes to existing alarm.
                    currentAlarm.Name = name;
                    currentAlarm.Time = time;
                    tempAlarmList.Add(currentAlarm);

                    //Sync with arduino, send the alarm ID and date and time to the arduino.
                    SendAlarmToArduino(Commands.AlarmEdit, currentAlarm);

                    //Copy over the alarms list, skip any duplicates.
                    foreach (Alarm a in alarms)
                    {
                        if (currentAlarm.Id == a.Id)
                        {
                            continue;
                        }
                        tempAlarmList.Add(a);
                    }
                }
                //If we are adding a new alarm..
                else
                {
                    //Create a new alarm.
                    Alarm alarm = new Alarm((byte)alarms.Count, name, time);
                    tempAlarmList.Add(alarm);

                    //Sync with arduino, send the alarm ID and date and time to the arduino.
                    SendAlarmToArduino(Commands.AlarmAdd, alarm);

                    //Copy over the alarms list, skip any duplicates.
                    foreach (Alarm a in alarms)
                    {
                        if (alarm.Id == a.Id)
                        {
                            continue;
                        }
                        tempAlarmList.Add(a);
                    }
                }

                //Save the new alarm array.
                alarms = tempAlarmList;
                await IOWorker.SaveFile(AppFiles.Alarm, AppFileExtension.JSON, alarms);

                //Go back to the main alarm form.
                StartActivity(typeof(AlarmActivity));
            }

            //Else we do nothing, let the user have the chance to adjust the values and try again.
        }
示例#4
0
 /// <summary>init tcp server channel
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="port"></param>
 /// <param name="ioWorker">deal with networkstream</param>
 public TcpServerChannel(ILoggerFactory factory
                         , int port
                         , IOWorker ioWorker)
     : base(factory
            , port)
 {
     this._ioWorker   = ioWorker;
     this._tcpClients = new LinkedList <TcpClient>();
 }
示例#5
0
        /// <summary>
        ///     Disconnect to whatever we are connected to.
        ///     Also forget the previous connection details.
        /// </summary>
        private async void Disconnect()
        {
            SocketWorker.Disconnect();
            await IOWorker.ClearFile(AppFiles.Connection, AppFileExtension.JSON);

            SetConnectionDetails();

            Toast.MakeText(this, Resource.String.toast_disconnected, ToastLength.Long).Show();
        }
示例#6
0
 /// <summary>init tcp server channel
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="port"></param>
 /// <param name="ioWorker">deal with networkstream</param>
 public TcpServerChannel(ILoggerFactory factory
     , int port
     , IOWorker ioWorker)
     : base(factory
     , port)
 {
     this._ioWorker = ioWorker;
     this._tcpClients = new LinkedList<TcpClient>();
 }
示例#7
0
 /// <summary>init tcp server channel
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="port"></param>
 /// <param name="ioWorker">deal with networkstream</param>
 public TcpServerChannel(ITopLogger log
                         , int port
                         , IOWorker ioWorker)
     : base(log
            , port)
 {
     this._ioWorker   = ioWorker;
     this._tcpClients = new LinkedList <TcpClient>();
 }
示例#8
0
        /// <summary>
        ///     Store the connection data on disk.
        /// </summary>
        private async void SaveConnectionDetails(string ip)
        {
            ConnectionData data = new ConnectionData(
                ip,
                SocketWorker.ConnectedPort
                );

            string json = data.Serialize();
            await IOWorker.SaveFile(AppFiles.Connection, AppFileExtension.JSON, data);
        }
示例#9
0
        protected async override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            base.SetContentView(Resource.Layout.main);

            //TODO, REMOVE: Debug code.
            await IOWorker.ClearFile(AppFiles.Alarm, AppFileExtension.JSON);

            await IOWorker.ClearFile(AppFiles.LightSocket, AppFileExtension.JSON);

            //await IOWorker.ClearFile(AppFiles.Connection, AppFileExtension.JSON);

            //Fetch the buttons.
            buttonSnoozeAlarms       = FindViewById <Button>(Resource.Id.btn_quick_snooze);
            buttonStopAlarms         = FindViewById <Button>(Resource.Id.btn_quick_stop);
            buttonAlarmActivity      = FindViewById <Button>(Resource.Id.btn_alarm_management);
            buttonConnectionActivity = FindViewById <Button>(Resource.Id.btn_connection_management);
            buttonKakuActivity       = FindViewById <Button>(Resource.Id.btn_light_management);
            buttonTimeActivity       = FindViewById <Button>(Resource.Id.btn_time_management);
            textStatus = FindViewById <TextView>(Resource.Id.text_status);

            //Tell each button to start a different activity.
            buttonAlarmActivity.Click      += (o, s) => StartActivity(typeof(AlarmActivity));
            buttonConnectionActivity.Click += (o, s) => StartActivity(typeof(ConnectionActivity));
            buttonKakuActivity.Click       += (o, s) => StartActivity(typeof(KakuActivity));
            buttonTimeActivity.Click       += (o, s) => StartActivity(typeof(TimeActivity));
            buttonSnoozeAlarms.Click       += (o, s) => SnoozeAlarms();
            buttonStopAlarms.Click         += (o, s) => StopAlarms();

            SetUI(SocketWorker.IsConnected);

            //Check if we previously connected and try those connection details.
            ConnectionData data = await IOWorker.ReadFile <ConnectionData>(AppFiles.Connection);

            //Try to connect to connections details we used earlier.
            if (!data.Equals(default(ConnectionData)))
            {
                if (!SocketWorker.IsConnected)
                {
                    SockErr err = SocketWorker.Connect(data.IP, data.Port);

                    //If we do not have an error, notify the user we connected successfully
                    if (err == SockErr.None)
                    {
                        Toast.MakeText(this, Resource.String.toast_connected, ToastLength.Long).Show();
                        SetUI(SocketWorker.IsConnected);
                    }
                }
            }
        }
示例#10
0
        protected async override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            base.SetContentView(Resource.Layout.AddEditAlarm);

            //Load elements from view.
            btnSetButton               = FindViewById <Button>(Resource.Id.btn_alarm_set);
            btnCancel                  = FindViewById <Button>(Resource.Id.btn_alarm_cancel);
            btnRemove                  = FindViewById <Button>(Resource.Id.btn_alarm_remove);
            editTextAlarmName          = FindViewById <EditText>(Resource.Id.etext_alarm_name);
            dpAlarmDatePicker          = FindViewById <DatePicker>(Resource.Id.dt_alarm_date);
            npAlarmHourNumberPicker    = FindViewById <NumberPicker>(Resource.Id.np_alarm_hour);
            npAlarmMinutesNumberPicker = FindViewById <NumberPicker>(Resource.Id.np_alarm_minute);

            //Set min max for hours.
            npAlarmHourNumberPicker.MinValue = 0;
            npAlarmHourNumberPicker.MaxValue = 23;

            //Set min max for minutes.
            npAlarmMinutesNumberPicker.MinValue = 0;
            npAlarmMinutesNumberPicker.MaxValue = 59;

            //Load the current alarms from disk.
            alarms = await IOWorker.ReadFile <List <Alarm> >(AppFiles.Alarm);

            string serializedAlarm = Intent.GetStringExtra("alarm");

            //Read in existing values or use a default values.
            if (!string.IsNullOrEmpty(serializedAlarm))
            {
                currentAlarm = JsonConvert.DeserializeObject <Alarm>(serializedAlarm);
                SetAlarmFields(currentAlarm.Name, currentAlarm.Time);
                btnRemove.Enabled = true;
            }
            else
            {
                SetAlarmFields("", DateTime.Now);
                btnRemove.Enabled = false;
            }

            //Bind elements to method(s).
            btnSetButton.Click += (o, s) => SaveAlarms();
            btnRemove.Click    += (o, s) => DeleteAlarm();
            btnCancel.Click    += (o, s) => StartActivity(typeof(AlarmActivity));
        }
示例#11
0
        /// <summary>
        ///     Add or edit an alarm.
        /// </summary>
        /// <param name="alarm">The alarm we want to edit, alarm is null when a new alarm is added.</param>
        private async void GotoAddEditAlarmsActivity(Alarm alarm)
        {
            //Save the current alarms.
            await IOWorker.SaveFile(AppFiles.Alarm, AppFileExtension.JSON, alarms);

            //If the given alarm is not null, we are editing an existing one.
            //There for we pass the alarm in question via the Intent.
            if (alarm != null)
            {
                Intent intent = new Intent(this, typeof(AddEditAlarmActivity));
                intent.PutExtra("alarm", alarm.Serialize());
                StartActivity(intent);
            }
            //If we do not have given an alarm, we are creating a new one.
            //Thus we do not pass in any json that could be deserialized.
            //Code in the other activity should handle this.
            else
            {
                Intent intent = new Intent(this, typeof(AddEditAlarmActivity));
                intent.PutExtra("alarm", "");
                StartActivity(intent);
            }
        }
示例#12
0
        protected async override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            base.SetContentView(Resource.Layout.Lights);

            //Find all the elements from the view.
            btnBack        = FindViewById <Button>(Resource.Id.btn_lights_cancel);
            cbLightSocket1 = FindViewById <CheckBox>(Resource.Id.cb_light1);
            cbLightSocket2 = FindViewById <CheckBox>(Resource.Id.cb_light2);
            cbLightSocket3 = FindViewById <CheckBox>(Resource.Id.cb_light3);
            cbLightSocket4 = FindViewById <CheckBox>(Resource.Id.cb_light4);
            cbLightSocket5 = FindViewById <CheckBox>(Resource.Id.cb_light5);

            //Read current values from disk.
            bool[] lightSocketDataFromDisk = await IOWorker.ReadFile <bool[]>(AppFiles.LightSocket);

            //Assign loaded data to local variable.
            if (lightSocketDataFromDisk != null)
            {
                socketStates = lightSocketDataFromDisk;
            }

            //Set the values we fetched from disk or default values.
            cbLightSocket1.Checked = socketStates[0];
            cbLightSocket2.Checked = socketStates[1];
            cbLightSocket3.Checked = socketStates[2];
            cbLightSocket4.Checked = socketStates[3];
            cbLightSocket5.Checked = socketStates[4];

            //Register all the events for those elements.
            cbLightSocket1.CheckedChange += (s, e) => OnLightSocketChanged(1);
            cbLightSocket2.CheckedChange += (s, e) => OnLightSocketChanged(2);
            cbLightSocket3.CheckedChange += (s, e) => OnLightSocketChanged(3);
            cbLightSocket4.CheckedChange += (s, e) => OnLightSocketChanged(4);
            cbLightSocket5.CheckedChange += (s, e) => OnLightSocketChanged(5);
            btnBack.Click += (s, e) => StartActivity(typeof(MainActivity));
        }
示例#13
0
        protected async override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            base.SetContentView(Resource.Layout.Connection);

            //Find all the elements from the view.
            textConnectionStatus = FindViewById <TextView>(Resource.Id.text_connection_status_value);
            textIpAddress        = FindViewById <TextView>(Resource.Id.text_connection_ip_value);
            textPort             = FindViewById <TextView>(Resource.Id.text_connection_port_value);
            editTextIp           = FindViewById <EditText>(Resource.Id.etext_connection_ip);
            editTextPort         = FindViewById <EditText>(Resource.Id.etext_connection_port);
            buttonConnect        = FindViewById <Button>(Resource.Id.btn_connection_connect);
            buttonDisconect      = FindViewById <Button>(Resource.Id.btn_connection_disconnect);
            buttonBack           = FindViewById <Button>(Resource.Id.btn_connection_back);

            //Update the connection if a connection state changes.
            SocketWorker.OnSocketConnect    += SetConnectionDetails;
            SocketWorker.OnSocketDisconnect += SetConnectionDetails;

            buttonConnect.Click   += (o, s) => Connect();
            buttonDisconect.Click += (o, s) => Disconnect();
            buttonBack.Click      += (o, s) => StartActivity(typeof(MainActivity));

            //Check if we previously connected and try those connection details.
            ConnectionData data =
                await IOWorker.ReadFile <ConnectionData>(AppFiles.Connection);

            //Set the connection details from previous connection.
            if (!data.Equals(default(ConnectionData)))
            {
                textIpAddress.Text = data.IP;
                textPort.Text      = data.Port.ToString();
            }

            SetConnectionDetails();
        }
示例#14
0
        /// <summary>
        ///     Called when any of the 5 light sockets checkboxes change.
        ///     Required to sync up with the arduino.
        /// </summary>
        /// <param name="id">The id of the light socket we are changing.</param>
        private async void OnLightSocketChanged(int id)
        {
            bool state = false;

            //Check which kaku was edited, set the state accordingly
            switch (id)
            {
            case 1:
                state           = cbLightSocket1.Checked;
                socketStates[0] = state;
                break;

            case 2:
                state           = cbLightSocket2.Checked;
                socketStates[1] = state;
                break;

            case 3:
                state           = cbLightSocket3.Checked;
                socketStates[2] = state;
                break;

            case 4:
                state           = cbLightSocket4.Checked;
                socketStates[3] = state;
                break;

            case 5:
                state           = cbLightSocket5.Checked;
                socketStates[4] = state;
                break;
            }

            //Save the changes to disk.
            await IOWorker.SaveFile(AppFiles.LightSocket, AppFileExtension.JSON, socketStates);

            //Disable the checkboxes for 1/3 of a second.
            //This could lead to the disk and arduino being spammed
            //by the users, so we want to protect them from doing to.
            await Task.Run(async() =>
            {
                cbLightSocket1.Enabled = false;
                cbLightSocket2.Enabled = false;
                cbLightSocket3.Enabled = false;
                cbLightSocket4.Enabled = false;
                cbLightSocket5.Enabled = false;
                await Task.Delay(333);
            }).ContinueWith((task) =>
            {
                cbLightSocket1.Enabled = true;
                cbLightSocket2.Enabled = true;
                cbLightSocket3.Enabled = true;
                cbLightSocket4.Enabled = true;
                cbLightSocket5.Enabled = true;
            });

            //Syncronize arduino from app.
            string s = state ? "1" : "0";

            SocketWorker.Send(Commands.SyncKaku, id.ToString(), s);
        }
示例#15
0
 // GET: api/Sync
 public string Get()
 {
     IOWorker.DoIoWork();
     return("Done");
 }
        // GET: api/Async
        public async Task <string> Get()
        {
            await IOWorker.DoIoWorkAsync();

            return("Done");
        }