public override void Execute()
 {
     if (Parameters.Count > 0)
     {
         IResource resource = _Client.GetResource(Parameters[0]);
         if (resource != null)
         {
             List <IResource> items = resource.Children.ToArray().ToList();
             items.Sort(CompareResources);
             foreach (IResource item in items)
             {
                 LWM2MResources resources = item as LWM2MResources;
                 if (resources == null)
                 {
                     LWM2MResource itemResource = item as LWM2MResource;
                     if (itemResource == null)
                     {
                         Console.Write(item.Name.PadRight(33, ' '));
                     }
                     else
                     {
                         Console.Write(item.Name.PadRight(3, ' '));
                         if (string.IsNullOrEmpty(itemResource.Description))
                         {
                             Console.Write(new string(' ', 30));
                         }
                         else
                         {
                             Console.Write(itemResource.Description.PadRight(30, ' '));
                         }
                     }
                     Console.Write(":");
                     Console.WriteLine(item.ToString());
                 }
                 else
                 {
                     Console.Write(item.Name.PadRight(3, ' '));
                     Console.Write(resources.Description);
                     Console.WriteLine();
                     List <IResource> children = resources.Children.ToArray().ToList();
                     children.Sort(CompareResources);
                     foreach (IResource childItem in children)
                     {
                         Console.Write("\t\t");
                         Console.Write(childItem.Name);
                         Console.Write("\t:");
                         Console.WriteLine(childItem.ToString());
                     }
                 }
             }
         }
     }
     else
     {
         IResource resource = _Client.GetResource(null);
         if (resource != null)
         {
             List <IResource> items = resource.Children.Where((e) => { return(e.Visible); }).ToArray().ToList();
             items.Sort(CompareResources);
             foreach (IResource item in items)
             {
                 List <IResource> children = item.Children.ToArray().ToList();
                 children.Sort(CompareResources);
                 if (children.Count > 0)
                 {
                     foreach (IResource childItem in children)
                     {
                         Console.Write("  ");
                         Console.Write(item.Name);
                         Console.Write("/");
                         Console.WriteLine(childItem.Name);
                     }
                 }
                 else
                 {
                     Console.Write("  ");
                     Console.WriteLine(item.Name);
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        public void LoadResources()
        {
            if (Directory.Exists(RESOURCES_CACHE))
            {
                foreach (string directory in Directory.GetDirectories(RESOURCES_CACHE))
                {
                    string id = Path.GetFileName(directory);
                    int    objectID;
                    if (int.TryParse(id, out objectID))
                    {
                        LWM2MResources resources = null;
                        switch (objectID)
                        {
                        case 0:
                            resources = _SecurityResources;
                            break;

                        case 1:
                            resources = _ServerResources;
                            break;

                        case 3:
                            resources = new DeviceResources();
                            break;

                        case 4:
                            resources = new ConnectivityMonitoringResources();
                            break;

                        case 5:
                            resources = new FirmwareUpdateResources();
                            break;

                        case 6:
                            resources = new LocationResources();
                            break;

                        case 7:
                            resources = new ConnectivityStatisticsResources();
                            break;

                        case 15:
                            resources = new DeviceCapabilityResources();
                            break;

                        case 20000:
                            resources = new FlowObjectResources();
                            break;

                        case 20001:
                            resources = new FlowAccessResources();
                            break;

                        case 20005:
                            resources = new FlowCommandResources();
                            EventHandler <ChildCreatedEventArgs> handler = (s, e) =>
                            {
                                FlowCommandResource flowCommandResource = e.Resource as FlowCommandResource;
                                if (flowCommandResource != null)
                                {
                                    flowCommandResource.Updated += new EventHandler(FlowCommand_Updated);
                                    FlowCommand_Updated(flowCommandResource, null);
                                }
                            };
                            resources.ChildCreated += handler;
                            break;

                        default:
                            break;
                        }
                        if (resources != null)
                        {
                            foreach (string fileName in Directory.GetFiles(directory, "*.tlv"))
                            {
                                LWM2MResource resource = null;
                                switch (objectID)
                                {
                                case 0:
                                    resource = new SecurityResource(Path.GetFileNameWithoutExtension(fileName));
                                    break;

                                case 1:
                                    resource = new ServerResource(Path.GetFileNameWithoutExtension(fileName));
                                    break;

                                case 3:
                                    resource = new DeviceResource();
                                    break;

                                case 4:
                                    resource = new ConnectivityMonitoringResource();
                                    break;

                                case 5:
                                    resource = new FirmwareUpdateResource();
                                    break;

                                case 6:
                                    resource = new LocationResource();
                                    break;

                                case 7:
                                    resource = new ConnectivityStatisticsResource();
                                    break;

                                case 15:
                                    resource = new DeviceCapabilityResource(Path.GetFileNameWithoutExtension(fileName));
                                    break;

                                case 20000:
                                    resource          = new FlowObjectResource();
                                    resource.Updated += new EventHandler(FlowObject_Updated);
                                    break;

                                case 20001:
                                    resource = new FlowAccessResource();
                                    break;

                                case 20005:
                                    resource          = new FlowCommandResource(Path.GetFileNameWithoutExtension(fileName));
                                    resource.Updated += new EventHandler(FlowCommand_Updated);
                                    break;

                                default:
                                    break;
                                }
                                if (resource != null)
                                {
                                    using (Stream stream = File.OpenRead(fileName))
                                    {
                                        TlvReader reader = new TlvReader(stream);
                                        if (resource.Deserialise(reader))
                                        {
                                            resources.Add(resource);
                                        }
                                    }

                                    if (objectID == 0)
                                    {
                                        SecurityResource securityResource = resource as SecurityResource;
                                        if (securityResource.SecurityMode == TSecurityMode.PreSharedKey)
                                        {
                                            UsePSK(System.Text.Encoding.UTF8.GetString(securityResource.ClientPublicKey), StringUtils.HexString(securityResource.SecretKey));
                                        }
                                    }
                                    else if (objectID == 20000)
                                    {
                                        FlowObjectResource flowObjectResource = resource as FlowObjectResource;
                                        if (flowObjectResource != null)
                                        {
                                            flowObjectResource.TenantHash = null;
                                        }
                                    }
                                }
                            }
                            this.AddResources(resources);
                        }
                    }
                }
            }
            else
            {
                LoadDefaultResources();
            }
        }
Exemplo n.º 3
0
        public override void Execute()
        {
            if (Parameters.Count > 1)
            {
                if (Parameters[0].StartsWith("/"))
                {
                    Parameters[0] = Parameters[0].Substring(1);
                }
                LWM2MResource resource = _Client.GetResource(Parameters[0]) as LWM2MResource;
                if (resource == null)
                {
                    IResource parentResource = _Client.GetParentResource(Parameters[0]);

                    resource = parentResource as LWM2MResource;

                    if (resource == null && parentResource.Parent != null && String.IsNullOrEmpty(parentResource.Parent.Path))
                    {
                        // object instance does not exist
                        LWM2MResources resources  = parentResource as LWM2MResources;
                        string         instanceID = Parameters[0].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[1];
                        parentResource = resource = resources.CreateResource(instanceID);

                        resources.ModifiedResource = resource;

                        resources.Changed();
                        //(parentResource as LWM2MResource).Changed();
                        resource = null;
                    }

                    if (resource == null)
                    {
                        LWM2MResources resources = parentResource as LWM2MResources;

                        if (resources != null)
                        {
                            string[]         paths            = Parameters[0].Split('/');
                            string           name             = paths[paths.Length - 1];
                            LWM2MResource    childResource    = null;
                            BooleanResources booleanResources = resources as BooleanResources;
                            if (booleanResources == null)
                            {
                                DateTimeResources dateTimeResources = resources as DateTimeResources;
                                if (dateTimeResources == null)
                                {
                                    FloatResources floatResources = resources as FloatResources;
                                    if (floatResources == null)
                                    {
                                        IntegerResources integerResources = resources as IntegerResources;
                                        if (integerResources == null)
                                        {
                                            OpaqueResources opaqueResources = resources as OpaqueResources;
                                            if (opaqueResources == null)
                                            {
                                                StringResources stringResources = resources as StringResources;
                                                if (stringResources == null)
                                                {
                                                }
                                                else
                                                {
                                                    childResource = new StringResource(name);
                                                }
                                            }
                                            else
                                            {
                                                childResource = new OpaqueResource(name);
                                            }
                                        }
                                        else
                                        {
                                            childResource = new IntegerResource(name);
                                        }
                                    }
                                    else
                                    {
                                        childResource = new FloatResource(name);
                                    }
                                }
                                else
                                {
                                    childResource = new DateTimeResource(name);
                                }
                            }
                            else
                            {
                                childResource = new BooleanResource(name);
                            }


                            if (childResource != null)
                            {
                                childResource.SetValue(Parameters[1]);
                                resources.Add(childResource);
                            }
                        }
                    }
                    else
                    {
                    }
                }
                else
                {
                    resource.SetValue(Parameters[1]);
                    resource.Changed();
                    LWM2MResource parent = resource.Parent as LWM2MResource;
                    if (parent != null)
                    {
                        parent.Changed();
                        LWM2MResources grandparent = parent.Parent as LWM2MResources;
                        if (grandparent != null)
                        {
                            grandparent.Changed();
                        }
                    }
                }
            }
        }