예제 #1
0
        public async Task <ResponseData <Dashboard> > UpdateAppliance(string applianceid, string accountId, string power)
        {
            try
            {
                var appliance = _applianceService.GetApplianceById(Convert.ToInt64(applianceid));
                var account   = _accountService.GetAccountById(appliance.AccountId.Value);

                if (appliance.GeoFenceEnabled != null && appliance.GeoFenceEnabled.HasValue && !appliance.GeoFenceEnabled.Value && appliance.TimerEnabled != null && appliance.TimerEnabled.HasValue && !appliance.TimerEnabled.Value)
                {
                    appliance.GeoFenceEnabled = true;
                    appliance.TimerEnabled    = true;
                    _applianceService.Update(appliance);
                    if (appliance.IsOn.Value)
                    {
                        var updateTimerState = await TelitApi.UpdateTimerState(appliance.SerialNumber, Configuration.TimerEnableAirgapOn, false);
                    }
                }

                string message   = "Update from WebPortal by " + account.Email;
                var    isSuccess = await TelitApi.UpdateAlarmState(appliance.SerialNumber, power == Configuration.TurnOff.ToLower()?AlarmStatus.OffPortal : AlarmStatus.OnPortal, message);

                var _event = new Event()
                {
                    ApplianceId = Convert.ToInt64(applianceid),
                    AccountId   = Convert.ToInt64(accountId),
                    EventTypeId = Constant.EventType.ManualConnectedStatusChange,
                    EventDetail = Constant.ResponseMessage.ManualConnectedStatusChange,
                    Timestamp   = DateTime.UtcNow,
                    Message     = Constant.ResponseMessage.Wifi + " " + power.ToUpper()
                };

                _eventService.Insert(_event);


                var response = new ResponseData <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);
            }
        }
예제 #2
0
        public async Task <ResponseData <Dashboard> > UpdateApplianceForMobile(string applianceid, string uuId, string power)
        {
            try
            {
                var appliance = _applianceService.GetApplianceById(Convert.ToInt64(applianceid));
                var account   = _accountApplianceService.GetAccountByUUID(uuId);

                string message   = "Update from Mobile App by " + account.FirstName;
                var    isSuccess = await TelitApi.UpdateAlarmState(appliance.SerialNumber, power == Configuration.TurnOff.ToLower()?AlarmStatus.OffMobile : AlarmStatus.OnMobile, message);


                var _event = new Event()
                {
                    ApplianceId = Convert.ToInt64(applianceid),
                    AccountId   = Convert.ToInt64(account.Id),
                    EventTypeId = Constant.EventType.ManualConnectedStatusChange,
                    EventDetail = Constant.ResponseMessage.ManualConnectedStatusChange,
                    Timestamp   = DateTime.UtcNow,
                    Message     = Constant.ResponseMessage.Wifi + " " + power.ToUpper()
                };

                _eventService.Insert(_event);


                var response = new ResponseData <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);
            }
        }
예제 #3
0
        public async Task <ResponseData <Dashboard> > UpdateGeoFenceTrip(string uuid, string lat, string lon)
        {
            try
            {
                var response = new ResponseData <Dashboard>();

                var lAccountAppliance = _accountApplianceService.GetAccountApplianceByUUID(uuid);

                //get all appliance by uuid
                var listAppliance = _accountApplianceService.GetAllApplianceOfUUID(uuid);

                if (listAppliance != null && listAppliance.Count() > 0)
                {
                    foreach (var item in listAppliance)
                    {
                        if (item.GeoFenceEnabled == false && item.TimerEnabled == false)
                        {
                            continue;
                        }

                        bool isTurnOnNetwork = false;

                        if (item.GeoFenceEnabled.Value)
                        {
                            if (lAccountAppliance != null && lAccountAppliance.Count > 0)
                            {
                                foreach (var accountAppliance in lAccountAppliance)
                                {
                                    if (accountAppliance.ApplianceId == item.Id)
                                    {
                                        accountAppliance.Lat = Convert.ToDouble(lat);
                                        accountAppliance.Lon = Convert.ToDouble(lon);
                                        _accountApplianceService.Update(accountAppliance);
                                    }
                                }
                            }
                            var lAccounts = _accountApplianceService.GetAccountByApplianceId(item.Id, true);
                            //lAccounts = lAccounts.Where(x => x.Id != item.Id).ToList();

                            foreach (var account in lAccounts)
                            {
                                if (!isTurnOnNetwork)
                                {
                                    double lon1       = item.Lon != null && item.Lon.HasValue ? item.Lon.Value : 0;
                                    double lat1       = item.Lat != null && item.Lat.HasValue ? item.Lat.Value : 0;
                                    double lon2       = account.Lon != null && account.Lon.HasValue ? account.Lon.Value : 0;
                                    double lat2       = account.Lat != null && account.Lat.HasValue ? account.Lat.Value : 0;
                                    var    isMatching = DistanceBetweenPlaces(lon1, lat1, lon2, lat2);
                                    if (isMatching < Convert.ToDouble(item.TriggerMile))
                                    {
                                        isTurnOnNetwork = true;
                                    }
                                }
                            }

                            //get appliance in telit
                            string applianceEnvironment = string.Empty;
                            var    thingFind            = await TelitApi.ThingFind(item.SerialNumber);

                            Telit.ThingFind.Params _params = new Telit.ThingFind.Params();
                            bool?wifi = null;

                            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[] { };

                                wifi = array.Length == 4 ? Convert.ToBoolean(Convert.ToInt16(array[1].ToString())) : false;
                            }

                            if (!string.IsNullOrEmpty(applianceEnvironment) && wifi != null && wifi.HasValue)
                            {
                                if (wifi.Value != isTurnOnNetwork)
                                {
                                    if (isTurnOnNetwork)
                                    {
                                        var updateTimerState = await TelitApi.UpdateTimerState(item.SerialNumber, Configuration.TimerEnable, true);

                                        string message = "Update from Geofence";
                                        await TelitApi.UpdateAlarmState(item.SerialNumber, AlarmStatus.OnGeofence, message);

                                        var UpdateNetwork = await TelitApi.UpdateEnvironment(item.SerialNumber, Convert.ToInt32(ReplaceAt(applianceEnvironment, 1, '1'), 2), Constant.EventType.StatusChangeFromGeoFence.ToString());

                                        var isSuccess = await TelitApi.UpdateAlarmState(item.SerialNumber, AlarmStatus.OnGeofence, string.Empty);
                                    }
                                    else
                                    {
                                        var updateTimerState = await TelitApi.UpdateTimerState(item.SerialNumber, Configuration.TimerDisable, false);

                                        string message = "Update from Geofence";
                                        await TelitApi.UpdateAlarmState(item.SerialNumber, AlarmStatus.OffGeofence, message);

                                        var UpdateNetwork = await TelitApi.UpdateEnvironment(item.SerialNumber, Convert.ToInt32(ReplaceAt(applianceEnvironment, 1, '0'), 2), Constant.EventType.StatusChangeFromGeoFence.ToString());

                                        var isSuccess = await TelitApi.UpdateAlarmState(item.SerialNumber, AlarmStatus.OffGeofence, string.Empty);
                                    }
                                }
                            }
                        }
                    }
                }

                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);
            }
        }
예제 #4
0
        public async Task <ResponseData <Dashboard> > SetAirGapOn(string applianceid, string accountId, string airGapAlwaysStatus)
        {
            try
            {
                var appliance = _applianceService.GetApplianceById(Convert.ToInt64(applianceid));
                var account   = _accountService.GetAccountById(appliance.AccountId.Value);
                var thingFind = await TelitApi.ThingFind(appliance.SerialNumber);

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


                if (thingFind != null && thingFind.Things != null && thingFind.Things.success && thingFind.Things.@params != null)
                {
                    _params = thingFind.Things.@params;
                }
                bool isOn = _params != null && _params.alarms != null && _params.alarms.on != null?CheckApplianceIsOn(Convert.ToInt16(_params.alarms.on.state)) : false;

                if (airGapAlwaysStatus.ToLower() == Configuration.TurnOff.ToLower())
                {
                    appliance.TimerEnabled    = true;
                    appliance.GeoFenceEnabled = true;
                    var isSuccess = await TelitApi.UpdateTimerState(appliance.SerialNumber, Configuration.TimerEnable, appliance.IsOn.Value);
                }
                else
                {
                    if (isOn == false)
                    {
                        string message   = "Update from WebPortal by " + account.Email;
                        var    isSuccess = await TelitApi.UpdateAlarmState(appliance.SerialNumber, airGapAlwaysStatus == Configuration.TurnOff.ToLower()?AlarmStatus.OffPortal : AlarmStatus.OnPortal, message);

                        var updateTimerState = await TelitApi.UpdateTimerState(appliance.SerialNumber, Configuration.TimerDisable, appliance.IsOn.Value);
                    }
                    else
                    {
                        var updateTimerState = await TelitApi.UpdateTimerState(appliance.SerialNumber, Configuration.TimerDisable, appliance.IsOn.Value);
                    }

                    appliance.TimerEnabled    = false;
                    appliance.GeoFenceEnabled = false;
                }

                _applianceService.Update(appliance);

                var _event = new Event()
                {
                    ApplianceId = Convert.ToInt64(applianceid),
                    AccountId   = Convert.ToInt64(accountId),
                    EventTypeId = Constant.EventType.AlwaysOnStatusChange,
                    EventDetail = Constant.ResponseMessage.AlwaysOnStatusChange,
                    Timestamp   = DateTime.UtcNow,
                    Message     = Constant.ResponseMessage.AlwaysOnStatusChange + " " + airGapAlwaysStatus.ToUpper()
                };

                _eventService.Insert(_event);

                var response = new ResponseData <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);
            }
        }