-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstFragment.java
290 lines (224 loc) · 11.4 KB
/
FirstFragment.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package de.shaase9.xdripgb_gateway;
import static java.util.Date.from;
import android.content.Context;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import de.shaase9.xdripgb_gateway.databinding.FragmentFirstBinding;
public class FirstFragment extends Fragment {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private FragmentFirstBinding binding;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
binding = FragmentFirstBinding.inflate(inflater, container, false);
return binding.getRoot();
}
public Context mycontext;
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
mycontext = getContext();
DataStorage MyDataStorage = new DataStorage();
super.onViewCreated(view, savedInstanceState);
final int[] cyclecount = {0};
// Test cyclic task
final Runnable testcycle = new Runnable() {
public void run() {
// read Data from xDRIP
InputStream myStream=getStream("http://127.0.0.1:17580/pebble");
InputStreamReader isReader = new InputStreamReader(myStream);
BufferedReader reader = new BufferedReader(isReader);
StringBuffer sb = new StringBuffer();
String str = "";
try {
while ((str = reader.readLine()) != null) {
sb.append(str);
}
}
catch(Exception e){
// Toast.makeText(getContext(), "can't read xDrip data from http://127.0.0.1:17580/pebble",Toast.LENGTH_SHORT).show();
}
String BG = "";
String bgdelta = "";
String bgTrend = "";
String Datetime = "";
int timedelay = 0;
// decode JSON
try {
String jsonStr = sb.toString();
JSONObject jsonObject = new JSONObject(jsonStr);
BG = String.valueOf(jsonObject.getJSONArray("bgs").getJSONObject(0).get("sgv"));
bgdelta = String.valueOf(jsonObject.getJSONArray("bgs").getJSONObject(0).get("bgdelta"));
bgTrend = String.valueOf(jsonObject.getJSONArray("bgs").getJSONObject(0).get("trend"));
Datetime = String.valueOf(jsonObject.getJSONArray("bgs").getJSONObject(0).get("datetime"));
timedelay = (int) (Long.parseLong(Datetime) / 1000) - (int) (Calendar.getInstance().getTimeInMillis()/1000);
// Toast.makeText(getContext(), BG,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// Toast.makeText(getContext(), "JSON Decode from xdrip failed",Toast.LENGTH_SHORT).show();
}
// end read Data xDRIP
//cyclecount[0]++;
/* MyDataStorage.SetData(1,String.valueOf(cyclecount[0]));
MyDataStorage.SetDataInt(2,cyclecount[0]); // Temp
MyDataStorage.SetDataInt(0,601); // snow
MyDataStorage.SetDataInt(1,0); // min Temp
MyDataStorage.SetDataInt(3,50); // max Temp
MyDataStorage.SetData(1,"Text1b,"); // weather location
MyDataStorage.SetData(2,"Text2"); // weather condition
*/
//long blub;
//blub = Long.valueOf(Datetime);
//final Time myTime = new Time(123456789);
final Time myTime = new Time(Long.valueOf(Datetime));
DateFormat format = new SimpleDateFormat("HH:mm");
//format.format(myTime.getTime());
/*Integer BGsmall = Integer.valueOf(BG) / 10;
if (Integer.valueOf(bgdelta) < 0) { // Blutzucker fallend
BGsmall = -BGsmall; // -minuszeichen hinzufügen für fallend.
}
*/
/*Integer BGmin = Integer.valueOf(BG) % 10;
if (BGsmall < BGmin) {
BGmin = -BGmin; // minuszeichen hinzufügen, damit es immer kleiner bleibt. 89 => 8/-9
}
//BGmin = -12;
*/
Integer CBG = Integer.valueOf(BG);
if (CBG > 254 ) {CBG = 0; } //sets an upper bound as humidity could diplay up to 254
/*
Integer BGmin = Integer.valueOf(BG) / 100;
Integer BGmax = Integer.valueOf(BG) - BGmin*100;
MyDataStorage.SetDataInt(3,minutes); // max Temp
if (BGsmall < 10){ // wenn unter 100, dann nur die max Anzeige
BGmin = 0;MyDataStorage.SetDataInt(3,minutes); // max Temp
BGmax = Integer.valueOf(BG);
}
if (BGsmall < 0) { // fall fallend
if (Integer.valueOf(BG)< 100){ // nur bei min was anzeigen
BGmin = -Integer.valueOf(BG);
BGmax = 0;
}
BGmin = -Integer.valueOf(BG) / 10;
BGmax = Integer.valueOf(BG) + BGmin * 10;
}
*/
Integer bgdeltaInt = Integer.valueOf(bgdelta);
Integer weatherIcon = 800; // default 800 - clear sky
if (bgdeltaInt > 5) { weatherIcon = 500; } //moderat steigend
if (bgdeltaInt > 10) { weatherIcon = 501; } // schnell steigend
if (bgdeltaInt > 20) { weatherIcon = 502; } // wahnsinn steigend
if (bgdeltaInt < -5) { weatherIcon = 600; } //moderat fallend
if (bgdeltaInt < -10) { weatherIcon = 601; } // schnell fallend
if (bgdeltaInt < -20) { weatherIcon = 602; } // wahnsinn fallend
//Date date = format.parse(datetime);
int hours = myTime.getHours();
int minutes = myTime.getMinutes();
MyDataStorage.SetDataInt(2,CBG); // Humidity current BG
MyDataStorage.SetDataInt(0,weatherIcon); // 800 clear sky
MyDataStorage.SetDataInt(1,bgdeltaInt); // min temp - delta bg
MyDataStorage.SetDataInt(3,minutes); // max Temp
MyDataStorage.SetDataInt(4,hours); // max Temp
MyDataStorage.SetData(1,format.format(myTime.getTime())); // weather location
MyDataStorage.SetData(2,BG + " " + bgdelta + "(" +String.valueOf(timedelay/60) + ")"); // weather condition
// update watch
// context = context.getApplicationContext();
// GadgetbridgeAPI gadgetbridgeAPI = new GadgetbridgeAPI(context);
// gadgetbridgeAPI.sendWeatherBroadcastIfEnabled();
GadgetbridgeAPI gadgetbridgeAPI = new GadgetbridgeAPI(mycontext);
gadgetbridgeAPI.sendWeatherBroadcastIfEnabled();
// Toast.makeText(mycontext, "Cycle TEst",Toast.LENGTH_SHORT).show();
}
};
scheduler.scheduleAtFixedRate(testcycle, 20, 20, TimeUnit.SECONDS); // alle 2 sec ausführen
Toast.makeText(getContext(), "Los gehts",Toast.LENGTH_SHORT).show();
// end cycle test
binding.buttonFirst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(getContext(),"geklickt.... NexT",Toast.LENGTH_SHORT).show();
//String action="blabla";
InputStream myStream=getStream("http://127.0.0.1:17580/pebble");
//InputStream myStream=getStream("http://127.0.0.1:17580/sgv.json");
//InputStream myStream=getStream("http://192.168.0.44");
//String mystreamtoString = new String(myStream.toString());
InputStreamReader isReader = new InputStreamReader(myStream);
//Creating a BufferedReader object
BufferedReader reader = new BufferedReader(isReader);
StringBuffer sb = new StringBuffer();
String str = "";
try {
while ((str = reader.readLine()) != null) {
sb.append(str);
}
}
catch(Exception e){
Toast.makeText(getContext(), "can't read xDrip data from http://127.0.0.1:17580/pebble",Toast.LENGTH_SHORT).show();
}
String BG = "";
String bgdelta = "";
String bgTrend = "";
String Datetime = "";
int timedelay = 0;
// decode JSON
try {
String jsonStr = sb.toString();
JSONObject jsonObject = new JSONObject(jsonStr);
BG = String.valueOf(jsonObject.getJSONArray("bgs").getJSONObject(0).get("sgv"));
bgdelta = String.valueOf(jsonObject.getJSONArray("bgs").getJSONObject(0).get("bgdelta"));
bgTrend = String.valueOf(jsonObject.getJSONArray("bgs").getJSONObject(0).get("trend"));
Datetime = String.valueOf(jsonObject.getJSONArray("bgs").getJSONObject(0).get("datetime"));
timedelay = (int) (Long.parseLong(Datetime) / 1000) - (int) (Calendar.getInstance().getTimeInMillis()/1000);
// Toast.makeText(getContext(), BG,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getContext(), "JSON Decode from xdrip failed",Toast.LENGTH_SHORT).show();
}
Toast.makeText(getContext(), BG + " " + bgdelta + " age: " + timedelay,Toast.LENGTH_SHORT).show();
//NavHostFragment.findNavController(FirstFragment.this)
// .navigate(R.id.action_FirstFragment_to_SecondFragment);
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
// JSON READ TEST
private void test() {
//Response resp = new ObjectMapper().readValue(new URL("http://dot.com/api/?customerId=1234").openStream(),Response.class);
}
private InputStream getStream(String url1) {
// disable strict mode do allow netwok in main thread => issue non responsive app
StrictMode.ThreadPolicy gfgPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(gfgPolicy);
try {
URL url = new URL(url1);
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(1000);
return urlConnection.getInputStream();
} catch (Exception ex) {
return null;
}
}
// JSON READ TEST END
}