2021-06-06 21:31:41 +02:00
import React from 'react' ;
2021-05-24 13:19:19 +02:00
import {
StyleSheet ,
Text ,
View ,
TouchableOpacity ,
ScrollView ,
2021-06-06 21:31:41 +02:00
ActivityIndicator ,
2021-05-24 13:19:19 +02:00
} from "react-native" ;
import SetupView from '../components/SetupView' ;
import MaterialIcon from "react-native-vector-icons/MaterialCommunityIcons" ;
2021-07-28 15:08:25 +02:00
import { connect } from 'react-redux' ;
import { changeSwimTrackerHostname } from '../state/Reducer' ;
2021-05-24 13:19:19 +02:00
function WifiListElement ( props ) {
let iconName = "wifi-strength-" + props . strength ;
2021-06-06 21:31:41 +02:00
if ( props . lock ) {
2021-05-24 13:19:19 +02:00
iconName += "-lock" ;
}
return (
2021-06-06 21:31:41 +02:00
< TouchableOpacity onPress = { props . onPress } >
2021-05-24 13:19:19 +02:00
< View style = { wifiListElementStyles . container } >
< MaterialIcon style = { wifiListElementStyles . icon } name = { iconName } > < / M a t e r i a l I c o n >
< Text style = { wifiListElementStyles . text } > { props . text } < / T e x t >
< / V i e w >
< / T o u c h a b l e O p a c i t y >
)
}
const wifiListElementStyles = {
container : {
backgroundColor : "rgba(255,255,255,0.4)" ,
borderRadius : 5 ,
width : "100%" ,
height : 50 ,
flexDirection : "row" ,
alignItems : "center" ,
marginTop : 8 ,
marginBottom : 8 ,
} ,
icon : {
fontSize : 25 ,
color : "rgba(255,255,255,1)" ,
marginLeft : 15 ,
marginRight : 15 ,
} ,
text : {
color : "rgba(255,255,255,1)" ,
fontSize : 18 ,
width : "100%"
}
} ;
// ---------------------------------------------------------------------------------------------
2021-06-06 21:31:41 +02:00
class WifiSelectionView extends React . Component {
constructor ( ) {
super ( ) ;
this . state = { wifiInfo : [ ] } ;
2021-06-13 12:43:50 +02:00
this . mounted = false ;
2021-06-06 21:31:41 +02:00
}
2021-05-24 13:19:19 +02:00
2021-06-06 21:31:41 +02:00
processDeviceResponse ( response ) {
// sort from strong to weak
response . sort ( ( e1 , e2 ) => {
if ( e1 . rssi > e2 . rssi )
return - 1 ;
if ( e1 . rssi < e2 . rssi )
return 1 ;
else
return 0 ;
} ) ;
2021-05-24 13:19:19 +02:00
2021-06-06 21:31:41 +02:00
let ssidsAlreadyAdded = { } ;
let result = [ ] ;
for ( let i = 0 ; i < response . length ; i ++ ) {
if ( response [ i ] . ssid in ssidsAlreadyAdded )
continue ;
2021-05-24 13:19:19 +02:00
2021-06-06 21:31:41 +02:00
const locked = ( response [ i ] . sec != "open" ) ;
let strength = 1 ;
if ( response [ i ] . rssi > - 30 )
strength = 4 ;
else if ( response [ i ] . rssi > - 67 )
strength = 3 ;
else if ( response [ i ] . rssi > - 70 )
strength = 2 ;
2021-05-24 13:19:19 +02:00
2021-06-06 21:31:41 +02:00
result . push ( { ssid : response [ i ] . ssid , locked : locked , strength : strength } ) ;
ssidsAlreadyAdded [ response [ i ] . ssid ] = true ;
}
return result ;
}
2021-05-24 13:19:19 +02:00
2021-06-06 21:31:41 +02:00
componentDidMount ( ) {
2021-06-13 12:43:50 +02:00
let component = this ;
component . mounted = true ;
2021-06-06 21:31:41 +02:00
this . props . device . conn . scanWifiNetworks ( ) . then (
( result ) => {
2021-06-13 12:43:50 +02:00
if ( component . mounted ) {
this . setState ( { wifiInfo : this . processDeviceResponse ( result ) } )
}
2021-06-06 21:31:41 +02:00
}
) ;
}
2021-06-13 12:43:50 +02:00
componentWillUnmount ( ) {
this . mounted = false ;
}
2021-06-06 21:31:41 +02:00
render ( ) {
let inner ;
if ( this . state . wifiInfo . length > 0 ) {
inner = (
2021-06-13 12:43:50 +02:00
< View style = { styles . listContainer } >
2021-07-22 18:39:02 +02:00
< ScrollView style = { { centerContent : true , paddingTop : 20 } } >
2021-06-13 12:43:50 +02:00
{ this . state . wifiInfo . map ( e => (
< WifiListElement
text = { e . ssid }
strength = { e . strength }
lock = { e . locked }
key = { e . ssid }
onPress = { ( ) => {
this . props . navigation . navigate ( "WifiPasswordView" , {
ssid : e . ssid ,
lock : e . locked ,
strength : e . strength ,
confirmPwInput : false ,
buttonText : "OK" ,
subText : "Please enter the password for your home WiFi" ,
2021-07-22 18:39:02 +02:00
onSubmit : ( ssid , pw ) => {
2021-07-28 15:08:25 +02:00
console . log ( "1" ) ;
2021-07-22 18:39:02 +02:00
this . props . device . conn . wifiSetModeSTA ( ssid , pw ) ;
2021-07-28 15:08:25 +02:00
console . log ( "2" , this . props . deviceReportedHostname , changeSwimTrackerHostname , this . props ) ;
this . props . dispatch ( changeSwimTrackerHostname ( this . props . deviceReportedHostname ) ) ;
console . log ( "3" ) ;
2021-07-22 18:39:02 +02:00
} ,
2021-06-13 12:43:50 +02:00
} ) ;
} } >
< / W i f i L i s t E l e m e n t > )
) }
< / S c r o l l V i e w >
< / V i e w >
)
2021-06-06 21:31:41 +02:00
}
else {
2021-06-13 12:43:50 +02:00
inner = (
< View style = { { alignItems : "center" , justifyContent : "center" , height : "100%" } } >
< View style = { { paddingBottom : 20 } } > < Text style = { { fontSize : 16 , color : "#fff" } } > Scanning WiFi networks < / T e x t > < / V i e w >
< ActivityIndicator size = "large" color = "#ffffff" / >
< / V i e w >
2021-06-06 21:31:41 +02:00
)
}
return (
< SetupView
headerText = "WiFi Connection"
lowerLeftButtonText = "My WiFi wasn't found"
2021-06-13 12:43:50 +02:00
onLowerLeftButtonPress = { ( ) => {
this . props . navigation . navigate ( "WifiPasswordView" , {
ssid : "swimtracker-E2842S" , // todo real id here
lock : true ,
strength : 4 ,
confirmPwInput : true ,
buttonText : "Set Password" ,
subText : "Use this option only if you're home WiFi doesn't reach the SwimTracker. The SwimTracker creates its own WiFi with the password you set here." ,
2021-07-22 18:39:02 +02:00
onSubmit : ( ssid , pw ) => {
this . props . device . conn . wifiSetModeAP ( pw ) ;
} ,
2021-06-13 12:43:50 +02:00
} ) ;
} }
2021-06-06 21:31:41 +02:00
lowerRightButtonText = "Need help?"
>
2021-06-13 12:43:50 +02:00
{ inner }
2021-06-06 21:31:41 +02:00
< / S e t u p V i e w >
)
}
2021-05-24 13:19:19 +02:00
}
2021-06-06 21:31:41 +02:00
2021-05-24 13:19:19 +02:00
const styles = StyleSheet . create ( {
listContainer : {
height : "75%" ,
2021-06-13 12:43:50 +02:00
flex : 1 ,
2021-05-24 13:19:19 +02:00
}
} ) ;
2021-07-28 15:08:25 +02:00
const mapStateToProps = ( state ) => {
return { deviceReportedHostname : state . deviceState . deviceReportedHostname } ;
} ;
export default connect ( mapStateToProps ) ( WifiSelectionView ) ;