Esempio n. 1
0
        public static void Main()
        {
            // Instantiate a new web server on port 80.
            WebServer server = new WebServer(80);

            // Add a handler for commands that are received by the server.
            server.CommandReceived += new WebServer.CommandReceivedHandler(server_CommandReceived);

            // Add a command that the server will parse.
            // Any command name is allowed; you will decide what the command does
            // in the CommandReceived handler. The server will only fire CommandReceived
            // for commands that are defined here and that are called with the proper
            // number of arguments.
            // In this example, I define a command 'setLed', which needs one argument (on/off).
            // With this statement, I defined that we can call our server on (for example)
            // http://[server-ip]/setled/on
            // http://[server-ip]/setled/off
            server.AllowedCommands.Add(new WebCommand("setled", 1));

            // Start the server.
            server.Start();

            // Make sure Netduino keeps running.
            while (true)
            {
                Debug.Print("Netduino still running...");
                Thread.Sleep(10000);
            }
        }
Esempio n. 2
0
        public static void Main()
        {
            // Onboard LED
            onboardLed = new OutputPort(Pins.ONBOARD_LED, false);

            // Watchdog
            watchdog = new OutputPort(Pins.GPIO_PIN_D8, false);

            // Get network time
            DateTimeSetter.SetTimeFromNTPWhenPossible();

            // Event log
            eventLogger = new EventLogger(@"events.txt", @"debug.txt");

            // Settings
            settingsDb = new SettingsDb(@"Settings.txt");

            // RFID reader
            rfidReader = new RFIDReader();

            // RFID Tag Database
            rfidTagDb = new RfidTagDb(@"TagIds.txt");

            // Door strikes
            mainDoorStrike = new DoorStrike(Pins.GPIO_PIN_D1, Pins.GPIO_PIN_D4);
            innerDoorStrike = new DoorStrike(Pins.GPIO_PIN_D0, Pins.GPIO_NONE);

            // Tap decoder
            tapDecoder = new TapDecoder(NUM_DIGITS_IN_CODE, Pins.GPIO_PIN_D5);

            // Instantiate a new web server on port 80.
            WebServer server = new WebServer(80);

            // Add a handler for commands that are received by the server.
            server.CommandReceived += new WebServer.CommandReceivedHandler(server_CommandReceived);

            // Command to receive the index page
            server.AllowedCommands.Add(new WebCommand("", 0));
            server.AllowedCommands.Add(new WebCommand("setup", 0));
            server.AllowedCommands.Add(new WebCommand("setup.html", 0));
            server.AllowedCommands.Add(new WebCommand("status", 0));
            server.AllowedCommands.Add(new WebCommand("addtag", 1));
            server.AllowedCommands.Add(new WebCommand("testtag", 1));
            server.AllowedCommands.Add(new WebCommand("main-unlock", 0));
            server.AllowedCommands.Add(new WebCommand("main-lock", 0));
            server.AllowedCommands.Add(new WebCommand("inner-unlock", 0));
            server.AllowedCommands.Add(new WebCommand("inner-lock", 0));
            server.AllowedCommands.Add(new WebCommand("submit-settings", 1));
            server.AllowedCommands.Add(new WebCommand("get-settings", 0));
            server.AllowedCommands.Add(new WebCommand("get-tags", 0));
            server.AllowedCommands.Add(new WebCommand("get-events", 0));
            server.AllowedCommands.Add(new WebCommand("get-debug", 0));

            // Start the server.
            server.Start();

            // loop forever...
            while (true)
            {
                // Toggle LED
                onboardLedCount++;
                if (onboardLedCount > onboardLedRate)
                {
                    onboardLedCount = 0;
                    onboardLed.Write(!onboardLed.Read());
                }

                // Pulse watchdog to keep alive - but only if a web command received recently
                // otherwise allow the watchdog to reset the device
                if (DateTime.Now < timeOfLastWebCommandRx + timeoutOnWebCommandRx)
                {
                    watchdog.Write(true);
                    Thread.Sleep(1);
                    watchdog.Write(false);
                }

                // Service the rfid reader - this allows it to get characters and interpret them
                rfidReader.Service();

                // Service the door strikes - allowing them to time-out, etc
                mainDoorStrike.Service();
                innerDoorStrike.Service();

                // Check if there is a tag present
                bool tagPresentStatus = false;
                if (DateTime.Now > rfidCheckLoopTimer + timeoutOnRfidCheckLoop)
                {
                    String tagId = rfidReader.GetRFIDTagId();

                    // Test code to inject a tag
                    if (tagId.Length == 0)
                    {
                        if (testTagId.Length > 0)
                        {
                            tagId = testTagId;
                            if (DateTime.Now > testTimer + timeoutOnTest)
                            {
                                testTagId = "";
                            }
                        }
                    }

                    // Door strike action
                    if (tagId.Length > 0)
                    {
                        tagPresentStatus = true;
                        string holderName = "";
                        bool isEnabled = false;
                        if (rfidTagDb.IsTagKnown(tagId, out holderName, out isEnabled))
                        {
                            if (isEnabled)
                            {
                                // Unlock main door - and log event if it was previously locked
                                if (mainDoorStrike.Unlock(settingsDb.mainAutoTimeout))
                                    eventLogger.LogEntryEvent(tagId, holderName);

                                // Unlock inner too
                                innerDoorStrike.Unlock(settingsDb.innerAutoTimeout);
                            }
                        }
                        //Debug.Print(tagID);
                    }
                    rfidCheckLoopTimer = DateTime.Now;
                }

                // Check for state changes and broadcast status message if so
                bool stateChange = tagPresentStatus || tapDecoder.IsTapperPressed();
                stateChange |= (mainDoorLastLockState != mainDoorStrike.IsLocked());
                stateChange |= (innerDoorLastLockState != mainDoorStrike.IsLocked());
                stateChange |= (mainDoorLastOpenState != mainDoorStrike.IsOpen());
                if (stateChange)
                {
                    // Check we don't broadcast constantly
                    if ((DateTime.Now > lastStatusBroadcastTime + STATUS_BROADCAST_TIMEOUT))
                    {
                        BroadcastStatus.Send(GetStatusString());
                        lastStatusBroadcastTime = DateTime.Now;
                        mainDoorLastLockState = mainDoorStrike.IsLocked();
                        innerDoorLastLockState = mainDoorStrike.IsLocked();
                        mainDoorLastOpenState = mainDoorStrike.IsOpen();
                    }
                }

                // The following code attempts to emulate the "tap-lock" that I previously
                // implemented

                // Check for tapper
                if (tapDecoder.IsCodeReady())
                {
                    int theCode = tapDecoder.GetCode();
                    Debug.Print("Code is " + theCode.ToString());

                    // Currently the code is fixed here - but the digit 0 isn't valid so this
                    // needs to be changed to a 4 digit code using digits 1..9
                    if (theCode == 0000)
                    {
                        // Unlock main door - and log event if it was previously locked
                        if (mainDoorStrike.Unlock(settingsDb.mainAutoTimeout))
                            eventLogger.LogEntryEvent("Tapper", "Unknown");

                        // Unlock inner too
                        innerDoorStrike.Unlock(settingsDb.innerAutoTimeout);
                    }
                    eventLogger.LogDebug("Code Entry " + theCode, tapDecoder.GetTapTimingsStr());
                    tapDecoder.Reset();
                }

            }
        }