오윤석

기기 관련 API 추가

......@@ -8,6 +8,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sunnni.smartdoorlock.data.Auth;
import com.sunnni.smartdoorlock.data.Device;
import com.sunnni.smartdoorlock.data.RemoteRecord;
import com.sunnni.smartdoorlock.data.Setting;
import com.sunnni.smartdoorlock.data.Video;
......@@ -260,4 +261,57 @@ public class Api {
});
}
static public void getDevices(final Callback callback) {
callApi("GET", "/api/device", null, new Callback() {
@Override
public void callbackMethod(Object obj) {
ApiResult apiResult = (ApiResult) obj;
if(apiResult.isSuccess()) {
JsonObject resp = (JsonObject) apiResult.getData();
if(resp.has("deviceList")) {
ArrayList<Device> videos = new ArrayList<Device>();
Iterator it = resp.getAsJsonArray("deviceList").iterator();
while(it.hasNext()) {
JsonObject jsonObject = (JsonObject) it.next();
videos.add(new Device(jsonObject.get("device_id").getAsInt(), jsonObject.get("rfid_id").getAsString(), jsonObject.get("created").getAsString()));
}
callback.callbackMethod(videos);
} else {
callback.callbackMethod(null);
}
} else {
callback.callbackMethod(null);
}
}
});
}
static public void removeDevice(Device device, final Callback callback) {
callApi("DELETE", "/api/device/" + device.getDeviceId(), null, new Callback() {
@Override
public void callbackMethod(Object obj) {
ApiResult apiResult = (ApiResult) obj;
if(apiResult.isSuccess()) {
callback.callbackMethod(new Boolean(true));
} else {
callback.callbackMethod(null);
}
}
});
}
static public void requestAddDevice(final Callback callback) {
callApi("POST", "/api/device/request", null, new Callback() {
@Override
public void callbackMethod(Object obj) {
ApiResult apiResult = (ApiResult) obj;
if(apiResult.isSuccess()) {
callback.callbackMethod(new Boolean(true));
} else {
callback.callbackMethod(null);
}
}
});
}
}
......
package com.sunnni.smartdoorlock.data;
public class Device {
public String deviceNumber;
public String registerDate;
private int deviceId;
private String RFIDId;
private String created;
public Device(String number, String date){
this.deviceNumber = number;
this.registerDate = date;
public Device(int deviceId, String RFIDId, String created){
this.deviceId = deviceId;
this.RFIDId = RFIDId;
this.created = created;
}
public String getCreated() {
return created;
}
public int getDeviceId() {
return deviceId;
}
public String getRFIDId() {
return RFIDId;
}
}
......
......@@ -9,17 +9,22 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;
import com.sunnni.smartdoorlock.R;
import com.sunnni.smartdoorlock.api.Api;
import com.sunnni.smartdoorlock.data.Device;
import com.sunnni.smartdoorlock.data.RemoteRecord;
import java.util.ArrayList;
import java.util.Objects;
import static android.view.InputDevice.getDevice;
public class DeviceManagerActivity extends AppCompatActivity {
ArrayList<Device> mList = new ArrayList<Device>();
......@@ -37,7 +42,7 @@ public class DeviceManagerActivity extends AppCompatActivity {
setToolbar(mToolbar);
setRecyclerView();
setDeviceList();
getDevice();
init();
}
......@@ -78,26 +83,21 @@ public class DeviceManagerActivity extends AppCompatActivity {
mRecyclerView.setAdapter(mAdapter);
}
// 기기 목록 dummy data -> api 생성되면 수정
private void setDeviceList(){
Device temp;
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mList.add(temp = new Device("0047617826460", "2020.10.08 17:21:30"));
mAdapter.notifyDataSetChanged();
public void getDevice() {
Api.getDevices(new Api.Callback() {
@Override
public void callbackMethod(Object obj) {
if(obj == null) {
Toast.makeText(getApplicationContext(), "연결 상태가 불안정합니다.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(DeviceManagerActivity.this, MainActivity.class));
return;
} else {
mList.clear();
mList.addAll(0, (ArrayList<Device>) obj);
mAdapter.notifyDataSetChanged();
}
}
});
}
private void remoteControlDialog() {
......@@ -107,7 +107,18 @@ public class DeviceManagerActivity extends AppCompatActivity {
builder.setPositiveButton("추가", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DeviceManagerActivity.this, "도어락에 기기를 태그해주세요.", Toast.LENGTH_LONG).show();
Api.requestAddDevice(new Api.Callback() {
@Override
public void callbackMethod(Object obj) {
if(obj == null) {
Toast.makeText(getApplicationContext(), "연결 상태가 불안정합니다.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(DeviceManagerActivity.this, MainActivity.class));
return;
} else {
Toast.makeText(DeviceManagerActivity.this, "도어락에 기기를 태그해주세요.", Toast.LENGTH_LONG).show();
}
}
});
}
});
builder.setNegativeButton("취소", new DialogInterface.OnClickListener() {
......
package com.sunnni.smartdoorlock.ui;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
......@@ -14,6 +15,7 @@ import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.sunnni.smartdoorlock.R;
import com.sunnni.smartdoorlock.api.Api;
import com.sunnni.smartdoorlock.data.Device;
import java.util.ArrayList;
......@@ -33,9 +35,9 @@ public class DeviceRecyclerViewAdapter extends RecyclerView.Adapter<DeviceRecycl
this.mTrashcan = v.findViewById(R.id.img_trashcan);
}
void bind(Device device) {
mTvDeviceNum.setText(device.deviceNumber);
mTvRegisterDate.setText(device.registerDate);
void bind(final Device device) {
mTvDeviceNum.setText(device.getRFIDId());
mTvRegisterDate.setText(device.getCreated());
mTrashcan.setOnClickListener(new View.OnClickListener() {
@Override
......@@ -46,7 +48,19 @@ public class DeviceRecyclerViewAdapter extends RecyclerView.Adapter<DeviceRecycl
builder.setPositiveButton("삭제", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(itemView.getContext(), "삭제되었습니다.", Toast.LENGTH_SHORT).show();
Api.removeDevice(device, new Api.Callback() {
@Override
public void callbackMethod(Object obj) {
if(obj == null) {
Toast.makeText(itemView.getContext(), "연결 상태가 불안정합니다.", Toast.LENGTH_SHORT).show();
return;
} else {
Toast.makeText(itemView.getContext(), "삭제되었습니다.", Toast.LENGTH_LONG).show();
// TODO : 목록 refresh
// DeviceManagerActivity.getDevices 를 호출하거나 DeviceManagerActivity.mList에서 device 제거
}
}
});
}
});
builder.setNegativeButton("취소", new DialogInterface.OnClickListener() {
......