예제 #1
0
        private static void AddWeMoSwitch()
        {
            var localDevice = UPnPDevice.CreateRootDevice(/* expiration */ 3600, /* version*/ 1, /* web dir */ null);

            localDevice.StandardDeviceType = "urn:Belkin:device:controllee";
            localDevice.UniqueDeviceName   = "Lightswitch-32f9a52c-79d2-4ae2-8957-1f5a0f044e36";
            localDevice.FriendlyName       = "Test Lamp";
            //localDevice.Icon = null;
            //localDevice.HasPresentation = true;
            //localDevice.PresentationURL = presentationUrl;
            localDevice.Major            = 1; localDevice.Minor = 0;
            localDevice.SerialNumber     = "1234567890";
            localDevice.ModelNumber      = "3.1234";
            localDevice.Manufacturer     = "Belkin International Inc.";
            localDevice.ManufacturerURL  = "http://www.belkin.com";
            localDevice.ModelName        = "Socket";
            localDevice.ModelDescription = "Belkin Plugin Socket 1.0";

            /*if (Uri.IsWellFormedUriString(manufacturerUrl, UriKind.Absolute))
             * {
             *  localDevice.ModelURL = new Uri(manufacturerUrl);
             * }
             */
            localDevice.UserAgentTag = "redsonic";

            // Create an instance of the BasicEvent service
            dynamic instance = new ExpandoObject();

            // Declare the "BasicEvent1" service
            var service = new UPnPService(
                // Version
                1.0,
                // Service ID
                "urn:Belkin:serviceId:basicevent1",
                // Service Type
                "urn:Belkin:service:basicevent:1",
                // Standard Service?
                true,
                // Service Object Instance
                instance
                );

            service.ControlURL = "/upnp/control/basicevent1";
            service.EventURL   = "/upnp/event/basicevent1";
            service.SCPDURL    = "/eventservice.xml";

            string stateVarName  = "BinaryState";
            var    stateVariable = new UPnPStateVariable(stateVarName, typeof(bool), true);

            stateVariable.AddAssociation("GetBinaryState", stateVarName);
            stateVariable.AddAssociation("SetBinaryState", stateVarName);
            stateVariable.Value = false;
            service.AddStateVariable(stateVariable);

            instance.GetBinaryState = new Func <bool>(() => (bool)service.GetStateVariable(stateVarName));
            instance.SetBinaryState = new Action <int>((BinaryState) => {
                Console.WriteLine("SetBinaryState({0})", BinaryState);
                service.SetStateVariable(stateVarName, BinaryState != 0);
            });

            // Add the methods
            service.AddMethod("GetBinaryState", stateVarName);
            service.AddMethod("SetBinaryState", stateVarName);

            // Add the service
            localDevice.AddService(service);
            // Start the WeMo switch device UPnP simulator
            localDevice.StartDevice();
        }
        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);
        }