Skip to content

Commit

Permalink
update location
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunhThanhDe committed Jun 6, 2023
1 parent 94eb22d commit a60c678
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,72 @@ INSERT INTO role (name) VALUES('ROLE_MODERATOR');
INSERT INTO role (name) VALUES('ROLE_ADMIN');
INSERT INTO listdevice(name, location) VALUES('all', 'all');
INSERT INTO listdevice(name, location) VALUES('prototype', 'all');
INSERT INTO listdevice(name, location) VALUES('An Giang', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Bà Rịa - Vũng Tàu', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Bắc Giang', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Bắc Kạn', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Bạc Liêu', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Bắc Ninh', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Bến Tre', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Bình Định', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Bình Dương', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Bình Phước', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Bình Thuận', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Cà Mau', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Cần Thơ', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Cao Bằng', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Đà Nẵng', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Đắk Lắk', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Đắk Nông', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Điện Biên', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Đồng Nai', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Đồng Tháp', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Gia Lai', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Hà Giang', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Hà Nam', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Hà Nội', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Hà Tĩnh', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Hải Dương', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Hải Phòng', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Hậu Giang', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Hòa Bình', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Hưng Yên', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Khánh Hòa', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Kiên Giang', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Kon Tum', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Lai Châu', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Lâm Đồng', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Lạng Sơn', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Lào Cai', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Long An', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Nam Định', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Nghệ An', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Ninh Bình', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Ninh Thuận', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Phú Thọ','Bắc');
INSERT INTO listdevice(name, location) VALUES('Phú Yên', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Quảng Bình', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Quảng Nam', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Quảng Ngãi', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Quảng Ninh', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Quảng Trị', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Sóc Trăng', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Sơn La', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Tây Ninh', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Thái Bình', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Thái Nguyên', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Thanh Hóa', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Thừa Thiên Huế', 'Trung');
INSERT INTO listdevice(name, location) VALUES('Tiền Giang', 'Nam');
INSERT INTO listdevice(name, location) VALUES('TP. Hồ Chí Minh', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Trà Vinh', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Tuyên Quang', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Vĩnh Long', 'Nam');
INSERT INTO listdevice(name, location) VALUES('Vĩnh Phúc', 'Bắc');
INSERT INTO listdevice(name, location) VALUES('Yên Bái', 'Bắc');
INSERT INTO listdevice_user(device_id, user_id) VALUES("1","1");
INSERT INTO user(name, username, password, company, email, contact) VALUES ("MODERATOR","root","$2a$10$icvl6beFqq9A9yL8Th5xGuhaM8dRkAFQOLgLXwJv.6SUX3lqOPWXu","none","none",0);
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>

<build>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/vnptt/tms/service/impl/DeviceService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.vnptt.tms.service.impl;

import com.maxmind.geoip2.WebServiceClient;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.vnptt.tms.api.output.TerminalStudioOutput;
import com.vnptt.tms.converter.DeviceConverter;
import com.vnptt.tms.dto.DeviceDTO;
Expand All @@ -15,6 +18,7 @@
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -125,9 +129,28 @@ public List<DeviceDTO> findAll() {
@Override
public DeviceDTO findOneBySn(String ip, String serialnumber) {
DeviceEntity entity = deviceRepository.findOneBySn(serialnumber);

if (entity == null) {
throw new ResourceNotFoundException("Not found device with Serialnumber = " + serialnumber);
}

WebServiceClient client = new WebServiceClient.Builder(874837, "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")
.build();


InetAddress ipAddress = null;
try {
ipAddress = InetAddress.getByName(ip);
// Do the lookup
CityResponse response = client.city(ipAddress);

City city = response.getCity();
System.out.println(city.getName()); // 'HaNoi'
entity.setLocation(city.getName());
} catch (Exception e) {
System.out.print("ip can check " + ip + " error: " + e);
}

if (!Objects.equals(entity.getIp(), ip)) {
entity.setIp(ip);
deviceRepository.save(entity);
Expand Down

0 comments on commit a60c678

Please sign in to comment.