DBhelper.js
3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Define your models and their properties
import Realm from 'realm'
class Waste {}
Waste.schema = {
name: 'Waste',
properties: {
weight: 'int',
diff: 'int',
date: 'float',
}
};
class ID {}
ID.schema={
name:'ID',
properties:{
id : 'string',
}
};
export function registerID(_id){
if(_id != ''){
realm.write(()=>{
let user = realm.create('ID',{id:_id});
console.log('success')
});
}
}
export function getId(){
let user = realm.objects('ID');
console.log(user.length);
if(user.length>0)
return true;
return false;
}
export function deleteId(){
let user = realm.objects('ID');
realm.write(()=>{
realm.delete(user);
console.log('success')
})
}
// Get the default Realm with support for our objects
let realm = new Realm({schema: [Waste, ID]});
export function add_data(responseJson){
for(i = 0; i < responseJson.length; i++){
realm.write(() => {
// responseJson[0].ras_id.S
let myWaste = realm.create('Waste', {
weight: parseFloat(responseJson[i].weight.N),
diff: parseFloat(responseJson[i].diff.N),
date: parseFloat(responseJson[i].save_date.S),
});
});
console.log(responseJson[i].save_date.S)
}
return responseJson.length;
}
export function get_now_weight(){
let wastes = realm.objects('Waste');
let recent = wastes.max('date');
var recentWeight = wastes.filtered('date = $0', recent);
console.log("now_weight",recentWeight)
if(recentWeight.length != 0)
return recentWeight[0].weight;
else return 0;
}
export function get_last_update(){
let recent = realm.objects('Waste').max('date');
let recentWeight = wastes.filtered('date = %0', recent);
console.log(recentWeight[0].weight)
return recentWeight[0].weight;
}
export function get_recent_date(){
return realm.objects('Waste').max("date");
}
// export function get_today_data(today){
// let wastes = realm.objects('Waste');
// var list = [];
// for(var i =0; i<wastes.length; i++){
// if(get_date(wastes[i].date) == get_date(today)){
// temp = [waste[i].date, waste[i].diff, waste[i].weight]
// list.append(temp);
// }
// }
// return list;
// }
export function get_today_data(){
var today_start = get_today_start();
console.log("today_start: ",today_start)
let wastes = realm.objects('Waste').filtered('date >= $0',today_start);
var list = [];
for(var i = 0; i < wastes.length; i++){
var save_time = new Date(wastes[i].date);
var temp = [save_time.getHours()+":"+save_time.getMinutes()+":"+save_time.getSeconds(), wastes[i].diff, wastes[i].weight+"g"]
list.push(temp);
}
console.log(list);
console.log("count:", wastes.length)
return list;
}
export function delete_all_data(today){
let all_wastes = realm.objects('Waste');
realm.write(() => {
realm.delete(all_wastes);
})
var wastes = realm.objects('Waste')
console.log("delete, count: ", wastes.length)
}
function get_today_start(){
var today = new Date();
var dd = today. getDate();
var mm = today.getMonth();
var yyyy = today. getFullYear();
var togettime = new Date(2019,11,13).getTime();
console.log(togettime)
return togettime;
}