public void Execute() { var thermostats = Thermostats.Read(); var thermostat = thermostats.ThermostatWithSerial(serial); if (thermostat == null) { Console.Error.WriteLine($"Thermostat with serial {serial} not found. Have you run the read command first?"); Environment.Exit(1); } var parsedThermostat = new ParsedThermostat(thermostat); switch (attributeName) { case SET_POINT_TEMPERATURE: SetSetPointTemperature(thermostat); break; case VACATION_PERIOD: SetVacationPeriod(thermostat); break; case CANCEL_VACATION: CancelVacation(thermostat); break; default: Console.Error.WriteLine($"Only setting {SET_POINT_TEMPERATURE}, {VACATION_PERIOD}, and {CANCEL_VACATION} supported for now"); Environment.Exit(1); break; } thermostats.Write(); }
async Task Connect(Thermostat thermostat) { var name = thermostat.Serial; var uuid = thermostat.Uuid; var connectedThermostat = await accessor.ConnectToPeripheralWithNameAndUuid(name, uuid); var mainService = FindService(connectedThermostat, Uuids.MAIN_SERVICE); var batteryService = FindService(connectedThermostat, Uuids.BATTERY_SERVICE); var mainServiceCharacteristics = await accessor.DiscoverCharacteristicsFor(mainService); var secretValueCharacteristic = Array.Find(mainServiceCharacteristics, c => c.Uuid == Uuids.SECRET_KEY); if (secretValueCharacteristic == null && !thermostats.HasSecretAndUuidFor(serial)) { Console.Error.WriteLine("You need to push the timer button on the thermostat"); Environment.Exit(1); } var pinCodeCharacteristic = CharacteristicWithUuid(mainServiceCharacteristics, Uuids.PIN_CODE_CHARACTERISTIC); if (pinCodeCharacteristic == null) { Console.Error.WriteLine($"Did not find pin code characteristic ({Uuids.PIN_CODE_CHARACTERISTIC})"); Environment.Exit(1); } Console.Error.WriteLine("Writing pin code"); byte[] zeroBytes = { 0, 0, 0, 0 }; await accessor.WriteCharacteristicValue(mainService, pinCodeCharacteristic, zeroBytes); Console.Error.WriteLine("Wrote pin code"); var batteryServiceCharacteristics = await accessor.DiscoverCharacteristicsFor(batteryService); Console.Error.WriteLine("Discovered battery service characteristics"); thermostat.BatteryLevel = await ReadCharacteristicWithUuid(batteryService, batteryServiceCharacteristics, Uuids.BATTERY_LEVEL); thermostat.Name = await ReadCharacteristicWithUuid(mainService, mainServiceCharacteristics, Uuids.DEVICE_NAME); thermostat.Temperature = await ReadCharacteristicWithUuid(mainService, mainServiceCharacteristics, Uuids.TEMPERATURE); thermostat.Settings = await ReadCharacteristicWithUuid(mainService, mainServiceCharacteristics, Uuids.SETTINGS); thermostat.Schedule1 = await ReadCharacteristicWithUuid(mainService, mainServiceCharacteristics, Uuids.SCHEDULE_1); thermostat.Schedule2 = await ReadCharacteristicWithUuid(mainService, mainServiceCharacteristics, Uuids.SCHEDULE_2); thermostat.Schedule3 = await ReadCharacteristicWithUuid(mainService, mainServiceCharacteristics, Uuids.SCHEDULE_3); var parsedThermostat = new ParsedThermostat(thermostat); parsedThermostat.ApplyUpdates(); await WriteCharacteristicWithUuid(mainService, mainServiceCharacteristics, Uuids.TEMPERATURE, thermostat.Temperature); await WriteCharacteristicWithUuid(mainService, mainServiceCharacteristics, Uuids.SETTINGS, thermostat.Settings); await accessor.Disconnect(); }
public void Execute() { var thermostats = Thermostats.Read(); var thermostat = thermostats.ThermostatWithSerial(serial); if (thermostat == null) { Console.Error.WriteLine($"Thermostat with serial {serial} not found. Have you run the read command first?"); Environment.Exit(1); } var parsed = new ParsedThermostat(thermostat); Console.WriteLine($"Device name: {parsed.DeviceName}"); Console.WriteLine($"Device UUID: {thermostat.Uuid}"); Console.WriteLine($"Battery level: {parsed.BatteryLevelPercent}%"); Console.WriteLine(""); Console.WriteLine($"Set-point/room temperature: {parsed.SetPointTemperature} / {parsed.RoomTemperature}"); Console.WriteLine($"Home/away temperature: {parsed.HomeTemperature} / {parsed.AwayTemperature}"); Console.WriteLine($"Vacation/frost protection temperature: {parsed.VacationTemperature} / {parsed.FrostProtectionTemperature}"); Console.WriteLine($"Schedule mode: {parsed.ScheduleMode}"); if (parsed.VacationFrom != null && parsed.VacationTo != null) { Console.WriteLine($"Vacation: {parsed.VacationFrom} - {parsed.VacationTo}"); } Console.WriteLine(""); Console.WriteLine($"Monday:\n{parsed.MondaySchedule}"); Console.WriteLine(""); Console.WriteLine($"Tuesday:\n{parsed.TuesdaySchedule}"); Console.WriteLine(""); Console.WriteLine($"Wednesday:\n{parsed.WednesdaySchedule}"); Console.WriteLine(""); Console.WriteLine($"Thursday:\n{parsed.ThursdaySchedule}"); Console.WriteLine(""); Console.WriteLine($"Friday:\n{parsed.FridaySchedule}"); Console.WriteLine(""); Console.WriteLine($"Saturday:\n{parsed.SaturdaySchedule}"); Console.WriteLine(""); Console.WriteLine($"Sunday:\n{parsed.SundaySchedule}"); if (thermostat.HasUpdatedAttributes) { Console.WriteLine(""); Console.WriteLine("Attributes to be updated:"); if (thermostat.UpdatedSetPointTemperature != null) { Console.WriteLine($"Set-point temperature: {thermostat.UpdatedSetPointTemperature}"); } if (thermostat.HasUpdatedVacationPeriod) { if (thermostat.UpdatedVacationPeriod != null) { Console.WriteLine($"Vacation: {thermostat.UpdatedVacationPeriod.From} - {thermostat.UpdatedVacationPeriod.To}"); } else { Console.WriteLine("Clear upcoming vacation period"); } } } }