/// <summary> /// Worker function for write threading in OnepV1Examples /// </summary> public static void waitWrite(string cik, string rid, OnepV1 oneConn) { Thread.Sleep(10000); int val = new Random().Next(1, 100); Result result = oneConn.write(cik, rid, val); if (result.status == Result.OK) { Console.WriteLine(val+10 +" is the expected output of the wait"); } }
/// <summary> /// OnepV1Examples method - example sequence for layer 1 calls. Layer 1 /// calls are direct bindings to the 1P API. /// </summary> public static void OnepV1Examples(string cik) { //OnepV1(url, timeout) OnepV1 oneConn = new OnepV1("https://m2.exosite.com/onep:v1/rpc/process", 35); Result result; try { // Get resource id of dataport given its alias name 'X1' string alias_name = "X1"; result = oneConn.lookup(cik, "alias", alias_name); string rid = null; if (result.status == Result.OK) { rid = result.message; } else { // If dataport with alias 'X1' didn't exist, we will create it Console.WriteLine("Could not find Dataport with alias " + alias_name + ", creating..."); result = oneConn.create(cik, "dataport", getDescObject()); if (result.status == Result.OK) { rid = result.message; result = oneConn.map(cik, rid, alias_name); Console.WriteLine("Dataport: " + rid + " (" + alias_name + ") is created."); } } // Write data to dataport int val = new Random().Next(1, 100); result = oneConn.write(cik, rid, val); if (result.status == Result.OK) { Console.WriteLine("Dataport " + rid + " is written with raw value " + val + "."); } // Read data from dataport result = oneConn.read(cik, rid, EmptyOption.Instance); if (result.status == Result.OK) { object[][] read = JsonConvert.DeserializeObject<object[][]>(result.message); val = Int32.Parse(read[0][1].ToString()); Console.WriteLine("Dataport " + rid + " is read back as: " + val + " (value stored is different from raw write value due to pre-process rule)."); } // Create and then drop a dataport (note - a client can have many dataports w/ same name, but alias & RID must be unique) object desc = getDescObject(); result = oneConn.create(cik, "dataport", desc); if (result.status == Result.OK) { rid = result.message; Console.WriteLine("\r\nDataport: " + rid + " is created."); alias_name = "test_alias"; // map/unmap alias to dataport result = oneConn.map(cik, rid, alias_name); if (result.status == Result.OK) { Console.WriteLine("Dataport: " + rid + " is mapped to alias '" + alias_name + "'"); // Un-map the alias from the dataport result = oneConn.unmap(cik, alias_name); if (result.status == Result.OK) { Console.WriteLine("Dataport: " + rid + " is unmapped from alias '" + alias_name + "'"); } } result = oneConn.drop(cik, rid); if (result.status == Result.OK) { Console.WriteLine("Dataport: " + rid + " is dropped."); } } // List a client's dataports string[] options = new string[] { "dataport" }; result = oneConn.listing(cik, options); if (result.status == Result.OK) { Console.WriteLine("\r\nList of all Dataport RIDs for client CIK " + cik + ":"); Console.WriteLine(result.message); } /* Get all mapping alias information for dataports */ // Get resource id of device given device key result = oneConn.lookup(cik, "alias", ""); rid = result.message; // Get the alias information of given device Dictionary<string, object> option = new Dictionary<string, object>(); option.Add("aliases", true); result = oneConn.info(cik, rid, option); if (result.status == Result.OK) { Console.WriteLine("\r\nList of all Dataports with an alias for client CIK " + cik + ":"); Console.WriteLine(result.message); Console.WriteLine("\r\n"); } //wait example rid = oneConn.lookup(cik, "alias", "X1").message; ThreadStart starter = delegate { waitFunction(cik, rid, oneConn); }; Thread thread = new Thread(starter); ThreadStart starter2 = delegate { waitWrite(cik, rid, oneConn); }; Thread thread2 = new Thread(starter2); thread.Start(); thread2.Start(); Console.WriteLine("Waiting with timeout of 30 seconds for a write that will occur in 10 seconds"); thread2.Join(Timeout.Infinite); thread.Join(Timeout.Infinite); } catch (OneException e) { Console.WriteLine("\r\nOnepV1Examples sequence exception:"); Console.WriteLine(e.Message); } }