Subversion Repositories SmartDukaan

Rev

Rev 27423 | Rev 27427 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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();
27421 tejbeer 87
			hyperTrackKey.setHypertrackKeyId(hyperTrackKeyModel.getHyperTrackId());
27417 tejbeer 88
			hyperTrackKey.setDeviceId(hyperTrackKeyModel.getDeviceId());
89
		}
27422 tejbeer 90
 
27421 tejbeer 91
		hyperTrackKey.setUserId(hyperTrackKeyModel.getUserId());
27417 tejbeer 92
		hyperTrackKey.setCreatedTimestamp(LocalDateTime.now());
93
		hyperTrackKey.setUpdatedTimestamp(LocalDateTime.now());
94
		hypertrackKeyRepository.persist(hyperTrackKey);
95
		return responseSender.ok(true);
96
	}
97
 
27421 tejbeer 98
	@RequestMapping(value = "/devices/location", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
99
	public ResponseEntity<?> getLocationByKeyId(HttpServletRequest request, @RequestParam int userId,
27426 tejbeer 100
			@RequestParam int deviceId) throws IOException, MessagingException, ProfitMandiBusinessException {
27421 tejbeer 101
 
27426 tejbeer 102
		double lat1 = 28.516045882182738;
103
		double lng1 = 77.3771954997187;
27421 tejbeer 104
 
105
		HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
27426 tejbeer 106
		EmployeeAttendance employeeAttendance = employeeAttendanceRepository.selectLatestPunchTimeStamp();
27421 tejbeer 107
		OkHttpClient client = new OkHttpClient();
108
 
109
		String authString = "Basic "
110
				+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
111
 
112
		Request request1 = new Request.Builder()
27426 tejbeer 113
				.url("https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId()).get()
27421 tejbeer 114
				.addHeader("Authorization", authString).build();
115
 
116
		Response response = client.newCall(request1).execute();
117
		JSONObject jsonObj = new JSONObject(response.body().string());
118
		JSONObject c = jsonObj.getJSONObject("location");
119
		JSONObject d = c.getJSONObject("geometry");
120
		JSONArray coords = d.getJSONArray("coordinates");
121
		double lat2 = coords.getDouble(1);
122
		double lng2 = coords.getDouble(0);
123
		double dist = distance(lat1, lng1, lat2, lng2, "m");
124
		EmployeeAttendanceModel em = new EmployeeAttendanceModel();
125
		em.setDistance(dist);
27422 tejbeer 126
		em.setLatitude(lat2);
127
		em.setLongitude(lng2);
27426 tejbeer 128
		if (employeeAttendance.getCreateTimestamp().toLocalDate().equals(LocalDate.now())) {
129
			if (employeeAttendance.getPunch().equals("punchIn")) {
27421 tejbeer 130
				em.setPunchIn(true);
131
			} else {
132
				em.setPunchIn(false);
133
			}
134
		} else {
135
			em.setPunchIn(false);
136
		}
137
 
138
		return responseSender.ok(em);
139
	}
140
 
141
	private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
142
		if ((lat1 == lat2) && (lon1 == lon2)) {
143
			return 0;
144
		} else {
145
			double theta = lon1 - lon2;
146
			double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2))
147
					+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
148
			dist = Math.acos(dist);
149
			dist = Math.toDegrees(dist);
150
			dist = dist * 60 * 1.1515;
151
			if (unit.equals("m")) {
152
				dist = dist * 1.609344;
153
				dist = dist / 0.0010000;
154
			} else if (unit.equals("N")) {
155
				dist = dist * 0.8684;
156
			}
157
			return (dist);
158
		}
159
	}
160
 
161
	@RequestMapping(value = "/employee/attendance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
162
	@ApiImplicitParams({
163
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
164
	@ApiOperation(value = "")
165
	public ResponseEntity<?> employeeAttendance(HttpServletRequest request, @RequestParam(name = "userId") int userId,
166
			@RequestParam(name = "deviceId") int deviceId, @RequestParam(name = "punchType") PunchType punchType,
167
			@RequestParam(name = "punch") String punch) {
168
 
169
		EmployeeAttendance employeeAttendance = new EmployeeAttendance();
170
		employeeAttendance.setUserId(userId);
171
		employeeAttendance.setDeviceId(deviceId);
172
		employeeAttendance.setPunch(punch);
173
		employeeAttendance.setPunchType(punchType);
174
		employeeAttendance.setCreateTimestamp(LocalDateTime.now());
175
		employeeAttendanceRepository.persist(employeeAttendance);
176
		return responseSender.ok(true);
177
	}
178
 
27426 tejbeer 179
	@RequestMapping(value = "/create/geofence", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
180
	public ResponseEntity<?> createGreofence(HttpServletRequest request)
181
			throws IOException, ProfitMandiBusinessException {
182
 
183
		List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores();
184
		for (FofoStore fofoStore : fofoStores) {
185
			CustomRetailer customRetailer = retailerService.getFofoRetailer(fofoStore.getId());
186
			OkHttpClient client = new OkHttpClient();
187
 
188
			okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
189
			JSONObject geofe = new JSONObject();
190
			JSONArray geofences = new JSONArray();
191
			JSONObject geometry = new JSONObject();
192
			JSONObject geo = new JSONObject();
193
			JSONArray coordinates = new JSONArray();
194
			coordinates.put(fofoStore.getLongitude());
195
			coordinates.put(fofoStore.getLatitude());
196
			geo.put("type", "Point");
197
			geo.put("coordinates", coordinates);
198
			geometry.put("geometry", geo);
199
			JSONObject metadata = new JSONObject();
200
			metadata.put("name", customRetailer.getBusinessName());
201
			metadata.put("city", customRetailer.getAddress().getCity());
202
			geometry.put("metadata", metadata);
203
			geometry.put("radius", 500);
204
 
205
			geofences.put(geometry);
206
			geofe.put("geofences", geofences);
207
			okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, geofe.toString());
208
			String authString = "Basic "
209
					+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
210
 
211
			Request request1 = new Request.Builder().url("https://v3.api.hypertrack.com/geofences").post(body)
212
					.addHeader("Authorization", authString).build();
213
 
214
			Response response = client.newCall(request1).execute();
215
 
216
		}
217
		return responseSender.ok(true);
218
 
219
	}
220
 
27417 tejbeer 221
}