예제 #1
0
        /// <summary>
        /// Sets up all PROC driver and switch rules from a machine config. Pass in attribute collections which are populated here.
        /// </summary>
        /// <param name="config"></param>
        /// <param name="_coils"></param>
        /// <param name="_switches"></param>
        /// <param name="_lamps"></param>
        /// <param name="_leds"></param>
        /// <param name="_gi"></param>
        public void SetupProcMachine(MachineConfiguration config, AttrCollection <ushort, string, IDriver> _coils = null,
                                     AttrCollection <ushort, string, Switch> _switches = null, AttrCollection <ushort, string, IDriver> _lamps = null, AttrCollection <ushort, string, LED> _leds = null, AttrCollection <ushort, string, IDriver> _gi = null)
        {
            List <VirtualDriver> new_virtual_drivers = new List <VirtualDriver>();
            bool polarity = (g_machineType == MachineType.SternWhitestar || g_machineType == MachineType.SternSAM || g_machineType == MachineType.PDB);

            PDBConfig pdb_config = null;

            if (g_machineType == MachineType.PDB)
            {
                pdb_config = new PDBConfig(this, config);
            }

            //process and add coils, add virtual driver, drivers
            if (_coils != null)
            {
                foreach (CoilConfigFileEntry ce in config.PRCoils)
                {
                    Driver d;
                    int    number;
                    if (g_machineType == MachineType.PDB && pdb_config != null)
                    {
                        number = pdb_config.GetProcNumber("PRCoils", ce.Number);

                        if (number == -1)
                        {
                            Console.WriteLine("Coil {0} cannot be controlled by the P-ROC. Ignoring...", ce.Name);
                            continue;
                        }
                    }
                    else
                    {
                        number = PinProc.PRDecode(g_machineType, ce.Number);
                    }

                    if ((ce.Bus != null && ce.Bus == "AuxPort") || number >= PinProc.kPRDriverCount)
                    {
                        d = new VirtualDriver(this, ce.Name, (ushort)number, polarity);
                        new_virtual_drivers.Add((VirtualDriver)d);
                    }
                    else
                    {
                        d = new Driver(this, ce.Name, (ushort)number);
                        Logger?.Log("Adding driver " + d.ToString());
                        d.reconfigure(ce.Polarity);
                    }
                    _coils.Add(d.Number, d.Name, d);
                }
            }

            //process and add leds
            if (g_machineType == MachineType.PDB && _leds != null)
            {
                ushort i = 0;
                foreach (LampConfigFileEntry le in config.PRLeds)
                {
                    LED    led = new LED(this, le.Name, i, le.Number);
                    string number;
                    number = le.Number;
                    //todo: polarity
                    _leds.Add(i, led.Name, led);
                    i++;
                }
            }

            if (_switches != null)
            {
                foreach (SwitchConfigFileEntry se in config.PRSwitches)
                {
                    //Log (se.Number);
                    var s = new Switch(this, se.Name, PinProc.PRDecode(g_machineType, se.Number), se.Type);

                    ushort number = 0;
                    if (g_machineType == MachineType.PDB)
                    {
                        var num = pdb_config.GetProcNumber("PRSwitches", se.Number);
                        if (num == -1)
                        {
                            Console.WriteLine("Switch {0} cannot be controlled by the P-ROC. Ignoring...", se.Name);
                            continue;
                        }
                        else
                        {
                            number = Convert.ToUInt16(num);
                        }
                    }
                    else
                    {
                        number = PinProc.PRDecode(g_machineType, se.Number);
                    }

                    s.Number = number;
                    switch_update_rule(number,
                                       EventType.SwitchClosedDebounced,
                                       new SwitchRule {
                        NotifyHost = true, ReloadActive = false
                    },
                                       null,
                                       false
                                       );
                    switch_update_rule(number,
                                       EventType.SwitchOpenDebounced,
                                       new SwitchRule {
                        NotifyHost = true, ReloadActive = false
                    },
                                       null,
                                       false
                                       );
                    Logger?.Log("Adding switch " + s.ToString());
                    _switches.Add(s.Number, s.Name, s);
                }

                /// TODO: THIS SHOULD RETURN A LIST OF STATES
                EventType[] states = SwitchGetStates();
                foreach (Switch s in _switches.Values)
                {
                    s.SetState(states[s.Number] == EventType.SwitchClosedDebounced);
                }
            }

            if (_gi != null)
            {
                foreach (GIConfigFileEntry ge in config.PRGI)
                {
                    Driver d = new Driver(this, ge.Name, PinProc.PRDecode(g_machineType, ge.Number));
                    Logger?.Log("Adding GI " + d.ToString());
                    _gi.Add(d.Number, d.Name, d);
                }
            }

            foreach (VirtualDriver virtual_driver in new_virtual_drivers)
            {
                int           base_group_number = virtual_driver.Number / 8;
                List <Driver> items_to_remove   = new List <Driver>();
                foreach (Driver d in _coils?.Values)
                {
                    if (d.Number / 8 == base_group_number)
                    {
                        items_to_remove.Add(d);
                    }
                }
                foreach (Driver d in items_to_remove)
                {
                    _coils.Remove(d.Name);
                    VirtualDriver vd = new VirtualDriver(this, d.Name, d.Number, polarity);
                    _coils.Add(d.Number, d.Name, d);
                }
                items_to_remove.Clear();
                foreach (Driver d in _lamps?.Values)
                {
                    if (d.Number / 8 == base_group_number)
                    {
                        items_to_remove.Add(d);
                    }
                }
                foreach (Driver d in items_to_remove)
                {
                    _lamps.Remove(d.Name);
                    VirtualDriver vd = new VirtualDriver(this, d.Name, d.Number, polarity);
                    _lamps.Add(d.Number, d.Name, d);
                }
            }
        }