コード例 #1
0
        public ResponseData <Account> Signin([FromBody] Account account)
        {
            try
            {
                var response = new ResponseData <Account>();
                account.Password = _helperService.Hashing(account.Password);
                var acc = _accountService.Signin(account.Email, account.Password);
                if (acc == null || (acc != null && !acc.IsAdmin.Value))
                {
                    response.Data    = acc;
                    response.Message = Constant.ResponseMessage.EmailNotValid;
                    response.Status  = ResponseStatus.Success.ToString();
                    return(response);
                }

                var accountAppliance = _accountApplianceceService.GetAccountApplianceByAccountId(acc.Id);
                if (accountAppliance == null && accountAppliance.Count() == 0)
                {
                    response.Data    = acc;
                    response.Message = Constant.ResponseMessage.HaveNoAppliance;
                    response.Status  = ResponseStatus.Success.ToString();
                    return(response);
                }

                var _event = new Event()
                {
                    AccountId   = acc.Id,
                    EventTypeId = Constant.EventType.SigninSignoutOfWebPortal,
                    EventDetail = Constant.ResponseMessage.Signin,
                    Timestamp   = DateTime.UtcNow,
                    Message     = Constant.ResponseMessage.Signin
                };

                _eventService.Insert(_event);

                response.Data    = acc;
                response.Message = ResponseMessage.Success;
                response.Status  = ResponseStatus.Success.ToString();
                return(response);
            }
            catch (Exception ex)
            {
                var response = new ResponseData <Account>();
                response.Message = ex.Message;
                response.Status  = ResponseStatus.Error.ToString();
                return(response);
            }
        }
コード例 #2
0
        public async Task <ResponseData <Dashboard> > Index(string accountid, string applianceid)
        {
            try
            {
                var accountAppliance = _accountApplianceService.GetAccountApplianceByAccountId(Convert.ToInt64(accountid));
                var _event           = _eventService.GetEventByApplianceId(Convert.ToInt64(applianceid));
                var lAccounts        = _accountApplianceService.GetAccountByApplianceId(Convert.ToInt64(applianceid), true);
                List <AccountApplianceDTO> accountApplianceDTO = new List <AccountApplianceDTO>();

                if (accountAppliance != null && accountAppliance.Count > 0)
                {
                    foreach (var item in accountAppliance)
                    {
                        accountApplianceDTO.Add(new AccountApplianceDTO(item));
                    }
                }

                lAccounts = lAccounts.Where(x => x.Id != Convert.ToInt64(accountid)).ToList();

                if (!TelitApi.IsConnected)
                {
                    TelitApi.CheckIOTConnection().Wait();
                }

                var appliance = _applianceService.GetApplianceById(Convert.ToInt64(applianceid));
                var thingFind = await TelitApi.ThingFind(appliance.SerialNumber);



                Telit.ThingFind.Params _params = new Telit.ThingFind.Params();
                string applianceEnvironment    = string.Empty;

                ApplianceDTO applianceDTO = new ApplianceDTO();

                if (thingFind != null && thingFind.Things != null && thingFind.Things.success && thingFind.Things.@params != null)
                {
                    _params = thingFind.Things.@params;
                }

                if (_params != null && _params.alarms != null && _params.alarms.env != null && _params.alarms.env.state >= 0 && _params.alarms.env.state < 16)
                {
                    applianceEnvironment = Convert.ToString(_params.alarms.env.state, 2).PadLeft(4, '0');
                    var array = !string.IsNullOrEmpty(applianceEnvironment) ? applianceEnvironment.ToArray() : new char[] { };

                    applianceDTO.Cellular     = array.Length == 4 ? Convert.ToBoolean(Convert.ToInt16(array[2].ToString())) : false;
                    applianceDTO.Power        = array.Length == 4 ? Convert.ToBoolean(Convert.ToInt16(array[3].ToString())) : false;
                    applianceDTO.Wifi         = array.Length == 4 ? Convert.ToBoolean(Convert.ToInt16(array[1].ToString())) : false;
                    applianceDTO.WiFiInternet = array.Length == 4 ? Convert.ToBoolean(Convert.ToInt16(array[0].ToString())) : false;
                }

                applianceDTO.TimerEnabled = _params != null && _params.alarms != null && _params.alarms.timerState != null ? (Convert.ToInt64(_params.alarms.timerState.state) != 0 ? true : false) : false;
                applianceDTO.DeviceName   = _params != null && _params.attrs != null && _params.attrs.name != null ? _params.attrs.name.value : appliance.DeviceName;
                applianceDTO.SerialNumber = appliance.SerialNumber;
                applianceDTO.TriggerMile  = appliance.TriggerMile;
                applianceDTO.Id           = appliance.Id;
                applianceDTO.IsOn         = _params != null && _params.alarms != null && _params.alarms.on != null?CheckApplianceIsOn(Convert.ToInt16(_params.alarms.on.state)) : false;

                applianceDTO.IsConnected     = _params.connected;
                applianceDTO.Lat             = appliance.Lat;
                applianceDTO.Lon             = appliance.Lon;
                applianceDTO.GeoFenceEnabled = appliance.GeoFenceEnabled;
                applianceDTO.Status          = appliance.Status;

                if (_params != null && _params.alarms != null)
                {
                    applianceDTO.TrustLevel = _params.alarms.trust != null ? (int?)_params.alarms.trust.state : 3;
                }

                var plans = _stripeService.GetPlans();

                var      subscriptionId = _accountApplianceService.GetAccountApplianceByAccountIdAndApplianceId(Convert.ToInt64(accountid), Convert.ToInt64(applianceid));
                DateTime?expireDate     = null;
                bool     cancelAtEnd    = false;
                if (subscriptionId != null && !string.IsNullOrEmpty(subscriptionId.SubscriptionId))
                {
                    var stripe = _stripeService.RetrieveSubscription(subscriptionId.SubscriptionId);
                    // current period end
                    expireDate  = stripe.CurrentPeriodEnd;
                    cancelAtEnd = stripe.CancelAtPeriodEnd;
                }

                var dashboard = new Dashboard()
                {
                    AccountAppliance     = accountApplianceDTO,
                    Events               = _event,
                    ListAccountsDTO      = lAccounts,
                    IsIOTConnected       = TelitApi.IsConnected,
                    Plans                = plans,
                    AppDTO               = applianceDTO,
                    ExpireDate           = expireDate,
                    CancelAtEnd          = cancelAtEnd,
                    IsApplianceConnected = applianceDTO.IsConnected.Value
                };

                var response = new ResponseData <Dashboard>();

                response.Data    = dashboard;
                response.Message = ResponseMessage.Success;
                response.Status  = ResponseStatus.Success.ToString();
                return(response);
            }
            catch (Exception ex)
            {
                var response = new ResponseData <Dashboard>();
                response.Message = ex.Message;
                response.Status  = ResponseStatus.Error.ToString();
                return(response);
            }
        }