|
| 1 | +begin; |
| 2 | + |
| 3 | +-- Create the api schema |
| 4 | +-- No need, this is created in 00-init_postgrest.sql |
| 5 | +-- create schema api; |
| 6 | + |
| 7 | +comment on schema api is 'Screen Pilot API'; |
| 8 | + |
| 9 | +-- Create the asset table |
| 10 | +create table api.asset ( |
| 11 | + id bigint generated always as identity primary key, |
| 12 | + created_at timestamp not null default now(), |
| 13 | + filename text unique not null, |
| 14 | + mime text not null, |
| 15 | + size int not null |
| 16 | +); |
| 17 | + |
| 18 | +-- Create the playlist table |
| 19 | +create table api.playlist ( |
| 20 | + id bigint generated always as identity primary key, |
| 21 | + created_at timestamp not null default now(), |
| 22 | + name text not null |
| 23 | +); |
| 24 | + |
| 25 | +-- Create the playlist_assets table |
| 26 | +create table api.playlist_asset ( |
| 27 | + id bigint generated always as identity primary key, |
| 28 | + playlist_id bigint references api.playlist (id) not null, |
| 29 | + asset_id bigint references api.asset (id) not null, |
| 30 | + /* "The join table determines many-to-many relationships. It must contain |
| 31 | + foreign keys to other two tables and they must be part of its composite key." |
| 32 | + https://docs.postgrest.org/en/v12/references/api/resource_embedding.html#many-to-many-relationships |
| 33 | + */ |
| 34 | + primary key (id, playlist_id, asset_id) |
| 35 | +); |
| 36 | + |
| 37 | +-- Send an MPV command |
| 38 | +/* |
| 39 | +create function api.mpv_command(routing_key text, command text []) returns void |
| 40 | +language plpgsql as $$ |
| 41 | +begin |
| 42 | + perform amqp.publish(1, 'amq.topic', routing_key, json_build_object('event', 'MPVCommand', 'command', command)::text); |
| 43 | +end; |
| 44 | +$$; |
| 45 | +
|
| 46 | +comment on function api.mpv_command(text, text []) |
| 47 | +is 'Send an MPV command. Currently unused.'; |
| 48 | +*/ |
| 49 | + |
| 50 | +-- Select playlist files from db and play |
| 51 | +create function api.play_playlist_id( |
| 52 | + routing_key text, |
| 53 | + p_id int, |
| 54 | + index int |
| 55 | +) returns void language plpgsql as $$ |
| 56 | +declare |
| 57 | + files text[]; |
| 58 | + playlist_name text; |
| 59 | + json text; |
| 60 | +begin |
| 61 | + select name into playlist_name |
| 62 | + from api.playlist |
| 63 | + where id = p_id; |
| 64 | + |
| 65 | + select array_agg(filename) into files |
| 66 | + from api.playlist_asset |
| 67 | + left join asset on playlist_asset.id = asset.id |
| 68 | + where playlist_asset.playlist_id = p_id; |
| 69 | + |
| 70 | + perform amqp.publish(1, 'amq.topic', routing_key, json_build_object('event', 'LoadList', 'files', files)::text); |
| 71 | + perform amqp.publish(1, 'amq.topic', routing_key, json_build_object('event', 'MPVCommand', 'command', array["playlist-play-index", index])::text); |
| 72 | +end; |
| 73 | +$$; |
| 74 | + |
| 75 | +comment on function api.play_playlist_id(text, int, int) is |
| 76 | +'Select playlist files from the database and play them. |
| 77 | +
|
| 78 | +Parameters: |
| 79 | + - routing_key: The routing key of the screen |
| 80 | + - p_id: Playlist id |
| 81 | + - index: Index to begin playback from.'; |
| 82 | + |
| 83 | +-- Load files in a known playlist text file and play |
| 84 | +create function api.play_playlist_file( |
| 85 | + routing_key text, |
| 86 | + playlist_file text, |
| 87 | + index int |
| 88 | +) returns void language plpgsql as $$ |
| 89 | +begin |
| 90 | + perform amqp.publish(1, 'amq.topic', routing_key, json_build_object('event', 'MPVCommand', 'command', array["loadlist", playlist_file])::text); |
| 91 | + perform amqp.publish(1, 'amq.topic', routing_key, json_build_object('event', 'MPVCommand', 'command', array["playlist-play-index", index])::text); |
| 92 | +end; |
| 93 | +$$; |
| 94 | + |
| 95 | +comment on function api.play_playlist_file(text, text, int) is |
| 96 | +'Load files in a known playlist text file and play. |
| 97 | +
|
| 98 | +Parameters: |
| 99 | + - routing_key: The routing key of the screen |
| 100 | + - playlist_file: Contents of the playlist file. |
| 101 | + - index: Index to begin playback from.'; |
| 102 | + |
| 103 | +-- Write playlist text file and play |
| 104 | +create function api.play_files( |
| 105 | + routing_key text, |
| 106 | + files text [], |
| 107 | + index int |
| 108 | +) returns void language plpgsql as $$ |
| 109 | +begin |
| 110 | + perform amqp.publish(1, 'amq.topic', routing_key, json_build_object('event', 'LoadList', 'files', files)::text); |
| 111 | + perform amqp.publish(1, 'amq.topic', routing_key, json_build_object('event', 'MPVCommand', 'command', array['playlist-play-index', index::text])::text); |
| 112 | +end; |
| 113 | +$$; |
| 114 | + |
| 115 | +comment on function api.play_files(text, text [], int) is |
| 116 | +'Write playlist text file and play. |
| 117 | +
|
| 118 | +Tells the consumer write a list of files to a text file, then load that |
| 119 | +playlist text file. |
| 120 | +
|
| 121 | +Parameters: |
| 122 | + - routing_key: The routing key of the screen |
| 123 | + - files: Contents of the playlist file. |
| 124 | + - index: Index to begin playback from.'; |
| 125 | + |
| 126 | +commit; |
0 commit comments