/**
 * 取得電表總計資料 by baja
 * @param {any} devicePath 
 * @param {any} timeType
 * @param {any} callback
 */
function getElectricMeterTotalByBaja(devicePath, timeType, callback) {
    var _result = "";
    var _ss = "";
    var _index = 0;

    require(['baja!'], function (baja) {
        console.log('transform:slot:/' + devicePath + '/History/' + timeType);
        baja.Ord.make('transform:slot:/' + devicePath + '/History/' + timeType).get()
            .then(function (table) {
                return table.cursor({
                    each: function (record) {
                        if (_index == 0)
                            _ss += '{"timestamp":"' + record.get('timestamp') + '", "MP1":' + record.get('MP1') + '"}';
                        else
                            _ss += ',{"timestamp":"' + record.get('timestamp') + '", "MP1":' + record.get('MP1') + '"}';
                        _index++;
                    },
                    after: function () {
                        _result += '{' + '"count": ' + _index + ', "data":[';
                        _result += _ss;
                        _result += ']}';
                        if (typeof callback === 'function') {
                            callback(_result);
                        }
                    },
                    limit: -1,
                    offset: 0
                });
            });
    });
}

/**
 * 取得電表即時資料 by baja
 * @param {any} devicePath 
 * @param {any} callback
 */
function getElectricMeterNoweDataByBaja(devicePath, callback) {
    var _result = {count:0,data:[]};
    var _index = 0;

    require(['baja!'], function (baja) {//TPE/B1/EE/E4/R2F/NA/WHT/N1
        console.log('local:|foxs:|station:|slot:/' + devicePath + '|bql:select name, out, out.value from control:ControlPoint');
        baja.Ord.make('local:|foxs:|station:|slot:/' + devicePath + '|bql:select name, out, out.value from control:ControlPoint').get()
            .then(function (table) {
                return table.cursor({
                    each: function (record) {
                        let main = {}
                        main.name = record.get('name');
                        main.value = record.get('out').get('value');
                        _result.data.push(main);
                        _index++;
                    },
                    after: function () {
                        _result.count = _index;
                        
                        if (typeof callback === 'function') {
                            callback(_result);
                        }
                    },
                    limit: -1,
                    offset: 0
                });
            });
    });
}

///**
// * 取得電表每小時資料 by baja
// * @param {any} devicePath 
// * @param {any} startDate_millisecond
// * @param {any} endDate_millisecond 
// * @param {any} callback
// */
//function getElectricMeterHourDataByBaja(devicePath, startDate_millisecond, endDate_millisecond, callback) {
//    var _result = "";
//    var _ss = "";
//    var _index = 0;

//    require(['baja!'], function (baja) {//TPE/B1/EE/E4/R2F/NA/WHT/N1
//        console.log('transform:slot:/' + devicePath + '/History/TR_Daily|bql: select * where timestamp.millis > ' + startDate_millisecond + ' and timestamp.millis < ' + endDate_millisecond);
//        baja.Ord.make('transform:slot:/' + devicePath + '/History/TR_Daily|bql: select * where timestamp.millis > ' + startDate_millisecond + ' and timestamp.millis < ' + endDate_millisecond).get()
//            .then(function (table) {
//                return table.cursor({
//                    each: function (record) {
//                        if (_index == 0)
//                            _ss += '{"timestamp":' + record.get('timestamp') + ', "value":"' + record.get('kwh1') + '"}';
//                        else
//                            _ss += ',{"timestamp":' + record.get('timestamp') + ', "value":"' + record.get('kwh1') + '"}';
//                        _index++;
//                    },
//                    after: function () {
//                        _result += '{' + '"count": ' + _index + ', "data":[';
//                        _result += _ss;
//                        _result += ']}';
//                        if (typeof callback === 'function') {
//                            callback(_result);
//                        }
//                    },
//                    limit: -1,
//                    offset: 0
//                });
//            });
//    });
//}

/**
 * 取得電表 單日kwh by baja
 * @param {any} devicePath 
 * @param {any} date_millisecond
 * @param {any} callback
 */
function getElectricMeterOneDayKwhByBaja(devicePath, date_millisecond, callback) {
    var _result = "";
    var _ss = "";
    var _index = 0;

    require(['baja!'], function (baja) {//TPE/B1/EE/E4/R2F/NA/WHT/N1
        console.log('transform:slot:/' + devicePath + '/History/TR_Month|bql: select * where timestamp.millis = ' + date_millisecond);
        baja.Ord.make('transform:slot:/' + devicePath + '/History/TR_Month|bql: select * where timestamp.millis = ' + date_millisecond).get()
            .then(function (table) {
                return table.cursor({
                    each: function (record) {
                        if (_index == 0)
                            _ss += '{"timestamp":"' + record.get('timestamp') + '", "value":' + record.get('kwh1') + '"}';
                        else
                            _ss += ',{"timestamp":"' + record.get('timestamp') + '", "value":' + record.get('kwh1') + '"}';
                        _index++;
                    },
                    after: function () {
                        _result += '{' + '"count": ' + _index + ', "data":[';
                        _result += _ss;
                        _result += ']}';
                        if (typeof callback === 'function') {
                            callback(_result);
                        }
                    },
                    limit: -1,
                    offset: 0
                });
            });
    });
}

/**
 * 取得電表每周資料 by baja
 * @param {any} devicePath 
 * @param {any} startDate_millisecond
 * @param {any} endDate_millisecond 
 * @param {any} callback
 */
function getElectricMeterWeekDataByBaja(devicePath, startDate_millisecond, endDate_millisecond, callback) {
    var _result = { count: 0, data: [] };
    var _index = 0;

    require(['baja!'], function (baja) {//TPE/B1/EE/E4/R2F/NA/WHT/N1
        console.log('transform:slot:/' + devicePath + '/History/TR_Daily|bql: select * where timestamp.millis >= ' + startDate_millisecond + ' and timestamp.millis <= ' + endDate_millisecond);
        baja.Ord.make('transform:slot:/' + devicePath + '/History/TR_Month|bql: select * where timestamp.millis >= ' + startDate_millisecond + ' and timestamp.millis <= ' + endDate_millisecond).get()
            .then(function (table) {
                return table.cursor({
                    each: function (record) {
                        let main = {};
                        main.timestamp = record.get('timestamp');
                        main.value = record.get('kwh1');
                        _result.data.push(main);
                        _index++;
                    },
                    after: function () {
                        _result.count = _index;
                        if (typeof callback === 'function') {
                            callback(_result);
                        }
                    },
                    limit: -1,
                    offset: 0
                });
            });
    });
}

/**
 * 取得電表 期間內的平均用電量(kwh)資料 by baja
 * @param {any} devicePath 
 * @param {any} company 
 * @param {any} startDate_millisecond
 * @param {any} endDate_millisecond 
 * @param {any} callback
 */
function getElectricMeterKwhAvgDataByBaja(devicePath, company, startDate_millisecond, endDate_millisecond, callback) {
    var _result = "";
    var _ss = "";
    var _index = 0;

    require(['baja!'], function (baja) {//TPE_B1_EE_E4_R2F_NA_WHT_N1_KWH
        console.log('transform:slot:/' + devicePath + '/History/TR_Daily|bql: select * where timestamp.millis > ' + startDate_millisecond + ' and timestamp.millis < ' + endDate_millisecond);
        baja.Ord.make('local:|foxs:|history:/' + company + '/' + devicePath + '|bql:select AVG(value) from control:ControlPoint  where timestamp.millis  >= ' + startDate_millisecond + ' and timestamp.millis <= ' + endDate_millisecond).get()
            .then(function (table) {
                return table.cursor({
                    each: function (record) {
                        if (_index == 0)
                            _ss += '{"value":' + record.get('AVG$28value$29') + '"}';
                        else
                            _ss += ',{"value":' + record.get('AVG$28value$29') + '"}';
                        _index++;
                    },
                    after: function () {
                        _result += '{' + '"count": ' + _index + ', "data":[';
                        _result += _ss;
                        _result += ']}';
                        if (typeof callback === 'function') {
                            callback(_result);
                        }
                    },
                    limit: -1,
                    offset: 0
                });
            });
    });
}

/**
 * 取得點位 每小時資料 by baja
 * @param {any} devicePath 
 * @param {any} company
 * @param {any} startDateTime
 * @param {any} endDateTime 
 * @param {any} callback
 */
function getElectricMeterHourDataByBaja(devicePath, company, startDateTime, endDateTime, callback) {
    var _result = { count: 0, data: [] };
    var _index = 0;

    require(['baja!'], function (baja) {//TPE_B1_EE_E4_R2F_NA_WHT_N1_KWH
        console.log("local:|foxs:|history:/" + company + "/" + devicePath + "?peroid=timerange;start=" + startDateTime + ".000+08:00;end=" + endDateTime + ".000+08:00;|bql:history:HistoryRollup.rollup(baja:RelTime '3600000')");
        baja.Ord.make("local:|foxs:|history:/" + company + "/" + devicePath + "?peroid=timerange;start=" + startDateTime + ".000+08:00;end=" + endDateTime + ".000+08:00;|bql:history:HistoryRollup.rollup(baja:RelTime '3600000')").get()
            .then(function (table) {
                return table.cursor({
                    each: function (record) {
                        let main = {};
                        main.timestamp = record.get('timestamp');
                        main.endTimestamp = record.get('endTimestamp');
                        main.min = record.get('min');
                        main.max = record.get('max');
                        main.avg = record.get('avg');
                        main.sum = record.get('sum');
                        _result.data.push(main);
                        _index++;
                    },
                    after: function () {
                        _result.count = _index;
                        if (typeof callback === 'function') {
                            callback(_result);
                        }
                    },
                    limit: -1,
                    offset: 0
                });
            });
    });
}

/**
 * 取得點位 每日資料 by baja
 * @param {any} devicePath 
 * @param {any} company
 * @param {any} startDateTime
 * @param {any} endDateTime 
 * @param {any} callback
 */
function getElectricMeterDayDataByBaja(devicePath, company, startDateTime, endDateTime, callback) {
    var _result = { count: 0, data: [] };
    var _index = 0;
    
    require(['baja!'], function (baja) {//TPE_B1_EE_E4_R2F_NA_WHT_N1_KWH
        console.log("local:|foxs:|history:/" + company + "/" + devicePath + "?peroid=timerange;start=" + startDateTime + ".000+08:00;end=" + endDateTime + ".000+08:00;|bql:history:HistoryRollup.rollup(baja:RelTime '86400000')");
        baja.Ord.make("local:|foxs:|history:/" + company + "/" + devicePath + "?peroid=timerange;start=" + startDateTime + ".000+08:00;end=" + endDateTime + ".000+08:00;|bql:history:HistoryRollup.rollup(baja:RelTime '86400000')").get()
            .then(function (table) {
                table.cursor({
                    each: function (record) {
                        let main = {};
                        main.timestamp = record.get('timestamp');
                        main.endTimestamp = record.get('endTimestamp');
                        main.min = record.get('min');
                        main.max = record.get('max');
                        main.avg = record.get('avg');
                        main.sum = record.get('sum');
                        _result.data.push(main);
                        
                        _index++;
                    },
                    after: function () {
                        _result.count = _index;
                        if (typeof callback === 'function') {
                            callback(_result);
                        }
                    },
                    limit: -1,
                    offset: 0
                });
            });
    });
}