Exemplo n.º 1
0
        public async Task <Response> SendReferences(string parentObjectId, string parentDcaType, dynamic referencemodel)
        {
            try
            {
                dynamic parentObject = null;

                var response = await _dCARepository.GetObjects(parentObjectId, parentDcaType, false);

                if (response.IsSuccess == true)
                {
                    parentObject = response.ResponseMessage;
                }
                else
                {
                    return(response);
                }
                Dictionary <string, string> parent =
                    ((IEnumerable <KeyValuePair <string, JToken> >)parentObject)
                    .ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToString());

                if (parent == null)
                {
                    return(null);
                }
                else
                {
                    var jArray = JArray.Parse(referencemodel.ToString());
                    Dictionary <string, object> referenceCollection = new Dictionary <string, object>();

                    foreach (JObject content in jArray.Children <JObject>())
                    {
                        foreach (JProperty prop in content.Properties())
                        {
                            if (referenceCollection.ContainsKey(prop.Name) == false)
                            {
                                referenceCollection.Add(prop.Name, prop.Value);
                            }
                        }
                    }
                    //Fetch the DCA Type and get its corresponding Ability model and type
                    dynamic tobody = referenceCollection["to"];
                    Dictionary <string, string> dcatoBody =
                        ((IEnumerable <KeyValuePair <string, JToken> >)tobody)
                        .ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToString());


                    var childObjectId = dcatoBody["objectId"];
                    var childModel    = dcatoBody["dcaType"];
                    response = await _dCARepository.GetObjects(childObjectId, childModel);

                    //If child object doesn't exist. Create child model
                    if (response.IsSuccess == false)
                    {
                        var childDeviceModel = new DCADeviceModel()
                        {
                            model    = parent["model"],
                            type     = parent["type"],
                            objectId = childObjectId
                        };
                        //Create DCA Object if it doesn't exist
                        var result = await _dCARepository.SendObjects(childDeviceModel, false);

                        if (result.IsSuccess == false)
                        {
                            return(result);
                        }
                    }
                    dcatoBody.Remove("dcaType");
                    dcatoBody.Add("model", parent["model"]);
                    referenceCollection["to"] = JObject.FromObject(dcatoBody);
                    //Create reference object which returns reference Id if sucess
                    string path            = string.Format(referencePath, parentObjectId, parent["model"]);
                    var    referenceObject = JObject.FromObject(referenceCollection);
                    JArray referenceArray  = new JArray(referenceObject);
                    var    refResult       = await client.PostAsync(path, referenceArray);

                    return(refResult);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 2
0
 public async Task <Response> GetObjects(string objectId, string dcaType)
 {
     return(await _dCARepository.GetObjects(objectId, dcaType));
 }