77 lines
2.1 KiB
Dart
77 lines
2.1 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:device_info/device_info.dart';
|
|
import 'package:fcs/vo/device.dart';
|
|
|
|
import 'base_model.dart';
|
|
import 'constants.dart';
|
|
import 'firebase_helper.dart';
|
|
|
|
class PhoneDeviceModel extends BaseModel {
|
|
List<PhoneDevice> devices = new List();
|
|
|
|
bool isLogout = false;
|
|
void initUser(user) {
|
|
super.initUser(user);
|
|
_loadDevices();
|
|
}
|
|
|
|
Future<void> _loadDevices() async {
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
.collection(
|
|
"/$biz_collection/${setting.okEnergyId}/$user_collection/${user.docID}/$device_collection")
|
|
.where("primary_device", isEqualTo: false)
|
|
.snapshots();
|
|
|
|
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
|
|
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
|
|
|
|
snapshots.listen((snaps) async {
|
|
devices = snaps.documents.map((documentSnapshot) {
|
|
var data = PhoneDevice.fromMap(
|
|
documentSnapshot.data, documentSnapshot.documentID);
|
|
if (!data.deviceOn &&
|
|
!data.primaryDevice &&
|
|
data.id == androidInfo.androidId &&
|
|
!documentSnapshot.metadata.isFromCache) {
|
|
this.isLogout = true;
|
|
this.mainModel.logout();
|
|
notifyListeners();
|
|
}
|
|
return data;
|
|
}).toList();
|
|
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
|
|
@override
|
|
logout() async {
|
|
devices = [];
|
|
}
|
|
|
|
bool isLogoutDevice() {
|
|
return this.isLogout;
|
|
}
|
|
|
|
setDevice(bool status) {
|
|
this.isLogout = status;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> confirmDevice(String id, String deviceID) async {
|
|
await request("/dev/on", "POST",
|
|
payload: {"id": id, "device_id": deviceID}, token: await getToken());
|
|
}
|
|
|
|
Future<void> logoutDevice(String id, String deviceID) async {
|
|
await request("/dev/off", "POST",
|
|
payload: {"id": id, "device_id": deviceID}, token: await getToken());
|
|
}
|
|
|
|
Future<void> setPrimaryDevice(String id, String deviceID) async {
|
|
await request("/dev", "PUT",
|
|
payload: {"id": id, "device_id": deviceID}, token: await getToken());
|
|
}
|
|
}
|