Exemplo n.º 1
0
        protected override void WndProc(ref Message m)
        {
            bool IsHandled = false;

            USBPort.ProcessWindowsMessage(m.Msg, m.WParam, m.LParam, ref IsHandled);

            base.WndProc(ref m);
        }
Exemplo n.º 2
0
            // return a list of the down stream ports
            public ReadOnlyCollection <USBPort> GetPorts()
            {
                var PortList = new List <USBPort>();

                // Open a handle to the Hub device
                var h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

                if (h.ToInt32() == INVALID_HANDLE_VALUE)
                {
                    return(new ReadOnlyCollection <USBPort>(PortList));
                }

                var nBytes            = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX));
                var ptrNodeConnection = Marshal.AllocHGlobal(nBytes);

                // loop thru all of the ports on the hub
                // BTW: Ports are numbered starting at 1
                for (var i = 1; i <= HubPortCount; i++)
                {
                    int nBytesReturned;
                    var NodeConnection = new USB_NODE_CONNECTION_INFORMATION_EX
                    {
                        ConnectionIndex = i
                    };
                    Marshal.StructureToPtr(NodeConnection, ptrNodeConnection, true);

                    if (!DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ptrNodeConnection, nBytes,
                                         ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero))
                    {
                        continue;
                    }

                    NodeConnection = (USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(ptrNodeConnection, typeof(USB_NODE_CONNECTION_INFORMATION_EX));

                    // load up the USBPort class
                    var Status = (USB_CONNECTION_STATUS)NodeConnection.ConnectionStatus;
                    var Speed  = (USB_DEVICE_SPEED)NodeConnection.Speed;
                    var port   = new USBPort
                    {
                        PortPortNumber        = i,
                        PortHubDevicePath     = HubDevicePath,
                        PortStatus            = Status.ToString(),
                        PortSpeed             = Speed.ToString(),
                        PortIsDeviceConnected =
                            (NodeConnection.ConnectionStatus == (int)USB_CONNECTION_STATUS.DeviceConnected),
                        PortIsHub            = Convert.ToBoolean(NodeConnection.DeviceIsHub),
                        PortDeviceDescriptor = NodeConnection.DeviceDescriptor
                    };

                    // add it to the list
                    PortList.Add(port);
                }
                Marshal.FreeHGlobal(ptrNodeConnection);
                CloseHandle(h);
                // convert it into a Collection
                return(new ReadOnlyCollection <USBPort>(PortList));
            }
        //constructor
        public ControlSystem()
            : base()
        {
            string DeviceType = this.ControllerPrompt;

            if (DeviceType != "CP3" && DeviceType != "CP3N")
            {
                ErrorLog.Error("This program is for a CP3 or CP3N Control System");
                ErrorLog.Error("Terminating Execution");
                throw new NotSupportedException();
            }

            try
            {
                #region Onboard Devices
                //CP3 onboard devices
                //Slot-02.Slot2.1
                Ethernet_Information = AdapterInformation.GetAdapterInformation();
                //Slot-03.Slot-01
                Com01 = new TwoWaySerialDriver(this.ComPorts[1]);
                //Slot-03.Slot-02
                Com02 = new RS232OnlyTwoWaySerialDriver(this.ComPorts[2]);
                //Slot-03.Slot-03
                Com03 = new RS232OnlyTwoWaySerialDriver(this.ComPorts[3]);
                //Slot-04
                C2I_CP3_IO8 = IOs8AnalogInputCard.GetIO8AnalogInputCard(VersiPorts);
                //Slot-05
                C2I_CP3_RY8 = Relays8Card.GetRelay8Card(RelayPorts);
                //Slot-06 Port-01
                IR1 = this.IROutputPorts[1];
                //Slot-06 Port-02
                IR2 = this.IROutputPorts[2];
                //Slot-07
                C2I_CP3_SYSTEMMONITOR = System_Monitor.GetSystemMonitor();
                //Slot-07.Slot-01
                C2I_CP3_SYSTEMCONTROL = System_Control.GetSystemControl();
                //Slot-7.Slot-02
                C2I_CP3_SYSTEMINFORMATION = System_Information.GetSystemInformation();
                //Slot-10.Slot-01
                C2I_USB_HID = new USBPort(UsbHids[1]);
                #endregion

                //Single Port examples:
                //IOPort8 = new IOPort(VersiPorts[8]);
                //Relay8 = new SingleRelay(RelayPorts[8]);

                Thread.MaxNumberOfUserThreads = 20;
            }
            catch (Exception e)
            {
                ErrorLog.Error("Error in the constructor: {0}", e.Message);
            }
        }
Exemplo n.º 4
0
                    protected unsafe static void GetPortInformation(USBPort port)
                    {
                        // Collect the node information
                        IOCTLcommand ioctl = new IOCTLcommand();

                        ioctl.ioctlNo = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_NODE_CONNECTION_INFORMATION_EX, 0, 0x0);
                        port.connInfo = new UsbApi.UsbNativeType.USB_NODE_CONNECTION_INFORMATION();
                        port.connInfo.ConnectionIndex = port.portIndex;
                        ioctl.inputBuffer             = UsbApi.UsbNativeType.Serialize(port.connInfo);
                        ioctl.outputMaxSize           = (uint)Marshal.SizeOf(port.connInfo);
                        try {
                            // Open Root hub
                            IntPtr   hHub   = UsbApi.OpenDevice(port.rootHub.devicePath, true);
                            IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl);
                            UsbApi.CloseDevice(hHub);
                            port.connInfo = (UsbApi.UsbNativeType.USB_NODE_CONNECTION_INFORMATION)UsbApi.UsbNativeType.Deserialize(
                                status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_NODE_CONNECTION_INFORMATION));
                        } catch (Exception) {}
                    }
        public ReadOnlyCollection <USBPort> GetPorts()
        {
            List <USBPort> list   = new List <USBPort>();
            IntPtr         intPtr = Acer_USB_Library.CreateFile(this.HubDevicePath, 1073741824, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);

            if (intPtr.ToInt32() != -1)
            {
                int    num     = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX));
                IntPtr intPtr2 = Marshal.AllocHGlobal(num);
                for (int i = 1; i <= this.HubPortCount; i++)
                {
                    USB_NODE_CONNECTION_INFORMATION_EX uSB_NODE_CONNECTION_INFORMATION_EX = default(USB_NODE_CONNECTION_INFORMATION_EX);
                    uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionIndex = i;
                    Marshal.StructureToPtr(uSB_NODE_CONNECTION_INFORMATION_EX, intPtr2, true);
                    int num2 = default(int);
                    if (Acer_USB_Library.DeviceIoControl(intPtr, 2229320, intPtr2, num, intPtr2, num, out num2, IntPtr.Zero))
                    {
                        uSB_NODE_CONNECTION_INFORMATION_EX = (USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(intPtr2, typeof(USB_NODE_CONNECTION_INFORMATION_EX));
                        USBPort uSBPort = new USBPort();
                        uSBPort.PortPortNumber    = i;
                        uSBPort.PortHubDevicePath = this.HubDevicePath;
                        USB_CONNECTION_STATUS connectionStatus = (USB_CONNECTION_STATUS)uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionStatus;
                        uSBPort.PortStatus = connectionStatus.ToString();
                        USB_DEVICE_SPEED speed = (USB_DEVICE_SPEED)uSB_NODE_CONNECTION_INFORMATION_EX.Speed;
                        uSBPort.PortSpeed             = speed.ToString();
                        uSBPort.PortIsDeviceConnected = (uSB_NODE_CONNECTION_INFORMATION_EX.ConnectionStatus == 1);
                        uSBPort.PortIsHub             = Convert.ToBoolean(uSB_NODE_CONNECTION_INFORMATION_EX.DeviceIsHub);
                        uSBPort.PortDeviceDescriptor  = uSB_NODE_CONNECTION_INFORMATION_EX.DeviceDescriptor;
                        list.Add(uSBPort);
                    }
                }
                Marshal.FreeHGlobal(intPtr2);
                Acer_USB_Library.CloseHandle(intPtr);
            }
            return(new ReadOnlyCollection <USBPort>(list));
        }
Exemplo n.º 6
0
                    protected unsafe static string GetStringDescriptor(USBPort port, Byte stringIndex)
                    {
                        UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST descriptor = new UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST();
                        descriptor.ConnectionIndex = port.portIndex;
                        // USBD will automatically initialize these fields:
                        //     bmRequest = 0x80
                        //     bRequest  = 0x06
                        //     wValue    = Descriptor Type (high) and Descriptor Index (low byte)
                        descriptor.wValue = (ushort)(((ushort)UsbApi.UsbNativeType.UsbDescriptorType.USB_STRING_DESCRIPTOR_TYPE << 8) | (ushort)stringIndex);
                        //     wIndex    = Zero (or Language ID for String Descriptors)
                        descriptor.wIndex = (ushort)0;
                        //     wLength   = Length of descriptor buffer
                        descriptor.wLength = (ushort)Marshal.SizeOf(typeof(UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR));
                        // Collect the information string
                        IOCTLcommand ioctl = new IOCTLcommand();

                        ioctl.ioctlNo       = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, 0, 0x0);
                        ioctl.inputBuffer   = UsbApi.UsbNativeType.Serialize(descriptor);
                        ioctl.outputMaxSize = (uint)Marshal.SizeOf(descriptor);
                        string result = null;

                        try {
                            // Open Root hub
                            IntPtr   hHub   = UsbApi.OpenDevice(port.rootHub.devicePath, true);
                            IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl);
                            UsbApi.CloseDevice(hHub);
                            descriptor = (UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)UsbApi.UsbNativeType.Deserialize(
                                status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST));
                            UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR strDescr = (UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                descriptor.Data, 0, typeof(UsbApi.UsbNativeType.USB_STRING_DESCRIPTOR));
                            result = new String(Encoding.Unicode.GetChars(strDescr.bString, 0, strDescr.bLength));
                        }
                        catch (Exception) { result = "Invalid string value"; }

                        return(result);
                    }
 public TestUsbHids(USBPort UsbHid)
 {
     _UsbHid = UsbHid;
     Start();
 }
Exemplo n.º 8
0
            // return a list of the down stream ports
            public System.Collections.ObjectModel.ReadOnlyCollection<USBPort> GetPorts()
            {
                List<USBPort> PortList = new List<USBPort>();

                // Open a handle to the Hub device
                IntPtr h = CreateFile(HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
                if (h.ToInt32() != INVALID_HANDLE_VALUE)
                {
                    int nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX));
                    IntPtr ptrNodeConnection = Marshal.AllocHGlobal(nBytes);

                    // loop thru all of the ports on the hub
                    // BTW: Ports are numbered starting at 1
                    for (int i = 1; i <= HubPortCount; i++)
                    {
                        int nBytesReturned;
                        USB_NODE_CONNECTION_INFORMATION_EX NodeConnection = new USB_NODE_CONNECTION_INFORMATION_EX();
                        NodeConnection.ConnectionIndex = i;
                        Marshal.StructureToPtr(NodeConnection, ptrNodeConnection, true);

                        if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ptrNodeConnection, nBytes, ptrNodeConnection, nBytes, out nBytesReturned, IntPtr.Zero))
                        {
                            NodeConnection = (USB_NODE_CONNECTION_INFORMATION_EX)Marshal.PtrToStructure(ptrNodeConnection, typeof(USB_NODE_CONNECTION_INFORMATION_EX));

                            // load up the USBPort class
                            USBPort port = new USBPort();
                            port.PortPortNumber = i;
                            port.PortHubDevicePath = HubDevicePath;
                            USB_CONNECTION_STATUS Status = (USB_CONNECTION_STATUS)NodeConnection.ConnectionStatus;
                            port.PortStatus = Status.ToString();
                            USB_DEVICE_SPEED Speed = (USB_DEVICE_SPEED)NodeConnection.Speed;
                            port.PortSpeed = Speed.ToString();
                            port.PortIsDeviceConnected = (NodeConnection.ConnectionStatus == (int)USB_CONNECTION_STATUS.DeviceConnected);
                            port.PortIsHub = Convert.ToBoolean(NodeConnection.DeviceIsHub);
                            port.PortDeviceDescriptor = NodeConnection.DeviceDescriptor;

                            // add it to the list
                            PortList.Add(port);
                        }
                    }
                    Marshal.FreeHGlobal(ptrNodeConnection);
                    CloseHandle(h);
                }
                // convert it into a Collection
                return new System.Collections.ObjectModel.ReadOnlyCollection<USBPort>(PortList);
            }
Exemplo n.º 9
0
                    internal unsafe static List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR> GetConfigurationDescriptors(USBPort port, Byte configIndex)
                    {
                        UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST descriptor = new UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST();
                        descriptor.ConnectionIndex = port.portIndex;
                        // USBD will automatically initialize these fields:
                        //     bmRequest = 0x80
                        //     bRequest  = 0x06
                        //     wValue    = Descriptor Type (high) and Descriptor Index (low byte)
                        descriptor.wValue = (ushort)(((ushort)UsbApi.UsbNativeType.UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE << 8) | (ushort)configIndex);
                        //     wIndex    = Zero (or Language ID for String Descriptors)
                        descriptor.wIndex = (ushort)0;
                        //     wLength   = Length of descriptor buffer
                        descriptor.wLength = (ushort)512;
                        // Collect the information string
                        IOCTLcommand ioctl = new IOCTLcommand();

                        ioctl.ioctlNo       = UsbApi.CTL_CODE(0x00000022, (uint)UsbApi.UsbIoctlFunction.USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, 0, 0x0);
                        ioctl.inputBuffer   = UsbApi.UsbNativeType.Serialize(descriptor);
                        ioctl.outputMaxSize = (uint)Marshal.SizeOf(descriptor);
                        List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR> descriptorList = new List <UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR>();

                        try
                        {
                            // Open Root hub
                            IntPtr   hHub   = UsbApi.OpenDevice(port.rootHub.devicePath, true);
                            IoStatus status = UsbApi.DeviceIoControl(hHub, ioctl);
                            UsbApi.CloseDevice(hHub);
                            descriptor = (UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST)UsbApi.UsbNativeType.Deserialize(
                                status.buffer, 0, typeof(UsbApi.UsbNativeType.USB_DESCRIPTOR_REQUEST));
                            UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR confDescr = (UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                descriptor.Data, 0, typeof(UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR));
                            descriptorList.Add(confDescr);

                            // Now deserialize all returned descriptors
                            int descrStart = confDescr.bLength;
                            UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR common;
                            while (descrStart < confDescr.wTotalLength)
                            {
                                common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                    descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_COMMON_DESCRIPTOR));
                                switch (common.DescriptorType)
                                {
                                case UsbApi.UsbNativeType.UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE:
                                    common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                        descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_CONFIGURATION_DESCRIPTOR));
                                    break;

                                case UsbApi.UsbNativeType.UsbDescriptorType.USB_INTERFACE_DESCRIPTOR_TYPE:
                                    common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                        descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_INTERFACE_DESCRIPTOR));
                                    break;

                                case UsbApi.UsbNativeType.UsbDescriptorType.USB_ENDPOINT_DESCRIPTOR_TYPE:
                                    common = (UsbApi.UsbNativeType.IUSB_COMMON_DESCRIPTOR)UsbApi.UsbNativeType.Deserialize(
                                        descriptor.Data, descrStart, typeof(UsbApi.UsbNativeType.USB_ENDPOINT_DESCRIPTOR));
                                    break;
                                }
                                descrStart += common.Length;
                                descriptorList.Add(common);
                            }
                        }
                        catch (Exception) {}

                        return(descriptorList);
                    }
        public ControlSystem()
            : base()
        {
            TestControlSystemType.isDMPS4K150C(this, "This program is for a DMPS4K150 Control System");

            try
            {
                Thread.MaxNumberOfUserThreads = 20;

                #region Assigning SIMPL like classes
                //This clases are created to mimic the modules find in SIMPL
                //Slot-01
                Com01 = new DMPS3TwoWaySerialDriver(this.ComPorts[1]);
                //Slot-02
                IRIn1 = this.IROutputPorts[1];
                //Slot-03
                Digtal_Inputs = DigitalInputs2Card.GetSlot2DigitalInputs(this.DigitalInputPorts);
                //Slot-04
                Relays = Relays2Card.GetRelay2Card(this.RelayPorts);
                //Slot-07.Slot01
                Ethernet_Information = AdapterInformation.GetAdapterInformation();
                //Slot-08.Slot-01
                DeviceControl = DMPS4K150C_DeviceControl.GetDMPS4K150C_DeviceControl(this);
                //Slot-08.Slot-02
                AVControl = DMPS4K150C_AVControl.GetDMPS4K150C_AVControl(this);
                //Slot-08.Slot-02.Slot-01.Slot-01
                Mic1 = DMPS4K150C_Mic1.GetDMPS4K150C_Mic(this);
                //Slot-08.Slot-02.Slot-02.Slot-01
                VGA1 = DMPS4K150C_VGA_Input.GetVGAInput(this, eDmps34K150CInputs.Vga1);
                //Slot-08.Slot-02.Slot-02.Slot-02
                VGA2 = DMPS4K150C_VGA_Input.GetVGAInput(this, eDmps34K150CInputs.Vga2);
                //Slot-08.Slot-02.Slot-02.Slot-03
                VGA3 = DMPS4K150C_VGA_Input.GetVGAInput(this, eDmps34K150CInputs.Vga3);
                //Slot-08.Slot-02.Slot-02.Slot-04
                VGA4 = DMPS4K150C_VGA_Input.GetVGAInput(this, eDmps34K150CInputs.Vga4);
                //Slot-08.Slot-02.Slot-02.Slot-05
                HDMI1 = DMPS4K150C_HDMI_Input.GetHDMIInput(this, eDmps34K150CInputs.Hdmi1);
                //Slot-08.Slot-02.Slot-02.Slot-06
                HDMI2 = DMPS4K150C_HDMI_Input.GetHDMIInput(this, eDmps34K150CInputs.Hdmi2);
                //Slot-08.Slot-02.Slot-02.Slot-07
                HDMI3 = DMPS4K150C_HDMI_Input.GetHDMIInput(this, eDmps34K150CInputs.Hdmi3);
                //Slot-08.Slot-02.Slot-02.Slot-08
                HDMI4 = DMPS4K150C_HDMI_Input.GetHDMIInput(this, eDmps34K150CInputs.Hdmi4);
                //Slot-08.Slot-02.Slot-02.Slot-09
                DM1 = DMPS4K150C_DM_Input.GetDMInput(this, eDmps34K150CInputs.Dm1);
                //Slot-08.Slot-02.Slot-02.Slot-10
                DM2 = DMPS4K150C_DM_Input.GetDMInput(this, eDmps34K150CInputs.Dm2);
                //Slot-08.Slot-02.Slot-03.Slot-01
                AnalogAudioOutput = DMPS4K150C_AnalogAudioOutput.GetDMPS4K150C_Mic(this);
                //Slot-08.Slot-02.Slot-03.Slot-02
                DM1Out = DMPS4K150C_DM_Output.GetDMOutput(this, eDmps34K150COutputs.DmHdmiAnalogOutput);
                //Slot-09.Slot-01
                TT_1XX = new ConnectItDevice(new Tt1xx(this, 1));
                //Slot-11.Slot-01
                C2I_USB_HID1 = new USBPort(this.UsbHids[1]);
                //Slot-11.Slot-02
                C2I_USB_HID2 = new USBPort(this.UsbHids[2]);
                //Slot-11.Slot-03
                C2I_USB_HID3 = new USBPort(this.UsbHids[3]);
                //Slot-11.Slot-04
                C2I_USB_HID4 = new USBPort(this.UsbHids[4]);
                //Slot-12
                System_Monitor = System_Monitor.GetSystemMonitor();
                //Slot-12.Slot-01
                System_Control = System_Control.GetSystemControl();
                //Slot-12.Slot-02
                SystemInformation = System_Information.GetSystemInformation();
                #endregion

                #region Enpoints
                var DMTransmitter1 = this.SwitcherInputs[(uint)eDmps34K150CInputs.Dm1] as DMInput;
                Transmitter1 = new DM_TX_4K_100_C_1G(DMTransmitter1);
                var DMReciver = this.SwitcherOutputs[(uint)eDmps34K150COutputs.DmHdmiAnalogOutput] as DMOutput;
                Receiver1 = new DM_RMC_4K_100_C_1G(DMReciver);
                #endregion
            }
            catch (Exception e)
            {
                ErrorLog.Error("Error in the constructor: {0}", e.Message);
            }
        }
Exemplo n.º 11
0
        public int AddMonitorToDb(MonitorVm bind)
        {
            var speakers         = Mapper.Map <SpeakersVm, Speakers>(bind.Speakers);
            var camera           = Mapper.Map <CameraVm, Camera>(bind.Camera);
            var monitor          = Mapper.Map <MonitorVm, Monitor>(bind);
            var placeholderImage = @"/placeholder.jpg";

            foreach (var port in bind.VideoPorts)
            {
                var type = VideoPortEnum.VGA;
                Enum.TryParse(port.Type, out type);
                var videoPort = new VideoPort()
                {
                    Type = type,
                    Num  = port.Num,
                };
                monitor.VideoPorts.Add(videoPort);
            }

            foreach (var usbPort in bind.USBPorts)
            {
                var type = USBPortEnum.usb_2_0;
                Enum.TryParse(usbPort.Type, out type);
                var USBPort = new USBPort()
                {
                    Type = type,
                    Num  = usbPort.Num,
                };
                monitor.USBPorts.Add(USBPort);
            }

            foreach (var entry in bind.PanelColors)
            {
                monitor.PanelColors.Add(new PanelColor()
                {
                    Value = entry
                });
            }

            foreach (var entry in bind.CertificatesStandartsLicenses)
            {
                monitor.CertificatesStandartsLicenses.Add(new CertificateStandartLicense()
                {
                    Value = entry
                });
            }

            foreach (var entry in bind.Features)
            {
                monitor.Features.Add(new Feature()
                {
                    Value = entry
                });
            }

            foreach (var entry in bind.UnhandledDisplaySpecificationProperties)
            {
                monitor.UnhandledDisplaySpecificationProperties.Add(new UnhandledDIsplaySpecificationProperty()
                {
                    Value = entry
                });
            }

            monitor.Speakers  = speakers;
            monitor.Camera    = camera;
            monitor.Brand     = context.Brands.FirstOrDefault(x => x.Id == bind.BrandId);
            monitor.Thumbnail = placeholderImage;
            context.Monitors.Add(monitor);
            context.SaveChanges();

            return(monitor.Id);
        }
Exemplo n.º 12
0
        public int EditMonitor(MonitorVm bind)
        {
            var monitor = context.Monitors.Find(bind.Id);

            if (monitor.Brand.Id != bind.BrandId)
            {
                monitor.Brand = context.Brands.FirstOrDefault(x => x.Id == bind.BrandId);
            }

            monitor = Mapper.Map <MonitorVm, Monitor>(bind, monitor);

            var obsoleteVideoPorts  = context.VideoPorts.Where(x => x.MonitorId == bind.Id).ToList();
            var obsoleteUSBPorts    = context.USBPorts.Where(x => x.MonitorId == bind.Id).ToList();
            var obsoletePanelColors = context.PanelColors.Where(x => x.MonitorId == bind.Id).ToList();
            var obsoleteFeatures    = context.Features.Where(x => x.MonitorId == bind.Id).ToList();
            var obsoleteCsls        = context.CertificatesStandartsLicenses.Where(x => x.MonitorId == bind.Id).ToList();
            var obsoleteUnhandled   = context.UnhandledDisplaySpecificationProperties.Where(x => x.MonitorId == bind.Id).ToList();

            foreach (var port in obsoleteVideoPorts)
            {
                context.VideoPorts.Remove(port);
            }
            foreach (var usbPort in obsoleteUSBPorts)
            {
                context.USBPorts.Remove(usbPort);
            }
            foreach (var panelColor in obsoletePanelColors)
            {
                context.PanelColors.Remove(panelColor);
            }
            foreach (var feature in obsoleteFeatures)
            {
                context.Features.Remove(feature);
            }
            foreach (var csl in obsoleteCsls)
            {
                context.CertificatesStandartsLicenses.Remove(csl);
            }
            foreach (var unhandled in obsoleteUnhandled)
            {
                context.UnhandledDisplaySpecificationProperties.Remove(unhandled);
            }


            foreach (var port in bind.VideoPorts)
            {
                var type = VideoPortEnum.VGA;
                Enum.TryParse(port.Type, out type);
                var videoPort = new VideoPort()
                {
                    Type = type,
                    Num  = port.Num,
                };
                monitor.VideoPorts.Add(videoPort);
            }

            foreach (var USBport in bind.USBPorts)
            {
                var type = USBPortEnum.usb_2_0;
                Enum.TryParse(USBport.Type, out type);
                var USBPort = new USBPort()
                {
                    Type = type,
                    Num  = USBport.Num,
                };
                monitor.USBPorts.Add(USBPort);
            }

            foreach (var entry in bind.PanelColors)
            {
                monitor.PanelColors.Add(new PanelColor()
                {
                    Value = entry
                });
            }

            foreach (var entry in bind.CertificatesStandartsLicenses)
            {
                monitor.CertificatesStandartsLicenses.Add(new CertificateStandartLicense()
                {
                    Value = entry
                });
            }

            foreach (var entry in bind.Features)
            {
                monitor.Features.Add(new Feature()
                {
                    Value = entry
                });
            }

            foreach (var entry in bind.UnhandledDisplaySpecificationProperties)
            {
                monitor.UnhandledDisplaySpecificationProperties.Add(new UnhandledDIsplaySpecificationProperty()
                {
                    Value = entry
                });
            }

            context.Entry(monitor).State = EntityState.Modified;
            context.SaveChanges();

            return(monitor.Id);
        }