| 27417 |
tejbeer |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 27421 |
tejbeer |
3 |
import java.io.IOException;
|
|
|
4 |
import java.time.LocalDate;
|
| 27417 |
tejbeer |
5 |
import java.time.LocalDateTime;
|
| 27421 |
tejbeer |
6 |
import java.util.Base64;
|
|
|
7 |
import java.util.List;
|
| 27417 |
tejbeer |
8 |
|
| 27421 |
tejbeer |
9 |
import javax.mail.MessagingException;
|
| 27417 |
tejbeer |
10 |
import javax.servlet.http.HttpServletRequest;
|
|
|
11 |
|
|
|
12 |
import org.apache.logging.log4j.LogManager;
|
|
|
13 |
import org.apache.logging.log4j.Logger;
|
| 27421 |
tejbeer |
14 |
import org.apache.velocity.app.VelocityEngine;
|
|
|
15 |
import org.json.JSONArray;
|
|
|
16 |
import org.json.JSONObject;
|
| 27417 |
tejbeer |
17 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
18 |
import org.springframework.http.MediaType;
|
|
|
19 |
import org.springframework.http.ResponseEntity;
|
| 27421 |
tejbeer |
20 |
import org.springframework.mail.javamail.JavaMailSender;
|
| 27417 |
tejbeer |
21 |
import org.springframework.stereotype.Controller;
|
|
|
22 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
23 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
24 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
25 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 27421 |
tejbeer |
26 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 27417 |
tejbeer |
27 |
|
|
|
28 |
import com.google.gson.Gson;
|
| 27421 |
tejbeer |
29 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
30 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 27417 |
tejbeer |
31 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 27421 |
tejbeer |
32 |
import com.spice.profitmandi.dao.entity.dtr.EmployeeAttendance;
|
| 27417 |
tejbeer |
33 |
import com.spice.profitmandi.dao.entity.dtr.HyperTrackKeyModel;
|
|
|
34 |
import com.spice.profitmandi.dao.entity.dtr.HypertrackKey;
|
| 27421 |
tejbeer |
35 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
|
|
36 |
import com.spice.profitmandi.dao.enumuration.dtr.PunchType;
|
|
|
37 |
import com.spice.profitmandi.dao.model.EmployeeAttendanceModel;
|
|
|
38 |
import com.spice.profitmandi.dao.repository.dtr.EmployeeAttendanceRepository;
|
|
|
39 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
|
|
40 |
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
|
| 27417 |
tejbeer |
41 |
import com.spice.profitmandi.dao.repository.user.HypertrackKeyRepository;
|
| 27421 |
tejbeer |
42 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 27417 |
tejbeer |
43 |
|
|
|
44 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
45 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
46 |
import io.swagger.annotations.ApiOperation;
|
| 27421 |
tejbeer |
47 |
import okhttp3.OkHttpClient;
|
|
|
48 |
import okhttp3.Request;
|
|
|
49 |
import okhttp3.Response;
|
| 27417 |
tejbeer |
50 |
|
|
|
51 |
@Controller
|
|
|
52 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
53 |
public class HyperTrackController {
|
|
|
54 |
|
|
|
55 |
private static final Logger LOGGER = LogManager.getLogger(HyperTrackController.class);
|
|
|
56 |
|
|
|
57 |
@Autowired
|
|
|
58 |
ResponseSender<?> responseSender;
|
|
|
59 |
|
|
|
60 |
@Autowired
|
|
|
61 |
Gson gson;
|
|
|
62 |
|
|
|
63 |
@Autowired
|
|
|
64 |
private HypertrackKeyRepository hypertrackKeyRepository;
|
|
|
65 |
|
| 27421 |
tejbeer |
66 |
@Autowired
|
|
|
67 |
private EmployeeAttendanceRepository employeeAttendanceRepository;
|
|
|
68 |
|
|
|
69 |
@Autowired
|
|
|
70 |
private VelocityEngine velocityEngine;
|
|
|
71 |
|
|
|
72 |
@Autowired
|
|
|
73 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
74 |
|
|
|
75 |
@Autowired
|
|
|
76 |
private RetailerService retailerService;
|
|
|
77 |
@Autowired
|
|
|
78 |
JavaMailSender mailSender;
|
|
|
79 |
|
| 27417 |
tejbeer |
80 |
private static final String ACCOUNT_ID = "aZ6flHhrgPIEl18buHdPBdueEN4";
|
|
|
81 |
private static final String SECRET_KEY = "a7rsX5B4UNNfTTx1-IJ19qdH48BT4YvBKlQJg3n3_KKNe7WWych55g";
|
|
|
82 |
|
|
|
83 |
@RequestMapping(value = "/devices/hypertrackId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
84 |
@ApiImplicitParams({
|
|
|
85 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
86 |
@ApiOperation(value = "")
|
|
|
87 |
public ResponseEntity<?> deviceHypertrackId(HttpServletRequest request,
|
|
|
88 |
@RequestBody HyperTrackKeyModel hyperTrackKeyModel) {
|
|
|
89 |
HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(hyperTrackKeyModel.getUserId(),
|
|
|
90 |
hyperTrackKeyModel.getDeviceId());
|
|
|
91 |
if (hyperTrackKey == null) {
|
|
|
92 |
hyperTrackKey = new HypertrackKey();
|
| 27421 |
tejbeer |
93 |
hyperTrackKey.setHypertrackKeyId(hyperTrackKeyModel.getHyperTrackId());
|
| 27417 |
tejbeer |
94 |
hyperTrackKey.setDeviceId(hyperTrackKeyModel.getDeviceId());
|
|
|
95 |
}
|
| 27421 |
tejbeer |
96 |
|
|
|
97 |
hyperTrackKey.setUserId(hyperTrackKeyModel.getUserId());
|
| 27417 |
tejbeer |
98 |
hyperTrackKey.setCreatedTimestamp(LocalDateTime.now());
|
|
|
99 |
hyperTrackKey.setUpdatedTimestamp(LocalDateTime.now());
|
|
|
100 |
hypertrackKeyRepository.persist(hyperTrackKey);
|
|
|
101 |
return responseSender.ok(true);
|
|
|
102 |
}
|
|
|
103 |
|
| 27421 |
tejbeer |
104 |
@RequestMapping(value = "/devices/location", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
105 |
public ResponseEntity<?> getLocationByKeyId(HttpServletRequest request, @RequestParam int userId,
|
|
|
106 |
@RequestParam int deviceId) throws IOException, MessagingException {
|
|
|
107 |
|
|
|
108 |
double lat1 = 28.515867995437514;
|
|
|
109 |
double lng1 = 77.3770672558246;
|
|
|
110 |
|
|
|
111 |
HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
|
|
|
112 |
|
|
|
113 |
List<EmployeeAttendance> employeeAttendance = employeeAttendanceRepository.selectByUserIdKey(userId, deviceId,
|
|
|
114 |
LocalDate.now());
|
|
|
115 |
|
|
|
116 |
OkHttpClient client = new OkHttpClient();
|
|
|
117 |
|
|
|
118 |
String authString = "Basic "
|
|
|
119 |
+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
|
|
|
120 |
|
|
|
121 |
Request request1 = new Request.Builder()
|
|
|
122 |
.url("https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId()).get()
|
|
|
123 |
.addHeader("Authorization", authString).build();
|
|
|
124 |
|
|
|
125 |
Response response = client.newCall(request1).execute();
|
|
|
126 |
JSONObject jsonObj = new JSONObject(response.body().string());
|
|
|
127 |
JSONObject c = jsonObj.getJSONObject("location");
|
|
|
128 |
JSONObject d = c.getJSONObject("geometry");
|
|
|
129 |
JSONArray coords = d.getJSONArray("coordinates");
|
|
|
130 |
double lat2 = coords.getDouble(1);
|
|
|
131 |
double lng2 = coords.getDouble(0);
|
|
|
132 |
double dist = distance(lat1, lng1, lat2, lng2, "m");
|
|
|
133 |
EmployeeAttendanceModel em = new EmployeeAttendanceModel();
|
|
|
134 |
em.setDistance(dist);
|
|
|
135 |
|
|
|
136 |
if (!employeeAttendance.isEmpty()) {
|
|
|
137 |
if (employeeAttendance.get(0).getPunch().equals("punchIn")) {
|
|
|
138 |
em.setPunchIn(true);
|
|
|
139 |
} else {
|
|
|
140 |
em.setPunchIn(false);
|
|
|
141 |
}
|
|
|
142 |
} else {
|
|
|
143 |
em.setPunchIn(false);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
return responseSender.ok(em);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
|
|
|
150 |
if ((lat1 == lat2) && (lon1 == lon2)) {
|
|
|
151 |
return 0;
|
|
|
152 |
} else {
|
|
|
153 |
double theta = lon1 - lon2;
|
|
|
154 |
double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2))
|
|
|
155 |
+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
|
|
|
156 |
dist = Math.acos(dist);
|
|
|
157 |
dist = Math.toDegrees(dist);
|
|
|
158 |
dist = dist * 60 * 1.1515;
|
|
|
159 |
if (unit.equals("m")) {
|
|
|
160 |
dist = dist * 1.609344;
|
|
|
161 |
dist = dist / 0.0010000;
|
|
|
162 |
} else if (unit.equals("N")) {
|
|
|
163 |
dist = dist * 0.8684;
|
|
|
164 |
}
|
|
|
165 |
return (dist);
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
@RequestMapping(value = "/employee/attendance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
170 |
@ApiImplicitParams({
|
|
|
171 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
172 |
@ApiOperation(value = "")
|
|
|
173 |
public ResponseEntity<?> employeeAttendance(HttpServletRequest request, @RequestParam(name = "userId") int userId,
|
|
|
174 |
@RequestParam(name = "deviceId") int deviceId, @RequestParam(name = "punchType") PunchType punchType,
|
|
|
175 |
@RequestParam(name = "punch") String punch) {
|
|
|
176 |
|
|
|
177 |
EmployeeAttendance employeeAttendance = new EmployeeAttendance();
|
|
|
178 |
employeeAttendance.setUserId(userId);
|
|
|
179 |
employeeAttendance.setDeviceId(deviceId);
|
|
|
180 |
employeeAttendance.setPunch(punch);
|
|
|
181 |
employeeAttendance.setPunchType(punchType);
|
|
|
182 |
employeeAttendance.setCreateTimestamp(LocalDateTime.now());
|
|
|
183 |
employeeAttendanceRepository.persist(employeeAttendance);
|
|
|
184 |
return responseSender.ok(true);
|
|
|
185 |
}
|
|
|
186 |
|
| 27417 |
tejbeer |
187 |
}
|