Skip to content

Commit

Permalink
Merge pull request #17 from itead/updata-v3.3.0
Browse files Browse the repository at this point in the history
Updata v3.3.0
  • Loading branch information
ZZLinvec authored Jul 20, 2019
2 parents 2087897 + 4762db8 commit 573471b
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 372 deletions.
10 changes: 8 additions & 2 deletions code/Dialog_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,19 @@ def __init__(self, parent=None, **info):
grid = QGridLayout()
num = 0
for x in all_info.keys():
print(x)
sub_name = QLabel(parent=self)
sub_name.setText(x)
grid.addWidget(sub_name, num, 0, 1, 1)
sub_ret = QLabel(parent=self)
sub_ret.setText(all_info[x])
data = all_info[x]
if data['error'] == 0:
sub_ret.setText("succeed")
else:
sub_ret.setText("error")
grid.addWidget(sub_ret, num, 1, 1, 1)
sub_info = QLabel(parent=self)
sub_info.setText(str(all_info[x]))
grid.addWidget(sub_info, num, 2, 2, 1)
num += 1
buttonbox = QDialogButtonBox(parent=self)
buttonbox.setOrientation(Qt.Horizontal)
Expand Down
4 changes: 3 additions & 1 deletion code/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# /code
# /code(3.3.0)

------

Expand Down Expand Up @@ -43,3 +43,5 @@ This module is mainly used to send HTTP(POST) requests to devices
This module is dedicated to MDNS

### requirements.txt

Python runs the required libraries
203 changes: 65 additions & 138 deletions code/lan_ewlink_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ThreadForQT(Classes based on QT threads)
"""
import time
import time
import requests
import json
from PySide2.QtCore import *
Expand Down Expand Up @@ -42,7 +43,7 @@ def postRequest(self, url=None, data=None):
self.send_text = json_str
self.write_log("send ING")
self.write_log(self.send_text)
response = requests.post(url=url, data=json.dumps(data))
response = requests.post(url=url, data=json.dumps(data), timeout=10)
self.send_response = response
print("RETURN response:", response.text)
self.write_log("RETURN response")
Expand Down Expand Up @@ -181,21 +182,33 @@ def performer_func(self, sub_id):
sub_id=sub_id)
elif self. command_num == 7:
# 7 command("ROOT")
print("self.command_vrg==", self.command_vrg)
ret = self.set_unlock(ip=ip, port=port, sub_id=sub_id)
print("ret", ret)
if ret == 0:
# Only if it receives a return value that understands the lock
# success
ret = self.set_ota_flash(
sha256sum=self.command_vrg["sha256sum"],
sever_ip=self.command_vrg["sever_ip"],
sever_port=self. command_vrg["sever_port"],
ip=ip,
port=port,
sub_id=sub_id)
ret=self.get_signal_intensity(ip=ip,port=port,sub_id=sub_id)

This comment has been minimized.

Copy link
@piotrwozniak

piotrwozniak Mar 22, 2020

today is 22.03.2020, I am still fighing with run your code, binaries sucks, and I noticed that you have a problems in py files with tab / dot. Please correct it, replace tabs by dots because as you know this is a serious problem in python. Additionally I have message "Please check your network connection and retry", I cannot Flash device by this. Python is not my story but probably I had to solve next problem, and all weekend is done :/

This comment has been minimized.

Copy link
@piotrwozniak

piotrwozniak Mar 22, 2020

please inform me when you correct you code

elif self. command_num==8:
#8命令(获取设备状态信息)
ret=self.get_dev_info_api(ip=ip,port=port,sub_id=sub_id)
elif self. command_num==9:
#9命令(解锁ota)
print("self.command_vrg==",self.command_vrg)
ret=self.set_unlock(ip=ip,port=port,sub_id=sub_id)
print("ret",ret)
if ret['error'] == 0:
#10命令(发送升级信息)
ret=self.set_ota_flash(sha256sum= self.command_vrg["sha256sum"], sever_ip= self.command_vrg["sever_ip"], sever_port=self. command_vrg["sever_port"], ip=ip, port=port, sub_id=sub_id)
return ret


def send_data(self, send_url, send_data):
"""
send data to device by HTTP PORT
"""
#try:
print("send:",send_url,str(send_data))
response =self.ht.postRequest(send_url,send_data)
print("response:",str(response))
if response["result"]:
return json.loads(response["text"])
else:
return 1

def set_OUT(self, **info):
"""
Set the lights on and off
Expand All @@ -219,26 +232,12 @@ def set_OUT(self, **info):
data["sequence"] = str(int(time.time()))
sub_id = info["sub_id"]
data["deviceid"] = sub_id
data["encrypt"] = False
data["iv"] = "1234567890123456"
data["selfApikey"] = "123"
if out_sta:
data["data"] = '{\"switch\": \"on\"}'
else:
data["data"] = '{\"switch\": \"off\"}'
if out_sta:
data["data"]= {"switch": "on"}
else:
data["data"]= {"switch": "off"}
# 3. Call Http_API(postRequest) to send.
try:
print("%s发送数据:%s" % (url, str(data)))
response = self.ht.postRequest(url, data)
print("接收到数据:", str(response))
if response["result"]:
if json.loads(response["text"])["error"] != 0:
return 1
return 0
else:
return 1
except BaseException:
return 1
return self.send_data(send_url=url, send_data=data)

def set_power_up_state(self, **info):
"""
Expand All @@ -263,28 +262,14 @@ def set_power_up_state(self, **info):
data["sequence"] = str(int(time.time()))
sub_id = info["sub_id"]
data["deviceid"] = sub_id
data["encrypt"] = False
data["iv"] = "1234567890123456"
data["selfApikey"] = "123"
if state == 0:
data["data"] = '{\"startup\": \"off\"}'
elif state == 1:
data["data"] = '{\"startup\": \"on\"}'
elif state == 2:
data["data"] = '{\"startup\": \"stay\"}'
if(state== 0):
data["data"]= {"startup": "off"}
elif (state== 1):
data["data"]= {"startup": "on"}
elif (state== 2):
data["data"]= {"startup": "stay"}
# 3. Call Http_API(postRequest) to send.
try:
print("%s SEND:%s" % (url, str(data)))
response = self.ht.postRequest(url, data)
print("response:", str(response))
if response["result"]:
if json.loads(response["text"])["error"] != 0:
return 1
return 0
else:
return 1
except BaseException:
return 1
return self.send_data(send_url=url, send_data=data)

def get_signal_intensity(self, **info):
"""
Expand All @@ -307,23 +292,9 @@ def get_signal_intensity(self, **info):
data["sequence"] = str(int(time.time()))
sub_id = info["sub_id"]
data["deviceid"] = sub_id
data["encrypt"] = False
data["iv"] = "1234567890123456"
data["selfApikey"] = "123"
data["data"] = r'{\n\}'
data["data"] = { }
# 3. Call Http_API(postRequest) to send.
try:
print("%s SEND:%s" % (url, str(data)))
response = self.ht.postRequest(url, data)
print("response:", str(response))
if response["result"]:
if json.loads(response["text"])["error"] != 0:
return 1
return 0
else:
return 1
except BaseException:
return 1
return self.send_data(send_url=url, send_data=data)

def set_point_sewidth(self, **info):
"""
Expand All @@ -350,24 +321,9 @@ def set_point_sewidth(self, **info):
data["sequence"] = str(int(time.time()))
sub_id = info["sub_id"]
data["deviceid"] = sub_id
data["encrypt"] = False
data["iv"] = "1234567890123456"
data["selfApikey"] = "123"
data["data"] = '{\"pulse\": \"' + pulse + \
'\",\"pulseWidth\": ' + str(pulseWidth) + '}'
data["data"]= {"pulse": pulse,"pulseWidth": pulseWidth}
# 3. Call Http_API(postRequest) to send.
try:
print("%s SEND:%s" % (url, str(data)))
response = self.ht.postRequest(url, data)
print("response:", str(response))
if response["result"]:
if json.loads(response["text"])["error"] != 0:
return 1
return 0
else:
return 1
except BaseException:
return 1
return self.send_data(send_url=url, send_data=data)

def set_wifi(self, **info):
"""
Expand All @@ -394,24 +350,9 @@ def set_wifi(self, **info):
data["sequence"] = str(int(time.time()))
sub_id = info["sub_id"]
data["deviceid"] = sub_id
data["encrypt"] = False
data["iv"] = "1234567890123456"
data["selfApikey"] = "123"
data["data"] = '{\"ssid\": \"' + ssid + \
'\",\"password\": \"' + password + '\"}'
data["data"]= {"ssid": ssid, "password": password}
# 3. Call Http_API(postRequest) to send.
try:
print("%s SEND:%s" % (url, str(data)))
response = self.ht.postRequest(url, data)
print("response:", str(response))
if response["result"]:
if json.loads(response["text"])["error"] != 0:
return 1
return 0
else:
return 1
except BaseException:
return 1
return self.send_data(send_url=url, send_data=data)

def set_unlock(self, **info):
"""
Expand All @@ -435,23 +376,9 @@ def set_unlock(self, **info):
data["sequence"] = str(int(time.time()))
sub_id = info["sub_id"]
data["deviceid"] = sub_id
data["encrypt"] = False
data["iv"] = "1234567890123456"
data["selfApikey"] = "123"
data["data"] = '{\n\n}'
data["data"]= { }
# 3. Call Http_API(postRequest) to send.
try:
print("%s SEND:%s" % (url, str(data)))
response = self.ht.postRequest(url, data)
print("response:", str(response))
if response["result"]:
if json.loads(response["text"])["error"] != 0:
return 1
return 0
else:
return 1
except BaseException:
return 1
return self.send_data(send_url=url, send_data=data)

def set_ota_flash(self, **info):
"""
Expand All @@ -478,21 +405,21 @@ def set_ota_flash(self, **info):
data["sequence"] = str(int(time.time()))
sub_id = info["sub_id"]
data["deviceid"] = sub_id
data["encrypt"] = False
data["iv"] = "1234567890123456"
data["selfApikey"] = "123"
data["data"] = '{\"downloadUrl\":\"http://' + info["sever_ip"] + ':' + str(
info["sever_port"]) + '/itead.bin",\"sha256sum\":\"' + info["sha256sum"] + '"}'
data["data"]= { }
# 3. Call Http_API(postRequest) to send.
try:
print("%s SEND:%s" % (url, str(data)))
response = self.ht.postRequest(url, data)
print("response:", str(response))
if response["result"]:
if json.loads(response["text"])["error"] != 0:
return 1
return 0
else:
return 1
except BaseException:
return 1
return self.send_data(send_url=url, send_data=data)


def get_dev_info_api(self,**info):
"""
get device info
"""
sub_dict=info
data={}
#把组装好url
url="http://"+info["ip"]+":"+str(info["port"])+"/zeroconf/info"
#组装好data
sub_id=info["sub_id"]
data["deviceid"]=sub_id
data["data"]= { }
return self.send_data(send_url=url, send_data=data)
49 changes: 31 additions & 18 deletions code/mdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def remove_service(self, zeroconf, type, name):
This function is triggered when ServiceBrowser discovers that some device has logged out
"""
print("inter remove_service()")
if name not in self.all_info_dict:
return
self.all_sub_num -= 1
del self.all_info_dict[name]
self.all_del_sub.append(name)
Expand Down Expand Up @@ -124,23 +126,34 @@ def flash_all_sub_info(self,):
info = zeroconf.get_service_info(type=type, name=name)
current_info["info"] = info
self.all_info_dict[x] = current_info["info"]

def main():
zeroconf=Zeroconf()
listener=MyListener()
browser = ServiceBrowser(zeroconf, "_ewelink._tcp.local.",listener= listener)
while True:
if listener.all_sub_num>0:
dict=listener.all_info_dict.copy()
for x in dict.keys():
info=dict[x]
info=zeroconf.get_service_info(info.type,x)
if info!= None:
data=info.properties
cur_str=str(data)
print(cur_str)
time.sleep(0.5)


zeroconf = Zeroconf()
listener = MyListener()
browser = ServiceBrowser(zeroconf, "_ewelink._tcp.local.",listener= listener)
while True:
if listener.all_sub_num>0:
dict=listener.all_info_dict.copy()
for x in dict.keys():
info=dict[x]
info=zeroconf.get_service_info(info.type,x)
if info!= None:
data=info.properties
cur_str=x[8:18]+" "+parseAddress(info.address)+" "+str(info.port)+" " +str(data)
print(cur_str)
if len(listener.all_del_sub)>0:
for x in listener.all_del_sub:
cur_str=x[8:18]+"\nDEL"
print(cur_str)
time.sleep(0.5)

def parseAddress(address):
add_list = []
for i in range(4):
add_list.append(int(address.hex()[(i*2):(i+1)*2], 16))
add_str = str(add_list[0]) + "." + str(add_list[1]) + "." + str(add_list[2])+ "." + str(add_list[3])
return add_str


if __name__ == "__main__":
main()
main()
Loading

0 comments on commit 573471b

Please sign in to comment.