Exemple #1
0
        public void setDoor(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
        {
            Console.WriteLine("How many doors do you want to set? [1(default)-128]");
            Console.Write(">>>> ");
            char[]         delimiterChars = { ' ', ',', '.', ':', '\t' };
            int            amount         = Util.GetInput(1);
            List <BS2Door> doorList       = new List <BS2Door>();

            for (int idx = 0; idx < amount; ++idx)
            {
                BS2Door door = Util.AllocateStructure <BS2Door>();

                Console.WriteLine("Enter a value for door[{0}]", idx);
                Console.WriteLine("  Enter the ID for the door which you want to set");
                Console.Write("  >>>> ");
                door.doorID = (UInt32)Util.GetInput();
                Console.WriteLine("  Enter the name for the door which you want to set");
                Console.Write("  >>>> ");
                string doorName = Console.ReadLine();
                if (doorName.Length == 0)
                {
                    Console.WriteLine("  [Warning] door name will be displayed as empty.");
                }
                else if (doorName.Length > BS2Envirionment.BS2_MAX_DOOR_NAME_LEN)
                {
                    Console.WriteLine("  Name of door should less than {0} words.", BS2Envirionment.BS2_MAX_DOOR_NAME_LEN);
                    return;
                }
                else
                {
                    byte[] doorArray = Encoding.UTF8.GetBytes(doorName);
                    Array.Clear(door.name, 0, BS2Envirionment.BS2_MAX_DOOR_NAME_LEN);
                    Array.Copy(doorArray, door.name, doorArray.Length);
                }

                Console.WriteLine("  Enter the ID of the Reader for entrance");
                Console.Write("  >>>> ");
                door.entryDeviceID = Util.GetInput((UInt32)0);

                Console.WriteLine("  Enter the ID of the Reader for exit");
                Console.Write("  >>>> ");
                door.exitDeviceID = Util.GetInput((UInt32)0);

                Console.WriteLine("  Enter the AutoLock timeout in seconds: [3(default)]");
                Console.Write("  >>>> ");
                door.autoLockTimeout = Util.GetInput((UInt32)3);

                Console.WriteLine("  Enter the HeldOpen timeout in seconds: [3(default)]");
                Console.Write("  >>>> ");
                door.heldOpenTimeout = Util.GetInput((UInt32)3);

                Console.WriteLine("  Should this Door be locked instantly when it is closed? [Y/n]");
                Console.Write("  >>>> ");
                if (Util.IsYes())
                {
                    door.instantLock = 1;
                }
                else
                {
                    door.instantLock = 0;
                }

                Console.WriteLine("  Does this door has a relay? [Y/n]");
                Console.Write("  >>>> ");
                if (Util.IsYes())
                {
                    Console.WriteLine("  Enter the device id for the relay on this door.");
                    Console.Write("  >>>> ");
                    door.relay.deviceID = (UInt32)Util.GetInput();

                    Console.WriteLine("  Enter the the port of the relay on this door.[0(default)]");
                    Console.Write("  >>>> ");
                    door.relay.port = Util.GetInput(0);
                }

                Console.WriteLine("  Does this door has a door sensor? [Y/n]");
                Console.Write("  >>>> ");
                if (Util.IsYes())
                {
                    Console.WriteLine("  Enter the device id of the door sensor on this door.");
                    Console.Write("  >>>> ");
                    door.sensor.deviceID = (UInt32)Util.GetInput();

                    Console.WriteLine("  Enter the the port of the door sensor on this door.[0(default)]");
                    Console.Write("  >>>> ");
                    door.sensor.port = Util.GetInput(0);

                    Console.WriteLine("  Enter the switch type of the door sensor on this door: [0: normally open, 1: normally closed].");
                    Console.Write("  >>>> ");
                    door.sensor.switchType = Util.GetInput(0);
                }

                Console.WriteLine("  Does this door has a exit button? [Y/n]");
                Console.Write("  >>>> ");
                if (Util.IsYes())
                {
                    Console.WriteLine("  Enter the device id of the exit button on this door.");
                    Console.Write("  >>>> ");
                    door.button.deviceID = (UInt32)Util.GetInput();

                    Console.WriteLine("  Enter the the port of the exit button on this door.[0(default)]");
                    Console.Write("  >>>> ");
                    door.button.port = Util.GetInput(0);

                    Console.WriteLine("  Enter the switch type of the exit button on this door: [0: normally open, 1: normally closed].");
                    Console.Write("  >>>> ");
                    door.button.switchType = Util.GetInput(0);
                }

                Console.WriteLine("  How to act at lock door? [0: None(default), 1: Schedule, 2: Emergency, 4: Operator]");
                Console.Write("  >>>> ");
                door.lockFlags = Util.GetInput((byte)BS2DoorFlagEnum.NONE);

                Console.WriteLine("  How to act at unlock door? [0: None(default), 1: Schedule, 2: Emergency, 4: Operator]");
                Console.Write("  >>>> ");
                door.unlockFlags = Util.GetInput((byte)BS2DoorFlagEnum.NONE);

                BS2DoorAlarmFlagEnum doorAlarmFlag = BS2DoorAlarmFlagEnum.NONE;
                for (int loop = 0; loop < BS2Envirionment.BS2_MAX_FORCED_OPEN_ALARM_ACTION; ++loop)
                {
                    door.forcedOpenAlarm[loop].type = (byte)BS2ActionTypeEnum.NONE;
                }

                for (int loop = 0; loop < BS2Envirionment.BS2_MAX_HELD_OPEN_ALARM_ACTION; ++loop)
                {
                    door.heldOpenAlarm[loop].type = (byte)BS2ActionTypeEnum.NONE;
                }

#if false //please refer to ZoneControl
                Console.WriteLine("  Does this door need to forced open door alarm? [y/N]");
                if (!Util.IsNo())
                {
                    Console.WriteLine("  How many forced open door alarm do you want to set? [1(default)-{0}]", BS2Envirionment.BS2_MAX_FORCED_OPEN_ALARM_ACTION);
                    Console.Write("  >>>> ");
                    int alarmCount = Util.GetInput(1);

                    if (alarmCount > 0)
                    {
                        doorAlarmFlag |= BS2DoorAlarmFlagEnum.FORCED_OPEN;

                        for (int loop = 0; loop < alarmCount; ++loop)
                        {
                            Console.WriteLine("  Enter the action type which you want to set [6(default) : relay, 7 : ttl, 8 : sound, 9: display, 10 : buzzer, 11: led]");
                            Console.Write("  >>>> ");
                            door.forcedOpenAlarm[loop].type = Util.GetInput((byte)BS2ActionTypeEnum.RELAY);

                            switch ((BS2ActionTypeEnum)door.forcedOpenAlarm[loop].type)
                            {
                            case BS2ActionTypeEnum.RELAY:
                            {
                                BS2RelayAction relay = Util.AllocateStructure <BS2RelayAction>();

                                Console.WriteLine("  Enter the the port of the relay on this door.[0(default)]");
                                Console.Write("  >>>> ");
                                relay.relayIndex = Util.GetInput(0);
                            }
                            break;
                            }
                        }
                    }
                }
#endif

                door.unconditionalLock = (byte)doorAlarmFlag;

                Console.WriteLine("  Does this door need to dual authentication? [y/N]");
                if (Util.IsNo())
                {
                    door.dualAuthDevice            = (byte)BS2DualAuthDeviceEnum.NO_DEVICE;
                    door.dualAuthScheduleID        = (UInt32)BS2ScheduleIDEnum.NEVER;
                    door.dualAuthTimeout           = 0;
                    door.dualAuthApprovalType      = (byte)BS2DualAuthApprovalEnum.NONE;
                    door.numDualAuthApprovalGroups = 0;
                }
                else
                {
                    Console.WriteLine("  Which reader requires dual authentication? [1: Entrance Only(default), 2: Exit Only, 3: Both]");
                    Console.Write("  >>>> ");
                    door.dualAuthDevice = Util.GetInput((byte)BS2DualAuthDeviceEnum.ENTRY_DEVICE_ONLY);

                    Console.WriteLine("  Enter the id of access schedule for dual authentication: [0: Never, 1: Always(default), or the other schedule id]");
                    Console.Write("  >>>> ");
                    door.dualAuthScheduleID = Util.GetInput((UInt32)BS2ScheduleIDEnum.ALWAYS);

                    Console.WriteLine("  Enter the dual authentication timeout in seconds: [5(default)]");
                    Console.Write("  >>>> ");
                    door.dualAuthTimeout = Util.GetInput((UInt32)5);

                    Console.WriteLine("  Who should be the dual authentication approver for this door? [0: Not required(default), 1: Second user] ");
                    Console.Write("  >>>> ");
                    door.dualAuthApprovalType = Util.GetInput((byte)BS2DualAuthApprovalEnum.NONE);

                    Console.WriteLine("  Enter the ID of access groups for dual authentication approval: [ID_1,ID_2 ...]");
                    Console.Write("  >>>> ");
                    string[]      accessGroupIDs    = Console.ReadLine().Split(delimiterChars);
                    List <UInt32> accessGroupIDList = new List <UInt32>();

                    foreach (string accessGroupID in accessGroupIDs)
                    {
                        if (accessGroupID.Length > 0)
                        {
                            UInt32 item;
                            if (UInt32.TryParse(accessGroupID, out item))
                            {
                                accessGroupIDList.Add(item);
                            }
                        }
                    }

                    door.numDualAuthApprovalGroups = (byte)accessGroupIDList.Count;
                    for (int loop = 0; loop < accessGroupIDList.Count; ++loop)
                    {
                        door.dualAuthApprovalGroupID[loop] = accessGroupIDList[loop];
                    }

                    //If you want to set up one door apb zone, please refer to ZoneControl section.
                }

                doorList.Add(door);
            }

            int    structSize     = Marshal.SizeOf(typeof(BS2Door));
            IntPtr doorListObj    = Marshal.AllocHGlobal(structSize * doorList.Count);
            IntPtr curDoorListObj = doorListObj;
            foreach (BS2Door item in doorList)
            {
                Marshal.StructureToPtr(item, curDoorListObj, false);
                curDoorListObj = (IntPtr)((long)curDoorListObj + structSize);
            }

            Console.WriteLine("Trying to set doors to device.");
            BS2ErrorCode result = (BS2ErrorCode)API.BS2_SetDoor(sdkContext, deviceID, doorListObj, (UInt32)doorList.Count);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
            }

            Marshal.FreeHGlobal(doorListObj);
        }
        public static string getActionMsg(BS2Action action)
        {
            BS2ActionTypeEnum actionType = (BS2ActionTypeEnum)action.type;

            switch (actionType)
            {
            case BS2ActionTypeEnum.NONE:
                return("Not specified");

            case BS2ActionTypeEnum.RELAY:
            {
                BS2RelayAction relay = Util.ConvertTo <BS2RelayAction>(action.actionUnion);
                return(String.Format("RelayAction relayIndex[{0}] signalID[{1}] count[{2}] onDuration[{3}ms] offDuration[{4}ms] delay[{5}ms]",
                                     relay.relayIndex,
                                     relay.signal.signalID,
                                     relay.signal.count,
                                     relay.signal.onDuration,
                                     relay.signal.offDuration,
                                     relay.signal.delay));
            }

            case BS2ActionTypeEnum.TTL:
            {
                BS2OutputPortAction outputPort = Util.ConvertTo <BS2OutputPortAction>(action.actionUnion);
                return(String.Format("OutputPortAction relayIndex[{0}] signalID[{1}] count[{2}] onDuration[{3}ms] offDuration[{4}ms] delay[{5}ms]",
                                     outputPort.portIndex,
                                     outputPort.signal.signalID,
                                     outputPort.signal.count,
                                     outputPort.signal.onDuration,
                                     outputPort.signal.offDuration,
                                     outputPort.signal.delay));
            }

            case BS2ActionTypeEnum.DISPLAY:
            {
                BS2DisplayAction display = Util.ConvertTo <BS2DisplayAction>(action.actionUnion);
                return(String.Format("DisplayAction displayID[{0}] resourceID[{1}] delay[{2}ms]",
                                     display.displayID,
                                     display.resourceID,
                                     display.duration));
            }

            case BS2ActionTypeEnum.SOUND:
            {
                BS2SoundAction sound = Util.ConvertTo <BS2SoundAction>(action.actionUnion);
                return(String.Format("SoundAction soundIndex[{0}] count[{1}]", sound.soundIndex, sound.count));
            }

            case BS2ActionTypeEnum.LED:
            {
                BS2LedAction led          = Util.ConvertTo <BS2LedAction>(action.actionUnion);
                string       ledSignalStr = "";
                for (int idx = 0; idx < BS2Environment.BS2_LED_SIGNAL_NUM; ++idx)
                {
                    ledSignalStr += String.Format("[color[{0}] duration[{1}ms] delay[{2}ms]]",
                                                  (BS2LedColorEnum)led.signal[idx].color,
                                                  led.signal[idx].duration,
                                                  led.signal[idx].delay);

                    if (idx + 1 < BS2Environment.BS2_LED_SIGNAL_NUM)
                    {
                        ledSignalStr += ", ";
                    }
                }

                return(String.Format("LedAction count[{0}] {1}", led.count, ledSignalStr));
            }

            case BS2ActionTypeEnum.BUZZER:
            {
                BS2BuzzerAction buzzer          = Util.ConvertTo <BS2BuzzerAction>(action.actionUnion);
                string          buzzerSignalStr = "";
                for (int idx = 0; idx < BS2Environment.BS2_BUZZER_SIGNAL_NUM; ++idx)
                {
                    buzzerSignalStr += String.Format("[tone[{0}] fadeout[{1}] duration[{2}ms] delay[{3}ms]]",
                                                     (BS2BuzzerToneEnum)buzzer.signal[idx].tone,
                                                     Convert.ToBoolean(buzzer.signal[idx].fadeout),
                                                     buzzer.signal[idx].duration,
                                                     buzzer.signal[idx].delay);

                    if (idx + 1 < BS2Environment.BS2_BUZZER_SIGNAL_NUM)
                    {
                        buzzerSignalStr += ", ";
                    }
                }

                return(String.Format("BuzzerAction count[{0}] {1}", buzzer.count, buzzerSignalStr));
            }

            case BS2ActionTypeEnum.LIFT:
            {
                BS2LiftAction lift = Util.ConvertTo <BS2LiftAction>(action.actionUnion);
                return(String.Format("LiftAction deviceID[{0}] type[{1}]", lift.liftID, (BS2LiftActionTypeEnum)lift.type));
            }

            default:
                return("Not implemented yet.");
            }
        }