示例#1
0
    def get(self, mid):
        TAG = "Gauge:"
        module = Module()
        meter = Meter()
        start_time = time.time()

        current_user = get_jwt_identity()
        username = current_user['sub']
        if (not meter.isUserHasPerm(username, mid)):
            return module.unauthorized()
        args = request.args
        param = "V0, V1, V2, I0, I1, I2, pf0, pf1, pf2, P0, P1, P2, f0, f1, f2, E0, E1, E2"
        if (module.isQueryStr(args, "parameters")):
            param = args["parameters"]
            param = param.replace(" ", ", ")
            # print(TAG, "param=", param)
        command = """SELECT %s FROM %s ORDER BY DESC LIMIT 1 """ % (param, mid)
        # print(TAG, "cmd=", command)
        if (not module.isMeterExist(mid)):
            return module.measurementNotFound()
        res = module.getData(command)
        # print(TAG, "res=", res)
        results = {"meter_id": "", "parameters": [], "values": []}
        gauge_data = {}
        if (len(res) > 0):
            tmp_res = res[0]
            results["meter_id"] = tmp_res["name"]
            results["parameters"] = tmp_res["columns"]
            results["values"] = tmp_res["values"]
        else:
            return module.measurementNotFound()

        for i in range(len(results["parameters"])):
            param_name = results["parameters"][i]
            # print(TAG, "param_name=", param_name)
            gauge_data[param_name] = results["values"][0][i]

        elapsed_time = (time.time() - start_time) * 1000
        print(TAG, "times=", elapsed_time, "ms")
        return {
            "type": True,
            "message": "success",
            "elapsed_time_ms": elapsed_time,
            "result": gauge_data
        }
示例#2
0
    def get(self, mid):
        TAG = "energy_mng:"

        start_time = time.time()
        print(TAG, "energy mng recv")
        module = Module()
        meter = Meter()
        try:
            current_user = get_jwt_identity()
        except:
            return module.unauthorized()

        database = Database()

        print(TAG, "current_user="******"year")):
            return module.wrongAPImsg()
        year = args["year"]
        year = int(year)

        # if not (isinstance(year, int)):
        #     return module.wrongAPImsg()

        print(TAG, "user permitted")
        if (not module.isMeterExist(mid)):
            print(TAG, "meter not found")
            return module.measurementNotFound()

        time_cmd = """SELECT MONTH(CURRENT_DATE) AS CUR_MONTH, YEAR(CURRENT_DATE) AS CUR_YEAR"""
        time_res = database.getData(time_cmd)
        cur_month = time_res[0]['result'][0]['CUR_MONTH']
        cur_year = time_res[0]['result'][0]['CUR_YEAR']

        # fix last year data bug
        if (year < cur_year):
            cur_month = 12

        all_result = []

        days_of_feb = 28
        if (calendar.isleap(year)):
            days_of_feb = 29

        months_name = [
            "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
            "Oct", "Nov", "Dec"
        ]
        end_of_month = [
            31, days_of_feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
        ]

        for i in range(1, cur_month + 1):
            start_date = ""
            end_date = ""
            if (i < 10):
                start_date = "%s-0%s-01 00:00:00" % (year, i)
                end_date = "%s-0%s-%s 23:59:59" % (year, i,
                                                   end_of_month[i - 1])
            else:
                start_date = "%s-%s-01 00:00:00" % (year, i)
                end_date = "%s-%s-%s 23:59:59" % (year, i, end_of_month[i - 1])
            # print(TAG, "start_date=", start_date)
            # print(TAG, "end_date=", end_date)

            command = """SELECT E0, E1, E2, time 
            FROM %s 
            WHERE (time > '%s') AND (time < '%s') 
            ORDER BY time DESC LIMIT 1""" % (mid, start_date, end_date)

            # print(TAG, "cmd=", command)

            res = module.getData(command)

            # print(TAG, "res=", res)

            results = {"meter_id": "", "parameters": [], "values": []}

            print(TAG, "res=", res)

            gauge_data = {}

            if (len(res) > 0):
                tmp_res = res[0]
                results["meter_id"] = tmp_res["name"]
                results["parameters"] = tmp_res["columns"]
                results["values"] = tmp_res["values"]
                for j in range(len(results["parameters"])):
                    param_name = results["parameters"][j]
                    # print(TAG, "param_name=", param_name)
                    gauge_data[param_name] = results["values"][0][j]
            else:
                gauge_data = {"E0": 0, "E1": 0, "E2": 0, "time": end_date}

            gauge_data["month_name"] = months_name[i - 1]
            gauge_data["month_num"] = i

            all_result.append(gauge_data)

        last_year = year - 1
        last_year_cmd = """SELECT E0, E1, E2, time 
        FROM mm_600194433DCF 
        WHERE (time > '%s-01-01 00:00:00') AND (time < '%s-12-31 23:59:59') 
        ORDER BY time DESC LIMIT 1""" % (last_year, last_year)

        last_year_rec = module.getData(last_year_cmd)

        results = {"meter_id": "", "parameters": [], "values": []}
        ly_data = {}
        if (len(last_year_rec) > 0):
            tmp_res = last_year_rec[0]
            results["meter_id"] = tmp_res["name"]
            results["parameters"] = tmp_res["columns"]
            results["values"] = tmp_res["values"]
            for i in range(len(results["parameters"])):
                param_name = results["parameters"][i]
                # print(TAG, "param_name=", param_name)
                ly_data[param_name] = results["values"][0][i]
        else:
            ly_data = {
                "E0": 0,
                "E1": 0,
                "E2": 0,
                "time": "%s-12-31 23:59:59" % (last_year)
            }

        for i in range(0, len(all_result)):
            e_rec = all_result[i]
            e0_usage = 0
            e1_usage = 0
            e2_usage = 0
            if i == 0:
                e0_usage = e_rec["E0"] - ly_data["E0"]
                e1_usage = e_rec["E1"] - ly_data["E1"]
                e2_usage = e_rec["E2"] - ly_data["E2"]
            else:
                last_month_rec = all_result[i - 1]
                e0_usage = e_rec["E0"] - last_month_rec["E0"]
                e1_usage = e_rec["E1"] - last_month_rec["E1"]
                e2_usage = e_rec["E2"] - last_month_rec["E2"]

            e0_usage = self.toPos(e0_usage)
            e1_usage = self.toPos(e1_usage)
            e2_usage = self.toPos(e2_usage)

            e0_usage = round(e0_usage, 2)
            e1_usage = round(e1_usage, 2)
            e2_usage = round(e2_usage, 2)

            all_result[i]["E0_usage"] = e0_usage
            all_result[i]["E1_usage"] = e1_usage
            all_result[i]["E2_usage"] = e2_usage

            # e_total = all_result[i]["E0"] + all_result[i]["E1"] + all_result[i]["E2"]
            e_total = e0_usage + e1_usage + e2_usage
            e_total = round(e_total, 2)
            all_result[i]["E_total"] = e_total

            all_result[i]["time"] = all_result[i]["time"].split(".")[0]
            all_result[i]["time"] = all_result[i]["time"].replace("T", " ")

        elapsed_time = (time.time() - start_time) * 1000
        print(TAG, "times=", elapsed_time, "ms")

        return {
            "type": True,
            "message": "success",
            "elapsed_time_ms": elapsed_time,
            "result": all_result
        }