Example #1
0
        public IActionResult Dashboard()
        {
            MongoIndoorTempSetting_Model        mongoIndoor = new MongoIndoorTempSetting_Model();
            List <MongoIndoorTempSetting_Model> settingList = SETTINGCOLLECTION.Find(_ => true).ToList();

            if (settingList.Count <= 0)              //no record
            {
                mongoIndoor.Id               = "";
                mongoIndoor.acDefaultTemp    = 25.5;
                mongoIndoor.tempRangeHighest = 30;
                mongoIndoor.checkDegRange    = 2;
                mongoIndoor.eachTempRange    = 3;
                mongoIndoor.acLowestTemp     = 16;
            }
            foreach (MongoIndoorTempSetting_Model m in settingList)              //have setting record
            {
                mongoIndoor = m;
            }

            IntelligentControlDeviceUnit intelligentControlDeviceUnit = new IntelligentControlDeviceUnit();

            intelligentControlDeviceUnit.mongoIndoor = mongoIndoor;
            string checkTimeMin = "*/5 * * * *";

            if (mongoIndoor != null)
            {
                checkTimeMin = "*/" + mongoIndoor.checkTimeMinutes + " * * * *";
                Debug.WriteLine("checkTimeMin!!!!!!!!" + checkTimeMin);
            }
            RecurringJob.AddOrUpdate(() => intelligentControlDeviceUnit.IntelligentControlDevice(), checkTimeMin.ToString());
            RecurringJob.AddOrUpdate(() => intelligentControlDeviceUnit.scheduledControl(), "* * * * *");

            //batch
            DevicesPowerUseInputUtil devicesPowerUseInputUtil = new DevicesPowerUseInputUtil();

            RecurringJob.AddOrUpdate(() => devicesPowerUseInputUtil.updateRoomPower(), "* * * * *");


            DevicesPowerUseOutputUtil powerUseOutputUtil = new DevicesPowerUseOutputUtil();

            double TotalSavings = 0;
            double TotalUsage   = Math.Round(powerUseOutputUtil.getTotalPowerUse(), 2);
            double ACpower      = Math.Round(powerUseOutputUtil.getACPowerUse(), 2);
            double LTpower      = Math.Round(powerUseOutputUtil.getLPowerUse(), 2);
            double HDpower      = Math.Round(powerUseOutputUtil.getHUMPowerUse(), 2);
            double EFpower      = Math.Round(powerUseOutputUtil.getEXHFPowerUse(), 2);

            ViewBag.TotalUsage   = TotalUsage;
            ViewBag.TotalSavings = TotalSavings;
            ViewBag.ACpower      = ACpower;
            ViewBag.LTpower      = LTpower;
            ViewBag.HDpower      = HDpower;
            ViewBag.EFpower      = EFpower;

            ViewBag.trendJsonString = powerUseOutputUtil.getRoomPowerTrend();

            return(View());
        }
Example #2
0
        public string Post(object PostJson)
        {
            bool        isdone                 = false;
            StatusModel returnModel            = new StatusModel();
            List <MongoDevicesListModel> DList = new List <MongoDevicesListModel>();

            try {
                var json = System.Text.Json.JsonSerializer.Serialize(PostJson);
                var data = JsonConvert.DeserializeObject <List <StatusModel> >(json);

                if (!string.IsNullOrEmpty(data.First().deviceId))
                {
                    System.Diagnostics.Debug.WriteLine(">" + data.First().deviceId + "<");
                    DList = new FYP_WEB_APP.Models.DBManger().DataBase.GetCollection <MongoDevicesListModel>("DEVICES_LIST").Find(d => d.devicesId.Contains(data.First().deviceId)).ToList();
                    if (DList.Count() > 0)
                    {
                        var up = Builders <FYP_WEB_APP.Models.MongoModels.MongoDevicesListModel> .Update.Set(x => x.status, bool.Parse(data.First().status));

                        var UpdateResult = new Models.DBManger().DataBase.GetCollection <MongoDevicesListModel>("DEVICES_LIST").FindOneAndUpdateAsync(u => u.devicesId == data.First().deviceId, up);

                        if (data.First().deviceId.Contains("AC"))
                        {
                            if (!data.First().status.Contains("false") && data.First().set_value != null)
                            {
                                up = Builders <FYP_WEB_APP.Models.MongoModels.MongoDevicesListModel> .Update.Set(x => x.set_value, data.First().set_value);

                                new Models.DBManger().DataBase.GetCollection <MongoDevicesListModel>("DEVICES_LIST").FindOneAndUpdateAsync(u => u.devicesId == data.First().deviceId, up);
                                DateTime utcNow = DateTime.UtcNow.AddHours(8);
                                new DBManger().DataBase.GetCollection <BsonDocument>("AC").InsertOne(new BsonDocument {
                                    { "deviceId", data.First().deviceId }, { "current", data.First().set_value }, { "latest_checking_time", utcNow }
                                });
                                Debug.WriteLine("line 61 id done");
                            }
                            else if (data.First().status.Contains("false") && data.First().set_value != null)
                            {
                                DateTime utcNow = DateTime.UtcNow.AddHours(8);
                                new DBManger().DataBase.GetCollection <BsonDocument>("AC").InsertOne(new BsonDocument {
                                    { "deviceId", data.First().deviceId }, { "current", data.First().set_value }, { "latest_checking_time", utcNow }
                                });
                                Debug.WriteLine("line 67 id done");
                            }
                            else if (data.First().deviceId.Contains("AC") && !data.First().status.Contains("false") && data.First().set_value == null)
                            {
                                throw new System.ArgumentException("set_value error", "not set value");
                            }
                            else
                            {
                            }
                        }
                        DateTime nowTime = DateTime.UtcNow.AddHours(8);
                        if (bool.Parse(data.First().status))//true
                        {
                            up = Builders <FYP_WEB_APP.Models.MongoModels.MongoDevicesListModel> .Update.Set(x => x.turn_on_time, nowTime);

                            new Models.DBManger().DataBase.GetCollection <MongoDevicesListModel>("DEVICES_LIST").FindOneAndUpdateAsync(u => u.devicesId == data.First().deviceId, up);

                            isdone = true;
                        }
                        else
                        {
                            bool check = new DevicesPowerUseInputUtil().insertDevicesPowerUse(data.First().deviceId);
                            Debug.WriteLine("false check : " + check);
                            if (!check)
                            {
                                throw new System.ArgumentException("insertDevicesPowerUse error", data.First().deviceId + " insert Devices Power Use");
                            }
                        }

                        Thread.Sleep(2000);

                        DList = new FYP_WEB_APP.Models.DBManger().DataBase.GetCollection <MongoDevicesListModel>("DEVICES_LIST").Find(d => d.devicesId.Contains(data.First().deviceId)).ToList();
                        System.Diagnostics.Debug.WriteLine(DList.Count());
                        returnModel = new StatusModel()
                        {
                            deviceId              = DList.First().devicesId,
                            set_value             = DList.First().set_value,
                            status                = DList.First().status.ToString(),
                            lastest_checking_time = DList.First().lastest_checking_time
                        };
                    }
                    else
                    {
                        throw new System.ArgumentException("DevicesId error", " DevicesId not found");
                    }
                }
                else
                {
                    throw new System.ArgumentException("input error", " DevicesId not found");
                }
            }
            catch (Exception e) {
                return(e.Message.ToString());
            }
            return(returnModel.ToJson());
        }