public bool CheckSaveCongSuat(long idthietbi, string code)
        {
            var result = false;
            var today  = DateTime.Now.Date;
            LichSuHoatDongThietBi check = new LichSuHoatDongThietBi();

            if (code == "/dev/ttyUSB0")
            {
                check = _repository.GetAll().Where(x => x.IDTHIETBI == idthietbi && x.THONGTINQUANLY.CODE == "DONGDIEN" && x.STARTDATE > today).FirstOrDefault();
            }
            if (code == "/dev/ttyUSB1")
            {
                check = _repository.GetAll().Where(x => x.IDTHIETBI == idthietbi && x.THONGTINQUANLY.CODE == "DONGDIEN1" && x.STARTDATE > today).FirstOrDefault();
            }
            if (code == "/dev/ttyUSB2")
            {
                check = _repository.GetAll().Where(x => x.IDTHIETBI == idthietbi && x.THONGTINQUANLY.CODE == "DONGDIEN2" && x.STARTDATE > today).FirstOrDefault();
            }

            if (check == null)
            {
                result = true;
            }
            return(result);
        }
        public async Task <LichSuHoatDongThietBiDto> GetForEdit(GetLichSuHoatDongThietBi input)
        {
            LichSuHoatDongThietBi nhomVT = await _repository.GetAsync(input.Id);

            return(nhomVT.MapTo <LichSuHoatDongThietBiDto>());
        }
        public async Task SaveLogHoatDong(DM_ThietBiDto thietbi, string code, string trangthai, string des)
        {
            try
            {
                if (code.Contains("DONGDIEN"))
                {
                    var thongtin = _DM_THONGTINQUANLYAppService.GetThongTinQuanLyByCode(code, thietbi.Id);
                    LichSuHoatDongThietBi log = new LichSuHoatDongThietBi();
                    log.IDTHIETBI        = thietbi.Id;
                    log.IDTHONGTINQUANLY = thongtin.Id;
                    log.SUSSES           = 1;
                    log.STARTDATE        = DateTime.Now;
                    log.ENDDATE          = DateTime.Now;
                    log.CONGSUAT         = trangthai;
                    log.TenantId         = 1;
                    await _repository.InsertAsync(log);
                }
                else
                {
                    if (code.Contains("CAMERA"))
                    {
                        var thongtin = _DM_THONGTINQUANLYAppService.GetThongTinQuanLyByCode(code, thietbi.Id);
                        LichSuHoatDongThietBi log = new LichSuHoatDongThietBi();
                        log.IDTHIETBI        = thietbi.Id;
                        log.IDTHONGTINQUANLY = thongtin.Id;
                        log.SUSSES           = 0;
                        log.STARTDATE        = DateTime.Now;
                        log.ENDDATE          = DateTime.Now;
                        log.PATHIMAGE        = des;
                        log.TenantId         = 1;
                        await _repository.InsertAsync(log);
                    }
                    else
                    {
                        var hoatdong = _repository.GetAll().Where(x => x.IDTHIETBI == thietbi.Id && x.THONGTINQUANLY.CODE == code && x.SUSSES == 0).FirstOrDefault();
                        if (hoatdong == null)
                        {
                            if (trangthai == "1")
                            {
                                var thongtin = _DM_THONGTINQUANLYAppService.GetThongTinQuanLyByCode(code, thietbi.Id);

                                if (thongtin != null)
                                {
                                    LichSuHoatDongThietBi log = new LichSuHoatDongThietBi();
                                    log.IDTHIETBI        = thietbi.Id;
                                    log.IDTHONGTINQUANLY = thongtin.Id;
                                    log.SUSSES           = 0;
                                    log.STARTDATE        = DateTime.Now;
                                    log.ENDDATE          = DateTime.Now;
                                    log.PATHIMAGE        = des;
                                    log.TenantId         = 1;
                                    await _repository.InsertAsync(log);

                                    var checkOnOff = _thongTin_ThietBiAppService.CheckOnOffCanhBao(thietbi.Id, thongtin.Id);
                                    //Nhắn tin cảnh báo
                                    if (thietbi.SENDSMS == true && thongtin.SENDSMS == true && thongtin.CANHBAO == true && checkOnOff == true)
                                    {
                                        var phones = _userService.GetLsSoDienThoaiByIddonvi(thietbi.TRAM.IDDONVI);
                                        if (phones.Count > 0)
                                        {
                                            await _webService.SendSMS(phones, "GIAMSAT_NHATRAM:" + thietbi.CODE + " " + thongtin.DESCRIPTION + ": " + des + " BAT DAU " + DateTime.Now.ToLongTimeString() + " 10.10.117.186");
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (trangthai == "0")
                            {
                                var hoatdongdto = hoatdong.MapTo <LichSuHoatDongThietBiDto>();
                                hoatdong.SUSSES  = 1;
                                hoatdong.ENDDATE = DateTime.Now;
                                await _repository.UpdateAsync(hoatdong);

                                var checkOnOff = _thongTin_ThietBiAppService.CheckOnOffCanhBao(thietbi.Id, hoatdong.IDTHONGTINQUANLY);
                                //nhan tin canh bao
                                if (thietbi.SENDSMS == true && hoatdongdto.THONGTINQUANLY.SENDSMS == true && hoatdongdto.THONGTINQUANLY.CANHBAO == true && checkOnOff == true)
                                {
                                    var phones = _userService.GetLsSoDienThoaiByIddonvi(thietbi.TRAM.IDDONVI);
                                    if (phones.Count > 0)
                                    {
                                        await _webService.SendSMS(phones, "GIAMSAT_NHATRAM:" + thietbi.CODE + " " + hoatdongdto.THONGTINQUANLY.DESCRIPTION + " KET THUC: " + DateTime.Now.ToLongTimeString() + " 10.10.117.186");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }