public static async Task <string> GetHeaterMode(IotApi ccu) { string request = "HEATER MODE get"; string result = await RequestAndReceive(ccu, request); return(result); }
public static IotApi RegisterRetry(this IotApi api, int numOfRetries, TimeSpan delayTime) { RetryModule module = new Services.RetryModule(numOfRetries, delayTime); api.RegisterModule(module); return(api); }
public static async Task <string> GetDimmerMode(IotApi ccu) { string request = "DIMMER STATE get"; string result = await RequestAndReceive(ccu, request); return(result); }
public static async Task <string> SetHeaterMode(IotApi ccu, int mode) { string request = "HEATER MODE " + mode.ToString(); string result = await RequestAndReceive(ccu, request); return(result); }
public void TestBatchSend() { IotApi api = new IotApi(). RegisterModule(getDefaultModule(false)); api.Open(new System.Collections.Generic.Dictionary <string, object>()); api.SendAsync(new List <object> { new { Prop1 = 1.23, Prop2 = ":)" }, new { Prop1 = 1.2, Prop2 = ":):)" } }, (msgs) => { Assert.True(msgs.Count == 2); Assert.True(((dynamic)msgs[0]).Prop1 == 1.23); Assert.True(((dynamic)msgs[1]).Prop2 == ":):)"); }, (msgs, err) => { throw err; }, null).Wait(); }
public void GenerateUserTest() { Assert.Throws(typeof(IotApiException), () => { var username = new IotApi().GenerateUserName(m_GtwUri); }); }
public static async Task <string> GetTempSensor(IotApi ccu) { string request = "TEMP_SENSOR TEMPERATURE get"; string result = await RequestAndReceive(ccu, request); return(result); }
public static async Task <string> GetHumiditySensor(IotApi ccu) { string request = "TEMP_SENSOR HUMIDITY get"; string result = await RequestAndReceive(ccu, request); return(result); }
public static async Task <string> CloseDoor(IotApi ccu) { string request = "DOOR STATUS LOCK"; string result = await RequestAndReceive(ccu, request); return(result); }
public static IotApi UsePhilpsQueueRest(this IotApi api, string gatewayUrl, string userName) { PhilipsHueRestClient hue = new PhilipsHueRestClient(userName, gatewayUrl); api.RegisterModule(hue); return(api); }
public static async Task <string> GetListDevices(IotApi iotApi) { string request = "DEVICES LIST get"; string result = await RequestAndReceive(iotApi, request); return(result); }
public static async Task <string> GetHeaterTemp(IotApi ccu) { string request = "HEATER ACT_TEMP get"; string result = await RequestAndReceive(ccu, request); return(result); }
/// <summary> /// Connects to gateway and generates the username, which will be used in all subsequent calls. /// </summary> /// <param name="api"></param> /// <param name="gatewayUri"></param> /// <param name="retries"></param> /// <param name="delay"></param> /// <returns></returns> public static string GenerateUserName(this IotApi api, string gatewayUri, int retries = 3, int delay = 5000) { while (true) { var http = PhilipsHueRestClient.GetHttpClient(gatewayUri); var st = $"{{\"devicetype\": \"{new Random().Next()}\" }}"; try { while (true) { StringContent content = new StringContent(st); var result = http.PostAsync("api", content).Result; if (result.StatusCode != System.Net.HttpStatusCode.OK) { PhilipsHueRestClient.Throw(result); } else { var res = result.Content.ReadAsStringAsync().Result; var gtwResult = JsonConvert.DeserializeObject <JArray>(res); var err = PhilipsHueRestClient.LookupValue(gtwResult, "error"); if (err != null) { if (--retries > 0) { Task.Delay(delay).Wait(); } else { throw new IotApiException($"{err}"); } } else { dynamic keyToken = PhilipsHueRestClient.LookupValue(gtwResult, "success"); return(keyToken.username.Value); } } } } catch (Exception ex) { if (--retries > 0) { Task.Delay(delay).Wait(); } else { throw ex; } } } throw new Exception(""); }
/// <summary> /// Get IotApi /// </summary> /// <returns></returns> private IotApi getIotApi() { var api = new IotApi(); api.UsePhilpsQueueRest(m_Input.GatewayUrl, m_Input.UserName); api.Open(); return(api); }
public static IotApi RegisterPersist(this IotApi api, Dictionary <string, object> args = null) { PersistModule module = new Services.PersistModule(); api.RegisterModule(module); return(api); }
/// <summary> /// Register CoAP module to IotApi module /// </summary> /// <param name="mock">read in a Mock Object of ICoAPEndpoint</param> /// <exception cref=""></exception> /// <remarks>N/a</remarks> /// <seealso cref=""/> /// <see cref=""/> /// <permission cref="">This method can be called by: every user</permission> /// <exception cref=""></exception> private IotApi getApi(Mock <ICoapEndpoint> mock) { Dictionary <string, object> agr = new Dictionary <string, object>(); IotApi api = new IotApi().RegisterModule(new CoAPConnector.CoAPclientConnector()); agr.Add("endPoint", mock.Object); api.Open(agr); return(api); }
/// <summary> /// Format the string input as a XML-RPC Request and return the Response in string. If the operation has fault, it returns an exception message. /// </summary> /// <param name="ccu">Carrier Object</param> /// <param name="m_Request">Local string Request. Input format: "Sensor Action Value"</param> /// <param name="m_Result">Response message from server in string</param> /// <returns></returns> private async Task <string> SendandReceive(IotApi iotApi, string request) { Ccu ccu = new Ccu(); var methodCall = ccu.PrepareMethodCall(request); string response = ""; await iotApi.SendAsync(methodCall, (responseMessages) => { if (MethodResponse.isMethodResponse(responseMessages)) { MethodResponse res = responseMessages as MethodResponse; if (ccu.isGetList) { response = ccu.GetListDevices(res); } else { if (!ccu.isGetMethod) { // Set Methods do not return any value. Detecting no value means the operation is done if (res.ReceiveParams.Count() == 0) { response = "Operation is done!"; } // Set methods can not return any value else { throw new InvalidOperationException("The operation cannot return any value!"); } } else { // Get methods must return a value, if not it must be an error if (res.ReceiveParams.Count() == 0) { throw new InvalidOperationException("No value returned or error"); } // Collecting the returned value else { response = res.ReceiveParams.First().Value.ToString(); } } } } }, (error) => { response = error.Message; }); return(response); }
public void TestInit() { Assert.Throws(typeof(AggregateException), () => { IotApi api = new IotApi(); api.SendAsync(new { Prop1 = 1.2, Prop2 = ":)" }).Wait(); }); }
public void TestReceive() { IotApi api = new IotApi() .RegisterPersistModule(new Dictionary <string, object>()) .RegisterModule(getDefaultModule(true)); api.Open(new System.Collections.Generic.Dictionary <string, object>()); var msg = api.ReceiveAsync().Result; }
/// <summary> /// Used by all tests to create instance of IotApi. /// </summary> /// <returns></returns> private IotApi getApi() { IotApi api = new IotApi(); api.UsePhilpsQueueRest(m_GtwUri, m_UsrName); api.Open(); return(api); }
private static async void Doors(IotApi iotApi) { // Door int commandChoice; Console.WriteLine("Choose command to control the Door"); Console.WriteLine("1. Get door status"); Console.WriteLine("2. Open door"); Console.WriteLine("3. Close door"); Console.WriteLine("4. Back to Sensors selection"); commandChoice = RecursiveChoice(1, 4); string text = ""; switch (commandChoice) { case 1: text = await Command.GetDoorStatus(iotApi); Console.WriteLine("Status of door: " + text); break; case 2: text = await Command.OpenDoor(iotApi); Console.WriteLine(text); break; case 3: text = await Command.CloseDoor(iotApi); Console.WriteLine(text); break; case 4: Sensors(iotApi); break; } Console.WriteLine("Do you want to continue?"); Console.WriteLine("1. Yes"); Console.WriteLine("2. No"); switch (RecursiveChoice(1, 2)) { case 1: Functions(iotApi); break; case 2: Environment.Exit(2); break; } }
public void OpenTest() { Dictionary <string, object> arg = new Dictionary <string, object>(); arg.Add("endpoint", "opc.tcp://aqib:51210/UA/SampleServer"); IotApi api = new IotApi(); api.RegisterModule(new OPCConnector()); api.Open(arg); api.SendAsync("ffff").Wait(); }
public void TestOpen_Door() { IotApi connector = new IotApi() .RegisterModule(new XmlRpc()); Dictionary <string, object> agr = new Dictionary <string, object>() { { "Uri", "http://192.168.0.222:2001" }, { "Mock", false } }; connector.Open(agr); MethodCall request = new MethodCall() { MethodName = "setValue", SendParams = new List <Param>() { new Param() { Value = (string)"LEQ1335713:1", }, new Param() { Value = (string)"STATE", }, new Param() { Value = (bool)true, }, } }; try { connector.SendAsync(request, (onMessage) => { Assert.True(onMessage != null); }, (error) => { throw error; }).Wait(); }catch (Exception ex) { Assert.True(ex != null); } }
public static async Task <string> SetHeaterTemp(IotApi ccu, double tempVal) { if (tempVal <= 3.5) { tempVal = 3.5; } else if (tempVal >= 30.5) { tempVal = 30.5; } string request = "HEATER SET_TEMP " + tempVal.ToString(); string result = await RequestAndReceive(ccu, request); return(result); }
private static async void Weather(IotApi iotApi) { // Weather int commandChoice; Console.WriteLine("Choose command to control the Weather sensor"); Console.WriteLine("1. Get temperature"); Console.WriteLine("2. Get Humidity"); Console.WriteLine("3. Back to Sensors selection"); commandChoice = RecursiveChoice(1, 3); string text = ""; switch (commandChoice) { case 1: text = await Command.GetTempSensor(iotApi); Console.WriteLine("Temperature: " + text); break; case 2: text = await Command.GetHumiditySensor(iotApi); Console.WriteLine("Humidity: " + text); break; case 3: Sensors(iotApi); break; } Console.WriteLine("Do you want to continue?"); Console.WriteLine("1. Yes"); Console.WriteLine("2. No"); switch (RecursiveChoice(1, 2)) { case 1: Functions(iotApi); break; case 2: Environment.Exit(2); break; } }
public void TestSimpleSend() { IotApi api = new IotApi(); api.Open(); api.SendAsync(new { Prop1 = 1.2, Prop2 = ":)" }, (msgs) => { }, (msgs, err) => { }, null).Wait(); ; }
public void TestReceive() { IotApi api = new IotApi() .RegisterPersist(new Dictionary <string, object>()) .RegisterModule(getDefaultModule(true)); api.Open(new System.Collections.Generic.Dictionary <string, object>()); api.ReceiveAsync( (msgs) => { }, (msgs, err) => { }).Wait(); }
public static async Task <string> GetDoorStatus(IotApi ccu) { string request = "DOOR STATUS get"; string result = await RequestAndReceive(ccu, request); string state = ""; if (result.ToLower() == "true") { state = "Opened"; } if (result.ToLower() == "false") { state = "Closed"; } return(state); }
public void TestSimpleSend() { IotApi api = new IotApi(); api.Open(); api.SendAsync(new { Prop1 = 1.2, Prop2 = ":)" }, (msgs) => { throw new InvalidOperationException("This should not be executed, because no pipeline is defined!"); }, (msgs, err) => { throw new InvalidOperationException("This should not be executed, because no pipeline is defined!"); }, null).Wait(); }
public void TestSend() { IotApi api = new IotApi(); api.Open(new System.Collections.Generic.Dictionary <string, object>()); api.SendAsync(new { Prop1 = 1.2, Prop2 = ":)" }, (msgs) => { Assert.IsTrue(msgs.Count == 1); // Assert.IsTrue(((dynamic)msgs[0]).Prop1 == 1.2); }, (msgs, err) => { throw err; }, null).Wait(); }