/
scene-flows.ts
99 lines (77 loc) · 2.93 KB
/
scene-flows.ts
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
import {existsSync, readFileSync, writeFileSync} from 'fs';
import {join} from 'path';
import {EventEmitter} from 'events';
import * as NodeRED from 'node-red-interfaces';
export class SceneFlows extends EventEmitter implements NodeRED.IRuntimeFlowConfig {
static COMMON_DEFAULT = [
{ type: 'tab', id: 'tab.common', label: 'Common' }
];
private _logger: NodeRed.Scenes.ILogger;
private _flows: NodeRed.Scenes.HashTable<any>;
private _flowsPath: string;
private _timeout: NodeJS.Timer;
private _selected: string;
constructor(config: NodeRed.Scenes.IConfig, logger: NodeRed.Scenes.ILogger) {
super();
const self = this;
this._flows = {};
this._selected = config.initialFlow || 'default';
this._logger = logger;
this._flowsPath = join(config.userDir, 'flows.json');
if (existsSync(this._flowsPath)) {
logger.info('Loading saved flows from file: ' + this._flowsPath);
this._flows = require(this._flowsPath);
}
}
get current() : any {
return this._flows[this._selected];
}
getCurrentFlow() : any {
this._logger.debug('Getting flows for scene: ' + this._selected);
var f = this._flows[this._selected] || [
{ type: 'tab', id: 'tab.'+this._selected, label: this._selected },
{ type: 'current-scene', id: 'current-scene.'+this._selected, name:'', x:75, y:20, z:'tab.'+this._selected, wires:[]}
];
var common = this._flows['common'] || SceneFlows.COMMON_DEFAULT;
f = f.concat(common);
return f;
}
saveCurrentFlow(data: any) : void {
var current: any[] = [];
var common: any[] = [];
if (data.length > 0 && data[0].id !== 'tab.' + this._selected) {
throw new Error('Cannot deploy flow - scene has been changed. Please change scene to '+data[0].id+' then try again. (Scene is currently '+this._selected+')');
}
var commonTabs = data.filter(function (item: any) {
if (item.type !== 'tab') return false;
if (!item.label) return false;
var strStart = item.label.substring(0, 6);
return strStart === 'Common';
})
.map(function (item: any) {
return item.id;
});
data.forEach(function(item: any) {
if (
commonTabs.indexOf(item.id) >= 0 || // this item IS the tab
commonTabs.indexOf(item.z) >= 0 || // this item belongs in the tab
item.z === '' // shared config items have a z of "" - we need these in common
) {
common.push(item);
} else {
current.push(item);
}
});
data.scene = this._selected;
this._logger.info('Saving flows for scene: ' + this._selected);
this._flows[this._selected] = current;
this._flows['common'] = common;
writeFileSync(this._flowsPath, JSON.stringify(this._flows));
}
changeFlow(newId: string) {
this._selected = newId;
this._logger.debug('Changing flows to scene: ' + newId);
writeFileSync(this._flowsPath, JSON.stringify(this._flows));
this.emit('changed', newId);
}
}