Skip to content

Commit

Permalink
work in progress (backend)
Browse files Browse the repository at this point in the history
  • Loading branch information
unocelli committed Feb 17, 2022
1 parent 2ef1947 commit 98101c6
Show file tree
Hide file tree
Showing 15 changed files with 383 additions and 35 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/docker_rc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: BP Release Candidate

env:
app: fuxa
version: rc1
# for available platforms see output of a previous run -
# ie the "Setup Docker BuildX" / "Inspect Builder" section
# has eg "node_platforms": "linux/amd64,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/arm/v7,linux/arm/v6"
# but the platforms must also be included in the os image chosen -
# eg the ubuntu image doesn't include linux/arm/v6.
platforms: linux/amd64,linux/arm64,linux/arm/v7

on: workflow_dispatch
#push:
# branches:
# - master

jobs:
build-container:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up QEMU emulator
uses: docker/setup-qemu-action@v1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
push: true
platforms: ${{ env.platforms }}
tags: frangoteam/${{ env.app }}:${{ env.version }}
15 changes: 11 additions & 4 deletions client/src/app/_models/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@ export class Script {
}
}

export class ScriptTest extends Script {
test = true;
outputId: string; // to filter the console output sended from backend script runner

constructor(_id: string, _name: string) {
super(_id);
this.name = _name;
}
}

export class ScriptParam {
name: string;
type: ScriptParamType;
value: any;

constructor(_name: string, _type: ScriptParamType) {
this.name = _name;
this.type = _type;
}
}

export class ScriptTestParam extends ScriptParam {
value: any;
}

export enum ScriptParamType {
tagid = 'script.paramtype-tagid',
value = 'script.paramtype-value',
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/_services/plugin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { TranslateService } from '@ngx-translate/core';
export class PluginService {

@Output() onPluginsChanged: EventEmitter<any> = new EventEmitter();
v

private endPointConfig: string = EndPointApi.getURL();

constructor(private http: HttpClient,
Expand Down
16 changes: 16 additions & 0 deletions client/src/app/_services/script.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* tslint:disable:no-unused-variable */

import { TestBed, async, inject } from '@angular/core/testing';
import { ScriptService } from './script.service';

describe('Service: Script', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ScriptService]
});
});

it('should ...', inject([ScriptService], (service: ScriptService) => {
expect(service).toBeTruthy();
}));
});
34 changes: 34 additions & 0 deletions client/src/app/_services/script.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

import { EndPointApi } from '../_helpers/endpointapi';
import { environment } from '../../environments/environment';
import { Script, ScriptTest } from '../_models/script';

@Injectable({
providedIn: 'root'
})
export class ScriptService {

private endPointConfig: string = EndPointApi.getURL();

constructor(private http: HttpClient) { }

runScript(script: Script) {
return new Observable((observer) => {
if (environment.serverEnabled) {
let header = new HttpHeaders({ 'Content-Type': 'application/json' });
let params = { script: script };
this.http.post<any>(this.endPointConfig + '/api/runscript', { headers: header, params: params }).subscribe(result => {
observer.next();
}, err => {
console.error(err);
observer.error(err);
});
} else {
observer.next();
}
});
}
}
2 changes: 2 additions & 0 deletions client/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { SettingsService } from './_services/settings.service';
import { PluginService } from './_services/plugin.service';
import { AuthService } from './_services/auth.service';
import { DiagnoseService } from './_services/diagnose.service';
import { ScriptService } from './_services/script.service';
import { ResWebApiService } from './_services/rcgi/reswebapi.service';
import { ResDemoService } from './_services/rcgi/resdemo.service';
import { ResClientService } from './_services/rcgi/resclient.service';
Expand Down Expand Up @@ -291,6 +292,7 @@ export function createTranslateLoader(http: HttpClient) {
ProjectService,
UserService,
DiagnoseService,
ScriptService,
PluginService,
SettingsService,
TesterService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
}

.test-panel {
overflow: auto;
position: relative;
}

Expand All @@ -94,8 +95,14 @@
}

.test-console .content {
overflow: auto;
}

.console-text {
display: block;
line-height: 14px;
white-space: nowrap;
}

:host() ::ng-deep .mat-tab-label {
height: var(--tab-height) !important;
color: white;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ <h1 mat-dialog-title style="display:inline-block;cursor:move;width: calc(100% -
<div class="test-panel test-console">
<div>
<span class="header">{{'script.property-test-console' | translate}}</span>
<div *ngFor="let text of console; index as i" class="console-text">
<span>>{{text}}</span>
</div>
</div>
</div>
</div>
Expand Down
23 changes: 19 additions & 4 deletions client/src/app/scripts/script-editor/script-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { MatDialog, MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
import { CodemirrorComponent } from '@ctrl/ngx-codemirror';
import { ChangeDetectorRef } from '@angular/core';

import { ProjectService } from '../../_services/project.service';
import { ScriptService } from '../../_services/script.service';
import { EditNameComponent } from '../../gui-helpers/edit-name/edit-name.component';
import { TranslateService } from '@ngx-translate/core';
import { Utils } from '../../_helpers/utils';
import { DeviceTagDialog } from '../../device/device.component';
import { ScriptParamType, Script, SCRIPT_PREFIX, SystemFunctions, SystemFunction, ScriptParam, ScriptTestParam } from '../../_models/script';
import { ScriptParamType, Script, ScriptTest, SCRIPT_PREFIX, SystemFunctions, SystemFunction, ScriptParam } from '../../_models/script';
import { DevicesUtils, Tag } from '../../_models/device';

@Component({
Expand All @@ -32,9 +34,10 @@ export class ScriptEditorComponent implements OnInit, AfterViewInit {
systemFunctions = new SystemFunctions();
checkSystemFnc = this.systemFunctions.functions.map(sf => sf.name);
parameters: ScriptParam[] = [];
testParameters: ScriptTestParam[] = [];
testParameters: ScriptParam[] = [];
tagParamType = Utils.getEnumKey(ScriptParamType, ScriptParamType.tagid);

console: string[] = [];
script: Script;
msgRemoveScript = '';
ready = false;
Expand All @@ -43,6 +46,8 @@ export class ScriptEditorComponent implements OnInit, AfterViewInit {
public dialog: MatDialog,
private changeDetector: ChangeDetectorRef,
private translateService: TranslateService,
private projectService: ProjectService,
private scriptService: ScriptService,
@Inject(MAT_DIALOG_DATA) public data: any) {
this.script = data.script;
this.dialogRef.afterOpened().subscribe(() => setTimeout(() => {this.ready = true; this.setCM()}, 0));
Expand Down Expand Up @@ -165,7 +170,7 @@ export class ScriptEditorComponent implements OnInit, AfterViewInit {
});
}

onSetTestTagParam(param: ScriptTestParam) {
onSetTestTagParam(param: ScriptParam) {
let dialogRef = this.dialog.open(DeviceTagDialog, {
position: { top: '60px' },
data: { variableId: null, devices: this.data.devices, multiSelection: false }
Expand All @@ -179,7 +184,17 @@ export class ScriptEditorComponent implements OnInit, AfterViewInit {
}

onRunTest() {
this.projectService.setScript(this.script, null).subscribe(() => {
let torun = new ScriptTest(this.script.id, this.script.name);
torun.parameters = this.testParameters;
torun.outputId = this.script.id;
delete torun.code;
this.scriptService.runScript(torun).subscribe(result => {

}, err => {
this.console.push((err.message) ? err.message : err);
});
});
}

private insertText(text: string) {
Expand Down Expand Up @@ -208,7 +223,7 @@ export class ScriptEditorComponent implements OnInit, AfterViewInit {
private loadTestParameter() {
let params = [];
for (let i = 0; i < this.parameters.length; i++) {
let p = <ScriptTestParam> { name: this.parameters[i].name, type: this.parameters[i].type };
let p = new ScriptParam(this.parameters[i].name, this.parameters[i].type);
if (this.testParameters[i]) {
p.value = this.testParameters[i].value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class ScriptListComponent implements OnInit, AfterViewInit, OnDestroy {
this.loadScripts();
});
} else {
this.projectService.setScript(result, script).subscribe(result => {
this.projectService.setScript(result, script).subscribe(() => {
this.loadScripts();
});
}
Expand Down
3 changes: 3 additions & 0 deletions server/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var usersApi = require('./users');
var alarmsApi = require('./alarms');
var pluginsApi = require('./plugins');
var diagnoseApi = require('./diagnose');
var scriptsApi = require('./scripts');
var daqApi = require('./daq');

var apiApp;
Expand Down Expand Up @@ -46,6 +47,8 @@ function init(_server, _runtime) {
apiApp.use(diagnoseApi.app());
daqApi.init(runtime, authJwt.verifyToken, verifyGroups);
apiApp.use(daqApi.app());
scriptsApi.init(runtime, authJwt.verifyToken, verifyGroups);
apiApp.use(scriptsApi.app());

const limiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
Expand Down
26 changes: 13 additions & 13 deletions server/api/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ module.exports = {
* POST runscript
* Run script, can be call with script id or script content as test
*/
diagnoseApp.post("/api/runscript", secureFnc, function (req, res, next) {
scriptsApp.post("/api/runscript", secureFnc, function (req, res, next) {
var groups = checkGroupsFnc(req);
if (res.statusCode === 403) {
runtime.logger.error("api post runscript: Tocken Expired");
} else if (authJwt.adminGroups.indexOf(groups) === -1) {
res.status(401).json({ error: "unauthorized_error", message: "Unauthorized!" });
runtime.logger.error("api post runscript: Unauthorized");
} else {
// runtime.notificatorMgr.sendMail(req.body.params.msg, req.body.params.smtp).then(function () {
// res.end();
// }).catch(function (err) {
// if (err.code) {
// res.status(400).json({ error: err.code, message: err.message });
// runtime.logger.error("api post runscript: " + err.message);
// } else {
// res.status(400).json({ error: "unexpected_error", message: err.toString() });
// runtime.logger.error("api post runscript: " + err);
// }
// });
runtime.scriptsMgr.runScript(req.body.params.script).then(function () {
res.end();
}).catch(function (err) {
if (err.code) {
res.status(400).json({ error: err.code, message: err.message });
runtime.logger.error("api post runscript: " + err.message);
} else {
res.status(400).json({ error: "unexpected_error", message: err.toString() });
runtime.logger.error("api post runscript: " + err);
}
});
}
});

return diagnoseApp;
return scriptsApp;
}
}
4 changes: 4 additions & 0 deletions server/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var project = require('./project');
var users = require('./users');
var alarms = require('./alarms');
var notificator = require('./notificator');
var scripts = require('./scripts');
var plugins = require('./plugins');
var utils = require('./utils');
const daqstorage = require('./storage/daqstorage');
Expand All @@ -21,6 +22,7 @@ var logger;
var io;
var alarmsMgr;
var notificatorMgr;
var scriptsMgr;
var tagsSubscription = new Map();

function init(_io, _api, _settings, _log, eventsMain) {
Expand Down Expand Up @@ -66,6 +68,7 @@ function init(_io, _api, _settings, _log, eventsMain) {
});
alarmsMgr = alarms.create(runtime);
notificatorMgr = notificator.create(runtime);
scriptsMgr = scripts.create(runtime);
devices.init(runtime);

events.on('project-device:change', updateDevice);
Expand Down Expand Up @@ -422,6 +425,7 @@ var runtime = module.exports = {
get daqStorage() { return daqstorage },
get alarmsMgr() { return alarmsMgr },
get notificatorMgr() { return notificatorMgr },
get scriptsMgr() { return scriptsMgr },
events: events,

}
Loading

0 comments on commit 98101c6

Please sign in to comment.