|
25 | 25 | import cc.blynk.server.notifications.push.GCMWrapper;
|
26 | 26 | import cc.blynk.server.notifications.push.android.AndroidGCMMessage;
|
27 | 27 | import cc.blynk.server.notifications.push.ios.IOSGCMMessage;
|
| 28 | +import cc.blynk.utils.ByteUtils; |
28 | 29 | import cc.blynk.utils.StringUtils;
|
| 30 | +import net.glxn.qrgen.core.image.ImageType; |
| 31 | +import net.glxn.qrgen.javase.QRCode; |
29 | 32 | import org.apache.logging.log4j.LogManager;
|
30 | 33 | import org.apache.logging.log4j.Logger;
|
31 | 34 |
|
32 | 35 | import javax.ws.rs.*;
|
33 | 36 | import javax.ws.rs.core.MediaType;
|
| 37 | +import java.io.IOException; |
| 38 | +import java.util.Base64; |
34 | 39 |
|
35 | 40 | import static cc.blynk.core.http.Response.*;
|
36 | 41 | import static cc.blynk.server.core.protocol.enums.Command.*;
|
@@ -186,6 +191,39 @@ public Response getWidgetPinData(@PathParam("token") String token,
|
186 | 191 | return ok(widget.getJsonValue());
|
187 | 192 | }
|
188 | 193 |
|
| 194 | + @GET |
| 195 | + @Path("{token}/qr") |
| 196 | + //todo cover with test |
| 197 | + public Response updateWidgetPinData(@PathParam("token") String token) { |
| 198 | + globalStats.mark(HTTP_QR); |
| 199 | + |
| 200 | + User user = userDao.tokenManager.getUserByToken(token); |
| 201 | + |
| 202 | + if (user == null) { |
| 203 | + log.error("Requested token {} not found.", token); |
| 204 | + return Response.badRequest("Invalid token."); |
| 205 | + } |
| 206 | + |
| 207 | + Integer dashId = user.getDashIdByToken(token); |
| 208 | + |
| 209 | + if (dashId == null) { |
| 210 | + log.error("Dash id for token {} not found. User {}", token, user.name); |
| 211 | + return Response.badRequest("Didn't find dash id for token."); |
| 212 | + } |
| 213 | + |
| 214 | + DashBoard dashBoard = user.profile.getDashById(dashId); |
| 215 | + |
| 216 | + try { |
| 217 | + byte[] compressed = ByteUtils.compress(dashBoard.toString()); |
| 218 | + String qrData = "bp1" + Base64.getEncoder().encodeToString(compressed); |
| 219 | + byte[] qrDataBinary = QRCode.from(qrData).to(ImageType.PNG).withSize(500, 500).stream().toByteArray(); |
| 220 | + return ok(qrDataBinary, "image/png"); |
| 221 | + } catch (IOException ioe) { |
| 222 | + log.error("Error generating QR.", ioe); |
| 223 | + return Response.badRequest("Error generating QR."); |
| 224 | + } |
| 225 | + } |
| 226 | + |
189 | 227 | @PUT
|
190 | 228 | @Path("{token}/pin/{pin}")
|
191 | 229 | @Consumes(value = MediaType.APPLICATION_JSON)
|
|
0 commit comments