-
Notifications
You must be signed in to change notification settings - Fork 0
/
harmony-universe.ts
145 lines (137 loc) · 5.08 KB
/
harmony-universe.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
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
/**
* Module for obvserving multiple harmonies in the network and
* combines [[HarmonyDiscover]] and [[HarmonyHub]
*
* usage is the scanning of a network with udp broadcasts
*
* @module HarmonyUniverse
*/
import * as Debug from 'debug';
/**
*
*/
const debug = Debug('rxharmony:universe');
import {
Observable,
using,
OperatorFunction,
never,
} from 'rxjs';
import { switchMap, mergeMap, map, groupBy, filter } from 'rxjs/operators';
import { HarmonyHub, ActivityStatus, IHubEvent } from './harmony-hub.class';
import {
HarmonyDiscover,
IDiscoverDigest,
IDiscoverOptions,
HarmonyHubState,
} from './harmony-discover.class';
export interface IHarmonySimpleStatus {
label: string;
active: boolean;
friendlyName: string;
}
export namespace HarmonyOperators {
export function humanizeStatus(): OperatorFunction<
IHubEvent,
IHarmonySimpleStatus
> {
return (
source: Observable<IHubEvent>
): Observable<IHarmonySimpleStatus> => {
return source.pipe(
filter(
event =>
!(
(event.activityStatus === ActivityStatus.Off &&
event.runningActivityList) ||
(event.activityStatus === ActivityStatus.Running &&
event.runningActivityListLabel ===
event.activityLabel)
)
),
map((event: IHubEvent) => {
const r = {
active: true,
label: '',
friendlyName: event.friendlyName,
};
switch (event.activityStatus) {
case ActivityStatus.Off:
r.active = true;
r.label = event.activityLabel;
break;
case ActivityStatus.Starting:
r.active = false;
r.label =
event.runningActivityListLabel || 'PowerOff';
break;
case ActivityStatus.Running:
r.active = true;
r.label = event.activityLabel;
break;
case ActivityStatus.Stopping:
r.active = false;
r.label = event.activityLabel;
}
return r;
})
);
};
}
export function observeDiscoveredHubs(
on?: (info: IDiscoverDigest, hub: HarmonyHub) => void
): OperatorFunction<IDiscoverDigest, IHubEvent> {
return (source: Observable<IDiscoverDigest>): Observable<IHubEvent> => {
return source.pipe(
groupBy(info => info.ip),
mergeMap((infoByIp: Observable<IDiscoverDigest>) => {
return infoByIp.pipe(
switchMap((info: IDiscoverDigest) => {
debug(
'Detecting HarmonyHub %s(%s) is %s',
info.friendlyName,
info.ip,
info.state
);
if (info.state === HarmonyHubState.Online) {
return using(
() => {
const hub = HarmonyHub.create(
'xmpp://' + info.ip
);
if (on) {
on(info, hub);
}
return hub;
},
(hub: HarmonyHub) => {
return hub.observe().pipe(
map(event => {
event.friendlyName =
info.friendlyName;
return event;
})
);
}
);
} else {
if (on) {
on(info, null);
}
return never();
}
})
);
})
);
};
}
}
export function observeAll(
on?: (info: IDiscoverDigest, hub: HarmonyHub) => void,
opts?: IDiscoverOptions
): Observable<IHubEvent> {
return HarmonyDiscover.observe(opts).pipe(
HarmonyOperators.observeDiscoveredHubs(on)
);
}