/// <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)); }
/// <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. }
/// <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); }
/// <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); } }
/// <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); }