-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
166 lines (158 loc) · 4.22 KB
/
App.tsx
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
import React, {useEffect, useState} from 'react';
import {
Alert,
ScrollView,
TextInput,
View,
Text,
StyleSheet,
TouchableHighlight,
} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
getDeviceInfo,
getDevicePoints,
getSystemInfo,
getToken,
} from './NibeConnunication';
const App = () => {
const [clientId, setClientId] = useState('');
const [clientSecret, setClientSecret] = useState('');
const [data, setData] = useState<string[]>(['No data']);
useEffect(() => {
AsyncStorage.getItem('clientId')
.then(value => {
setClientId(value || '');
})
.catch(() => {});
AsyncStorage.getItem('clientSecret')
.then(value => {
setClientSecret(value || '');
})
.catch(() => {});
}, []);
return (
<View style={{flex: 1, backgroundColor: 'black', padding: 10}}>
<View style={{marginTop: 50}}>
<Text style={styles.title}>{'Nibe Reader'}</Text>
<TextInput
style={styles.input}
placeholder={'Client Identifier'}
placeholderTextColor={'#16ff16'}
value={clientId}
autoCapitalize={'none'}
onChangeText={value => {
setClientId(value);
}}
/>
<TextInput
style={styles.input}
placeholder={'Client Secret'}
placeholderTextColor={'#16ff16'}
value={clientSecret}
autoCapitalize={'none'}
onChangeText={value => {
setClientSecret(value);
}}
/>
<TouchableHighlight
onPress={async () => {
try {
storeCredentials(clientId, clientSecret);
setData(['']);
const token = await getToken(clientId, clientSecret);
setData(prev => {
return ['\nAccess token:', token, ...prev];
});
const deviceId = await getSystemInfo(token);
setData(prev => {
return ['\nDeviceId:', deviceId, ...prev];
});
const deviceInfo = await getDeviceInfo(token, deviceId);
setData(prev => {
return ['Connection:', deviceInfo, ...prev];
});
const points = await getDevicePoints(token, deviceId);
setData(prev => {
return ['Points:', points, ...prev];
});
} catch (error: any) {
Alert.alert('Nibe', error.message);
}
}}>
<Text style={styles.btn}>{'Connect'}</Text>
</TouchableHighlight>
</View>
<View style={styles.listParent}>
<ScrollView
showsVerticalScrollIndicator={true}
contentContainerStyle={styles.listContent}>
{data.map((d, i) => {
return (
<Text style={styles.listText} key={i}>
{d}
</Text>
);
})}
</ScrollView>
</View>
<Text style={[styles.label, {padding: 20}]}>{'Tero Paananen 2023'}</Text>
</View>
);
};
const storeCredentials = (clientId: string, clientSecret: string) => {
AsyncStorage.setItem('clientId', clientId).catch(() => {});
AsyncStorage.setItem('clientSecret', clientSecret).catch(() => {});
};
const styles = StyleSheet.create({
input: {
color: '#16ff16',
borderColor: '#16ff16',
borderBottomWidth: 0.4,
borderRadius: 2,
marginVertical: 10,
padding: 8,
marginHorizontal: 20,
fontFamily: 'Courier New',
minWidth: 200,
},
listParent: {
flex: 1,
},
listContent: {
padding: 10,
borderColor: '#16ff16',
borderRadius: 2,
borderWidth: 0.4,
},
listText: {
color: '#16ff16',
fontFamily: 'Courier New',
fontSize: 12,
},
title: {
padding: 10,
fontSize: 18,
alignSelf: 'center',
textAlign: 'center',
color: '#16ff16',
fontFamily: 'Courier New',
},
btn: {
padding: 10,
minWidth: 100,
fontSize: 16,
fontFamily: 'Courier New',
alignSelf: 'center',
textAlign: 'center',
color: '#16ff16',
marginBottom: 20,
},
label: {
fontSize: 10,
textAlign: 'center',
color: '#16ff16',
fontFamily: 'Courier New',
},
});
export default App;