示例#1
0
        protected override void HandleRequest()
        {
            string email    = Parameters["email"];
            string password = Parameters["password"];

            try
            {
                StationInfo stationInfo = StationCollection.Instance.FindOne();
                if (stationInfo != null)
                {
                    logger.DebugFormat("Station logon with stationId = {0}", stationInfo.Id);

                    StationLogOnResponse logonRes;
                    if (email != null && password != null)
                    {
                        using (WebClient agent = new WebClient())
                        {
                            Driver driver = DriverCollection.Instance.FindOne(Query.EQ("email", email));
                            if (driver == null)
                            {
                                throw new InvalidOperationException("User is not registered to this station");
                            }

                            // station.logon must be called before user.login to handle non-existing driver case
                            logonRes = StationApi.LogOn(new WebClient(), stationInfo.Id, email, password, StatusChecker.GetDetail());

                            User user = User.LogIn(agent, email, password);

                            // update user's session token
                            driver.session_token = user.Token;
                            DriverCollection.Instance.Save(driver);
                        }
                    }
                    else
                    {
                        StationApi api = new StationApi(stationInfo.Id, stationInfo.SessionToken);
                        logonRes = api.LogOn(new WebClient(), StatusChecker.GetDetail());
                    }

                    // update session in DB
                    stationInfo.SessionToken = logonRes.session_token;
                    StationCollection.Instance.Save(stationInfo);

                    logger.Debug("Station logon successfully, disable block auth of function server");
                    functionServer.BlockAuth(false);
                }

                logger.Debug("Start function server");
                functionServer.Start();
                stationTimer.Start();
                logger.Debug("Start function server successfully");

                RespondSuccess();
            }
            catch (WammerCloudException e)
            {
                if (e.WammerError == 0x4000 + 3)                 // driver already registered another station
                {
                    logger.Error("Driver already registered another station");

                    // force user re-register the station on next startup
                    CleanDB();

                    // function server should be stopped if driver's info is removed
                    logger.Debug("Try to stop function server");
                    functionServer.Stop();
                    stationTimer.Stop();

                    throw new ServiceUnavailableException("Driver already registered another station", (int)StationApiError.AlreadyHasStaion);
                }
                else if (e.WammerError == 0x4000 + 4)                 // user does not exist
                {
                    logger.Error("Driver account does not exist");

                    // force user re-register the station on next startup
                    CleanDB();

                    // function server should be stopped if driver's info is removed
                    logger.Debug("Try to stop function server");
                    functionServer.Stop();
                    stationTimer.Stop();

                    throw;
                }
                else
                {
                    throw;
                }
            }
        }
示例#2
0
        private void SendHeartbeat(Object obj)
        {
            StationDetail detail = GetDetail();

            Model.StationInfo sinfo = Model.StationCollection.Instance.FindOne();
            if (sinfo != null)
            {
                bool   locChange = false;
                string baseurl   = NetworkHelper.GetBaseURL();
                if (baseurl != sinfo.Location)
                {
                    // update location if baseurl changed
                    logger.DebugFormat("station location changed: {0}", baseurl);
                    sinfo.Location = baseurl;
                    locChange      = true;
                }

                try
                {
                    WebClient        agent = new WebClient();
                    Cloud.StationApi api   = new Cloud.StationApi(sinfo.Id, sinfo.SessionToken);
                    if (logon == false || DateTime.Now - sinfo.LastLogOn > TimeSpan.FromDays(1))
                    {
                        logger.Debug("cloud logon start");
                        api.LogOn(agent, detail);
                        logon = true;

                        // update station info in database
                        logger.Debug("update station information");
                        sinfo.LastLogOn = DateTime.Now;
                        Model.StationCollection.Instance.Save(sinfo);
                    }

                    if (locChange)
                    {
                        // update station info in database
                        logger.Debug("update station information");
                        Model.StationCollection.Instance.Save(sinfo);
                    }
                    api.Heartbeat(agent, detail);
                }
                catch (WammerCloudException ex)
                {
                    WebException webex = (WebException)ex.InnerException;
                    if (webex != null)
                    {
                        HttpWebResponse response = (HttpWebResponse)webex.Response;
                        if (response != null)
                        {
                            if (response.StatusCode == HttpStatusCode.Unauthorized)
                            {
                                // station's session token expired, it might be caused by:
                                // 1. server maintenance
                                // 2. driver registered another station
                                // in this situation, client has to re-login/re-register the station
                                functionServer.BlockAuth(true);
                            }
                        }
                    }
                    logger.Warn("cloud send heartbeat error", ex);
                }
                catch (Exception ex)
                {
                    logger.Warn("cloud send heartbeat error", ex);
                }
            }
        }