| 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;
|
|
|
20 |
import org.springframework.stereotype.Controller;
|
|
|
21 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
22 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
23 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
24 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 27421 |
tejbeer |
25 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 27417 |
tejbeer |
26 |
|
|
|
27 |
import com.google.gson.Gson;
|
| 27421 |
tejbeer |
28 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
29 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
| 27417 |
tejbeer |
30 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 27421 |
tejbeer |
31 |
import com.spice.profitmandi.dao.entity.dtr.EmployeeAttendance;
|
| 27417 |
tejbeer |
32 |
import com.spice.profitmandi.dao.entity.dtr.HyperTrackKeyModel;
|
|
|
33 |
import com.spice.profitmandi.dao.entity.dtr.HypertrackKey;
|
| 27421 |
tejbeer |
34 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
|
|
35 |
import com.spice.profitmandi.dao.enumuration.dtr.PunchType;
|
|
|
36 |
import com.spice.profitmandi.dao.model.EmployeeAttendanceModel;
|
|
|
37 |
import com.spice.profitmandi.dao.repository.dtr.EmployeeAttendanceRepository;
|
|
|
38 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
|
|
39 |
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
|
| 27417 |
tejbeer |
40 |
import com.spice.profitmandi.dao.repository.user.HypertrackKeyRepository;
|
| 27421 |
tejbeer |
41 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 27417 |
tejbeer |
42 |
|
|
|
43 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
44 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
45 |
import io.swagger.annotations.ApiOperation;
|
| 27421 |
tejbeer |
46 |
import okhttp3.OkHttpClient;
|
|
|
47 |
import okhttp3.Request;
|
|
|
48 |
import okhttp3.Response;
|
| 27417 |
tejbeer |
49 |
|
|
|
50 |
@Controller
|
|
|
51 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
52 |
public class HyperTrackController {
|
|
|
53 |
|
|
|
54 |
private static final Logger LOGGER = LogManager.getLogger(HyperTrackController.class);
|
|
|
55 |
|
|
|
56 |
@Autowired
|
|
|
57 |
ResponseSender<?> responseSender;
|
|
|
58 |
|
|
|
59 |
@Autowired
|
|
|
60 |
Gson gson;
|
|
|
61 |
|
|
|
62 |
@Autowired
|
|
|
63 |
private HypertrackKeyRepository hypertrackKeyRepository;
|
|
|
64 |
|
| 27421 |
tejbeer |
65 |
@Autowired
|
|
|
66 |
private EmployeeAttendanceRepository employeeAttendanceRepository;
|
|
|
67 |
|
|
|
68 |
@Autowired
|
|
|
69 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
70 |
|
|
|
71 |
@Autowired
|
|
|
72 |
private RetailerService retailerService;
|
|
|
73 |
|
| 27417 |
tejbeer |
74 |
private static final String ACCOUNT_ID = "aZ6flHhrgPIEl18buHdPBdueEN4";
|
|
|
75 |
private static final String SECRET_KEY = "a7rsX5B4UNNfTTx1-IJ19qdH48BT4YvBKlQJg3n3_KKNe7WWych55g";
|
|
|
76 |
|
|
|
77 |
@RequestMapping(value = "/devices/hypertrackId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
78 |
@ApiImplicitParams({
|
|
|
79 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
80 |
@ApiOperation(value = "")
|
|
|
81 |
public ResponseEntity<?> deviceHypertrackId(HttpServletRequest request,
|
|
|
82 |
@RequestBody HyperTrackKeyModel hyperTrackKeyModel) {
|
|
|
83 |
HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(hyperTrackKeyModel.getUserId(),
|
|
|
84 |
hyperTrackKeyModel.getDeviceId());
|
|
|
85 |
if (hyperTrackKey == null) {
|
|
|
86 |
hyperTrackKey = new HypertrackKey();
|
| 27444 |
tejbeer |
87 |
hyperTrackKey.setUserId(hyperTrackKeyModel.getUserId());
|
|
|
88 |
hyperTrackKey.setCreatedTimestamp(LocalDateTime.now());
|
| 27417 |
tejbeer |
89 |
hyperTrackKey.setDeviceId(hyperTrackKeyModel.getDeviceId());
|
|
|
90 |
}
|
| 27422 |
tejbeer |
91 |
|
| 27444 |
tejbeer |
92 |
hyperTrackKey.setHypertrackKeyId(hyperTrackKeyModel.getHyperTrackId());
|
|
|
93 |
|
| 27417 |
tejbeer |
94 |
hyperTrackKey.setUpdatedTimestamp(LocalDateTime.now());
|
|
|
95 |
hypertrackKeyRepository.persist(hyperTrackKey);
|
|
|
96 |
return responseSender.ok(true);
|
|
|
97 |
}
|
|
|
98 |
|
| 27421 |
tejbeer |
99 |
@RequestMapping(value = "/devices/location", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
100 |
public ResponseEntity<?> getLocationByKeyId(HttpServletRequest request, @RequestParam int userId,
|
| 27426 |
tejbeer |
101 |
@RequestParam int deviceId) throws IOException, MessagingException, ProfitMandiBusinessException {
|
| 27421 |
tejbeer |
102 |
|
| 27426 |
tejbeer |
103 |
double lat1 = 28.516045882182738;
|
|
|
104 |
double lng1 = 77.3771954997187;
|
| 27421 |
tejbeer |
105 |
|
|
|
106 |
HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
|
| 27428 |
tejbeer |
107 |
// EmployeeAttendance employeeAttendance =
|
|
|
108 |
// employeeAttendanceRepository.selectLatestPunchTimeStamp();
|
|
|
109 |
|
|
|
110 |
List<EmployeeAttendance> employeeAttendances = employeeAttendanceRepository.selectByUserIdKey(userId, deviceId,
|
|
|
111 |
LocalDate.now());
|
| 27432 |
tejbeer |
112 |
|
|
|
113 |
LOGGER.info("hyperTrackKey" + hyperTrackKey);
|
|
|
114 |
LOGGER.info("employeeAttendance" + employeeAttendances);
|
| 27421 |
tejbeer |
115 |
OkHttpClient client = new OkHttpClient();
|
|
|
116 |
|
|
|
117 |
String authString = "Basic "
|
|
|
118 |
+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
|
|
|
119 |
|
|
|
120 |
Request request1 = new Request.Builder()
|
| 27426 |
tejbeer |
121 |
.url("https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId()).get()
|
| 27421 |
tejbeer |
122 |
.addHeader("Authorization", authString).build();
|
|
|
123 |
|
|
|
124 |
Response response = client.newCall(request1).execute();
|
|
|
125 |
JSONObject jsonObj = new JSONObject(response.body().string());
|
|
|
126 |
JSONObject c = jsonObj.getJSONObject("location");
|
|
|
127 |
JSONObject d = c.getJSONObject("geometry");
|
|
|
128 |
JSONArray coords = d.getJSONArray("coordinates");
|
|
|
129 |
double lat2 = coords.getDouble(1);
|
|
|
130 |
double lng2 = coords.getDouble(0);
|
|
|
131 |
double dist = distance(lat1, lng1, lat2, lng2, "m");
|
|
|
132 |
EmployeeAttendanceModel em = new EmployeeAttendanceModel();
|
|
|
133 |
em.setDistance(dist);
|
| 27422 |
tejbeer |
134 |
em.setLatitude(lat2);
|
|
|
135 |
em.setLongitude(lng2);
|
| 27432 |
tejbeer |
136 |
|
| 27428 |
tejbeer |
137 |
if (!employeeAttendances.isEmpty()) {
|
|
|
138 |
if (employeeAttendances.get(0).getCreateTimestamp().toLocalDate().equals(LocalDate.now())) {
|
|
|
139 |
if (employeeAttendances.get(0).getPunch().equals("punchIn")) {
|
|
|
140 |
em.setPunchIn(true);
|
|
|
141 |
} else {
|
|
|
142 |
em.setPunchIn(false);
|
|
|
143 |
}
|
| 27421 |
tejbeer |
144 |
} else {
|
|
|
145 |
em.setPunchIn(false);
|
|
|
146 |
}
|
|
|
147 |
} else {
|
|
|
148 |
em.setPunchIn(false);
|
|
|
149 |
}
|
|
|
150 |
return responseSender.ok(em);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
|
|
|
154 |
if ((lat1 == lat2) && (lon1 == lon2)) {
|
|
|
155 |
return 0;
|
|
|
156 |
} else {
|
|
|
157 |
double theta = lon1 - lon2;
|
|
|
158 |
double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2))
|
|
|
159 |
+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
|
|
|
160 |
dist = Math.acos(dist);
|
|
|
161 |
dist = Math.toDegrees(dist);
|
|
|
162 |
dist = dist * 60 * 1.1515;
|
|
|
163 |
if (unit.equals("m")) {
|
|
|
164 |
dist = dist * 1.609344;
|
|
|
165 |
dist = dist / 0.0010000;
|
|
|
166 |
} else if (unit.equals("N")) {
|
|
|
167 |
dist = dist * 0.8684;
|
|
|
168 |
}
|
|
|
169 |
return (dist);
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
@RequestMapping(value = "/employee/attendance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
174 |
@ApiImplicitParams({
|
|
|
175 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
176 |
@ApiOperation(value = "")
|
|
|
177 |
public ResponseEntity<?> employeeAttendance(HttpServletRequest request, @RequestParam(name = "userId") int userId,
|
|
|
178 |
@RequestParam(name = "deviceId") int deviceId, @RequestParam(name = "punchType") PunchType punchType,
|
|
|
179 |
@RequestParam(name = "punch") String punch) {
|
|
|
180 |
|
|
|
181 |
EmployeeAttendance employeeAttendance = new EmployeeAttendance();
|
|
|
182 |
employeeAttendance.setUserId(userId);
|
|
|
183 |
employeeAttendance.setDeviceId(deviceId);
|
|
|
184 |
employeeAttendance.setPunch(punch);
|
|
|
185 |
employeeAttendance.setPunchType(punchType);
|
|
|
186 |
employeeAttendance.setCreateTimestamp(LocalDateTime.now());
|
|
|
187 |
employeeAttendanceRepository.persist(employeeAttendance);
|
|
|
188 |
return responseSender.ok(true);
|
|
|
189 |
}
|
|
|
190 |
|
| 27426 |
tejbeer |
191 |
@RequestMapping(value = "/create/geofence", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
192 |
public ResponseEntity<?> createGreofence(HttpServletRequest request)
|
|
|
193 |
throws IOException, ProfitMandiBusinessException {
|
|
|
194 |
|
|
|
195 |
List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores();
|
|
|
196 |
for (FofoStore fofoStore : fofoStores) {
|
|
|
197 |
CustomRetailer customRetailer = retailerService.getFofoRetailer(fofoStore.getId());
|
|
|
198 |
OkHttpClient client = new OkHttpClient();
|
|
|
199 |
|
|
|
200 |
okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
|
|
|
201 |
JSONObject geofe = new JSONObject();
|
|
|
202 |
JSONArray geofences = new JSONArray();
|
|
|
203 |
JSONObject geometry = new JSONObject();
|
|
|
204 |
JSONObject geo = new JSONObject();
|
|
|
205 |
JSONArray coordinates = new JSONArray();
|
|
|
206 |
coordinates.put(fofoStore.getLongitude());
|
|
|
207 |
coordinates.put(fofoStore.getLatitude());
|
|
|
208 |
geo.put("type", "Point");
|
|
|
209 |
geo.put("coordinates", coordinates);
|
|
|
210 |
geometry.put("geometry", geo);
|
|
|
211 |
JSONObject metadata = new JSONObject();
|
|
|
212 |
metadata.put("name", customRetailer.getBusinessName());
|
|
|
213 |
metadata.put("city", customRetailer.getAddress().getCity());
|
|
|
214 |
geometry.put("metadata", metadata);
|
|
|
215 |
geometry.put("radius", 500);
|
|
|
216 |
|
|
|
217 |
geofences.put(geometry);
|
|
|
218 |
geofe.put("geofences", geofences);
|
|
|
219 |
okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, geofe.toString());
|
|
|
220 |
String authString = "Basic "
|
|
|
221 |
+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
|
|
|
222 |
|
|
|
223 |
Request request1 = new Request.Builder().url("https://v3.api.hypertrack.com/geofences").post(body)
|
|
|
224 |
.addHeader("Authorization", authString).build();
|
|
|
225 |
|
|
|
226 |
Response response = client.newCall(request1).execute();
|
|
|
227 |
|
|
|
228 |
}
|
|
|
229 |
return responseSender.ok(true);
|
|
|
230 |
|
|
|
231 |
}
|
|
|
232 |
|
| 27442 |
tejbeer |
233 |
@RequestMapping(value = "/device/status", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
234 |
@ApiImplicitParams({
|
|
|
235 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
236 |
@ApiOperation(value = "")
|
|
|
237 |
public ResponseEntity<?> deviceStatus(HttpServletRequest request, @RequestParam String hyperTrackKey)
|
|
|
238 |
throws IOException {
|
|
|
239 |
OkHttpClient client = new OkHttpClient();
|
|
|
240 |
|
|
|
241 |
String authString = "Basic "
|
|
|
242 |
+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
|
|
|
243 |
|
|
|
244 |
Request request1 = new Request.Builder().url("https://v3.api.hypertrack.com/devices/" + hyperTrackKey).get()
|
|
|
245 |
.addHeader("Authorization", authString).build();
|
|
|
246 |
|
|
|
247 |
Response response = client.newCall(request1).execute();
|
| 27443 |
tejbeer |
248 |
return responseSender.ok(response.body().string());
|
| 27442 |
tejbeer |
249 |
|
|
|
250 |
}
|
|
|
251 |
|
| 27417 |
tejbeer |
252 |
}
|