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)
            {
            }
        }
Ejemplo n.º 2
0
        private async Task AutoDieuHoa(DM_ThietBiDto thietbi, string ndtp, string ndmt)
        {
            if (float.Parse(ndtp) > thietbi.NGUONGNHIETDOCANHBAO)
            {
                if (_lichSuHoatDongThietBiAppService.CheckSaveLog(thietbi.Id, "NHIETDO", "1"))
                {
                    var canhbao = "NHIETDO@1@" + ndtp + "°C";
                    await SendMessageTrangThai(canhbao);
                }
            }
            else
            {
                if (_lichSuHoatDongThietBiAppService.CheckSaveLog(thietbi.Id, "NHIETDO", "0"))
                {
                    var canhbao = "NHIETDO@0@";
                    await SendMessageTrangThai(canhbao);
                }
            }
            var lsthongtin = _thongTin_ThietBiAppService.GetThongTinQuanLyByIdThietBi(thietbi.Id);

            if (thietbi.AUTODIEUHOA == "AUTO")
            {
                if (float.Parse(ndtp) > thietbi.NGUONGNHIETDOCAO)
                {
                    //Bật 2 điều hòa, Tắt quạt
                    foreach (var item in lsthongtin)
                    {
                        if (item.THONGTINQUANLY.CODE.Contains("DIEUHOA"))
                        {
                            if (item.TRANGTHAI != "1")
                            {
                                var dieuhoa = item.THONGTINQUANLY.CODE + "@1@" + "{\"cmd_type\":4,\"source\":" + item.THONGTINQUANLY.DESCRIPTION + ",\"brand\":0, \"power\":1, \"temp\":25, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                await SendMessagePrivate(thietbi.ConnectionID, dieuhoa);

                                Thread.Sleep(30000);
                            }
                        }
                        if (item.THONGTINQUANLY.CODE.Contains("QUATGIO"))
                        {
                            if (item.TRANGTHAI != "0")
                            {
                                var quatgio = "QUATGIO@0@";
                                await SendMessagePrivate(thietbi.ConnectionID, quatgio);

                                Thread.Sleep(30000);
                            }
                        }
                    }
                }
                else
                {
                    if (thietbi.NGUONGNHIETDOANTOAN < float.Parse(ndtp) && float.Parse(ndtp) <= thietbi.NGUONGNHIETDOCAO)
                    {
                        //Chạy luân phiên 1 điều hòa, Tắt quạt
                        var lasthoatdongDH1 = _lichSuHoatDongThietBiAppService.GetLastLichSuHoatDongByCode(thietbi.Id, "DIEUHOAIR1");
                        var lasthoatdongDH2 = _lichSuHoatDongThietBiAppService.GetLastLichSuHoatDongByCode(thietbi.Id, "DIEUHOAIR2");
                        if (lasthoatdongDH1 != null)
                        {
                            if (lasthoatdongDH1.SUSSES == 1)
                            {
                                var tnghi = (DateTime.Now - lasthoatdongDH1.ENDDATE).TotalMinutes;
                                if (tnghi < thietbi.THOIGIANCHAYLUANPHIEN)
                                {
                                    //Bật điều hòa 2
                                    var thongtindieuhoa2 = _thongTin_ThietBiAppService.GetThongTinThietBiByIdThietBiAndCode(thietbi.Id, "DIEUHOAIR2");
                                    if (thongtindieuhoa2.TRANGTHAI != "1")
                                    {
                                        var dieuhoa2 = "DIEUHOAIR2@1@" + "{\"cmd_type\":4,\"source\":" + thongtindieuhoa2.THONGTINQUANLY.DESCRIPTION + ",\"brand\":0, \"power\":1, \"temp\":25, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                        await SendMessagePrivate(thietbi.ConnectionID, dieuhoa2);

                                        Thread.Sleep(30000);
                                    }
                                }
                                else
                                {
                                    //Bật điều hòa 1
                                    var thongtindieuhoa1 = _thongTin_ThietBiAppService.GetThongTinThietBiByIdThietBiAndCode(thietbi.Id, "DIEUHOAIR1");
                                    if (thongtindieuhoa1.TRANGTHAI != "1")
                                    {
                                        var dieuhoa1 = "DIEUHOAIR1@1@" + "{\"cmd_type\":4,\"source\":0,\"brand\":0, \"power\":1, \"temp\":25, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                        await SendMessagePrivate(thietbi.ConnectionID, dieuhoa1);

                                        Thread.Sleep(30000);
                                    }

                                    //Tắt điều hòa 2
                                    var thongtindieuhoa2 = _thongTin_ThietBiAppService.GetThongTinThietBiByIdThietBiAndCode(thietbi.Id, "DIEUHOAIR2");
                                    if (thongtindieuhoa2.TRANGTHAI != "0" && (DateTime.Now - lasthoatdongDH2.STARTDATE).TotalMinutes > 10)
                                    {
                                        var dieuhoa2 = "DIEUHOAIR2@0@" + "{\"cmd_type\":4,\"source\":" + thongtindieuhoa2.THONGTINQUANLY.DESCRIPTION + ",\"brand\":0, \"power\":0, \"temp\":25, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                        await SendMessagePrivate(thietbi.ConnectionID, dieuhoa2);

                                        Thread.Sleep(30000);
                                    }
                                }
                            }
                            else
                            {
                                var tchay = (DateTime.Now - lasthoatdongDH1.STARTDATE).TotalMinutes;
                                if (tchay > thietbi.THOIGIANCHAYLUANPHIEN)
                                {
                                    //Bật điều hòa 2
                                    var thongtindieuhoa2 = _thongTin_ThietBiAppService.GetThongTinThietBiByIdThietBiAndCode(thietbi.Id, "DIEUHOAIR2");
                                    if (thongtindieuhoa2.TRANGTHAI != "1")
                                    {
                                        var dieuhoa2 = "DIEUHOAIR2@1@" + "{\"cmd_type\":4,\"source\":" + thongtindieuhoa2.THONGTINQUANLY.DESCRIPTION + ",\"brand\":0, \"power\":1, \"temp\":25, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                        await SendMessagePrivate(thietbi.ConnectionID, dieuhoa2);

                                        Thread.Sleep(30000);
                                    }
                                    var thongtindieuhoa1 = _thongTin_ThietBiAppService.GetThongTinThietBiByIdThietBiAndCode(thietbi.Id, "DIEUHOAIR1");
                                    if (tchay > 10 && thongtindieuhoa1.TRANGTHAI != "0")
                                    {
                                        //Tắt điều hòa 1
                                        var dieuhoa1 = "DIEUHOAIR1@0@" + "{\"cmd_type\":4,\"source\":0,\"brand\":0, \"power\":0, \"temp\":25, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                        await SendMessagePrivate(thietbi.ConnectionID, dieuhoa1);

                                        Thread.Sleep(30000);
                                    }
                                }
                                else
                                {
                                    //Tắt điều hòa 2
                                    var thongtindieuhoa2 = _thongTin_ThietBiAppService.GetThongTinThietBiByIdThietBiAndCode(thietbi.Id, "DIEUHOAIR2");
                                    if (thongtindieuhoa2.TRANGTHAI != "0" && (DateTime.Now - lasthoatdongDH2.STARTDATE).TotalMinutes > 10)
                                    {
                                        var dieuhoa2 = "DIEUHOAIR2@0@" + "{\"cmd_type\":4,\"source\":" + thongtindieuhoa2.THONGTINQUANLY.DESCRIPTION + ",\"brand\":0, \"power\":0, \"temp\":25, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                        await SendMessagePrivate(thietbi.ConnectionID, dieuhoa2);

                                        Thread.Sleep(30000);
                                    }
                                }
                            }
                        }

                        //Tắt quạt
                        var thongtinquat = _thongTin_ThietBiAppService.GetThongTinThietBiByIdThietBiAndCode(thietbi.Id, "QUATGIO");
                        if (thongtinquat != null)
                        {
                            if (thongtinquat.TRANGTHAI != "0")
                            {
                                var quatgio = "QUATGIO@0@";
                                await SendMessagePrivate(thietbi.ConnectionID, quatgio);

                                Thread.Sleep(30000);
                            }
                        }
                    }
                    else
                    {
                        if (thietbi.NGUONGNHIETDOTHAP < float.Parse(ndtp) && float.Parse(ndtp) <= thietbi.NGUONGNHIETDOANTOAN)
                        {
                            if (float.Parse(ndmt) < float.Parse(ndtp))
                            {
                                //Tắt 2 điều hòa, bật quạt
                                foreach (var item in lsthongtin)
                                {
                                    if (item.THONGTINQUANLY.CODE.Contains("DIEUHOA"))
                                    {
                                        var hoatdong = _lichSuHoatDongThietBiAppService.GetLastLichSuHoatDongByCode(thietbi.Id, item.THONGTINQUANLY.CODE);
                                        if (item.TRANGTHAI != "0" && (DateTime.Now - hoatdong.STARTDATE).TotalMinutes > 10)
                                        {
                                            var dieuhoa = item.THONGTINQUANLY.CODE + "@0@" + "{\"cmd_type\":4,\"source\":" + item.THONGTINQUANLY.DESCRIPTION + ",\"brand\":0, \"power\":0, \"temp\":20, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                            await SendMessagePrivate(thietbi.ConnectionID, dieuhoa);

                                            Thread.Sleep(30000);
                                        }
                                    }
                                    if (item.THONGTINQUANLY.CODE.Contains("QUATGIO"))
                                    {
                                        if (item.TRANGTHAI != "1")
                                        {
                                            var quatgio = "QUATGIO@1@";
                                            await SendMessagePrivate(thietbi.ConnectionID, quatgio);

                                            Thread.Sleep(30000);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //Tắt 2 điều hòa, tắt quạt
                                foreach (var item in lsthongtin)
                                {
                                    if (item.THONGTINQUANLY.CODE.Contains("DIEUHOA"))
                                    {
                                        var hoatdong = _lichSuHoatDongThietBiAppService.GetLastLichSuHoatDongByCode(thietbi.Id, item.THONGTINQUANLY.CODE);
                                        if (item.TRANGTHAI != "0" && (DateTime.Now - hoatdong.STARTDATE).TotalMinutes > 10)
                                        {
                                            var dieuhoa = item.THONGTINQUANLY.CODE + "@0@" + "{\"cmd_type\":4,\"source\":" + item.THONGTINQUANLY.DESCRIPTION + ",\"brand\":0, \"power\":0, \"temp\":25, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                            await SendMessagePrivate(thietbi.ConnectionID, dieuhoa);

                                            Thread.Sleep(30000);
                                        }
                                    }
                                    if (item.THONGTINQUANLY.CODE.Contains("QUATGIO"))
                                    {
                                        if (item.TRANGTHAI != "0")
                                        {
                                            var quatgio = "QUATGIO@0@";
                                            await SendMessagePrivate(thietbi.ConnectionID, quatgio);

                                            Thread.Sleep(30000);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            //Tắt 2 điều hòa, tắt quạt
                            foreach (var item in lsthongtin)
                            {
                                if (item.THONGTINQUANLY.CODE.Contains("DIEUHOA"))
                                {
                                    if (item.TRANGTHAI != "0")
                                    {
                                        var dieuhoa = item.THONGTINQUANLY.CODE + "@0@" + "{\"cmd_type\":4,\"source\":" + item.THONGTINQUANLY.DESCRIPTION + ",\"brand\":0, \"power\":0, \"temp\":20, \"mode\":1, \"fan\":4, \"swing\":1, \"model\":4}";
                                        SendMessagePrivate(thietbi.ConnectionID, dieuhoa);
                                        Thread.Sleep(60000);
                                    }
                                }
                                if (item.THONGTINQUANLY.CODE.Contains("QUATGIO"))
                                {
                                    if (item.TRANGTHAI != "0")
                                    {
                                        var quatgio = "QUATGIO@0@";
                                        SendMessagePrivate(thietbi.ConnectionID, quatgio);
                                        Thread.Sleep(60000);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }