Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Program
public static int current = 0;
public static int max = 10;
public static List<device> devices = new List<device>();

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;
Expand Down
86 changes: 47 additions & 39 deletions src/methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,42 @@ 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<device> deviceID)
public static async Task deviceMethod(string methodName, List<device> deviceID)
{
// This sample accepts the service connection string as a parameter, if present
ValidateConnectionString();

// 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<device> iotDevice)
private static async Task InvokeMethodAsync(string method, List<device> iotDevice)
{
var methodInvocation = new CloudToDeviceMethod(method)
{
ResponseTimeout = TimeSpan.FromSeconds(30),
};
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);
}

}
}

Expand All @@ -62,42 +63,51 @@ private static void ValidateConnectionString()
}
}
public static void setDoor()
{
{
List<device> doors = new List<device>();
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<Server.Program.max)
if (Server.Program.systemDisabled == true)
{
deviceMethod("unlock",doors).Wait();
for(int i = 0; i < doors.Count;i++)
deviceMethod("unlock", doors).Wait();
for (int i = 0; i < doors.Count; i++)
{
doors[i].status = false;
}
}
else if (Server.Program.current < Server.Program.max)
{
deviceMethod("unlock", doors).Wait();
for (int i = 0; i < doors.Count; i++)
{
doors[i].status = false;
}
}
else
{
deviceMethod("lock",doors).Wait();
for(int i = 0;i < doors.Count;i++ )
deviceMethod("lock", doors).Wait();
for (int i = 0; i < doors.Count; i++)
{
doors[i].status = true;
}
}
}
}

public static void overrideDoor(int door)
{
int temp = -1;
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].id == door)
if (Server.Program.devices[i].id == door && Server.Program.systemDisabled==false)
{
temp = i;
goto gotDoor;
Expand All @@ -106,38 +116,36 @@ public static void overrideDoor(int door)
gotDoor:
try
{
if(Server.Program.devices[temp].operation)
if (Server.Program.devices[temp].operation)
{
Server.Program.devices[temp].operation = false;
if(Server.Program.devices[temp].type == "out"||Server.Program.devices[temp].type == "both")
if (Server.Program.devices[temp].type == "out" || Server.Program.devices[temp].type == "both")
{
Server.Program.devices[temp].status = false;
List<device> methodList = new List<device>();methodList.Add(Server.Program.devices[temp]);
deviceMethod("unlock",methodList).Wait();
List<device> methodList = new List<device>(); 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<device> methodList = new List<device>();methodList.Add(Server.Program.devices[temp]);
deviceMethod("unlock",methodList).Wait();
List<device> methodList = new List<device>(); methodList.Add(Server.Program.devices[temp]);
deviceMethod("unlock", methodList).Wait();
Server.Program.devices[temp].status = false;
}
else
{
List<device> methodList = new List<device>();methodList.Add(Server.Program.devices[temp]);
deviceMethod("lock",methodList).Wait();
List<device> methodList = new List<device>(); methodList.Add(Server.Program.devices[temp]);
deviceMethod("lock", methodList).Wait();
Server.Program.devices[temp].status = true;
}


}
}
catch(Exception)
catch (Exception)
{

}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/systemController.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}