Subversion Repositories SmartDukaan

Rev

Rev 30590 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 30590 Rev 36418
Line 25... Line 25...
25
import org.springframework.web.bind.annotation.RequestMethod;
25
import org.springframework.web.bind.annotation.RequestMethod;
26
import org.springframework.web.bind.annotation.RequestParam;
26
import org.springframework.web.bind.annotation.RequestParam;
27
 
27
 
28
import com.google.gson.Gson;
28
import com.google.gson.Gson;
29
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
29
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
30
import com.spice.profitmandi.common.web.client.HttpClientFactory;
-
 
31
import com.spice.profitmandi.common.web.client.RestClient;
30
import com.spice.profitmandi.common.web.util.ResponseSender;
32
import com.spice.profitmandi.common.web.util.ResponseSender;
31
import com.spice.profitmandi.dao.entity.dtr.EmployeeAttendance;
33
import com.spice.profitmandi.dao.entity.dtr.EmployeeAttendance;
32
import com.spice.profitmandi.dao.entity.dtr.HyperTrackKeyModel;
34
import com.spice.profitmandi.dao.entity.dtr.HyperTrackKeyModel;
33
import com.spice.profitmandi.dao.entity.dtr.HypertrackKey;
35
import com.spice.profitmandi.dao.entity.dtr.HypertrackKey;
34
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
36
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
Line 40... Line 42...
40
import com.spice.profitmandi.service.user.RetailerService;
42
import com.spice.profitmandi.service.user.RetailerService;
41
 
43
 
42
import io.swagger.annotations.ApiImplicitParam;
44
import io.swagger.annotations.ApiImplicitParam;
43
import io.swagger.annotations.ApiImplicitParams;
45
import io.swagger.annotations.ApiImplicitParams;
44
import io.swagger.annotations.ApiOperation;
46
import io.swagger.annotations.ApiOperation;
-
 
47
import org.apache.http.client.methods.CloseableHttpResponse;
-
 
48
import org.apache.http.client.methods.HttpDelete;
-
 
49
import org.apache.http.impl.client.CloseableHttpClient;
45
import okhttp3.OkHttpClient;
50
import org.apache.http.util.EntityUtils;
-
 
51
 
46
import okhttp3.Request;
52
import java.util.Collections;
47
import okhttp3.Response;
53
import java.util.Map;
48
 
54
 
49
@Controller
55
@Controller
50
@Transactional(rollbackFor = Throwable.class)
-
 
51
public class HyperTrackController {
56
public class HyperTrackController {
52
 
57
 
53
	private static final Logger LOGGER = LogManager.getLogger(HyperTrackController.class);
58
	private static final Logger LOGGER = LogManager.getLogger(HyperTrackController.class);
54
 
59
 
55
	@Autowired
60
	@Autowired
Line 68... Line 73...
68
	private FofoStoreRepository fofoStoreRepository;
73
	private FofoStoreRepository fofoStoreRepository;
69
 
74
 
70
	@Autowired
75
	@Autowired
71
	private RetailerService retailerService;
76
	private RetailerService retailerService;
72
 
77
 
-
 
78
	@Autowired
-
 
79
	private RestClient restClient;
-
 
80
 
-
 
81
	@Autowired
-
 
82
	private HyperTrackService hyperTrackService;
-
 
83
 
73
	private static final String ACCOUNT_ID = "aZ6flHhrgPIEl18buHdPBdueEN4";
84
	private static final String ACCOUNT_ID = "aZ6flHhrgPIEl18buHdPBdueEN4";
74
	private static final String SECRET_KEY = "a7rsX5B4UNNfTTx1-IJ19qdH48BT4YvBKlQJg3n3_KKNe7WWych55g";
85
	private static final String SECRET_KEY = "a7rsX5B4UNNfTTx1-IJ19qdH48BT4YvBKlQJg3n3_KKNe7WWych55g";
75
 
86
 
-
 
87
	private Map<String, String> hypertrackAuthHeader() {
-
 
88
		String authString = "Basic "
-
 
89
				+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
-
 
90
		return Collections.singletonMap("Authorization", authString);
-
 
91
	}
-
 
92
 
76
	@RequestMapping(value = "/devices/hypertrackId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
93
	@RequestMapping(value = "/devices/hypertrackId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
77
	@ApiImplicitParams({
94
	@ApiImplicitParams({
78
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
95
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
79
	@ApiOperation(value = "")
96
	@ApiOperation(value = "")
-
 
97
	@Transactional
80
	public ResponseEntity<?> deviceHypertrackId(HttpServletRequest request,
98
	public ResponseEntity<?> deviceHypertrackId(HttpServletRequest request,
81
			@RequestBody HyperTrackKeyModel hyperTrackKeyModel) {
99
			@RequestBody HyperTrackKeyModel hyperTrackKeyModel) {
82
		HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(hyperTrackKeyModel.getUserId(),
100
		HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(hyperTrackKeyModel.getUserId(),
83
				hyperTrackKeyModel.getDeviceId());
101
				hyperTrackKeyModel.getDeviceId());
84
		if (hyperTrackKey == null) {
102
		if (hyperTrackKey == null) {
Line 101... Line 119...
101
 
119
 
102
		LOGGER.info("UserId -> {}, Device Id -> {}", userId, deviceId);
120
		LOGGER.info("UserId -> {}, Device Id -> {}", userId, deviceId);
103
		double lat1 = 28.516045882182738;
121
		double lat1 = 28.516045882182738;
104
		double lng1 = 77.3771954997187;
122
		double lng1 = 77.3771954997187;
105
 
123
 
-
 
124
		// Read-only tx in the service: connection released before the external HTTP call below.
106
		HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
125
		HyperTrackService.LocationContext ctx = hyperTrackService.loadLocationContext(userId, deviceId);
107
 
-
 
-
 
126
		HypertrackKey hyperTrackKey = ctx.hypertrackKey;
108
		List<EmployeeAttendance> employeeAttendances = employeeAttendanceRepository.selectByUserIdKey(userId, deviceId,
127
		List<EmployeeAttendance> employeeAttendances = ctx.employeeAttendances;
109
				LocalDate.now());
-
 
110
		EmployeeAttendanceModel em = null;
128
		EmployeeAttendanceModel em = null;
111
		LOGGER.info("hyperTrackKey" + hyperTrackKey);
129
		LOGGER.info("hyperTrackKey" + hyperTrackKey);
112
		LOGGER.info("employeeAttendance" + employeeAttendances);
130
		LOGGER.info("employeeAttendance" + employeeAttendances);
113
		OkHttpClient client = new OkHttpClient();
-
 
114
 
-
 
115
		String authString = "Basic "
131
		String responseBody = restClient.get(
116
				+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
-
 
117
 
-
 
118
		Request request1 = new Request.Builder()
-
 
119
				.url("https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId()).get()
132
				"https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId(),
120
				.addHeader("Authorization", authString).build();
133
				null, hypertrackAuthHeader());
121
 
-
 
122
		Response response = client.newCall(request1).execute();
-
 
123
 
-
 
124
		JSONObject jsonObj = new JSONObject(response.body().string());
134
		JSONObject jsonObj = new JSONObject(responseBody);
125
 
135
 
126
		JSONObject deviceStatus = jsonObj.getJSONObject("device_status");
136
		JSONObject deviceStatus = jsonObj.getJSONObject("device_status");
127
		String status = deviceStatus.getString("value");
137
		String status = deviceStatus.getString("value");
128
 
138
 
129
		if (status.equals("inactive") || status.equals("disconnected")) {
139
		if (status.equals("inactive") || status.equals("disconnected")) {
130
 
140
 
131
			LOGGER.info("status" + status);
141
			LOGGER.info("status" + status);
132
			okhttp3.RequestBody reqbody = okhttp3.RequestBody.create(null, new byte[0]);
-
 
133
 
-
 
134
			Request request2 = new Request.Builder()
142
			String startResp = restClient.post(
135
					.url("https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId() + "/start")
143
					"https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId() + "/start",
136
					.post(reqbody).addHeader("Authorization", authString).build();
-
 
137
 
-
 
138
			Response resp = client.newCall(request2).execute();
144
					"", hypertrackAuthHeader());
139
			LOGGER.info("resp" + resp);
145
			LOGGER.info("resp" + startResp);
140
			em = new EmployeeAttendanceModel();
146
			em = new EmployeeAttendanceModel();
141
			em.setPunchIn(false);
147
			em.setPunchIn(false);
142
			em.setDeviceStatus(status);
148
			em.setDeviceStatus(status);
143
		} else if (status.equals("active")) {
149
		} else if (status.equals("active")) {
144
			JSONObject c = jsonObj.getJSONObject("location");
150
			JSONObject c = jsonObj.getJSONObject("location");
Line 226... Line 232...
226
			return (dist);
232
			return (dist);
227
		}
233
		}
228
	}
234
	}
229
 
235
 
230
	@RequestMapping(value = "/getPunchHistory", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
236
	@RequestMapping(value = "/getPunchHistory", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
-
 
237
	@Transactional(readOnly = true)
231
	public ResponseEntity<?> getPunchHistory(HttpServletRequest request, @RequestParam int userId,
238
	public ResponseEntity<?> getPunchHistory(HttpServletRequest request, @RequestParam int userId,
232
			@RequestParam int deviceId) {
239
			@RequestParam int deviceId) {
233
 
240
 
234
		List<EmployeeAttendance> employeeAttendances = employeeAttendanceRepository.selectByUserIdKey(userId, deviceId,
241
		List<EmployeeAttendance> employeeAttendances = employeeAttendanceRepository.selectByUserIdKey(userId, deviceId,
235
				LocalDate.now());
242
				LocalDate.now());
Line 239... Line 246...
239
 
246
 
240
	@RequestMapping(value = "/employee/attendance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
247
	@RequestMapping(value = "/employee/attendance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
241
	@ApiImplicitParams({
248
	@ApiImplicitParams({
242
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
249
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
243
	@ApiOperation(value = "")
250
	@ApiOperation(value = "")
-
 
251
	@Transactional
244
	public ResponseEntity<?> employeeAttendance(HttpServletRequest request, @RequestParam(name = "userId") int userId,
252
	public ResponseEntity<?> employeeAttendance(HttpServletRequest request, @RequestParam(name = "userId") int userId,
245
			@RequestParam(name = "deviceId") int deviceId, @RequestParam(name = "punchType") PunchType punchType,
253
			@RequestParam(name = "deviceId") int deviceId, @RequestParam(name = "punchType") PunchType punchType,
246
			@RequestParam(name = "punch") String punch) {
254
			@RequestParam(name = "punch") String punch) {
247
 
255
 
248
		EmployeeAttendance employeeAttendance = new EmployeeAttendance();
256
		EmployeeAttendance employeeAttendance = new EmployeeAttendance();
Line 297... Line 305...
297
		 * List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores(); for
305
		 * List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores(); for
298
		 * (FofoStore fofoStore : fofoStores) { if (fofoStore.getLatitude() != null &&
306
		 * (FofoStore fofoStore : fofoStores) { if (fofoStore.getLatitude() != null &&
299
		 * fofoStore.getLongitude() != null) { CustomRetailer customRetailer =
307
		 * fofoStore.getLongitude() != null) { CustomRetailer customRetailer =
300
		 * retailerService.getFofoRetailer(fofoStore.getId());
308
		 * retailerService.getFofoRetailer(fofoStore.getId());
301
		 */
309
		 */
302
		OkHttpClient client = new OkHttpClient();
-
 
303
 
-
 
304
		okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
-
 
305
		JSONObject geofe = new JSONObject();
310
		JSONObject geofe = new JSONObject();
306
		JSONArray geofences = new JSONArray();
311
		JSONArray geofences = new JSONArray();
307
		JSONObject geometry = new JSONObject();
312
		JSONObject geometry = new JSONObject();
308
		JSONObject geo = new JSONObject();
313
		JSONObject geo = new JSONObject();
309
		JSONArray coordinates = new JSONArray();
314
		JSONArray coordinates = new JSONArray();
Line 315... Line 320...
315
		JSONObject metadata = new JSONObject();
320
		JSONObject metadata = new JSONObject();
316
		metadata.put("name", "Test");
321
		metadata.put("name", "Test");
317
		geometry.put("radius", 500);
322
		geometry.put("radius", 500);
318
		geofences.put(geometry);
323
		geofences.put(geometry);
319
		geofe.put("geofences", geofences);
324
		geofe.put("geofences", geofences);
320
		okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, geofe.toString());
-
 
321
		String authString = "Basic "
-
 
322
				+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
-
 
323
 
-
 
324
		Request request1 = new Request.Builder().url("https://v3.api.hypertrack.com/geofences").post(body)
-
 
325
				.addHeader("Authorization", authString).build();
-
 
326
 
325
 
-
 
326
		String responseBody = restClient.postJson("https://v3.api.hypertrack.com/geofences",
327
		Response response = client.newCall(request1).execute();
327
				geofe.toString(), hypertrackAuthHeader());
328
		String geofenceId = null;
328
		String geofenceId = null;
329
		JSONArray ja = new JSONArray(response.body().string());
329
		JSONArray ja = new JSONArray(responseBody);
330
		for (int i = 0; i < ja.length(); i++) {
330
		for (int i = 0; i < ja.length(); i++) {
331
			JSONObject c = ja.getJSONObject(i);
331
			JSONObject c = ja.getJSONObject(i);
332
			geofenceId = c.getString("geofence_id");
332
			geofenceId = c.getString("geofence_id");
333
		}
333
		}
334
 
334
 
335
		LOGGER.info("response" + response.body().string());
335
		LOGGER.info("response" + responseBody);
336
 
336
 
337
		return responseSender.ok(geofenceId);
337
		return responseSender.ok(geofenceId);
338
 
338
 
339
	}
339
	}
340
 
340
 
341
	@RequestMapping(value = "/getgeofence", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
341
	@RequestMapping(value = "/getgeofence", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
342
	public ResponseEntity<?> getAllGeofences(HttpServletRequest request)
342
	public ResponseEntity<?> getAllGeofences(HttpServletRequest request)
343
			throws IOException, ProfitMandiBusinessException {
343
			throws IOException, ProfitMandiBusinessException {
344
 
-
 
345
		OkHttpClient client = new OkHttpClient();
-
 
346
 
-
 
347
		String authString = "Basic "
-
 
348
				+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
-
 
349
 
-
 
350
		// Get geofences created for all app users
-
 
351
		Request request1 = new Request.Builder().url("https://v3.api.hypertrack.com/geofences")
344
		String body = restClient.get("https://v3.api.hypertrack.com/geofences", null, hypertrackAuthHeader());
352
				.addHeader("Authorization", authString).build();
-
 
353
 
-
 
354
		Response response = client.newCall(request1).execute();
-
 
355
		return responseSender.ok(response.body().string());
345
		return responseSender.ok(body);
356
 
-
 
357
	}
346
	}
358
 
347
 
359
	@RequestMapping(value = "/deletegeofence", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
348
	@RequestMapping(value = "/deletegeofence", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
360
	public ResponseEntity<?> deleteGeofences(HttpServletRequest request, String geofenceId)
349
	public ResponseEntity<?> deleteGeofences(HttpServletRequest request, String geofenceId)
361
			throws IOException, ProfitMandiBusinessException {
350
			throws IOException, ProfitMandiBusinessException {
362
		OkHttpClient client = new OkHttpClient();
351
		HttpDelete delete = new HttpDelete("https://v3.api.hypertrack.com/geofences/" + geofenceId);
363
 
-
 
364
		String authString = "Basic "
352
		hypertrackAuthHeader().forEach(delete::setHeader);
365
				+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
353
		try (CloseableHttpClient client = HttpClientFactory.apacheHttp();
366
 
-
 
367
		Request request1 = new Request.Builder().url("https://v3.api.hypertrack.com/geofences/" + geofenceId).delete()
-
 
368
				.addHeader("Authorization", authString).build();
354
		     CloseableHttpResponse response = client.execute(delete)) {
369
 
-
 
370
		Response response = client.newCall(request1).execute();
355
			String body = response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "";
371
		return responseSender.ok(response.body().string());
356
			return responseSender.ok(body);
372
 
357
		}
373
	}
358
	}
374
 
359
 
375
	@RequestMapping(value = "/devices/partner/location", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
360
	@RequestMapping(value = "/devices/partner/location", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
376
	public ResponseEntity<?> getPartnerLocationByKeyId(HttpServletRequest request, @RequestParam int userId,
361
	public ResponseEntity<?> getPartnerLocationByKeyId(HttpServletRequest request, @RequestParam int userId,
377
			@RequestParam int deviceId, @RequestParam int fofoId)
362
			@RequestParam int deviceId, @RequestParam int fofoId)
378
			throws IOException, MessagingException, ProfitMandiBusinessException {
363
			throws IOException, MessagingException, ProfitMandiBusinessException {
379
 
364
 
380
		LOGGER.info("UserId -> {}, Device Id -> {}", userId, deviceId);
365
		LOGGER.info("UserId -> {}, Device Id -> {}", userId, deviceId);
-
 
366
		// Read-only tx in service: connection released before the external HTTP call below.
-
 
367
		HyperTrackService.PartnerLocationContext ctx = hyperTrackService.loadPartnerLocationContext(userId, deviceId, fofoId);
381
		FofoStore fs = fofoStoreRepository.selectByRetailerId(fofoId);
368
		FofoStore fs = ctx.fofoStore;
382
 
-
 
383
		LOGGER.info("fs{}", fs);
369
		LOGGER.info("fs{}", fs);
384
		Double dist = null;
370
		Double dist = null;
385
		HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
371
		HypertrackKey hyperTrackKey = ctx.hypertrackKey;
386
 
-
 
387
		LOGGER.info("hyperTrackKey {}" + hyperTrackKey);
372
		LOGGER.info("hyperTrackKey {}" + hyperTrackKey);
388
		OkHttpClient client = new OkHttpClient();
-
 
389
 
-
 
390
		String authString = "Basic "
373
		String responseBody = restClient.get(
391
				+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
-
 
392
 
-
 
393
		Request request1 = new Request.Builder()
-
 
394
				.url("https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId()).get()
374
				"https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId(),
395
				.addHeader("Authorization", authString).build();
375
				null, hypertrackAuthHeader());
396
 
-
 
397
		Response response = client.newCall(request1).execute();
-
 
398
 
-
 
399
		JSONObject jsonObj = new JSONObject(response.body().string());
376
		JSONObject jsonObj = new JSONObject(responseBody);
400
 
377
 
401
		LOGGER.info("jsonObj" + jsonObj);
378
		LOGGER.info("jsonObj" + jsonObj);
402
		JSONObject deviceStatus = jsonObj.getJSONObject("device_status");
379
		JSONObject deviceStatus = jsonObj.getJSONObject("device_status");
403
		String status = deviceStatus.getString("value");
380
		String status = deviceStatus.getString("value");
404
 
381
 
405
		if (status.equals("inactive") || status.equals("disconnected")) {
382
		if (status.equals("inactive") || status.equals("disconnected")) {
406
 
383
 
407
			LOGGER.info("status" + status);
384
			LOGGER.info("status" + status);
408
			okhttp3.RequestBody reqbody = okhttp3.RequestBody.create(null, new byte[0]);
-
 
409
 
-
 
410
			Request request2 = new Request.Builder()
385
			String startResp = restClient.post(
411
					.url("https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId() + "/start")
386
					"https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId() + "/start",
412
					.post(reqbody).addHeader("Authorization", authString).build();
-
 
413
 
-
 
414
			Response resp = client.newCall(request2).execute();
387
					"", hypertrackAuthHeader());
415
			LOGGER.info("resp" + resp);
388
			LOGGER.info("resp" + startResp);
416
 
389
 
417
		} else if (status.equals("active")) {
390
		} else if (status.equals("active")) {
418
			JSONObject c = jsonObj.getJSONObject("location");
391
			JSONObject c = jsonObj.getJSONObject("location");
419
			JSONObject d = c.getJSONObject("geometry");
392
			JSONObject d = c.getJSONObject("geometry");
420
			JSONArray coords = d.getJSONArray("coordinates");
393
			JSONArray coords = d.getJSONArray("coordinates");