Subversion Repositories SmartDukaan

Rev

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

Rev 27423 Rev 27426
Line 15... Line 15...
15
import org.json.JSONArray;
15
import org.json.JSONArray;
16
import org.json.JSONObject;
16
import org.json.JSONObject;
17
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.http.MediaType;
18
import org.springframework.http.MediaType;
19
import org.springframework.http.ResponseEntity;
19
import org.springframework.http.ResponseEntity;
20
import org.springframework.mail.javamail.JavaMailSender;
-
 
21
import org.springframework.stereotype.Controller;
20
import org.springframework.stereotype.Controller;
22
import org.springframework.transaction.annotation.Transactional;
21
import org.springframework.transaction.annotation.Transactional;
23
import org.springframework.web.bind.annotation.RequestBody;
22
import org.springframework.web.bind.annotation.RequestBody;
24
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestMapping;
25
import org.springframework.web.bind.annotation.RequestMethod;
24
import org.springframework.web.bind.annotation.RequestMethod;
Line 65... Line 64...
65
 
64
 
66
	@Autowired
65
	@Autowired
67
	private EmployeeAttendanceRepository employeeAttendanceRepository;
66
	private EmployeeAttendanceRepository employeeAttendanceRepository;
68
 
67
 
69
	@Autowired
68
	@Autowired
70
	private VelocityEngine velocityEngine;
-
 
71
 
-
 
72
	@Autowired
-
 
73
	private FofoStoreRepository fofoStoreRepository;
69
	private FofoStoreRepository fofoStoreRepository;
74
 
70
 
75
	@Autowired
71
	@Autowired
76
	private RetailerService retailerService;
72
	private RetailerService retailerService;
77
	@Autowired
-
 
78
	JavaMailSender mailSender;
-
 
79
 
73
 
80
	private static final String ACCOUNT_ID = "aZ6flHhrgPIEl18buHdPBdueEN4";
74
	private static final String ACCOUNT_ID = "aZ6flHhrgPIEl18buHdPBdueEN4";
81
	private static final String SECRET_KEY = "a7rsX5B4UNNfTTx1-IJ19qdH48BT4YvBKlQJg3n3_KKNe7WWych55g";
75
	private static final String SECRET_KEY = "a7rsX5B4UNNfTTx1-IJ19qdH48BT4YvBKlQJg3n3_KKNe7WWych55g";
82
 
76
 
83
	@RequestMapping(value = "/devices/hypertrackId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
77
	@RequestMapping(value = "/devices/hypertrackId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
Line 101... Line 95...
101
		return responseSender.ok(true);
95
		return responseSender.ok(true);
102
	}
96
	}
103
 
97
 
104
	@RequestMapping(value = "/devices/location", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
98
	@RequestMapping(value = "/devices/location", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
105
	public ResponseEntity<?> getLocationByKeyId(HttpServletRequest request, @RequestParam int userId,
99
	public ResponseEntity<?> getLocationByKeyId(HttpServletRequest request, @RequestParam int userId,
106
			@RequestParam int deviceId) throws IOException, MessagingException {
100
			@RequestParam int deviceId) throws IOException, MessagingException, ProfitMandiBusinessException {
107
 
101
 
108
		double lat1 = 28.515867995437514;
102
		double lat1 = 28.516045882182738;
109
		double lng1 = 77.3770672558246;
103
		double lng1 = 77.3771954997187;
110
 
104
 
111
		HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
105
		HypertrackKey hyperTrackKey = hypertrackKeyRepository.selectByUserIdAndDeviceId(userId, deviceId);
112
 
-
 
113
		List<EmployeeAttendance> employeeAttendance = employeeAttendanceRepository.selectByUserIdKey(userId, deviceId,
106
		EmployeeAttendance employeeAttendance = employeeAttendanceRepository.selectLatestPunchTimeStamp();
114
				LocalDate.now());
-
 
115
 
-
 
116
		OkHttpClient client = new OkHttpClient();
107
		OkHttpClient client = new OkHttpClient();
117
 
108
 
118
		String authString = "Basic "
109
		String authString = "Basic "
119
				+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
110
				+ Base64.getEncoder().encodeToString(String.format("%s:%s", ACCOUNT_ID, SECRET_KEY).getBytes());
120
 
111
 
121
		Request request1 = new Request.Builder()
112
		Request request1 = new Request.Builder()
122
				.url("https://v3.api.hypertrack.com/devices/"+hyperTrackKey.getHypertrackKeyId())
113
				.url("https://v3.api.hypertrack.com/devices/" + hyperTrackKey.getHypertrackKeyId()).get()
123
				.get()
-
 
124
				.addHeader("Authorization", authString).build();
114
				.addHeader("Authorization", authString).build();
125
 
115
 
126
		Response response = client.newCall(request1).execute();
116
		Response response = client.newCall(request1).execute();
127
		JSONObject jsonObj = new JSONObject(response.body().string());
117
		JSONObject jsonObj = new JSONObject(response.body().string());
128
		JSONObject c = jsonObj.getJSONObject("location");
118
		JSONObject c = jsonObj.getJSONObject("location");
Line 133... Line 123...
133
		double dist = distance(lat1, lng1, lat2, lng2, "m");
123
		double dist = distance(lat1, lng1, lat2, lng2, "m");
134
		EmployeeAttendanceModel em = new EmployeeAttendanceModel();
124
		EmployeeAttendanceModel em = new EmployeeAttendanceModel();
135
		em.setDistance(dist);
125
		em.setDistance(dist);
136
		em.setLatitude(lat2);
126
		em.setLatitude(lat2);
137
		em.setLongitude(lng2);
127
		em.setLongitude(lng2);
138
		if (!employeeAttendance.isEmpty()) {
128
		if (employeeAttendance.getCreateTimestamp().toLocalDate().equals(LocalDate.now())) {
139
			if (employeeAttendance.get(0).getPunch().equals("punchIn")) {
129
			if (employeeAttendance.getPunch().equals("punchIn")) {
140
				em.setPunchIn(true);
130
				em.setPunchIn(true);
141
			} else {
131
			} else {
142
				em.setPunchIn(false);
132
				em.setPunchIn(false);
143
			}
133
			}
144
		} else {
134
		} else {
Line 184... Line 174...
184
		employeeAttendance.setCreateTimestamp(LocalDateTime.now());
174
		employeeAttendance.setCreateTimestamp(LocalDateTime.now());
185
		employeeAttendanceRepository.persist(employeeAttendance);
175
		employeeAttendanceRepository.persist(employeeAttendance);
186
		return responseSender.ok(true);
176
		return responseSender.ok(true);
187
	}
177
	}
188
 
178
 
-
 
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
 
189
}
221
}