get channels api
This commit is contained in:
parent
358f9c8784
commit
a74b90c6be
48
src/http.ts
48
src/http.ts
@ -8,15 +8,42 @@ export default class HttpServer {
|
|||||||
private httpServer: http.Server;
|
private httpServer: http.Server;
|
||||||
private port: number;
|
private port: number;
|
||||||
private root: string;
|
private root: string;
|
||||||
public constructor(port: number, root: string, tune: (ch: string, adp?: number) => void) {
|
public constructor(port: number, root: string, tune: (ch: string, adp?: number) => void, getChannels: ()=>string[]) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.root = root;
|
this.root = root;
|
||||||
this.httpServer = http.createServer((req, res) => {
|
this.httpServer = http.createServer((req, res) => {
|
||||||
let status: number = 404;
|
let status: number = 404;
|
||||||
let body: any;
|
let body: any = "";
|
||||||
const url = new URL(req.url, `http://${req.headers.host}`);
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
||||||
const pathname = path.normalize(url.pathname);
|
const pathname = path.normalize(url.pathname);
|
||||||
if (req.method === 'GET') {
|
|
||||||
|
if (pathname.startsWith('/api/')) {
|
||||||
|
const query = pathname.split('/');
|
||||||
|
const api = query[2];
|
||||||
|
switch (req.method) {
|
||||||
|
case "GET":
|
||||||
|
switch (api) {
|
||||||
|
case "list":
|
||||||
|
body = JSON.stringify(getChannels());
|
||||||
|
status = 200;
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "PUT":
|
||||||
|
switch (api) {
|
||||||
|
case "tune":
|
||||||
|
const channel = query[3];
|
||||||
|
const adapter = parseInt(url.searchParams.get('adapter'));
|
||||||
|
tune(channel, adapter);
|
||||||
|
status = 202;
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (req.method === 'GET') {
|
||||||
const filePath = path.join(root, path.extname(pathname) === '' ? pathname + "/index.html" : pathname);
|
const filePath = path.join(root, path.extname(pathname) === '' ? pathname + "/index.html" : pathname);
|
||||||
try {
|
try {
|
||||||
body = fs.readFileSync(filePath);
|
body = fs.readFileSync(filePath);
|
||||||
@ -25,21 +52,6 @@ export default class HttpServer {
|
|||||||
catch (err) {
|
catch (err) {
|
||||||
body = "Invalid File"
|
body = "Invalid File"
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
else if (req.method === 'PUT' && pathname.startsWith('/api/')) {
|
|
||||||
const query = pathname.split('/');
|
|
||||||
const api = query[2];
|
|
||||||
switch (api) {
|
|
||||||
case "channel":
|
|
||||||
const channel = query[3];
|
|
||||||
const adapter = parseInt(url.searchParams.get('adapter'));
|
|
||||||
tune(channel, adapter);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
body = "Invalid API Endpoint"
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
body = "Invalid Request"
|
body = "Invalid Request"
|
||||||
|
@ -22,7 +22,10 @@ const tune = (reqChannel: string, reqAdapter?: number) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT, tune);
|
const getChannels = () =>
|
||||||
|
zap.getChannels();
|
||||||
|
|
||||||
|
const httpServer = new HttpServer(HTTP_PORT, STATIC_ROOT, tune, getChannels);
|
||||||
const tvWebSocket = new TVWebSocket(WS_PORT);
|
const tvWebSocket = new TVWebSocket(WS_PORT);
|
||||||
httpServer.start();
|
httpServer.start();
|
||||||
|
|
||||||
|
12
src/zap.ts
12
src/zap.ts
@ -33,7 +33,11 @@ export default class Zap {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private nextChannel(channel: string) {
|
public getChannels(): string[] {
|
||||||
|
return this.channelNameList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private nextChannel(channel: string) :string {
|
||||||
const size = this.channelNameList.length;
|
const size = this.channelNameList.length;
|
||||||
const currentIndex = this.channelNameList.indexOf(channel);
|
const currentIndex = this.channelNameList.indexOf(channel);
|
||||||
if (currentIndex >= 0) {
|
if (currentIndex >= 0) {
|
||||||
@ -44,11 +48,11 @@ export default class Zap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private mod(n: number, m: number) {
|
private mod(n: number, m: number) :number{
|
||||||
return ((n % m) + m) % m;
|
return ((n % m) + m) % m;
|
||||||
}
|
}
|
||||||
|
|
||||||
private previousChannel(channel: string) {
|
private previousChannel(channel: string):string {
|
||||||
const size = this.channelNameList.length;
|
const size = this.channelNameList.length;
|
||||||
const currentIndex = this.channelNameList.indexOf(channel);
|
const currentIndex = this.channelNameList.indexOf(channel);
|
||||||
if (currentIndex >= 0) {
|
if (currentIndex >= 0) {
|
||||||
@ -65,7 +69,7 @@ export default class Zap {
|
|||||||
proc.once('exit', () =>
|
proc.once('exit', () =>
|
||||||
resolve()
|
resolve()
|
||||||
)
|
)
|
||||||
proc.on("error", (err:Error) =>
|
proc.on("error", (err: Error) =>
|
||||||
reject(err)
|
reject(err)
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user