Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | 92x 57x 57x 57x 285x 285x 242x 259x 243x 285x 204x 31x 119x 113x 113x 113x 113x 8x 101x 15x 2x 2x 2x 130x 130x 9x 130x 10x 130x 26x 130x 124x 124x 1475x 1475x 1475x 1475x 124x 124x 124x 21x 21x 7x 7x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 6x 13x 13x 2x 2x 2x 2x 44x 2x 11x 11x 11x 11x 13x 11x 1x 3x 3x 29x 29x 29x 29x 29x 29x 29x 29x 1x 1x 1x 1x 1x 4x 4x 4x 1x 1x 1x 9x 9x 2x 2x 1x 1x 1x 9x 3x 2x 2x 1x 1x 1x | import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable, interval, of, map, startWith, exhaustMap, timer } from 'rxjs';
import { Signal, SignalData, SignalsData, SignalSample, SignalValue, SampleStats, SignalStats } from '../models/signals';
import { TwinPadStatus, ServicesStatus, Slash } from '../models/status';
import { ListResponse } from '../models/response';
import { Device, DeviceSetup, Mode, DeviceState } from '../models/devices';
import { EventRule, Event } from '../models/events';
import { Campaign, Phase } from '../models/campaign';
import { environment } from '../../environments/environment';
import { catchError, switchMap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class TwinpadApiService {
apiUrl: string = environment['apiUrl'];
constructor(private http: HttpClient) {
Iif (!this.apiUrl){
this.apiUrl = window.location.protocol+"//"+window.location.hostname.toLowerCase()+':5001';
}
}
// Generic periodic function
callPeriodically<T>(
method: () => Observable<T>,
period: number,
initialDelay: number = 0
): Observable<T> {
return new Observable(observer => {
const subscription = timer(initialDelay).pipe(
switchMap(() => interval(period).pipe(startWith(0))),
exhaustMap(() =>
method().pipe(
catchError(err => {
return new Observable<T>(subscriber => {
subscriber.error(err);
subscriber.complete();
});
})
)
)
).subscribe({
next: value => observer.next(value),
error: err => observer.error(err),
complete: () => observer.complete()
});
return () => {
subscription.unsubscribe();
};
});
}
getVersion(): Observable<Slash> {
return this.http.get<Slash>(`${this.apiUrl}`);
}
getStatus(): Observable<TwinPadStatus> {
return this.http.get<TwinPadStatus>(`${this.apiUrl}/status`).pipe(
map((response: TwinPadStatus) => {
// Deserialize response into TwinPadStatus instance. Move this to models?
const services = Object.assign(new ServicesStatus('down'), response['services']);
const status = Object.assign(new TwinPadStatus(services), response);
status.services = services;
return status;
}),
catchError(error => {
console.error('Error fetching status:', error);
// Return a custom object in case of error
const services = new ServicesStatus("down");
const status = new TwinPadStatus(services);
return of(status);
})
);
}
getSignalStats(): Observable<SignalStats>{
return this.http.get<SignalStats>(`${this.apiUrl}/signals/stats`);
}
getDevices(): Observable<Device[]> {
return this.http.get<Device[]>(`${this.apiUrl}/devices`);
}
getDevice(device_id: string): Observable<Device> {
return this.http.get<Device>(`${this.apiUrl}/devices/${device_id}`);
}
getDeviceSetups(): Observable<DeviceSetup[]> {
return this.http.get<DeviceSetup[]>(`${this.apiUrl}/device-setups`);
}
getDeviceSetup(device_setup_id: string): Observable<DeviceSetup> {
return this.http.get<DeviceSetup>(`${this.apiUrl}/device-setups/${device_setup_id}`);
}
deleteDeviceSetup(device_setup_id: string): Observable<DeviceSetup> {
return this.http.delete<DeviceSetup>(`${this.apiUrl}/device-setups/${device_setup_id}`);
}
getDeviceStates(device_id: string, limit:number, offset:number = 0, sort_by: string): Observable<ListResponse<DeviceState>> {
const params: HttpParams = new HttpParams({fromObject: {limit:limit, offset:offset, sort_by:sort_by}});
return this.http.get<ListResponse<DeviceState>>(`${this.apiUrl}/devices/${device_id}/states`, {params: params});
}
getSignals(limit:number, offset:number=0, filter: string|null=null, device_id:string|null=null, signalIds:string[]|null=null): Observable<ListResponse<Signal>> {
let params: HttpParams = new HttpParams({fromObject: {limit:limit, offset:offset}});
if (device_id !== null){
params = params.set("signal_id", "startswith:"+device_id);
}
if (signalIds !== null){
params = params.set("signal_id", "in:["+signalIds.join(',')+']');
}
if (filter !== null){
params = params.set("signal_id", "contains:"+filter);
}
return this.http.get<ListResponse<Signal>>(`${this.apiUrl}/signals`, {params:params}).pipe(
map((response: ListResponse<Signal>) => {
const signals = [];
for (const item of response['items']){
const device = Object.assign(new Device(), item['device']);
const signal = Object.assign(new Signal(), item);
signal.device = device;
signals.push(signal);
}
response = Object.assign(new ListResponse(), response);
response.items = signals;
return response;
})
);
}
getSignal(signal_id: string): Observable<Signal> {
return this.http.get<Signal>(`${this.apiUrl}/signals/${signal_id}`).pipe(
map((response: Signal) => {
// Deserialize
return Object.assign(new Signal(), response);}));
}
getSignalData(signal_id: string, number_samples_max: number=5000): Observable<SignalData> {
return this.http.get<SignalData>(`${this.apiUrl}/signals/${signal_id}/data`, {params:{number_samples_max:number_samples_max}});
}
getSignalNumberSamples(signal_id: string): Observable<SampleStats> {
return this.http.get<SampleStats>(`${this.apiUrl}/signals/${signal_id}/number-samples`).pipe(
map((response: SampleStats) => {
return response;
})
);
}
getSignalsData(signal_ids: string[], number_samples_max: number=5000, min_timestamp: number|null=null, max_timestamp: number|null=null): Observable<SignalsData> {
let params: HttpParams = new HttpParams({fromObject: {signal_ids:signal_ids, number_samples_max:number_samples_max}});
if (min_timestamp !== null){
params = params.set("min_timestamp", min_timestamp);
}
if (max_timestamp !== null){
params = params.set("max_timestamp", max_timestamp);
}
return this.http.get<SignalsData>(`${this.apiUrl}/signals-data`, {params:params});
}
getSignalsDataWithInterestWindow(signal_ids: string[], windowMaxNumberSamples: number=300, outsideMaxNumberSamples : number=300,
windowMinTs : number|null=null, windowMaxTs : number|null=null,
minTimestamp:number|null=null, maxTimestamp:number|null=null): Observable<SignalsData> {
let params: HttpParams = new HttpParams({fromObject: {signal_ids:signal_ids, window_number_samples_max:windowMaxNumberSamples, outside_number_samples_max:outsideMaxNumberSamples}});
Eif (windowMinTs !== null){
params = params.set("window_min_timestamp", windowMinTs);
}
Eif (windowMaxTs !== null){
params = params.set("window_max_timestamp", windowMaxTs);
}
Eif (minTimestamp !== null){
params = params.set("min_timestamp", minTimestamp);
}
Eif (maxTimestamp !== null){
params = params.set("max_timestamp", maxTimestamp);
}
return this.http.get<SignalsData>(`${this.apiUrl}/signals-data/interest-window`, {params:params}).pipe(
map((response: SignalsData) => { return SignalsData.deserialize(response); })
);
}
getSignalValue(signal_id: string){
return this.http.get<SignalSample>(`${this.apiUrl}/signals/${signal_id}/last-value`).pipe(
map((response: SignalSample) => {
// Deserialize
return Object.assign(new SignalSample(), response);}));
}
getSignalFirstValue(signal_id: string){
return this.http.get<SignalSample>(`${this.apiUrl}/signals/${signal_id}/first-value`).pipe(
map((response: SignalSample) => {
// Deserialize
return Object.assign(new SignalSample(), response);}));
}
getSignalValues(signal_ids: string[]){
const params: HttpParams = new HttpParams({fromObject: {signal_ids:signal_ids}});
return this.http.get<SignalSample[]>(`${this.apiUrl}/signals/last-value`, {params:params}).pipe(
map((response: SignalSample[]) => {
// Deserialize
const samples = [];
for (const response_sample of response){
samples.push(Object.assign(new SignalSample(), response_sample));}
return samples;
}));
}
getSignalFirstValues(signal_ids: string[]){
const params: HttpParams = new HttpParams({fromObject: {signal_ids:signal_ids}});
return this.http.get<SignalSample[]>(`${this.apiUrl}/signals/first-value`, {params:params}).pipe(
map((response: SignalSample[]) => {
// Deserialize
const samples = [];
for (const response_sample of response){
samples.push(Object.assign(new SignalSample(), response_sample));}
return samples;
}));
}
changeDeviceMode(device: Device, newMode:Mode){
return this.http.patch<Device>(`${this.apiUrl}/devices/${device.device_id}`, {mode_id: newMode.mode_id});
}
sendSignalCommand(signal: Signal, value:number|string|boolean, forced_value:number|string|boolean|null){
return this.http.patch<SignalValue>(`${this.apiUrl}/signals/${signal.signal_id}`, {value: value, forced_value:forced_value});
}
sendSignalForcedValue(signal: Signal, forced_value:number|string|boolean){
return this.http.patch<SignalValue>(`${this.apiUrl}/signals/${signal.signal_id}`, {forced_value: forced_value});
}
sendSignalUnforce(signal: Signal){
return this.http.patch<SignalValue>(`${this.apiUrl}/signals/${signal.signal_id}`, {forced_value: null});
}
getEvents(limit:number, offset:number=0, sortBy:string[], timestamp_min:number|null=null, timestamp_max:number|null=null, eventRuleId:string|null=null): Observable<ListResponse<Event>> {
let params = new HttpParams({fromObject:{limit:limit, offset:offset}});
params = params.set("sort_by", sortBy.join(','));
const timestamp_req: string[] = [];
Iif (timestamp_min !== null){
timestamp_req.push('gte:'+timestamp_min.toString());
}
Iif (timestamp_max !== null){
timestamp_req.push('lte:'+timestamp_max.toString());
}
Iif (timestamp_req.length > 0){
params = params.set("timestamp", timestamp_req.join('|'));
}
Iif (eventRuleId!==null){
params = params.set("event_rule_id", eventRuleId);
}
return this.http.get<ListResponse<Event>>(`${this.apiUrl}/events`, {params:params});
}
getEvent(eventId: string): Observable<Event> {
return this.http.get<Event>(`${this.apiUrl}/events/${eventId}`).pipe(
map((response: Event) => {
// Deserialize
const eventRule = Object.assign(new EventRule(), response['event_rule']);
const event = Object.assign(new Event(), response);
event.event_rule = eventRule;
return event;
}));
}
getEventRules(limit:number, offset:number=0, sortBy:string[]): Observable<ListResponse<EventRule>> {
let params = new HttpParams({fromObject:{limit:limit, offset:offset}});
params = params.set("sort_by", sortBy.join(','));
return this.http.get<ListResponse<EventRule>>(`${this.apiUrl}/event-rules`, {params:params});
}
getEventRule(eventRuleId: string): Observable<EventRule> {
return this.http.get<EventRule>(`${this.apiUrl}/event-rules/${eventRuleId}`).pipe(
map((response: EventRule) => {
// Deserialize
const event_rule = Object.assign(new EventRule(), response);
return event_rule;
}));
}
downloadSignalsDataZip(format: string, signal_ids: string[], min_timestamp: number|null=null, max_timestamp: number|null=null): Observable<Blob>{
let params: HttpParams = new HttpParams({fromObject: {format: format, signal_ids:signal_ids}});
if (min_timestamp !== null){
params = params.set("min_timestamp", min_timestamp);
}
if (max_timestamp !== null){
params = params.set("max_timestamp", max_timestamp);
}
return this.http.get<Blob>(`${this.apiUrl}/signals-data/export`, {params: params, responseType: 'blob' as 'json' }, );
}
getCampaigns(): Observable<Campaign[]>{
return this.http.get<Campaign[]>(`${this.apiUrl}/campaigns`);
}
getCampaignById(campaignId: string): Observable<Campaign>{
return this.http.get<Campaign>(`${this.apiUrl}/campaigns/${campaignId}`, {});
}
addCampaign(campaignName: string, campaignDescription?: string): Observable<object>{
const options = { headers: new HttpHeaders().set('Content-type', 'application/json') };
return this.http.post(`${this.apiUrl}/campaigns`, {"name": campaignName, "description": campaignDescription}, options);
}
editCampaign(campaignId: string, campaignName: string, campaignDescription?: string): Observable<object>{
const options = { headers: new HttpHeaders().set('Content-type', 'application/json') };
return this.http.patch(`${this.apiUrl}/campaigns/${campaignId}`, {"name": campaignName, "description": campaignDescription}, options);
}
deleteCampaign(campaignId: string): Observable<object>{
return this.http.delete(`${this.apiUrl}/campaigns/${campaignId}`, {});
}
getPhases(campaignId: string): Observable<Phase[]>{
return this.http.get<Phase[]>(`${this.apiUrl}/campaigns/${campaignId}/phases`);
}
getPhase(phaseId: string): Observable<Phase>{
return this.http.get<Phase>(`${this.apiUrl}/phases/${phaseId}`);
}
addPhase(campaignId: string, phaseName: string, phaseDateStart: number, phaseDateEnd: number, phaseDescription?: string){
const options = { headers: new HttpHeaders().set('Content-type', 'application/json') };
return this.http.post(`${this.apiUrl}/phases`, {"name": phaseName, "description": phaseDescription, "start_at": phaseDateStart, "end_at": phaseDateEnd, "campaign_id": campaignId}, options);
}
editPhase(phaseName: string, phaseDateStart: number, phaseDateEnd: number, campaignId: string, phaseId?: string, phaseDescription?: string,){
const options = { headers: new HttpHeaders().set('Content-type', 'application/json') };
return this.http.patch(`${this.apiUrl}/phases/${phaseId}`, {"name": phaseName, "description": phaseDescription, "start_at": phaseDateStart, "end_at": phaseDateEnd, "campaign_id": campaignId}, options);
}
deletePhase(phaseId?: string){
return this.http.delete(`${this.apiUrl}/phases/${phaseId}`, {});
}
}
|