Subversion Repositories SmartDukaan

Rev

Rev 27419 | Rev 27422 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 27419 Rev 27421
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
-
 
3
import java.io.IOException;
-
 
4
import java.time.LocalDate;
3
import java.time.LocalDateTime;
5
import java.time.LocalDateTime;
-
 
6
import java.util.Base64;
-
 
7
import java.util.List;
4
 
8
 
-
 
9
import javax.mail.MessagingException;
5
import javax.servlet.http.HttpServletRequest;
10
import javax.servlet.http.HttpServletRequest;
6
 
11
 
7
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.Logger;
13
import org.apache.logging.log4j.Logger;
-
 
14
import org.apache.velocity.app.VelocityEngine;
-
 
15
import org.json.JSONArray;
-
 
16
import org.json.JSONObject;
9
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.http.MediaType;
18
import org.springframework.http.MediaType;
11
import org.springframework.http.ResponseEntity;
19
import org.springframework.http.ResponseEntity;
-
 
20
import org.springframework.mail.javamail.JavaMailSender;
12
import org.springframework.stereotype.Controller;
21
import org.springframework.stereotype.Controller;
13
import org.springframework.transaction.annotation.Transactional;
22
import org.springframework.transaction.annotation.Transactional;
14
import org.springframework.web.bind.annotation.RequestBody;
23
import org.springframework.web.bind.annotation.RequestBody;
15
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestMapping;
16
import org.springframework.web.bind.annotation.RequestMethod;
25
import org.springframework.web.bind.annotation.RequestMethod;
-
 
26
import org.springframework.web.bind.annotation.RequestParam;
17
 
27
 
18
import com.google.gson.Gson;
28
import com.google.gson.Gson;
-
 
29
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
30
import com.spice.profitmandi.common.model.CustomRetailer;
19
import com.spice.profitmandi.common.web.util.ResponseSender;
31
import com.spice.profitmandi.common.web.util.ResponseSender;
-
 
32
import com.spice.profitmandi.dao.entity.dtr.EmployeeAttendance;
20
import com.spice.profitmandi.dao.entity.dtr.HyperTrackKeyModel;
33
import com.spice.profitmandi.dao.entity.dtr.HyperTrackKeyModel;
21
import com.spice.profitmandi.dao.entity.dtr.HypertrackKey;
34
import com.spice.profitmandi.dao.entity.dtr.HypertrackKey;
-
 
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;
22
import com.spice.profitmandi.dao.repository.user.HypertrackKeyRepository;
41
import com.spice.profitmandi.dao.repository.user.HypertrackKeyRepository;
-
 
42
import com.spice.profitmandi.service.user.RetailerService;
23
 
43
 
24
import io.swagger.annotations.ApiImplicitParam;
44
import io.swagger.annotations.ApiImplicitParam;
25
import io.swagger.annotations.ApiImplicitParams;
45
import io.swagger.annotations.ApiImplicitParams;
26
import io.swagger.annotations.ApiOperation;
46
import io.swagger.annotations.ApiOperation;
-
 
47
import okhttp3.OkHttpClient;
-
 
48
import okhttp3.Request;
-
 
49
import okhttp3.Response;
27
 
50
 
28
@Controller
51
@Controller
29
@Transactional(rollbackFor = Throwable.class)
52
@Transactional(rollbackFor = Throwable.class)
30
public class HyperTrackController {
53
public class HyperTrackController {
31
 
54
 
Line 38... Line 61...
38
	Gson gson;
61
	Gson gson;
39
 
62
 
40
	@Autowired
63
	@Autowired
41
	private HypertrackKeyRepository hypertrackKeyRepository;
64
	private HypertrackKeyRepository hypertrackKeyRepository;
42
 
65
 
-
 
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
 
43
	private static final String ACCOUNT_ID = "aZ6flHhrgPIEl18buHdPBdueEN4";
80
	private static final String ACCOUNT_ID = "aZ6flHhrgPIEl18buHdPBdueEN4";
44
	private static final String SECRET_KEY = "a7rsX5B4UNNfTTx1-IJ19qdH48BT4YvBKlQJg3n3_KKNe7WWych55g";
81
	private static final String SECRET_KEY = "a7rsX5B4UNNfTTx1-IJ19qdH48BT4YvBKlQJg3n3_KKNe7WWych55g";
45
 
82
 
46
	@RequestMapping(value = "/devices/hypertrackId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
83
	@RequestMapping(value = "/devices/hypertrackId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
47
	@ApiImplicitParams({
84
	@ApiImplicitParams({
Line 51... Line 88...
51
			@RequestBody HyperTrackKeyModel hyperTrackKeyModel) {
88
			@RequestBody HyperTrackKeyModel hyperTrackKeyModel) {
52
		HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(hyperTrackKeyModel.getUserId(),
89
		HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(hyperTrackKeyModel.getUserId(),
53
				hyperTrackKeyModel.getDeviceId());
90
				hyperTrackKeyModel.getDeviceId());
54
		if (hyperTrackKey == null) {
91
		if (hyperTrackKey == null) {
55
			hyperTrackKey = new HypertrackKey();
92
			hyperTrackKey = new HypertrackKey();
-
 
93
			hyperTrackKey.setHypertrackKeyId(hyperTrackKeyModel.getHyperTrackId());
56
			hyperTrackKey.setDeviceId(hyperTrackKeyModel.getDeviceId());
94
			hyperTrackKey.setDeviceId(hyperTrackKeyModel.getDeviceId());
57
			hyperTrackKey.setUserId(hyperTrackKeyModel.getUserId());
-
 
58
		}
95
		}
-
 
96
	
59
		hyperTrackKey.setHypertrackKeyId(hyperTrackKeyModel.getHyperTrackId());
97
		hyperTrackKey.setUserId(hyperTrackKeyModel.getUserId());
60
		hyperTrackKey.setCreatedTimestamp(LocalDateTime.now());
98
		hyperTrackKey.setCreatedTimestamp(LocalDateTime.now());
61
		hyperTrackKey.setUpdatedTimestamp(LocalDateTime.now());
99
		hyperTrackKey.setUpdatedTimestamp(LocalDateTime.now());
62
		hypertrackKeyRepository.persist(hyperTrackKey);
100
		hypertrackKeyRepository.persist(hyperTrackKey);
63
		return responseSender.ok(true);
101
		return responseSender.ok(true);
64
	}
102
	}
65
 
103
 
-
 
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
 
66
}
187
}