public VirtualRoomConfig GetDeepCopy() { var copy = new VirtualRoomConfig(this.PartitionKey, this.RowKey); copy.Lights_room = this.Lights_room; copy.Lights_bathroom = this.Lights_bathroom; copy.Television = this.Television; copy.Blinds = this.Blinds; copy.AC = this.AC; copy.Temperature = this.Temperature; return(copy); }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { log.LogInformation("C# HTTP trigger function processed a request."); string connectionsJson = File.ReadAllText(Path.Combine(context.FunctionAppDirectory, "Connections.json")); JObject ConnectionsObject = JObject.Parse(connectionsJson); string connectionString = ConnectionsObject["AZURE_STORAGE_URL"].ToString(); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference("virtualroomconfig"); await table.CreateIfNotExistsAsync(); var room = req.Headers["room"]; if (string.IsNullOrEmpty(room)) { room = req.Query["room"]; } if (string.IsNullOrEmpty(room)) { return(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Please pass a room name on the query string or in the header") }); } var partitionKey = "Demo"; var rowKey = room; try { // get the room from the table var getRoom = TableOperation.Retrieve <VirtualRoomConfig>(partitionKey, rowKey); var query = await table.ExecuteAsync(getRoom); var currRoomConfig = (VirtualRoomConfig)query.Result; // if room not exist, create a record using default config if (currRoomConfig == null) { var defaultRoom = new VirtualRoomConfig(partitionKey, rowKey); var createRoom = TableOperation.Insert(defaultRoom); await table.ExecuteAsync(createRoom); currRoomConfig = (VirtualRoomConfig)(await table.ExecuteAsync(getRoom)).Result; } var operation = req.Query["operation"].ToString().ToLower(); var updated = false; if (!string.IsNullOrEmpty(operation)) { if (operation.Equals("reset")) { currRoomConfig.LoadDefaultConfig(); updated = true; } else if (operation.Equals("turn")) { var item = req.Query["item"].ToString().ToLower(); var instance = req.Query["instance"].ToString().ToLower(); var value = req.Query["value"].ToString().ToLower(); bool?valueBool = (value.Equals("on") || value.Equals("open")) ? true : ((value.Equals("off") || value.Equals("close")) ? (bool?)false : null); if (valueBool == null) { updated = false; } else if (item.Equals("lights")) { if (instance.Equals("all")) { if (currRoomConfig.Lights_bathroom == (bool)valueBool && currRoomConfig.Lights_room == (bool)valueBool) { currRoomConfig.Message = $"All lights already {value}"; } else { currRoomConfig.Lights_bathroom = (bool)valueBool; currRoomConfig.Lights_room = (bool)valueBool; currRoomConfig.Message = $"Ok, turning all the lights {value}"; } updated = true; } else if (instance.Equals("room")) { if (currRoomConfig.Lights_room == (bool)valueBool) { currRoomConfig.Message = $"Room light already {value}"; } else { currRoomConfig.Lights_room = (bool)valueBool; currRoomConfig.Message = $"Ok, turning {value} the room light"; } updated = true; } else if (instance.Equals("bathroom")) { if (currRoomConfig.Lights_bathroom == (bool)valueBool) { currRoomConfig.Message = $"Bathroom light already {value}"; } else { currRoomConfig.Lights_bathroom = (bool)valueBool; currRoomConfig.Message = $"Ok, turning {value} the bathroom light"; } updated = true; } } else if (item.Equals("tv")) { if (currRoomConfig.Television == (bool)valueBool) { currRoomConfig.Message = $"TV already {value}"; } else { currRoomConfig.Television = (bool)valueBool; currRoomConfig.Message = $"Ok, turning the TV {value}"; } updated = true; } else if (item.Equals("blinds")) { if (currRoomConfig.Blinds == (bool)valueBool) { currRoomConfig.Message = (bool)valueBool ? "Blinds already opened" : "Blinds already closed"; } else { currRoomConfig.Blinds = (bool)valueBool; currRoomConfig.Message = (bool)valueBool ? "All right, opening the blinds" : "All right, closing the blinds"; } updated = true; } else if (item.Equals("ac")) { if (currRoomConfig.AC == (bool)valueBool) { currRoomConfig.Message = $"AC already {value}"; } else { currRoomConfig.AC = (bool)valueBool; currRoomConfig.Message = $"Ok, turning the AC {value}"; } updated = true; } } else if (operation.Equals("settemperature")) { currRoomConfig.Temperature = int.Parse(req.Query["value"]); currRoomConfig.Message = "set temperature to " + req.Query["value"]; updated = true; } else if (operation.Equals("increasetemperature")) { currRoomConfig.Temperature += int.Parse(req.Query["value"]); currRoomConfig.Message = "raised temperature by " + req.Query["value"] + " degrees"; updated = true; } else if (operation.Equals("decreasetemperature")) { currRoomConfig.Temperature -= int.Parse(req.Query["value"]); currRoomConfig.Message = "decreased temperature by " + req.Query["value"] + " degrees"; updated = true; } } if (updated) { var updateRoom = TableOperation.Replace(currRoomConfig as VirtualRoomConfig); await table.ExecuteAsync(updateRoom); log.LogInformation("successfully updated the record"); } return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(currRoomConfig, Formatting.Indented), Encoding.UTF8, "application/json") }); } catch (Exception e) { log.LogError(e.Message); return(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Failed to process request") }); } }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { log.LogInformation("C# HTTP trigger function processed a request."); string connectionsJson = File.ReadAllText(Path.Combine(context.FunctionAppDirectory, "Connections.json")); JObject ConnectionsObject = JObject.Parse(connectionsJson); string connectionString = ConnectionsObject["AZURE_STORAGE_URL"].ToString(); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference("virtualroomconfig"); await table.CreateIfNotExistsAsync(); var room = req.Headers["room"]; if (string.IsNullOrEmpty(room)) { room = req.Query["room"]; } if (string.IsNullOrEmpty(room)) { return(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Please pass a room name on the query string or in the header") }); } var partitionKey = "Demo"; var rowKey = room; try { // get the room from the table var getRoom = TableOperation.Retrieve <VirtualRoomConfig>(partitionKey, rowKey); var query = await table.ExecuteAsync(getRoom); var currRoomConfig = (VirtualRoomConfig)query.Result; // if room not exist, create a record using default config if (currRoomConfig == null) { var defaultRoom = new VirtualRoomConfig(partitionKey, rowKey); var createRoom = TableOperation.Insert(defaultRoom); await table.ExecuteAsync(createRoom); currRoomConfig = (VirtualRoomConfig)(await table.ExecuteAsync(getRoom)).Result; } var operation = req.Query["operation"].ToString().ToLower(); var updated = false; if (string.IsNullOrEmpty(operation)) { // When no operation is specified, return only the current state of the room. // The Custom Commands client's knowledge of the previous state (clientContext" is unaltered. return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(currRoomConfig, Formatting.Indented), Encoding.UTF8, "application/json") }); } else { // Store the state of the room before changes are made. var previousConfig = currRoomConfig.GetDeepCopy(); if (operation.Equals("reset")) { currRoomConfig.LoadDefaultConfig(); updated = true; } else if (operation.Equals("turn")) { var item = req.Query["item"].ToString().ToLower(); var instance = req.Query["instance"].ToString().ToLower(); var value = req.Query["value"].ToString().ToLower(); bool?valueBool = (value.Equals("on") || value.Equals("open")) ? true : ((value.Equals("off") || value.Equals("close")) ? (bool?)false : null); if (valueBool == null) { updated = false; } else if (item.Equals("lights")) { if (instance.Equals("all")) { currRoomConfig.Lights_bathroom = (bool)valueBool; currRoomConfig.Lights_room = (bool)valueBool; updated = true; } else if (instance.Equals("room")) { currRoomConfig.Lights_room = (bool)valueBool; updated = true; } else if (instance.Equals("bathroom")) { currRoomConfig.Lights_bathroom = (bool)valueBool; updated = true; } } else if (item.Equals("tv")) { currRoomConfig.Television = (bool)valueBool; updated = true; } else if (item.Equals("blinds")) { currRoomConfig.Blinds = (bool)valueBool; updated = true; } else if (item.Equals("ac")) { currRoomConfig.AC = (bool)valueBool; updated = true; } } else if (operation.Equals("settemperature")) { currRoomConfig.Temperature = int.Parse(req.Query["value"]); updated = true; } else if (operation.Equals("increasetemperature")) { currRoomConfig.Temperature += int.Parse(req.Query["value"]); updated = true; } else if (operation.Equals("decreasetemperature")) { currRoomConfig.Temperature -= int.Parse(req.Query["value"]); updated = true; } if (updated) { var updateRoom = TableOperation.Replace(currRoomConfig as VirtualRoomConfig); await table.ExecuteAsync(updateRoom); log.LogInformation("successfully updated the record"); } // When an opeartion is attempted, return both the initial and final state to the Custom Commands client. // Initial state is used to update the client's clientContext field. var stateUpdateConfig = new VirtualRoomCustomCommandsState(previousConfig, currRoomConfig); return(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(stateUpdateConfig, Formatting.Indented), Encoding.UTF8, "application/json") }); } } catch (Exception e) { log.LogError(e.Message); return(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Failed to process request") }); } }
public VirtualRoomCustomCommandsState(VirtualRoomConfig previousState, VirtualRoomConfig currentState) { clientContext = previousState; this.currentState = currentState; }