/// <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); } }
static void Main(string[] args) { provisionForm frm = new provisionForm(); frm.ShowDialog(); string vendorname = frm.vendorname; string vendortoken = frm.vendortoken; string clonecik = frm.clonecik; string cloneportalcik = frm.cloneportalcik; //use only if managing by sharecode string portalcik = frm.portalcik; int r = new Random().Next(1, 10000000); string model = "MyTestModel" + r.ToString(); string sn1 = "001" + r.ToString(); string sn2 = "002" + r.ToString(); string sn3 = "003" + r.ToString(); OnepV1 op = new OnepV1("https://m2.exosite.com/onep:v1/rpc/process", 3); Result portalridResult = op.lookup(portalcik, "alias", ""); string portalrid = null; string clonerid = null; if (portalridResult.status == Result.OK) { portalrid = portalridResult.message; Console.WriteLine("\r\nportalrid: " + portalrid); } else Console.WriteLine("\r\nFailed to look up portal RID"); Result cloneridResult = op.lookup(clonecik, "alias", ""); Provision provision = null; if (cloneridResult.status == Result.OK) { clonerid = cloneridResult.message; Console.WriteLine("\r\nclonerid: " + clonerid); provision = new Provision("https://m2.exosite.com", 3, false, true); } else Console.WriteLine("\r\nFailed to look up clone RID"); Dictionary<string, string> meta = new Dictionary<string, string>(); string[] options = new string[2]; options[0] = vendorname; options[1] = model; string option = "[" + "\"" + vendorname + "\"" + ", " + "\"" + model + "\"" + "]"; meta.Add("meta", option); string sharecode = op.share(cloneportalcik, clonerid, meta).message; try { Console.WriteLine("\r\nmodel_create()"); Console.WriteLine("\r\n" + provision.model_create(vendortoken, model, sharecode, false, true, true).message); Console.WriteLine("\r\nmodel_list()\r\n"+provision.model_list(vendortoken).message); Console.WriteLine("\r\nmodel_info()\r\n" + provision.model_info(vendortoken, model).message); Console.WriteLine("\r\nserialnumber_add()"); Console.WriteLine("\r\n" + provision.serialnumber_add(vendortoken, model, sn1).message); Console.WriteLine("\r\nserialnumber_add_batch()"); string[] sn2andsn3 = new string[2]; sn2andsn3[0] = sn2; sn2andsn3[1] = sn3; Console.WriteLine("\r\n" + provision.serialnumber_add_batch(vendortoken, model, sn2andsn3).message); Console.WriteLine("\r\nserialnumber_list()\r\n" + provision.serialnumber_list(vendortoken, model, 0, 10).message); Console.WriteLine("\r\nserialnumber_remove_batch()"); Console.WriteLine("\r\n" + provision.serialnumber_remove_batch(vendortoken, model, sn2andsn3).message); Console.WriteLine("\r\nserialnumber_list()\r\n" + provision.serialnumber_list(vendortoken, model, 0, 1000).message); Console.WriteLine("\r\nserialnumber_enable()"); provision.serialnumber_enable(vendortoken, model, sn1, portalrid); //return clientid Console.WriteLine("\r\nAFTER ENABLE: " + provision.serialnumber_info(vendortoken, model, sn1).message); Console.WriteLine("\r\nserialnumber_disable()"); provision.serialnumber_disable(vendortoken, model, sn1); Console.WriteLine("\r\nAFTER DISABLE: " + provision.serialnumber_info(vendortoken, model, sn1).message); Console.WriteLine("\r\nserialnumber_reenable()"); provision.serialnumber_reenable(vendortoken, model, sn1); Console.WriteLine("\r\nAFTER REENABLE: " + provision.serialnumber_info(vendortoken, model, sn1).message); Console.WriteLine("\r\nserialnumber_activate()"); //return client key string sn_cik = provision.serialnumber_activate(model, sn1, vendorname).message; Console.WriteLine("\r\nAFTER ACTIVATE: " + provision.serialnumber_info(vendortoken, model, sn1).message); Console.WriteLine("\r\ncontent_create()"); Console.WriteLine("\r\n" + provision.content_create(vendortoken, model, "a.txt", "This is text", false).message); Console.WriteLine("\r\ncontent_upload()"); Console.WriteLine("\r\n" + provision.content_upload(vendortoken, model, "a.txt", "This is content data", "text/plain").message); Console.WriteLine("\r\ncontent_list()\r\n" + provision.content_list(vendortoken, model).message); Console.WriteLine(vendortoken, model, "a.txt"); Console.WriteLine("\r\ncontent_remove()"); provision.content_remove(vendortoken, model, "a.txt"); Console.WriteLine("\r\nmodel_remove()"); provision.model_remove(vendortoken, model); } catch (ProvisionException e) { Console.WriteLine("\r\nprovisionExample sequence exception:"); Console.WriteLine(e.Message); } }