Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit 7966552

Browse files
authored
Merge pull request #1166 from SteffoSpieler/feat/add-pishock-service
Add PiShock Service
2 parents 9946634 + fb872b8 commit 7966552

File tree

12 files changed

+264
-10
lines changed

12 files changed

+264
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ nodecg-io is the successor of [ChatOverflow](https://github.com/codeoverflow-org
3939
- OBS
4040
- [OpenTTS](https://github.com/synesthesiam/opentts)
4141
- Philips Hue
42+
- [PiShock](https://pishock.com)
4243
- RCON
4344
- Reddit
4445
- sACN Receiver

package-lock.json

Lines changed: 49 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/pishock/extension/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import NodeCG from "@nodecg/types";
2+
import { PiShockClient } from "nodecg-io-pishock";
3+
import { requireService } from "nodecg-io-core";
4+
5+
module.exports = function (nodecg: NodeCG.ServerAPI) {
6+
nodecg.log.info("Sample bundle for the PiShock service started.");
7+
8+
const pishock = requireService<PiShockClient>(nodecg, "pishock");
9+
10+
pishock?.onAvailable((client) => {
11+
nodecg.log.info("PiShock client has been updated, printing all device's infos");
12+
client.connectedDevices.forEach(async (device) => {
13+
const info = await device.getInfo();
14+
nodecg.log.info(
15+
`Client ID: ${info.clientId}, ID: ${info.id}, Name: ${info.name}, Paused: ${info.paused}, ` +
16+
`MaxIntensity: ${info.maxIntensity}, MaxDuration: ${info.maxDuration}, Online: ${info.online}`,
17+
);
18+
});
19+
});
20+
21+
pishock?.onUnavailable(() => {
22+
nodecg.log.info("PiShock service unavailable.");
23+
});
24+
};

samples/pishock/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "pishock",
3+
"version": "0.3.0",
4+
"private": true,
5+
"nodecg": {
6+
"compatibleRange": ">=1.1.1 <3.0.0",
7+
"bundleDependencies": {
8+
"nodecg-io-pishock": "^0.3.0"
9+
}
10+
},
11+
"license": "MIT",
12+
"dependencies": {
13+
"@types/node": "^20.12.2",
14+
"@nodecg/types": "^2.1.12",
15+
"nodecg-io-core": "^0.3.0",
16+
"nodecg-io-pishock": "^0.3.0",
17+
"typescript": "^5.4.3",
18+
"nodecg-io-tsconfig": "^1.0.0"
19+
}
20+
}

samples/pishock/tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "nodecg-io-tsconfig",
3+
"references": [
4+
{
5+
"path": "../../nodecg-io-core"
6+
},
7+
{
8+
"path": "../../services/nodecg-io-template"
9+
}
10+
]
11+
}

services/nodecg-io-gametts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"contributors": [
1111
{
1212
"name": "SteffoSpieler",
13-
"url": "https://about.steffospieler.de"
13+
"url": "https://steffo.dev"
1414
}
1515
],
1616
"repository": {

services/nodecg-io-google-cast/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"contributors": [
1111
{
1212
"name": "SteffoSpieler",
13-
"url": "https://about.steffospieler.de"
13+
"url": "https://steffo.dev"
1414
}
1515
],
1616
"repository": {

services/nodecg-io-opentts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"contributors": [
1111
{
1212
"name": "SteffoSpieler",
13-
"url": "https://about.steffospieler.de"
13+
"url": "https://steffo.dev"
1414
}
1515
],
1616
"repository": {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import NodeCG from "@nodecg/types";
2+
import { Result, emptySuccess, success, ServiceBundle, Logger, error } from "nodecg-io-core";
3+
import { PiShockDevice, PiShockAuthentication } from "pishock-ts";
4+
5+
export interface PiShockConfig {
6+
authentications: Array<PiShockAuthentication>;
7+
}
8+
9+
export interface PiShockClient {
10+
connectedDevices: Array<PiShockDevice>;
11+
}
12+
13+
module.exports = (nodecg: NodeCG.ServerAPI) => {
14+
new PiShockService(nodecg, "pishock", __dirname, "../schema.json").register();
15+
};
16+
17+
class PiShockService extends ServiceBundle<PiShockConfig, PiShockClient> {
18+
async validateConfig(config: PiShockConfig): Promise<Result<void>> {
19+
for (const deviceConfig of config.authentications) {
20+
if (!/[0-9a-f-]+/.test(deviceConfig.apiKey)) {
21+
return error(`Invalid PiShock apiKey format: ${deviceConfig.apiKey}`);
22+
}
23+
24+
if (!/[0-9A-Z]+/.test(deviceConfig.code)) {
25+
return error(`Invalid PiShock code format: ${deviceConfig.code}`);
26+
}
27+
}
28+
29+
return emptySuccess();
30+
}
31+
32+
async createClient(config: PiShockConfig, logger: Logger): Promise<Result<PiShockClient>> {
33+
const devices = config.authentications.map((c) => {
34+
// Set name if missing.
35+
c.name ??= "nodecg-io PiShock Service";
36+
return new PiShockDevice(c);
37+
});
38+
39+
// Test connection and return error if any provided auth details fail to do the request.
40+
const connectionStates = await Promise.all(
41+
devices.map(async (dev) => {
42+
try {
43+
await dev.getInfo();
44+
return true;
45+
} catch (err) {
46+
return err;
47+
}
48+
}),
49+
);
50+
51+
for (const state of connectionStates) {
52+
if (state instanceof Error) {
53+
return error(`Failed to connect to PiShock api: ${state.message}`);
54+
}
55+
}
56+
57+
const client = { connectedDevices: devices };
58+
logger.info("Successfully created PiShock client.");
59+
return success(client);
60+
}
61+
62+
stopClient(_: PiShockClient, _logger: Logger): void {
63+
// Stateless REST API, cannot be stopped
64+
}
65+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "nodecg-io-pishock",
3+
"version": "0.3.0",
4+
"description": "Allows using the PiShock api.",
5+
"homepage": "https://nodecg.io/RELEASE/samples/pishock",
6+
"author": {
7+
"name": "CodeOverflow team",
8+
"url": "https://github.com/codeoverflow-org"
9+
},
10+
"contributors": [
11+
{
12+
"name": "SteffoSpieler",
13+
"url": "https://steffo.dev"
14+
}
15+
],
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/codeoverflow-org/nodecg-io.git",
19+
"directory": "services/nodecg-io-pishock"
20+
},
21+
"files": [
22+
"**/*.js",
23+
"**/*.js.map",
24+
"**/*.d.ts",
25+
"*.json"
26+
],
27+
"main": "extension/index",
28+
"keywords": [
29+
"nodecg-io",
30+
"nodecg-bundle",
31+
"pishock"
32+
],
33+
"nodecg": {
34+
"compatibleRange": ">=1.1.1 <3.0.0",
35+
"bundleDependencies": {
36+
"nodecg-io-core": "^0.3.0"
37+
}
38+
},
39+
"license": "MIT",
40+
"devDependencies": {
41+
"@types/node": "^20.12.2",
42+
"@nodecg/types": "^2.1.12",
43+
"typescript": "^5.4.3",
44+
"nodecg-io-tsconfig": "^1.0.0"
45+
},
46+
"dependencies": {
47+
"nodecg-io-core": "^0.3.0",
48+
"pishock-ts": "^1.0.1"
49+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"properties": {
6+
"authentications": {
7+
"type": "array",
8+
"items": {
9+
"type": "object",
10+
"required": ["username", "apiKey", "code"],
11+
"properties": {
12+
"username": {
13+
"type": "string",
14+
"description": "Username you use to log into PiShock.com. Can be found in the Account section of the website."
15+
},
16+
"apiKey": {
17+
"type": "string",
18+
"description": "API Key generated on PiShock.com. Can be found in the Account section of the website."
19+
},
20+
"code": {
21+
"type": "string",
22+
"description": "Sharecode generated on PiShock.com. Limitations can be set when generating the code."
23+
},
24+
"name": {
25+
"type": "string",
26+
"description": "Name of what sent the commands. This will show up in the PiShock logs on the website."
27+
}
28+
}
29+
}
30+
}
31+
},
32+
"required": ["authentications"]
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "nodecg-io-tsconfig",
3+
"references": [
4+
{
5+
"path": "../../nodecg-io-core"
6+
}
7+
]
8+
}

0 commit comments

Comments
 (0)