-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdummy.module.ts
More file actions
147 lines (127 loc) · 5.13 KB
/
dummy.module.ts
File metadata and controls
147 lines (127 loc) · 5.13 KB
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
import * as PiStation from "../../node_modules/pistation-definitions/PiStation";
import {Observable} from 'rxjs/Rx';
import {Module} from "../../app/module";
import {Server} from "../../app/server";
import {StoreReadData} from "../../app/server";
export class Dummy extends Module {
static moduleId:string;
configuredLights: string[] = [];
constructor(private server:Server){
super('Dummy');
const storeReadStream = server.createModuleStoreReadStream(this);
storeReadStream.subscribe((data: StoreReadData) => {
console.log(`module config read ${data.key} = ${JSON.stringify(data.value, null, 4)}`);
});
storeReadStream.find((data: StoreReadData) => data.key == 'lights')
.subscribe((data: StoreReadData) => {
this.configuredLights = JSON.parse(data.value).configuredLights; //parse saved JSON
console.log('settings found', data.value);
this.addFunction(this.createDynamicLightFunction(JSON.parse(data.value).configuredLights)); //register on module
});
let addLight = new PiStation.Function('addLight', [
new PiStation.ArgumentTextbox({
key:'name',
value: 'Light 1',
label:'Light name'
})
]);
//this.addFunction(rainbowFunction); //register on module
this.addFunction(addLight); //register on module
}
private createDynamicLightFunction(lights : string[]) {
let dummyFunction = new PiStation.Function('powerControl', [
new PiStation.ArgumentTextbox({
value: lights[0],
key: 'power',
label: 'Power',
required: true,
}),
new PiStation.ArgumentMultiple({
key: 'light',
label: 'Light',
options: [{key: 'a', value: 'Jorrit'}],
required: true,
}),
]);
return dummyFunction;
};
powerControl(args : PiStation.Argument<any>[]){
console.log(`Called Dummy Function with arguments`, args);
const dummyFunctionUpdates = Observable //dummy update stream from connector
.interval(500)
.timeInterval()
.take(20);
return dummyFunctionUpdates;
}
addLight(args : {name: string}){
console.log('add light to the store', args);
this.configuredLights.push(args.name);
this.server.getModuleStore(this)
.put('lights', JSON.stringify({configuredLights: this.configuredLights}));
this.addFunction(this.createDynamicLightFunction([args.name]));
}
//rainbowLights(args : any){
//
// const colors = [[62,35,255],
// [60,255,60],
// [255,35,98],
// [45,175,230],
// [255,0,255],
// [255,128,0]];
//
// let step = 0;
// //color table indices for:
// // current color left
// // next color left
// // current color right
// // next color right
// let colorIndices = [0,1,2,3];
//
// //transition speed
// let gradientSpeed = 0.02;
//
// function createGradient()
// {
// var c0_0 = colors[colorIndices[0]];
// var c0_1 = colors[colorIndices[1]];
// var c1_0 = colors[colorIndices[2]];
// var c1_1 = colors[colorIndices[3]];
//
// var istep = 1 - step;
// var r1 = Math.round(istep * c0_0[0] + step * c0_1[0]);
// var g1 = Math.round(istep * c0_0[1] + step * c0_1[1]);
// var b1 = Math.round(istep * c0_0[2] + step * c0_1[2]);
// var color1 = "rgb("+r1+","+g1+","+b1+")";
//
// var r2 = Math.round(istep * c1_0[0] + step * c1_1[0]);
// var g2 = Math.round(istep * c1_0[1] + step * c1_1[1]);
// var b2 = Math.round(istep * c1_0[2] + step * c1_1[2]);
// var color2 = "rgb("+r2+","+g2+","+b2+")";
//
// const bgobj = {background:`-webkit-gradient(linear, left top, right top, from(${color1}), to(${color2}))`};
// step += gradientSpeed;
// if ( step >= 1 )
// {
// step %= 1;
// colorIndices[0] = colorIndices[1];
// colorIndices[2] = colorIndices[3];
//
// //pick two new target color indices
// //do not pick the same as the current one
// colorIndices[1] = ( colorIndices[1] + Math.floor( 1 + Math.random() * (colors.length - 1))) % colors.length;
// colorIndices[3] = ( colorIndices[3] + Math.floor( 1 + Math.random() * (colors.length - 1))) % colors.length;
//
// }
// return bgobj;
// }
//
// return Observable
// .interval(100)
// .takeUntil(Observable.interval(10000))
// .map(()=>createGradient())
// .map(gradient => `<div style="background: ${gradient.background}; height: 50px; width: ${args.width}; display: block;">test</div>`)
// .map((html) => {
// return {'value': html}
// });
//}
}