Skip to content

Commit

Permalink
Organize Epics handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelLyra8 committed Oct 27, 2023
1 parent 12891d2 commit 666e9b2
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 216 deletions.
2 changes: 1 addition & 1 deletion src/components/SiriusChart/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/components/SiriusInvisible/index.tsx
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/components/SiriusLabel/index.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion src/components/SiriusLed/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
211 changes: 0 additions & 211 deletions src/components/epics.ts

This file was deleted.

80 changes: 80 additions & 0 deletions src/controllers/epics_base.ts
Original file line number Diff line number Diff line change
@@ -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<T extends string|string[]> {
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<number>, 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<M>(): Dict<EpicsData<M>> {
let pvData: Dict<EpicsData<M>> = 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<T>(this.pv_name);
}

_unsubscribe2epics_con(){
EpicsCon.remove_pv<T>(this.pv_name);
}
}

export default EpicsBase;
74 changes: 74 additions & 0 deletions src/controllers/epics_con.ts
Original file line number Diff line number Diff line change
@@ -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<T extends string|string[]>(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<T extends string|string[]>(new_pv: T){
let list2add: string[] = [];
let new_pv_list: string[] = this.convert2list<T>(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<T extends string|string[]>(rem_pv: T){
let pv_list: string[] = this.pv_name;
const rem_pv_list: string[] = this.convert2list<T>(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<M>(): Dict<EpicsData<M>> {
let pvData: Dict<EpicsData<M>> = this.epics.pvData;
return pvData;
}
}

export default EpicsCon;
Loading

0 comments on commit 666e9b2

Please sign in to comment.