diff --git a/src/Program.cs b/src/Program.cs index f595b07..a6d10ad 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -26,7 +26,7 @@ public class Program public static int current = 0; public static int max = 10; public static List devices = new List(); - + public static bool systemDisabled = false; //boolean to set system status, false means system is not disabled static ThreadStart stats = new ThreadStart(getStats); static ThreadStart hostTS = new ThreadStart(host); static string[] tempargs; diff --git a/src/methods.cs b/src/methods.cs index bb2010d..de147ee 100644 --- a/src/methods.cs +++ b/src/methods.cs @@ -12,8 +12,8 @@ public class Program // Connection string for your IoT Hub // az iot hub show-connection-string --hub-name {your iot hub name} --policy-name service public static string s_connectionString = Server.Program.config["Service"]; - - public static async Task deviceMethod(string methodName,List deviceID) + + public static async Task deviceMethod(string methodName, List deviceID) { // This sample accepts the service connection string as a parameter, if present ValidateConnectionString(); @@ -21,13 +21,13 @@ public static async Task deviceMethod(string methodName,List deviceID) // Create a ServiceClient to communicate with service-facing endpoint on your hub. s_serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString); - await InvokeMethodAsync(methodName,deviceID); + await InvokeMethodAsync(methodName, deviceID); s_serviceClient.Dispose(); } // Invoke the direct method on the device, passing the payload - private static async Task InvokeMethodAsync(string method,List iotDevice) + private static async Task InvokeMethodAsync(string method, List iotDevice) { var methodInvocation = new CloudToDeviceMethod(method) { @@ -35,18 +35,19 @@ private static async Task InvokeMethodAsync(string method,List iotDevice }; methodInvocation.SetPayloadJson("10"); - for(int i = 0;i < iotDevice.Count; i++){ - // Invoke the direct method asynchronously and get the response from the simulated device. - try - { + for (int i = 0; i < iotDevice.Count; i++) + { + // Invoke the direct method asynchronously and get the response from the simulated device. + try + { var response = await s_serviceClient.InvokeDeviceMethodAsync(iotDevice[i].name, methodInvocation); //Console.WriteLine($"\nResponse status: {response.Status}, payload:\n\t{response.GetPayloadAsJson()}"); - } - catch(Exception e) - { - //Console.WriteLine(e); - } - + } + catch (Exception e) + { + //Console.WriteLine(e); + } + } } @@ -62,42 +63,51 @@ private static void ValidateConnectionString() } } public static void setDoor() - { + { List doors = new List(); - for(int i = 0; i < Server.Program.devices.Count;i++) + + for (int i = 0; i < Server.Program.devices.Count; i++) { - if(Server.Program.devices[i].type == "in") + if (Server.Program.devices[i].type == "in") { - if(Server.Program.devices[i].operation == false) + if (Server.Program.devices[i].operation == false) { doors.Add(Server.Program.devices[i]); } } } - if(Server.Program.current methodList = new List();methodList.Add(Server.Program.devices[temp]); - deviceMethod("unlock",methodList).Wait(); + List methodList = new List(); methodList.Add(Server.Program.devices[temp]); + deviceMethod("unlock", methodList).Wait(); } } else { Server.Program.devices[temp].operation = true; - if(Server.Program.devices[temp].status) + if (Server.Program.devices[temp].status) { - List methodList = new List();methodList.Add(Server.Program.devices[temp]); - deviceMethod("unlock",methodList).Wait(); + List methodList = new List(); methodList.Add(Server.Program.devices[temp]); + deviceMethod("unlock", methodList).Wait(); Server.Program.devices[temp].status = false; } else { - List methodList = new List();methodList.Add(Server.Program.devices[temp]); - deviceMethod("lock",methodList).Wait(); + List methodList = new List(); methodList.Add(Server.Program.devices[temp]); + deviceMethod("lock", methodList).Wait(); Server.Program.devices[temp].status = true; } - - } } - catch(Exception) + catch (Exception) { - + } } } diff --git a/src/systemController.cs b/src/systemController.cs new file mode 100644 index 0000000..e366fae --- /dev/null +++ b/src/systemController.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc; +using Server; +using devices; + +namespace Server.Controllers +{ + [Route("api/system")] + [ApiController] + public class systemController : ControllerBase + { + + [HttpPut("{id}")] + public ActionResult systemControl(string id) + { + if (id == "disable") + { + Program.systemDisabled=true; + } + if (id == "enable") + { + Program.systemDisabled=false; + } + return NoContent(); + } + } +} \ No newline at end of file