1# -*- coding: UTF-8 -*- 2 3 4class NetWorkClient(object): 5 6 """ 7 该模块实现网络管理相关的功能,包括初始化,联网,状态信息等. 8 """ 9 def __init__(self): 10 nm.init() 11 12 def __str_is_empty(self,value): 13 if value is None or value == "": 14 return True 15 else: 16 return False 17 18 def connect(self,data): 19 """ 20 连接网络 21 22 **入参** 23 24 data类型是字典,字典的key信息如下 25 26 - ssid : 必选,值是字符串 27 - password : 必选,值是字符串 28 29 30 使用示例:: 31 32 # -*- coding: UTF-8 -*- 33 import network 34 net = network.NetWorkClient() 35 net.connect({ 36 'ssid' : 'KIDS' , 37 'password' : '12345678' 38 } 39 ) 40 """ 41 if isinstance(data, dict): 42 pass 43 else: 44 raise ValueError("connect func param must be dict") 45 46 if not 'ssid' in data: 47 raise ValueError('connect : param must have key "ssid"') 48 elif self.__str_is_empty(data['ssid']): 49 raise ValueError("ssid wrong") 50 51 if not 'password' in data: 52 raise ValueError('connect : param must have key "password"') 53 elif self.__str_is_empty(data['password']): 54 raise ValueError("password wrong") 55 nm.connect(data['ssid'],data['password']) 56 57 def disconnect(self): 58 """ 59 断开网络 60 """ 61 nm.disconnect 62 63 def getType(self): 64 """ 65 获取当前网络类型: 66 67 - ``0`` : NETWORK_WIFI 68 - ``1`` : NETWORK_CELLULAR 69 - ``2`` : NETWORK_ETHERNET 70 - ``3`` : NETWORK_UNKNOW 71 72 """ 73 return nm.getType() 74 75 def getStatus(self): 76 """ 77 78 获取当前网络状态 79 80 - ``True`` 已连接 81 - ``False`` 未连接 82 83 """ 84 return nm.getStatus 85