-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1166 from SteffoSpieler/feat/add-pishock-service
Add PiShock Service
- Loading branch information
Showing
12 changed files
with
264 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import NodeCG from "@nodecg/types"; | ||
import { PiShockClient } from "nodecg-io-pishock"; | ||
import { requireService } from "nodecg-io-core"; | ||
|
||
module.exports = function (nodecg: NodeCG.ServerAPI) { | ||
nodecg.log.info("Sample bundle for the PiShock service started."); | ||
|
||
const pishock = requireService<PiShockClient>(nodecg, "pishock"); | ||
|
||
pishock?.onAvailable((client) => { | ||
nodecg.log.info("PiShock client has been updated, printing all device's infos"); | ||
client.connectedDevices.forEach(async (device) => { | ||
const info = await device.getInfo(); | ||
nodecg.log.info( | ||
`Client ID: ${info.clientId}, ID: ${info.id}, Name: ${info.name}, Paused: ${info.paused}, ` + | ||
`MaxIntensity: ${info.maxIntensity}, MaxDuration: ${info.maxDuration}, Online: ${info.online}`, | ||
); | ||
}); | ||
}); | ||
|
||
pishock?.onUnavailable(() => { | ||
nodecg.log.info("PiShock service unavailable."); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "pishock", | ||
"version": "0.3.0", | ||
"private": true, | ||
"nodecg": { | ||
"compatibleRange": ">=1.1.1 <3.0.0", | ||
"bundleDependencies": { | ||
"nodecg-io-pishock": "^0.3.0" | ||
} | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@types/node": "^20.12.2", | ||
"@nodecg/types": "^2.1.12", | ||
"nodecg-io-core": "^0.3.0", | ||
"nodecg-io-pishock": "^0.3.0", | ||
"typescript": "^5.4.3", | ||
"nodecg-io-tsconfig": "^1.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "nodecg-io-tsconfig", | ||
"references": [ | ||
{ | ||
"path": "../../nodecg-io-core" | ||
}, | ||
{ | ||
"path": "../../services/nodecg-io-template" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import NodeCG from "@nodecg/types"; | ||
import { Result, emptySuccess, success, ServiceBundle, Logger, error } from "nodecg-io-core"; | ||
import { PiShockDevice, PiShockAuthentication } from "pishock-ts"; | ||
|
||
export interface PiShockConfig { | ||
authentications: Array<PiShockAuthentication>; | ||
} | ||
|
||
export interface PiShockClient { | ||
connectedDevices: Array<PiShockDevice>; | ||
} | ||
|
||
module.exports = (nodecg: NodeCG.ServerAPI) => { | ||
new PiShockService(nodecg, "pishock", __dirname, "../schema.json").register(); | ||
}; | ||
|
||
class PiShockService extends ServiceBundle<PiShockConfig, PiShockClient> { | ||
async validateConfig(config: PiShockConfig): Promise<Result<void>> { | ||
for (const deviceConfig of config.authentications) { | ||
if (!/[0-9a-f-]+/.test(deviceConfig.apiKey)) { | ||
return error(`Invalid PiShock apiKey format: ${deviceConfig.apiKey}`); | ||
} | ||
|
||
if (!/[0-9A-Z]+/.test(deviceConfig.code)) { | ||
return error(`Invalid PiShock code format: ${deviceConfig.code}`); | ||
} | ||
} | ||
|
||
return emptySuccess(); | ||
} | ||
|
||
async createClient(config: PiShockConfig, logger: Logger): Promise<Result<PiShockClient>> { | ||
const devices = config.authentications.map((c) => { | ||
// Set name if missing. | ||
c.name ??= "nodecg-io PiShock Service"; | ||
return new PiShockDevice(c); | ||
}); | ||
|
||
// Test connection and return error if any provided auth details fail to do the request. | ||
const connectionStates = await Promise.all( | ||
devices.map(async (dev) => { | ||
try { | ||
await dev.getInfo(); | ||
return true; | ||
} catch (err) { | ||
return err; | ||
} | ||
}), | ||
); | ||
|
||
for (const state of connectionStates) { | ||
if (state instanceof Error) { | ||
return error(`Failed to connect to PiShock api: ${state.message}`); | ||
} | ||
} | ||
|
||
const client = { connectedDevices: devices }; | ||
logger.info("Successfully created PiShock client."); | ||
return success(client); | ||
} | ||
|
||
stopClient(_: PiShockClient, _logger: Logger): void { | ||
// Stateless REST API, cannot be stopped | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "nodecg-io-pishock", | ||
"version": "0.3.0", | ||
"description": "Allows using the PiShock api.", | ||
"homepage": "https://nodecg.io/RELEASE/samples/pishock", | ||
"author": { | ||
"name": "CodeOverflow team", | ||
"url": "https://github.com/codeoverflow-org" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "SteffoSpieler", | ||
"url": "https://steffo.dev" | ||
} | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/codeoverflow-org/nodecg-io.git", | ||
"directory": "services/nodecg-io-pishock" | ||
}, | ||
"files": [ | ||
"**/*.js", | ||
"**/*.js.map", | ||
"**/*.d.ts", | ||
"*.json" | ||
], | ||
"main": "extension/index", | ||
"keywords": [ | ||
"nodecg-io", | ||
"nodecg-bundle", | ||
"pishock" | ||
], | ||
"nodecg": { | ||
"compatibleRange": ">=1.1.1 <3.0.0", | ||
"bundleDependencies": { | ||
"nodecg-io-core": "^0.3.0" | ||
} | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^20.12.2", | ||
"@nodecg/types": "^2.1.12", | ||
"typescript": "^5.4.3", | ||
"nodecg-io-tsconfig": "^1.0.0" | ||
}, | ||
"dependencies": { | ||
"nodecg-io-core": "^0.3.0", | ||
"pishock-ts": "^1.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"authentications": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"required": ["username", "apiKey", "code"], | ||
"properties": { | ||
"username": { | ||
"type": "string", | ||
"description": "Username you use to log into PiShock.com. Can be found in the Account section of the website." | ||
}, | ||
"apiKey": { | ||
"type": "string", | ||
"description": "API Key generated on PiShock.com. Can be found in the Account section of the website." | ||
}, | ||
"code": { | ||
"type": "string", | ||
"description": "Sharecode generated on PiShock.com. Limitations can be set when generating the code." | ||
}, | ||
"name": { | ||
"type": "string", | ||
"description": "Name of what sent the commands. This will show up in the PiShock logs on the website." | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["authentications"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "nodecg-io-tsconfig", | ||
"references": [ | ||
{ | ||
"path": "../../nodecg-io-core" | ||
} | ||
] | ||
} |