11
11
import one .microproject .rpi .camera .client .CameraClient ;
12
12
import one .microproject .rpi .camera .client .ClientException ;
13
13
import one .microproject .rpi .camera .client .dto .CameraConfiguration ;
14
- import one .microproject .rpi .camera .client .dto .CameraSelectRequest ;
14
+ import one .microproject .rpi .camera .client .dto .CameraSelect ;
15
15
import one .microproject .rpi .camera .client .dto .ImageCapture ;
16
16
import one .microproject .rpi .camera .client .dto .CameraInfo ;
17
+ import one .microproject .rpi .camera .client .dto .Resolutions ;
18
+ import one .microproject .rpi .camera .client .dto .Rotations ;
17
19
import one .microproject .rpi .device .dto .SystemInfo ;
18
20
import org .slf4j .Logger ;
19
21
import org .slf4j .LoggerFactory ;
20
22
21
23
import java .io .IOException ;
24
+ import java .io .InputStream ;
22
25
import java .net .URL ;
23
26
import java .util .Base64 ;
24
27
@@ -63,11 +66,17 @@ public SystemInfo<CameraInfo> getSystemInfo() {
63
66
64
67
@ Override
65
68
public CameraConfiguration getConfiguration () {
69
+ return getData ("/system/config" , CameraConfiguration .class );
70
+ }
71
+
72
+ @ Override
73
+ public CameraConfiguration setConfiguration (CameraConfiguration configuration ) {
66
74
try {
75
+ RequestBody body = RequestBody .create (mapper .writeValueAsString (configuration ), MediaType .get ("application/json" ));
67
76
Request request = new Request .Builder ()
68
77
.url (baseURL + "/system/config" )
69
78
.addHeader (AUTHORIZATION , createBasicAuthorizationFromCredentials (clientId , clientSecret ))
70
- .get ( )
79
+ .post ( body )
71
80
.build ();
72
81
Response response = client .newCall (request ).execute ();
73
82
if (response .code () == 200 ) {
@@ -81,17 +90,21 @@ public CameraConfiguration getConfiguration() {
81
90
}
82
91
83
92
@ Override
84
- public CameraConfiguration setConfiguration ( CameraConfiguration configuration ) {
93
+ public ImageCapture captureImage ( ) {
85
94
try {
86
- RequestBody body = RequestBody . create ( mapper . writeValueAsString ( configuration ), MediaType . get ( "application/json" ) );
95
+ HttpUrl . Builder httpUrlBuilder = HttpUrl . parse ( baseURL + "/system/capture" ). newBuilder ( );
87
96
Request request = new Request .Builder ()
88
- .url (baseURL + "/system/config" )
97
+ .url (httpUrlBuilder . build () )
89
98
.addHeader (AUTHORIZATION , createBasicAuthorizationFromCredentials (clientId , clientSecret ))
90
- .post ( body )
99
+ .get ( )
91
100
.build ();
101
+
92
102
Response response = client .newCall (request ).execute ();
93
103
if (response .code () == 200 ) {
94
- return mapper .readValue (response .body ().string (), CameraConfiguration .class );
104
+ String mimeType = response .header ("Content-Type" );
105
+ String fileName = getFileNameFromHeader (response .header ("Content-Disposition" ), mimeType );
106
+ LOG .debug ("Http OK: {} {}" , mimeType , fileName );
107
+ return new ImageCapture (response .body ().byteStream (), fileName , mimeType );
95
108
}
96
109
LOG .warn ("Http error: {}" , response .code ());
97
110
throw new ClientException (ERROR_MESSAGE + response .code ());
@@ -101,9 +114,9 @@ public CameraConfiguration setConfiguration(CameraConfiguration configuration) {
101
114
}
102
115
103
116
@ Override
104
- public ImageCapture captureImage () {
117
+ public InputStream captureVideo () {
105
118
try {
106
- HttpUrl .Builder httpUrlBuilder = HttpUrl .parse (baseURL + "/system/capture " ).newBuilder ();
119
+ HttpUrl .Builder httpUrlBuilder = HttpUrl .parse (baseURL + "/system/stream.mjpg " ).newBuilder ();
107
120
Request request = new Request .Builder ()
108
121
.url (httpUrlBuilder .build ())
109
122
.addHeader (AUTHORIZATION , createBasicAuthorizationFromCredentials (clientId , clientSecret ))
@@ -115,7 +128,7 @@ public ImageCapture captureImage() {
115
128
String mimeType = response .header ("Content-Type" );
116
129
String fileName = getFileNameFromHeader (response .header ("Content-Disposition" ), mimeType );
117
130
LOG .debug ("Http OK: {} {}" , mimeType , fileName );
118
- return new ImageCapture ( response .body ().byteStream (), fileName , mimeType );
131
+ return response .body ().byteStream ();
119
132
}
120
133
LOG .warn ("Http error: {}" , response .code ());
121
134
throw new ClientException (ERROR_MESSAGE + response .code ());
@@ -125,7 +138,7 @@ public ImageCapture captureImage() {
125
138
}
126
139
127
140
@ Override
128
- public Integer selectCamera (CameraSelectRequest cameraSelectRequest ) {
141
+ public CameraSelect selectCamera (CameraSelect cameraSelectRequest ) {
129
142
try {
130
143
RequestBody body = RequestBody .create (mapper .writeValueAsString (cameraSelectRequest ), MediaType .get ("application/json" ));
131
144
Request request = new Request .Builder ()
@@ -135,7 +148,7 @@ public Integer selectCamera(CameraSelectRequest cameraSelectRequest) {
135
148
.build ();
136
149
Response response = client .newCall (request ).execute ();
137
150
if (response .code () == 200 ) {
138
- return mapper .readValue (response .body ().string (), CameraSelectRequest .class ). getCamera ( );
151
+ return mapper .readValue (response .body ().string (), CameraSelect .class );
139
152
}
140
153
LOG .warn ("Http error: {}" , response .code ());
141
154
throw new ClientException (ERROR_MESSAGE + response .code ());
@@ -144,6 +157,21 @@ public Integer selectCamera(CameraSelectRequest cameraSelectRequest) {
144
157
}
145
158
}
146
159
160
+ @ Override
161
+ public CameraSelect getSelectedCamera () {
162
+ return getData ("/system/camera" , CameraSelect .class );
163
+ }
164
+
165
+ @ Override
166
+ public Resolutions getResolutions () {
167
+ return getData ("/system/resolutions" , Resolutions .class );
168
+ }
169
+
170
+ @ Override
171
+ public Rotations getRotations () {
172
+ return getData ("/system/rotations" , Rotations .class );
173
+ }
174
+
147
175
public static String createBasicAuthorizationFromCredentials (String clientId , String clientSecret ) {
148
176
String authorization = clientId + ":" + clientSecret ;
149
177
byte [] encodedBytes = Base64 .getEncoder ().encode (authorization .getBytes ());
@@ -164,4 +192,22 @@ public static String getFileNameFromHeader(String contentDisposition, String mim
164
192
}
165
193
}
166
194
195
+ private <T > T getData (String path , Class <T > type ) {
196
+ try {
197
+ Request request = new Request .Builder ()
198
+ .url (baseURL + path )
199
+ .addHeader (AUTHORIZATION , createBasicAuthorizationFromCredentials (clientId , clientSecret ))
200
+ .get ()
201
+ .build ();
202
+ Response response = client .newCall (request ).execute ();
203
+ if (response .code () == 200 ) {
204
+ return mapper .readValue (response .body ().string (), type );
205
+ }
206
+ LOG .warn ("Http error: {}" , response .code ());
207
+ throw new ClientException (ERROR_MESSAGE + response .code ());
208
+ } catch (IOException e ) {
209
+ throw new ClientException (e );
210
+ }
211
+ }
212
+
167
213
}
0 commit comments