Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Promise from 'bluebird';
- import moment from 'moment';
- import StatisticSecondModule from './model';
- import powerMeterModel from './../powermeter/model';
- const StatisticSecond = StatisticSecondModule.StatisticSecond;
- const StatisticHour = StatisticSecondModule.StatisticHour;
- const powerMeter = powerMeterModel.PowerMeter;
- export const StatisticController = {};
- export default { StatisticController };
- // ========Menerima data dari power meter===========
- StatisticController.create = async (req, res, next) => {
- const { dev_id, amp, volt } = req.query;
- const ampArray = amp.match(/.{1,2}/g);
- const voltArray = volt.match(/.{1,2}/g);
- let dataPower = await powerMeter.byCode(dev_id);
- dataPower = dataPower[0];
- const dataInsert = [];
- const dataHours = {
- watt: 0,
- rate: 0,
- power_meter_id: dataPower.id,
- };
- for (let i = 0; i < ampArray.length; i += 1) {
- let voltvalue = 0;
- const ampString = ampArray[i].toString(16);
- const ampvalue = (parseInt(ampString, 16)) / 10;
- if (voltArray[i]) {
- const voltString = voltArray[i].toString(16);
- voltvalue = parseInt(voltString, 16);
- }
- const watt = (ampvalue * voltvalue * dataPower.power_factor) / 1000;
- const dataNew = {
- power_meter_id: dataPower.id,
- amp: ampvalue,
- volt: voltvalue,
- watt,
- rate: dataPower.rate_am,
- };
- dataInsert.push(dataNew);
- dataHours.watt += watt;
- dataHours.rate += (watt * dataPower.rate_am);
- }
- Promise.all(dataInsert.map(data => StatisticSecond.create(data)));
- const dateStart = moment().format('YYYY-MM-DD HH:00:00');
- const startDateIso = moment(dateStart).toISOString();
- const endDateIso = moment(dateStart).add(1, 'hours').toISOString();
- await StatisticHour.upsertData(dataPower.id, startDateIso, endDateIso, dataHours);
- req.resData = {
- status: true,
- message: 'OK',
- data: dataPower,
- };
- return next();
- };
- // ========Projection===========
- StatisticController.perDay = async (req, res, next) => {
- const { power_meter_id, start_date, end_date } = req.body;
- /* eslint-disable camelcase*/
- const powerId = power_meter_id;
- /* eslint-enable camelcase*/
- const param = { power_meter_id: powerId, start_date, end_date };
- const dataPerday = await StatisticHour.perDay(param);
- const dataDay = [];
- let { index = 0, totalWatt = 0, totalValue = 0 } = 0;
- for (let m = moment(start_date); m.isSameOrBefore(end_date); m.add(1, 'days')) {
- dataDay[index] = { date: m.format('YYYY-MM-DD'), watt: 0, value: 0 };
- for (let i = 0; i < dataPerday.length; i += 1) {
- if (dataPerday[i].date === m.format('YYYY-MM-DD')) {
- dataDay[index].watt = dataPerday[i].watt;
- dataDay[index].value = dataPerday[i].value;
- totalWatt += dataPerday[i].watt;
- totalValue += dataPerday[i].value;
- }
- }
- index += 1;
- }
- const paramThisMonth = {
- power_meter_id: powerId,
- start_date: moment().format('YYYY-MM-01'),
- end_date: moment().format('YYYY-MM-DD'),
- };
- const paramLastMonth = {
- power_meter_id: powerId,
- start_date: moment().subtract(1, 'months').format('YYYY-MM-01'),
- end_date: moment().subtract(1, 'months').format('YYYY-MM-DD'),
- };
- const paramFullLastMonth = {
- power_meter_id: powerId,
- start_date: moment().subtract(1, 'months').format('YYYY-MM-01'),
- end_date: moment(paramLastMonth.start_date).endOf('month').format('YYYY-MM-DD'),
- };
- const [sumThisMonth, sumLastMonth, sumFullLastMonth] = await Promise.join(
- StatisticHour.sumDate(paramThisMonth),
- StatisticHour.sumDate(paramLastMonth),
- StatisticHour.sumDate(paramFullLastMonth));
- let projection = 0;
- let projectionValue = 0;
- if (sumThisMonth[0]) {
- if (sumLastMonth[0]) {
- projection = (sumThisMonth[0].watt / sumLastMonth[0].watt) * sumFullLastMonth[0].watt;
- projectionValue = (sumThisMonth[0].value / sumLastMonth[0].value) * sumFullLastMonth[0].value;
- } else {
- projection = sumThisMonth[0].watt * (30 / parseInt(moment().format('DD'), 10));
- projectionValue = sumThisMonth[0].value * (30 / parseInt(moment().format('DD'), 10));
- }
- }
- const wattProjection = [];
- const costProjection = [];
- wattProjection[0] = { month: moment().format('MMMM'), bulan: moment().format('MM'), value: projection };
- costProjection[0] = { month: moment().format('MMMM'), bulan: moment().format('MM'), value: projectionValue };
- // =================MONTH AND YEAR===================
- param.year = moment().format('YYYY');
- param.month = moment().format('MM');
- const dataPerMonth = await StatisticHour.perMonth(param);
- let indexInt = 1;
- for (let i = (parseInt(moment().format('MM'), 10) - 1); i > 0; i -= 1) {
- wattProjection[indexInt] = {
- month: moment().subtract(indexInt, 'months').format('MMMM'),
- bulan: moment().subtract(indexInt, 'months').format('MM'),
- value: 0,
- };
- costProjection[indexInt] = {
- month: moment().subtract(indexInt, 'months').format('MMMM'),
- bulan: moment().subtract(indexInt, 'months').format('MM'),
- value: 0,
- };
- for (let ii = 0; ii < dataPerMonth.length; ii += 1) {
- if (parseInt(dataPerMonth[ii].month, 10) === parseInt(moment().subtract(indexInt, 'months').format('MM'), 10)) {
- wattProjection[indexInt].value = dataPerMonth[ii].watt;
- costProjection[indexInt].value = dataPerMonth[ii].value;
- }
- }
- indexInt += 1;
- }
- req.resData = {
- status: true,
- message: 'OK',
- data: {
- daily: dataDay,
- total: {
- watt: totalWatt,
- value: totalValue,
- },
- user_id: req.user.id,
- watt_projection: wattProjection,
- cost_projection: costProjection,
- },
- };
- return next();
- };
- // ========Actual===========
- StatisticController.actual = async (req, res, next) => {
- const { power_meter_id, start_date, end_date } = req.body;
- /* eslint-disable camelcase*/
- const powerId = power_meter_id;
- /* eslint-enable camelcase*/
- const param = { power_meter_id: powerId, start_date, end_date };
- const dataPerday = await StatisticHour.perDay(param);
- const dataDay = [];
- /* eslint-disable no-unused-vars*/
- let { index = 0, totalWatt = 0, totalValue = 0 } = 0;
- for (let m = moment(start_date); m.isSameOrBefore(end_date); m.add(1, 'days')) {
- dataDay[index] = { date: m.format('YYYY-MM-DD'), watt: 0, value: 0 };
- for (let i = 0; i < dataPerday.length; i += 1) {
- if (dataPerday[i].date === m.format('YYYY-MM-DD')) {
- dataDay[index].watt = dataPerday[i].watt;
- dataDay[index].value = dataPerday[i].value;
- totalWatt += dataPerday[i].watt;
- totalValue += dataPerday[i].value;
- }
- }
- index += 1;
- }
- const namaGroup = [];
- const powerLimit = [];
- const powerMeterCode = [];
- const rate = [];
- const paramThisMonth = {
- power_meter_id: powerId,
- start_date: moment().format('YYYY-MM-01'),
- end_date: moment().format('YYYY-MM-DD'),
- };
- const paramLastMonth = {
- power_meter_id: powerId,
- start_date: moment().subtract(1, 'months').format('YYYY-MM-01'),
- end_date: moment().subtract(1, 'months').format('YYYY-MM-DD'),
- };
- const paramFullLastMonth = {
- power_meter_id: powerId,
- start_date: moment().subtract(1, 'months').format('YYYY-MM-01'),
- end_date: moment(paramLastMonth.start_date).endOf('month').format('YYYY-MM-DD'),
- };
- const [dataGroup, sumThisMonth, sumLastMonth, sumFullLastMonth] = await Promise.join(
- powerMeter.getGroupName(param),
- StatisticHour.sumDate(paramThisMonth),
- StatisticHour.sumDate(paramLastMonth),
- StatisticHour.sumDate(paramFullLastMonth));
- if (dataGroup) {
- dataGroup.map(data => [
- namaGroup.push(data.group_name),
- powerLimit.push(data.power_limit),
- powerMeterCode.push(data.power_meter),
- rate.push(data.rate),
- ]);
- }
- let projection = 0;
- let projectionValue = 0;
- let currentWatt = 0;
- let currentCost = 0;
- const wattLastMonth = (sumFullLastMonth[0]) ? sumFullLastMonth[0].watt : 0;
- if (sumThisMonth[0]) {
- currentWatt = sumThisMonth[0].watt;
- currentCost = sumThisMonth[0].value;
- if (sumLastMonth[0]) {
- projection = (sumThisMonth[0].watt / sumLastMonth[0].watt) * sumFullLastMonth[0].watt;
- projectionValue = (sumThisMonth[0].value / sumLastMonth[0].value) * sumFullLastMonth[0].value;
- } else {
- projection = sumThisMonth[0].watt * (30 / parseInt(moment().format('DD'), 10));
- projectionValue = sumThisMonth[0].value * (30 / parseInt(moment().format('DD'), 10));
- }
- }
- // ==========average=======================
- // select min date
- let minDate = moment().subtract(11, 'months').format('YYYY-MM-01');
- const dateMinData = await StatisticHour.minDate(param);
- if (dateMinData[0]) {
- if (moment(dateMinData[0].datemin).isAfter(minDate)) {
- minDate = dateMinData[0].datemin;
- }
- }
- const MonthMin = moment(minDate);
- const monthNow = moment();
- const diff = monthNow.diff(MonthMin, 'months') + 1;
- // data dalam max setahun/12 bulan
- param.minDate = moment(MonthMin).toISOString();
- const dataSumWatt = await StatisticHour.sumWatt(param);
- const averageWatt = (dataSumWatt[0]) ? dataSumWatt[0].watt / diff : 0;
- req.resData = {
- status: true,
- message: 'OK',
- data: {
- daily: dataDay,
- user_id: req.user.id,
- group_name: namaGroup,
- power_limit: powerLimit,
- rate,
- last_watt: wattLastMonth,
- current_watt: currentWatt,
- current_cost: currentCost,
- average_watt: averageWatt,
- watt_projection: projection,
- cost_projection: projectionValue,
- power_meter: powerMeterCode,
- },
- };
- return next();
- };
Add Comment
Please, Sign In to add comment