2022-11-22 01:30:30 +08:00
let baja _subscribe _device _callback _func ; //設定BQL訂閱之後要回傳的Function
2022-12-01 19:32:32 +08:00
let baja _subscribe _end _device _callback _func ; //設定BQL訂閱結束之後要回傳的Function
2022-11-29 18:58:38 +08:00
let baja _my _user _account _func ; //取得帳號資料要回傳的Function
2022-11-22 01:30:30 +08:00
var ordPath ; //當前點選選單的tag, 用來抓出設備路徑, 例如:旅館棟->H, 消防偵煙器->F3
2022-11-25 18:46:40 +08:00
window . tolSubList = [ ] ;
2022-11-29 18:58:38 +08:00
var user _name = "" ;
2022-11-22 01:30:30 +08:00
function subscriptionDevices ( ) {
// 用BQL的方式去訂閱
this . setSubscribeDevicesByBql = function ( tempOrdPath ) {
ordPath = tempOrdPath ;
BajaSubscribeDevicesByBql ( ) ;
} ;
// BQL去訂閱回傳的Function
this . setSubscribeDevicesCallBack = function ( callBackFunc ) {
if ( callBackFunc != undefined && callBackFunc != null ) {
baja _subscribe _device _callback _func = callBackFunc ;
}
}
2022-12-01 19:32:32 +08:00
// BQL訂閱結束回傳的Function
this . setSubscribeDeviceEndCallBack = function ( callBackFunc ) {
if ( callBackFunc != undefined && callBackFunc != null ) {
baja _subscribe _end _device _callback _func = callBackFunc ;
}
}
2022-11-29 18:58:38 +08:00
}
function MyBaja ( ) {
// 取得使用者帳號
2022-12-02 12:18:58 +08:00
this . setMyUserAccount = function ( callBackFunc = null ) {
require ( [ 'baja!' ] , function ( baja ) {
console . log ( "baja" , baja )
user _name = baja . getUserName ( ) ;
callBackFunc ? callBackFunc ( user _name ) : "" ;
} ) ;
2022-11-29 18:58:38 +08:00
} ;
2022-11-22 01:30:30 +08:00
}
/ * *
* 使用者透過BQL指定路徑去訂閱設備點位
* /
function BajaSubscribeDevicesByBql ( ) {
2022-11-24 16:27:00 +08:00
2022-11-22 01:30:30 +08:00
require ( [ 'baja!' ] , function ( baja ) {
console . log ( "進入Function 準備執行BQL訂閱" ) ;
var init _start = new Date ( Date . now ( ) ) ;
var sub = new baja . Subscriber ( ) ;
sub . attach ( 'changed' , function ( prop ) {
if ( prop . getName ( ) === 'out' ) {
var out _value = this . getOutDisplay ( ) ;
var target _device _number = this . $parent . getDisplayName ( ) . split ( '_' ) . slice ( 0 , 5 ) . join ( '_' ) ;
var point _name = this . getDisplayName ( ) ;
//依據Facets判斷回傳的內容值為何
var facets _split = this . getFacets1 ( ) . $cEncStr . split ( '|' ) ;
var facets _arr = [ ] ;
facets _split . forEach ( function ( item , index ) {
facets _arr . push ( item . split ( '=s:' ) ) ;
} ) ;
var facets = facets _arr . reduce ( ( obj , cur ) => ( { ... obj , [ cur [ 0 ] ] : cur [ 1 ] } ) , { } )
var point _out _split = this . getOutDisplay ( ) . split ( ' ' ) ;
let key = Object . keys ( facets ) . find ( k => facets [ k ] === point _out _split [ 0 ] ) ;
if ( key == undefined ) {
key = point _out _split [ 0 ] ;
}
var modify _target _device = {
"device_number" : target _device _number ? target _device _number : null ,
2022-12-01 19:32:32 +08:00
"device_number_full" : this . $parent . getSlotPath ( ) . $names . join ( "_" ) ,
2022-11-22 01:30:30 +08:00
"point_name" : point _name ? point _name : null ,
"value" : key
}
if ( baja _subscribe _device _callback _func != undefined && baja _subscribe _device _callback _func != null ) {
baja _subscribe _device _callback _func ( modify _target _device , true ) ; //第2參數用在平面圖, 刷新畫面
}
}
2022-11-24 16:27:00 +08:00
} , function ( err ) {
console . log ( err )
2022-11-22 01:30:30 +08:00
} ) ;
//使用bql語法
// var building_tag = "H";
// var system_tag = "M10";
// baja.Ord.make(`ip:greencloud.fic.com.tw|foxs:|station:|slot:/Arena/${building_tag}/${system_tag}|bql:select name, displayname, slotPath, out.value, out from control:ControlPoint`)
console . log ( ` local:|foxs:|station:|slot:/TPE/ ${ ordPath . building _tag } / ${ ordPath . system _tag } / ${ ordPath . name _tag } |bql:select name, displayname, slotPath, out.value, out, facets from control:ControlPoint ` ) ;
baja . Ord . make ( ` local:|foxs:|station:|slot:/TPE/ ${ ordPath . building _tag } / ${ ordPath . system _tag } / ${ ordPath . name _tag } |bql:select name, displayname, slotPath, out.value, out, facets from control:ControlPoint ` )
. get (
function ( table ) {
var tableStart , tableFinish ;
var subStart , subFinish ;
var component _index = 0 ;
var total _component _index = 0 ;
2022-12-01 19:32:32 +08:00
var totalTargetDevice = [ ] ;
2022-11-22 01:30:30 +08:00
var readBqlFinish = new Date ( Date . now ( ) ) ;
// $("#readPath-finish-timestamp").html(readBqlFinish.toISOString());
// $("#readPath-finish-time").html((readBqlFinish.getTime() - init_start.getTime()) / 1000 + "sec");
console . log ( "讀取路徑完成-花費時間" , ( readBqlFinish . getTime ( ) - init _start . getTime ( ) ) / 1000 + "sec" ) ;
table . cursor ( {
before : function ( ) {
2022-12-01 19:32:32 +08:00
totalTargetDevice = [ ] ;
2022-11-22 01:30:30 +08:00
tableStart = new Date ( Date . now ( ) ) ;
$ ( "#table-start-timestamp" ) . html ( tableStart . toISOString ( ) ) ;
render _start = new Date ( Date . now ( ) ) ;
} ,
each : function ( item , index ) {
if ( index < 1 ) {
subStart = new Date ( Date . now ( ) ) ;
$ ( "#sub-start" ) . html ( subStart . toISOString ( ) ) ;
}
$ ( "#sub-number" ) . html ( index + 1 ) ;
total _component _index = index ;
2022-12-01 19:32:32 +08:00
2022-11-22 01:30:30 +08:00
var target _device _number _split = this . getDisplay ( "slotPath" ) . split ( '/' ) ;
var target _device _number = target _device _number _split [ target _device _number _split . length - 2 ] ;
//console.log(target_device_number);
var point _name = this . getDisplay ( "name" ) ;
var facets = this . getDisplay ( "facets" ) ;
//依據Facets判斷回傳的內容值為何
var facets _split = facets . split ( ',' ) ;
var facets _arr = [ ] ;
facets _split . forEach ( function ( item , index ) {
facets _arr . push ( item . split ( '=' ) ) ;
} ) ;
facets = facets _arr . reduce ( ( obj , cur ) => ( { ... obj , [ cur [ 0 ] ] : cur [ 1 ] } ) , { } )
2022-12-01 19:32:32 +08:00
2022-11-22 01:30:30 +08:00
var point _out _split = this . getDisplay ( "out" ) . split ( ' ' ) ;
let key = Object . keys ( facets ) . find ( k => facets [ k ] === point _out _split [ 0 ] ) ;
if ( key == undefined ) {
key = point _out _split [ 0 ] ;
}
var modify _target _device = {
"device_number" : target _device _number ? target _device _number : null ,
2022-12-01 19:32:32 +08:00
"device_number_full" : this . getDisplay ( "slotPath" ) . split ( "slot:" ) [ 1 ] . split ( '/' ) . slice ( 1 , - 1 ) . join ( "_" ) ,
2022-11-22 01:30:30 +08:00
"point_name" : point _name ? point _name : null ,
"value" : key
}
2022-12-01 19:32:32 +08:00
totalTargetDevice . push ( modify _target _device ) ;
2022-11-22 01:30:30 +08:00
//取得component當下就更新設備點位
if ( baja _subscribe _device _callback _func != undefined && baja _subscribe _device _callback _func != null ) {
baja _subscribe _device _callback _func ( modify _target _device ) ;
}
baja . Ord . make ( "local:|foxs:|station:|" + this . getDisplay ( "slotPath" ) )
. get ( )
. then ( function ( component ) {
component _index ++ ;
var target _device _number = component . $parent . getDisplayName ( ) . split ( '_' ) . slice ( 0 , 5 ) . join ( '_' ) ;
var point _name = component . getDisplayName ( ) ;
// //依據Facets判斷回傳的內容值為何
// var facets_split = component.getFacets1().$cEncStr.split('|');
// var facets_arr = [];
// facets_split.forEach(function (item, index) {
// facets_arr.push(item.split('=s:'));
// });
// var facets = facets_arr.reduce((obj, cur) => ({ ...obj, [cur[0]]: cur[1] }), {})
// var point_out_split = component.getOutDisplay().split(' ');
// let key = Object.keys(facets).find(k => facets[k] === point_out_split[0]);
// if (key == undefined) {
// key = point_out_split[0];
// }
// var modify_target_device = {
// "device_number": target_device_number ? target_device_number : null,
// "point_name": point_name ? point_name : null,
// "value": key
// }
// //取得component當下就更新設備點位
// if (baja_subscribe_device_callback_func != undefined && baja_subscribe_device_callback_func != null) {
// if (component_index == total_component_index) {
// baja_subscribe_device_callback_func(modify_target_device, true);
// }
// else {
// baja_subscribe_device_callback_func(modify_target_device);
// }
// }
sub . subscribe ( {
comps : component ,
} ) ;
2022-11-25 18:46:40 +08:00
tolSubList . push ( sub ) ;
2022-11-22 01:30:30 +08:00
subFinish = new Date ( Date . now ( ) ) ;
$ ( "#sub-end" ) . html ( subFinish . toISOString ( ) ) ;
$ ( "#sub-time" ) . html ( ( subFinish . getTime ( ) - subStart . getTime ( ) ) / 1000 + "sec" ) ;
// console.log("訂閱完成時間", (subFinish.getTime() - subStart.getTime()) / 1000 + "sec");
} ) ;
} ,
after : function ( ) {
tableFinish = new Date ( Date . now ( ) ) ;
// $("#table-finish-timestamp").html(tableFinish.toISOString());
// $("#table-time").html((tableFinish.getTime() - tableStart.getTime()) / 1000 + "sec");
2022-12-01 19:32:32 +08:00
//取得component當下就更新設備點位
if ( baja _subscribe _end _device _callback _func != undefined && baja _subscribe _end _device _callback _func != null ) {
baja _subscribe _end _device _callback _func ( totalTargetDevice ) ;
}
2022-11-22 01:30:30 +08:00
console . log ( "表格完成時間" , ( tableFinish . getTime ( ) - tableStart . getTime ( ) ) / 1000 + "sec" ) ;
} ,
limit : - 1 ,
offset : 0
} ) ;
} ) ;
} ) ;
}
2022-11-15 14:24:48 +08:00
2022-11-24 16:27:00 +08:00
function getElevatorInfoByBaja ( path , callback ) {
var _result = "" ;
var _ss = '{' ;
var _index = 0 ;
//
require ( [ 'baja!' ] , function ( baja ) {
baja . Ord . make ( "local:|foxs:|station:|slot:/TPE/B1/ELEV/EL/R2F/NA/ELEV1/N1|bql:select name, displayName, out, out.value, slotPath, parent.name as 'device_number' from control:ControlPoint where name = 'CP' or name = 'RD' or name = 'DS' or name = 'ST' or name = 'LOAD' or name = 'MID' or name = 'HAND' or name = 'IND' or name = 'MD' or name = 'DNO'" ) . get ( )
. then ( function ( table ) {
return table . cursor ( {
each : function ( record ) {
var item = null ;
var value = null ;
if ( record . get ( 'name' ) == "CP" ) {
item = "floor" ;
value = record . get ( 'out' ) . get ( 'value' ) ;
}
else if ( record . get ( 'name' ) == "RD" ) { //UP or DOWN
item = "direction" ;
value = record . get ( 'out' ) . get ( 'value' ) ;
}
else if ( record . get ( 'name' ) == "DS" ) {
item = "door_state" ;
value = record . get ( 'out' ) . get ( 'value' ) == true ? "OPEN" : "CLOSE" ; ;
}
else if ( record . get ( 'name' ) == "ST" ) {
item = "running_status" ;
value = record . get ( 'out' ) . get ( 'value' ) ;
}
else if ( record . get ( 'name' ) == "LOAD" ) {
item = "loading" ;
value = record . get ( 'out' ) . get ( 'value' ) ;
}
else if ( record . get ( 'name' ) == "MID" ) {
item = "maintenance" ;
value = record . get ( 'out' ) . get ( 'value' ) ;
}
else if ( record . get ( 'name' ) == "HAND" ) {
item = "manual" ;
value = record . get ( 'out' ) . get ( 'value' ) ;
}
else if ( record . get ( 'name' ) == "IND" ) {
item = "independent" ;
value = record . get ( 'out' ) . get ( 'value' ) ;
}
else if ( record . get ( 'name' ) == "MD" ) {
item = "vip" ;
value = record . get ( 'out' ) . get ( 'value' ) ;
}
else if ( record . get ( 'name' ) == "DNO" ) {
item = "automatic" ;
value = record . get ( 'out' ) . get ( 'value' ) ;
}
if ( item != null && item != "" )
{
if ( _index == 0 )
_ss += '{"item":"' + item + '", "value":"' + value + '"}' ;
else
_ss += ',{"item":"' + item + '", "value":"' + value + '"}' ;
_index ++ ;
}
} ,
after : function ( ) {
_result += '{' + '"count": ' + _index + ', "data":[' ;
_result += _ss ;
_result += ']}' ;
if ( typeof callback === 'function' ) {
callback ( _result ) ;
}
}
} ) ;
} ) ;
} ) ;
}
2022-12-02 12:18:58 +08:00
2022-11-24 16:27:00 +08:00
// baja.Ord.make('ip:greencloud.fic.com.tw|foxs:|station:|slot:/Arena/H/F3|bql:select name, displayname, slotPath, out.value, out from control:ControlPoint')
// // baja.Ord.make('ip:greencloud.fic.com.tw|foxs:|station:|slot:/Arena/H/E1/B1F/TestData/H_E1_B1F_TestData_Data|bql:select slotPath,out.value from control:NumericWritable')
// .get(
// function (table) {
// var tableStart, tableFinish;
// var subStart, subFinish;
// var readBqlFinish = new Date(Date.now());
// $("#readPath-finish-timestamp").html(readBqlFinish.toISOString());
// $("#readPath-finish-time").html((readBqlFinish.getTime() - init_start.getTime()) / 1000 + "sec");
// table.cursor({
// before: function () {
// tableStart = new Date(Date.now());
// $("#table-start-timestamp").html(tableStart.toISOString());
// subStart = new Date(Date.now());
// $("#sub-start-timestamp").html(subStart.toISOString());
// render_start = new Date(Date.now());
// },
// each: function (item, index) {
// // console.log("local:|foxs:|station:|" + this.getDisplay("slotPath"));
// baja.Ord.make("local:|foxs:|station:|" + this.getDisplay("slotPath"))
// .get()
// .then(function (component) {
// //紀錄 未render前花費約5秒
// $("#render-points").append(`
// <div>
// <label>${component.getDisplayName()}</label>
// <div id="${component.getDisplayName()}-value" style="text-align: center;">${component.getOutDisplay()}</div>
// </div>
// `);
// sub.subscribe({
// comps: component,
// });
// $("#sub-point-name").html(component.getDisplayName());
// subFinish = new Date(Date.now());
// $("#sub-finish-timestamp").html(subFinish.toISOString()); $("#sub-finish-timestamp").html(subFinish.toISOString());
// $("#sub-time").html((subFinish.getTime() - subStart.getTime()) / 1000 + "sec");
// });
// },
// after: function () {
// tableFinish = new Date(Date.now());
// $("#table-finish-timestamp").html(tableFinish.toISOString());
// $("#table-time").html((tableFinish.getTime() - tableStart.getTime()) / 1000 + "sec");
// },
// limit: -1,
// offset: 0
// });
// })
2022-12-01 19:32:32 +08:00
//測試
/ * *
* 取得電表即時資料 by baja
* @ param { any } devicePath
* @ param { any } callback
* /
function getElectricMeterNoweDataByBaja ( devicePath , callback ) {
var _result = "" ;
var _ss = "" ;
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()
baja . Ord . make ( 'local:|foxs:|station:|slot:/TPE/B1/EE/E4/R2F/NA/WHT/N1|bql:select name, out, out.value from control:ControlPoint' ) . get ( )
. then ( function ( table ) {
return table . cursor ( {
each : function ( record ) {
if ( _index == 0 )
_ss += '{"name":"' + record . get ( 'name' ) + '", "value":' + record . get ( 'out' ) . get ( 'value' ) + '"}' ;
else
_ss += ',{"name":"' + record . get ( 'name' ) + '", "value":' + record . get ( 'out' ) . get ( 'value' ) + '"}' ;
_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:/TPE/B1/EE/E4/R2F/NA/WHT/N1/History/TR_Month|bql: select * where timestamp.millis <= 1667404799000' ) . get ( )
. then ( function ( table ) {
return table . cursor ( {
each : function ( record ) {
if ( _index == 0 ) {
//_ss += '{"timestamp":"' + record.get('timestamp') + '", "kwh1":' + record.get('kwh1') + '"}';
_ss += '{"timestamp":"' + record . get ( 'timestamp' ) + '"}' ;
}
else {
//_ss += ',{"timestamp":"' + record.get('timestamp') + '", "kwh1":' + record.get('kwh1') + '"}';
_ss += ',{"timestamp":"' + record . get ( 'timestamp' ) + '"}' ;
}
_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 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:/TPE/B1/EE/E4/R2F/NA/WHT/N1/History/TR_Daily|bql: select timestamp, kwh1 where timestamp.millis >= 1667232000000 and timestamp.millis <= 1667404799000' ) . get ( )
. then ( function ( table ) {
return table . cursor ( {
each : function ( record ) {
if ( _index == 0 )
_ss += '{"kwh1":' + record . get ( 'kwh1' ) + ', "timestamp":"' + record . get ( 'timestamp' ) + '"}' ;
else
_ss += ',{"kwh1":' + record . get ( 'kwh1' ) + ', "timestamp":"' + record . get ( 'timestamp' ) + '"}' ;
_index ++ ;
} ,
after : function ( ) {
_result += '{' + '"count": ' + _index + ', "data":[' ;
_result += _ss ;
_result += ']}' ;
if ( typeof callback === 'function' ) {
callback ( _result ) ;
}
} ,
limit : - 1 ,
offset : 0
} ) ;
} ) ;
} ) ;
}
/ * *
* 在單一系統下 , 取得各個系統的狀態 異常與否
* @ param { any } systemPath
* @ param { any } callback
* /
function getOneSystemStateByBaja ( systemPath , callback ) {
var _result = "" ;
var _ss = "" ;
var _index = 0 ;
require ( [ 'baja!' ] , function ( baja ) {
baja . Ord . make ( "local:|foxs:|alarm:|bql:select top 1 alarmData, alarmData.sourceName, sourceState where alarmData.sourceName like '%" + systemPath + "%' order by timestamp desc" ) . get ( )
. then ( function ( table ) {
return table . cursor ( {
each : function ( record ) {
//if (_index == 0)
// _ss += '{"sourceState":"' + record.get('sourceState') + '"}';
//else
// _ss += '{"sourceState":"' + record.get('sourceState') + '"}';
_ss += '{"sourceState":"' + record . get ( 'sourceState' ) + '"}' ;
_index ++ ;
} ,
after : function ( ) {
_result += '{' + '"count": ' + _index + ', "data":[' ;
_result += _ss ;
_result += ']}' ;
if ( typeof callback === 'function' ) {
callback ( _result ) ;
}
} ,
limit : - 1 ,
offset : 0
} ) ;
} ) ;
} ) ;
}