Ejemplo n.º 1
0
        /// <summary>
        /// Add a method to expose in this service
        /// </summary>
        /// <param name="MethodName">The name of the method to expose</param>
        public void AddMethod(String MethodName)
        {
            string retname = "_ReturnValue";
            UPnPStateVariable[] ESV;
            bool DontCreate = false;
            ESV = this.GetStateVariables();
            UPnPStateVariable sv;
            MethodInfo minfo = ServiceInstance.GetType().GetMethod(MethodName);
            if (minfo == null)
            {
                throw (new Exception(MethodName + " does not exist in " + ServiceInstance.GetType().ToString()));
            }

            // Create Generic State Variables
            DontCreate = false;
            if (minfo.ReturnType.FullName != "System.Void")
            {
                if (minfo.GetCustomAttributes(true).Length > 0)
                {
                    foreach (Attribute a in minfo.GetCustomAttributes(true))
                    {
                        if (a.GetType() == typeof(OpenSource.UPnP.ReturnArgumentAttribute))
                        {
                            retname = ((ReturnArgumentAttribute)a).Name;
                            break;
                        }
                    }
                }

                // There is a return value
                sv = new UPnPStateVariable("A_ARG_TYPE_" + MethodName + "_RetType", minfo.ReturnType, false);
                sv.AddAssociation(MethodName, retname);

                foreach (UPnPStateVariable _ESV in ESV)
                {
                    foreach (UPnPStateVariable.AssociationNode AESV in _ESV.GetAssociations())
                    {
                        if ((AESV.ActionName == MethodName) && (AESV.ArgName == retname))
                        {
                            // Don't create state variable
                            DontCreate = true;
                        }
                    }
                }

                if (DontCreate == false) this.AddStateVariable(sv);
            }

            ParameterInfo[] pinfo = minfo.GetParameters();
            for (int x = 0; x < pinfo.Length; ++x)
            {
                sv = new UPnPStateVariable("A_ARG_TYPE_" + MethodName + "_" + pinfo[x].Name, pinfo[x].ParameterType, false);
                sv.AddAssociation(MethodName, pinfo[x].Name);
                DontCreate = false;
                foreach (UPnPStateVariable _ESV in ESV)
                {
                    foreach (UPnPStateVariable.AssociationNode AESV in _ESV.GetAssociations())
                    {
                        if ((AESV.ActionName == MethodName) && (AESV.ArgName == pinfo[x].Name))
                        {
                            // Don't create state variable
                            DontCreate = true;
                        }
                    }
                }
                if (DontCreate == false) this.AddStateVariable(sv);
            }
            UPnPAction NewAction = new UPnPAction();
            NewAction.Name = MethodName;
            NewAction.ParentService = this;
            NewAction.MethodPointer = minfo;
            UPnPArgument ARG;

            if (minfo.ReturnType.FullName != "System.Void")
            {
                ARG = new UPnPArgument(retname, "");
                ARG.DataType = UPnPStateVariable.ConvertToUPnPType(minfo.ReturnType);
                ARG.Direction = "out";
                ARG.IsReturnValue = true;
                ARG.ParentAction = NewAction;
                ARG.StateVarName = this.GetStateVariableObject(MethodName, retname).Name;
                NewAction.AddArgument(ARG);
            }
            foreach (ParameterInfo p in pinfo)
            {
                ARG = new UPnPArgument(p.Name, "");
                ARG.DataType = UPnPStateVariable.ConvertToUPnPType(p.ParameterType);

                ARG.Direction = p.Attributes == ParameterAttributes.Out ? "out" : "in";

                ARG.IsReturnValue = false;
                ARG.ParentAction = NewAction;
                ARG.StateVarName = this.GetStateVariableObject(MethodName, p.Name).Name;
                NewAction.AddArgument(ARG);
            }
            this.AddAction(NewAction);
        }
Ejemplo n.º 2
0
        public MainForm(string[] args)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            foreach (string parm in args)
            {
                if (parm.ToUpper().StartsWith("/CACHE:"))
                {
                    DText p = new DText();
                    p.ATTRMARK = ":";
                    p[0] = parm;
                    try
                    {
                        CacheTime = int.Parse(p[2]);
                    }
                    catch (Exception)
                    {
                    }
                }
                else if (parm.ToUpper() == "/DEBUG")
                {
                    OpenSource.Utilities.EventLogger.Enabled = true;
                    OpenSource.Utilities.EventLogger.ShowAll = true;
                    OpenSource.Utilities.InstanceTracker.Display();
                }
                else if (parm.ToUpper().StartsWith("/PORT:"))
                {
                    DText p = new DText();
                    p.ATTRMARK = ":";
                    p[0] = parm;
                    try
                    {
                        PortNum = int.Parse(p[2]);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            upnpLightDevice = UPnPDevice.CreateRootDevice(CacheTime, 1, "web\\");
            upnpLightDevice.Icon = iconImageList.Images[0];
            upnpLightDevice.HasPresentation = true;
            upnpLightDevice.PresentationURL = "/";
            upnpLightDevice.FriendlyName = this.Text + " (" + System.Windows.Forms.SystemInformation.ComputerName + ")";
            upnpLightDevice.Manufacturer = "OpenSource";
            upnpLightDevice.ManufacturerURL = "http://opentools.homeip.net";
            upnpLightDevice.ModelName = "Network Light Bulb";
            upnpLightDevice.ModelDescription = "Software Emulated Light Bulb";
            upnpLightDevice.ModelURL = new Uri("http://opentools.homeip.net");
            upnpLightDevice.ModelNumber = "XPC-L1";
            upnpLightDevice.StandardDeviceType = "DimmableLight";
            upnpLightDevice.UniqueDeviceName = System.Guid.NewGuid().ToString();

            // Switch Power
            upnpLightService = new UPnPService(1, "SwitchPower.0001", "SwitchPower", true, this);
            upnpLightService.AddMethod("SetTarget");
            upnpLightService.AddMethod("GetTarget");
            upnpLightService.AddMethod("GetStatus");

            UPnPStateVariable upnpStatusVar = new UPnPStateVariable("Status", typeof(bool), true);
            upnpStatusVar.AddAssociation("GetStatus", "ResultStatus");
            upnpStatusVar.Value = false;
            upnpLightService.AddStateVariable(upnpStatusVar);
            UPnPStateVariable upnpTargetVar = new UPnPStateVariable("Target", typeof(bool), false);
            upnpTargetVar.AddAssociation("SetTarget", "newTargetValue");
            upnpTargetVar.AddAssociation("GetTarget", "newTargetValue");
            upnpTargetVar.Value = false;
            upnpLightService.AddStateVariable(upnpTargetVar);

            // Dimmable device
            upnpDimmerService = new UPnPService(1, "Dimming.0001", "Dimming", true, this);
            upnpDimmerService.AddMethod("SetLoadLevelTarget");
            upnpDimmerService.AddMethod("GetLoadLevelTarget");
            upnpDimmerService.AddMethod("GetLoadLevelStatus");
            upnpDimmerService.AddMethod("GetMinLevel");

            UPnPStateVariable upnpLevelTargetVar = new UPnPStateVariable("LoadLevelTarget", typeof(byte), false);
            upnpLevelTargetVar.AddAssociation("SetLoadLevelTarget", "NewLoadLevelTarget");
            upnpLevelTargetVar.AddAssociation("GetLoadLevelTarget", "NewLoadLevelTarget");
            upnpLevelTargetVar.Value = (byte)100;
            upnpLevelTargetVar.SetRange((byte)0, (byte)100, null);
            upnpDimmerService.AddStateVariable(upnpLevelTargetVar);
            UPnPStateVariable upnpLevelStatusVar = new UPnPStateVariable("LoadLevelStatus", typeof(byte), true);
            upnpLevelStatusVar.AddAssociation("GetLoadLevelStatus", "RetLoadLevelStatus");
            upnpLevelStatusVar.Value = (byte)100;
            upnpLevelStatusVar.SetRange((byte)0, (byte)100, null);
            upnpDimmerService.AddStateVariable(upnpLevelStatusVar);
            UPnPStateVariable upnpMinLevelVar = new UPnPStateVariable("MinLevel", typeof(byte), false);
            upnpMinLevelVar.AddAssociation("GetMinLevel", "MinLevel");
            upnpMinLevelVar.Value = (byte)0;
            upnpDimmerService.AddStateVariable(upnpMinLevelVar);

            // Add Services
            upnpLightDevice.AddService(upnpLightService);
            upnpLightDevice.AddService(upnpDimmerService);
        }