Skip to content
Snippets Groups Projects
Commit 3839dd57 authored by Ondřej Trojan's avatar Ondřej Trojan
Browse files

control vlc working

parent 2eb5f1ca
No related branches found
No related tags found
No related merge requests found
package com.museum.projection.controller;
import com.museum.projection.models.ControlEntity;
import com.museum.projection.service.ControlService;
import com.museum.projection.service.ResponseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
......@@ -20,6 +20,8 @@ public class MainController {
@Resource()
public ResponseService responseService;
@Resource
public ControlService controlService;
@GetMapping()
......@@ -35,6 +37,31 @@ public class MainController {
}
@PostMapping(value = "/play", produces = "application/json")
public ResponseEntity play() {
controlService.play();
return new ResponseEntity<>(
"Video set to play", HttpStatus.OK);
}
@PostMapping(value = "/add", consumes = "application/json", produces = "application/json")
public ResponseEntity add(@RequestBody ControlEntity model) {
System.out.println(model.videoName);
controlService.add(model.videoName);
return new ResponseEntity<>(
"Video "+model.videoName+" set to play", HttpStatus.OK);
}
@PostMapping(value = "/pause", produces = "application/json")
public ResponseEntity pause() {
controlService.pause();
return new ResponseEntity<>(
"Video set to pause", HttpStatus.OK);
}
......
package com.museum.projection.models;
import com.fasterxml.jackson.annotation.JsonProperty;
public class ControlEntity {
@JsonProperty("video")
public String videoName;
}
package com.museum.projection.service;
import com.museum.projection.util.VlcClient;
import org.springframework.stereotype.Service;
@Service
public class ControlService {
VlcClient client1;
VlcClient client2;
private final String IP = "127.0.0.1";
private final int PORT1 = 5041;
private final int PORT2 = 5031;
private final String PATH_TO_VID = "/home/user/vid/";
public ControlService() {
client1 = new VlcClient(IP, PORT1);
client2 = new VlcClient(IP, PORT2);
}
public void play() {
client1.sendMessage("play");
client2.sendMessage("play");
}
public void add(String videoName) {
client1.sendMessage("add " + PATH_TO_VID + videoName);
client2.sendMessage("add " + PATH_TO_VID + videoName);
}
public void pause() {
client1.sendMessage("pause");
client2.sendMessage("pause");
}
public void stop() {
client1.sendMessage("stop");
client2.sendMessage("stop");
}
public void clear() {
client1.sendMessage("clear");
client2.sendMessage("clear");
}
public void logout1() {
client1.sendMessage("logout");
client1.close();
}
public void logout2() {
client2.sendMessage("logout");
client2.close();
}
public void logoutAll(){
logout1();
logout2();
}
}
package com.museum.projection.util;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class VlcClient {
private Socket socket;
private PrintWriter out;
private BufferedReader in;
public VlcClient(String address, int port) {
try {
socket = new Socket(address, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
//TODO test if connection is ready
// String resp = null;
// try {
// resp = in.readLine();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
public String sendMessage(String msg) {
out.println(msg+'\n');
String resp = null;
try {
resp = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return resp;
}
public void close() {
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
$(document).ready(function() {
//
//
var playBtn = document.getElementById('play');
var pauseBtn = document.getElementById('pause');
var addBtn = document.getElementById('add');
var URL = "localhost:8080/api/click"
const sendHttpRequest = (method, url, data) => {
const promise = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method,url)
// xhr.responseType = 'json';
console.log("data "+ data)
if(data){
xhr.setRequestHeader('Content-Type', 'application/json');
}
xhr.onload = () => {
if(xhr.status > 400){
reject(xhr.response)
} else {
resolve(xhr.response)
}
};
xhr.onerror = () => {
reject('Something went wrong check the backend log')
};
xhr.send(JSON.stringify(data));
});
return promise;
}
// sendHttpRequest('GET', 'http://localhost:8080/api/').then(responseData =>{
// console.log(responseData)
// })
const controlAdd = () => {
sendHttpRequest('POST', 'http://localhost:8080/control/add', {
video: '1.mp4'
}).then(responseData =>{
console.log(responseData);
}).catch(err => {
console.log(err);
})
}
const controlPlay = () => {
sendHttpRequest('POST', 'http://localhost:8080/control/play').then(responseData =>{
console.log(responseData);
}).catch(err => {
console.log(err);
})
}
const controlPause = () => {
sendHttpRequest('POST', 'http://localhost:8080/control/pause').then(responseData =>{
console.log(responseData);
}).catch(err => {
console.log(err);
})
}
playBtn.addEventListener('click', controlPlay)
pauseBtn.addEventListener('click', controlPause)
addBtn.addEventListener('click', controlAdd)
$("button").click(function() {
console.log('clicked')
$.post(URL,{
name: "Donald Duck",
city: "Duckburg"
},function(data, status){
alert("Data: " + data + "\nStatus: " + status);
})});
});
......@@ -17,6 +17,8 @@
<button id="play" type="button" class="btn btn-primary">Play</button>
<button id="pause" type="button" class="btn btn-primary">Pause</button>
<button id="add" type="button" class="btn btn-primary">Add 1.mp4</button>
</div>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment