diff --git a/src/components/SiriusChart/index.tsx b/src/components/SiriusChart/index.tsx index ab5adc5..cb70217 100644 --- a/src/components/SiriusChart/index.tsx +++ b/src/components/SiriusChart/index.tsx @@ -1,6 +1,6 @@ import React, { Component, createRef } from "react"; import Chart from 'chart.js/auto'; -import EpicsBase from "../epics"; +import EpicsBase from "../../controllers/epics_base"; import { default_colors } from "../../assets/themes"; import { Dict, ChartPv, RefChart, EpicsData } from "../../assets/interfaces"; import { chartOptions } from "./options"; diff --git a/src/components/SiriusInvisible/index.tsx b/src/components/SiriusInvisible/index.tsx index 871cb89..741efc1 100644 --- a/src/components/SiriusInvisible/index.tsx +++ b/src/components/SiriusInvisible/index.tsx @@ -1,5 +1,5 @@ import React from "react"; -import EpicsBase from "../epics"; +import EpicsBase from "../../controllers/epics_base"; import { PvInterface, Dict, EpicsData } from "../../assets/interfaces"; /** diff --git a/src/components/SiriusLabel/index.tsx b/src/components/SiriusLabel/index.tsx index ab3597c..bdfd8ed 100644 --- a/src/components/SiriusLabel/index.tsx +++ b/src/components/SiriusLabel/index.tsx @@ -1,5 +1,5 @@ import React from "react"; -import EpicsBase from "../epics"; +import EpicsBase from "../../controllers/epics_base"; import SiriusTooltip from "../SiriusTooltip"; import { Dict, EpicsData, LabelPv, State } from "../../assets/interfaces"; diff --git a/src/components/SiriusLed/index.tsx b/src/components/SiriusLed/index.tsx index 00ee931..66cdff7 100644 --- a/src/components/SiriusLed/index.tsx +++ b/src/components/SiriusLed/index.tsx @@ -1,5 +1,5 @@ import React from "react"; -import EpicsBase from "../epics"; +import EpicsBase from "../../controllers/epics_base"; import SiriusTooltip from "../SiriusTooltip"; import { State, LedPv, EpicsData, Dict } from "../../assets/interfaces"; import { default_colors, led_shape } from "../../assets/themes"; diff --git a/src/components/epics.ts b/src/components/epics.ts deleted file mode 100644 index 1904e7d..0000000 --- a/src/components/epics.ts +++ /dev/null @@ -1,211 +0,0 @@ -import Epics from "../access_data/Epics"; -import { Dict, EpicsData } from "../assets/interfaces"; - - -/** - * Class that return the state of the PV based on - * its value and the thresholds established. - */ -class Thresholds { - private thr_dict: Dict; - private thr_arr: [string, number][]; - - constructor(){ - this.thr_dict = {}; - this.thr_arr = []; - } - - set_thresholds(threshold: Dict): void { - this.thr_dict = threshold; - this.thr_arr = this.sort_dict(this.thr_dict); - } - - dict2array(dictionary: Dict): [string, number][] { - return Object.entries(dictionary).map(([key, value]: [string, number])=>{ - return [key, value]; - }); - } - - /** - * Sort threshold dictionary in crescent order - */ - sort_dict(dictionary: Dict): [string, number][] { - let thr_arr: [string, number][] = this.dict2array(dictionary); - thr_arr.sort(function(first, second){ - return second[1] - first[1]; - }); - return thr_arr; - } - - /** - * Get the last exeeded threshold key. - */ - get_final_state(value: number, threshold: [string, number][]): string { - for(let a = 0; a < threshold.length; a++) { - if(value >= threshold[a][1]){ - return threshold[a][0]; - } - } - return 'normal'; - } - - /** - * Get threshold state. - */ - get_biggest_threshold(value: number): string { - let final_state: string = 'normal'; - if( this.thr_dict !== undefined ){ - final_state = this.get_final_state(value, this.thr_arr); - } - return final_state - } -} - - -/** - * Create an static Epics connection that allows - * widget to subscribe or unsubscribe to the monitoring. - */ -class EpicsCon { - private static epics: Epics = new Epics([]); - private static pv_name: string[] = []; - - static convert2list(new_pv: T): string[]{ - if(Array.isArray(new_pv)){ - return new_pv; - } - return [new_pv]; - } - - /** - * Returns a list with the PVs that are not already subscribed. - */ - static new_pvs_list(pv_list: string[]): string[]{ - let pvs2add: string[] = []; - pv_list.map((pvname: string) => { - if(!(pvname in this.pv_name)){ - pvs2add.push(pvname); - } - }) - return pvs2add; - } - - /** - * Create a new connection using the Epics Object. - */ - static create_epics(){ - this.epics.disconnect(); - return new Epics(this.pv_name); - } - - /** - * Subscribe new list of PVs. - */ - static add_new_pv(new_pv: T){ - let list2add: string[] = []; - let new_pv_list: string[] = this.convert2list(new_pv); - list2add = this.new_pvs_list(new_pv_list); - this.pv_name = this.pv_name.concat(list2add); - this.epics = this.create_epics(); - } - - /** - * Unsubscribe new list of PVs. - */ - static remove_pv(rem_pv: T){ - let pv_list: string[] = this.pv_name; - const rem_pv_list: string[] = this.convert2list(rem_pv); - rem_pv_list.map((pvname: string) => { - if(pvname in this.pv_name){ - delete pv_list[pvname]; - } - }) - this.pv_name = pv_list; - } - - /** - * Get a list of the current values of all subscribed PVs. - */ - static get_pv_data(): Dict> { - let pvData: Dict> = this.epics.pvData; - return pvData; - } -} - - -/** - * Create a basic Epics object that updates its values in - * fixed time intervals. - */ -class EpicsBase { - public update_interval: number; - public thresholds: Thresholds; - public pv_name: T; - private timer: any; - - constructor(pvname: T){ - this.update_interval = 100; - this.pv_name = pvname; - this._subscribe2epics_con(); - this.timer = null; - this.thresholds = new Thresholds(); - } - - initialize(pv_name: T, threshold: undefined|Dict, update_interval: undefined|number): void { - this.set_pvname(pv_name); - this._subscribe2epics_con(); - - if(threshold !== undefined) { - this.thresholds.set_thresholds(threshold); - } - if(update_interval !=undefined){ - this._set_update_interval(update_interval); - } - } - - start_timer(fun: ()=>void): void { - this.timer = setInterval( - fun, this.update_interval); - } - - get_pv_data(): Dict> { - let pvData: Dict> = EpicsCon.get_pv_data(); - return pvData; - } - - get_threshold(value: number): string { - return this.thresholds.get_biggest_threshold(value); - } - - set_pvname(pvname: T): void { - if(this._equal_check(pvname)){ - this.pv_name = pvname; - this._subscribe2epics_con(); - } - } - - destroy(): void { - if(this.timer!=null){ - clearInterval(this.timer); - this._unsubscribe2epics_con(); - } - } - - _equal_check(pvname: T): boolean { - return JSON.stringify(this.pv_name) != JSON.stringify(pvname); - } - - _set_update_interval(milliseconds: number): void { - this.update_interval = milliseconds; - } - - _subscribe2epics_con(){ - EpicsCon.add_new_pv(this.pv_name); - } - - _unsubscribe2epics_con(){ - EpicsCon.remove_pv(this.pv_name); - } -} - -export default EpicsBase; diff --git a/src/controllers/epics_base.ts b/src/controllers/epics_base.ts new file mode 100644 index 0000000..b8493b9 --- /dev/null +++ b/src/controllers/epics_base.ts @@ -0,0 +1,80 @@ +import { Dict, EpicsData } from "../assets/interfaces"; +import Thresholds from "./threshold"; +import EpicsCon from "./epics_con"; + +/** + * Create a basic Epics object that updates its values in + * fixed time intervals. + */ +class EpicsBase { + public update_interval: number; + public thresholds: Thresholds; + public pv_name: T; + private timer: any; + + constructor(pvname: T){ + this.update_interval = 100; + this.pv_name = pvname; + this._subscribe2epics_con(); + this.timer = null; + this.thresholds = new Thresholds(); + } + + initialize(pv_name: T, threshold: undefined|Dict, update_interval: undefined|number): void { + this.set_pvname(pv_name); + this._subscribe2epics_con(); + + if(threshold !== undefined) { + this.thresholds.set_thresholds(threshold); + } + if(update_interval !=undefined){ + this._set_update_interval(update_interval); + } + } + + start_timer(fun: ()=>void): void { + this.timer = setInterval( + fun, this.update_interval); + } + + get_pv_data(): Dict> { + let pvData: Dict> = EpicsCon.get_pv_data(); + return pvData; + } + + get_threshold(value: number): string { + return this.thresholds.get_biggest_threshold(value); + } + + set_pvname(pvname: T): void { + if(this._equal_check(pvname)){ + this.pv_name = pvname; + this._subscribe2epics_con(); + } + } + + destroy(): void { + if(this.timer!=null){ + clearInterval(this.timer); + this._unsubscribe2epics_con(); + } + } + + _equal_check(pvname: T): boolean { + return JSON.stringify(this.pv_name) != JSON.stringify(pvname); + } + + _set_update_interval(milliseconds: number): void { + this.update_interval = milliseconds; + } + + _subscribe2epics_con(){ + EpicsCon.add_new_pv(this.pv_name); + } + + _unsubscribe2epics_con(){ + EpicsCon.remove_pv(this.pv_name); + } +} + +export default EpicsBase; diff --git a/src/controllers/epics_con.ts b/src/controllers/epics_con.ts new file mode 100644 index 0000000..02ccc84 --- /dev/null +++ b/src/controllers/epics_con.ts @@ -0,0 +1,74 @@ +import Epics from "../access_data/Epics"; +import { Dict, EpicsData } from "../assets/interfaces"; + +/** + * Create an static Epics connection that allows + * widget to subscribe or unsubscribe to the monitoring. + */ +class EpicsCon { + private static epics: Epics = new Epics([]); + private static pv_name: string[] = []; + + static convert2list(new_pv: T): string[]{ + if(Array.isArray(new_pv)){ + return new_pv; + } + return [new_pv]; + } + + /** + * Returns a list with the PVs that are not already subscribed. + */ + static new_pvs_list(pv_list: string[]): string[]{ + let pvs2add: string[] = []; + pv_list.map((pvname: string) => { + if(!(pvname in this.pv_name)){ + pvs2add.push(pvname); + } + }) + return pvs2add; + } + + /** + * Create a new connection using the Epics Object. + */ + static create_epics(){ + this.epics.disconnect(); + return new Epics(this.pv_name); + } + + /** + * Subscribe new list of PVs. + */ + static add_new_pv(new_pv: T){ + let list2add: string[] = []; + let new_pv_list: string[] = this.convert2list(new_pv); + list2add = this.new_pvs_list(new_pv_list); + this.pv_name = this.pv_name.concat(list2add); + this.epics = this.create_epics(); + } + + /** + * Unsubscribe new list of PVs. + */ + static remove_pv(rem_pv: T){ + let pv_list: string[] = this.pv_name; + const rem_pv_list: string[] = this.convert2list(rem_pv); + rem_pv_list.map((pvname: string) => { + if(pvname in this.pv_name){ + delete pv_list[pvname]; + } + }) + this.pv_name = pv_list; + } + + /** + * Get a list of the current values of all subscribed PVs. + */ + static get_pv_data(): Dict> { + let pvData: Dict> = this.epics.pvData; + return pvData; + } +} + +export default EpicsCon; diff --git a/src/controllers/threshold.ts b/src/controllers/threshold.ts new file mode 100644 index 0000000..af10442 --- /dev/null +++ b/src/controllers/threshold.ts @@ -0,0 +1,53 @@ +import { Dict } from "../assets/interfaces"; + +/** + * Class that return the state of the PV based on + * its value and the thresholds established. + */ +class Thresholds { + private thr_dict: Dict; + private thr_arr: [string, number][]; + + constructor(){ + this.thr_dict = {}; + this.thr_arr = []; + } + + dict2array(dictionary: Dict): [string, number][] { + return Object.entries(dictionary).map(([key, value]: [string, number])=>{ + return [key, value]; + }); + } + + /** + * Sort threshold dictionary in crescent order + */ + sort_dict(): [string, number][] { + let thr_arr: [string, number][] = this.dict2array(this.thr_dict); + thr_arr.sort(function(first, second){ + return second[1] - first[1]; + }); + return thr_arr; + } + + set_thresholds(threshold: Dict): void { + this.thr_dict = threshold; + this.thr_arr = this.sort_dict(); + } + + /** + * Get threshold state. + */ + get_biggest_threshold(value: number): string { + if( this.thr_dict !== undefined ){ + for(let a = 0; a < this.thr_arr.length; a++) { + if(value >= this.thr_arr[a][1]){ + return this.thr_arr[a][0]; + } + } + } + return 'normal'; + } +} + +export default Thresholds; diff --git a/src/tests/Classes.test.tsx b/src/tests/Classes.test.tsx index c24bcb7..d900544 100644 --- a/src/tests/Classes.test.tsx +++ b/src/tests/Classes.test.tsx @@ -1,4 +1,4 @@ -import EpicsBase from "../components/epics"; +import EpicsBase from "../controllers/epics_con"; describe('Epics Base Class', () => { const base_threshold = {