Fix: 修复MicroPython MixGoAI和Microbit下一些py异常同时格式化代码

This commit is contained in:
王立帮
2024-12-03 10:36:49 +08:00
parent e6c9a30bdc
commit ee04dadb66
71 changed files with 3265 additions and 2382 deletions

View File

@@ -1,25 +1,29 @@
import network,time,random,request,base64,json,board
import network, time, random, request, base64, json, board
from machine import UART
wifi_en=board.pin(19,board.GPIO.OUT)
board.register(18,board.FPIOA.UART2_TX)
board.register(17,board.FPIOA.UART2_RX)
wifi_en = board.pin(19, board.GPIO.OUT)
board.register(18, board.FPIOA.UART2_TX)
board.register(17, board.FPIOA.UART2_RX)
def wifi_enable(en):
global wifi_en
wifi_en.value(en)
def wifi_reset():
global uart
wifi_enable(0)
time.sleep_ms(200)
wifi_enable(1)
time.sleep(2)
uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)
uart = UART(UART.UART2, 115200, timeout=1000, read_buf_len=4096)
tmp = uart.read()
uart.write("AT+UART_CUR=921600,8,1,0,0\r\n")
print(uart.read())
uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240) # important! baudrate too low or read_buf_len too small will loose data
uart = UART(
UART.UART2, 921600, timeout=1000, read_buf_len=10240
) # important! baudrate too low or read_buf_len too small will loose data
uart.write("AT\r\n")
tmp = uart.read()
print(tmp)
@@ -32,16 +36,17 @@ def wifi_reset():
return None
return nic
def nic_init(account,password):
nic=wifi_reset()
if not nic:
raise Exception("[Cool.AI]:WiFi init fail")
nic.connect(account,password)
nic.ifconfig()
def nic_init(account, password):
nic = wifi_reset()
if not nic:
raise Exception("[Cool.AI]:WiFi init fail")
nic.connect(account, password)
nic.ifconfig()
class SimpleEncode():
class SimpleEncode:
keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+-={}[]:;<,>.?/|"
keyLength = len(keyStr)
encryptionA = 17
@@ -51,87 +56,87 @@ class SimpleEncode():
postCount = 5
randomChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop"
randomCharLength = len(randomChar)
#base64字符
# base64字符
ALPHABET = "ABCDEFGHIJKLMN0123456789OPQRSTUVWXYZ+/abcdefghijklmnopqrstuvwxyz"
STANDARD = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" # 标准的字符串索引
#找到密钥
for i in range(1,keyLength):
if (encryptionA * i) % keyLength == 1:
decodeA = i
# 找到密钥
for i in range(1, keyLength):
if (encryptionA * i) % keyLength == 1:
decodeA = i
def base64Encode(self,sourceStr):
def base64Encode(self, sourceStr):
encode = ""
for ch in base64.b64encode(sourceStr.encode()).decode():
if ch == '=':
encode += '='
if ch == "=":
encode += "="
else:
for i in range(64):
if ch == self.STANDARD[i]:
encode += self.ALPHABET[i]
return encode
def encrpyt(self,sourceStr):
def encrpyt(self, sourceStr):
srcLength = len(sourceStr)
#先加入干扰字符的数量
addCharCount = random.randint(1,self.preCountMax) if srcLength < self.preCountMax else 0
#随机字符
sb = str(addCharCount)+"|"
# 先加入干扰字符的数量
addCharCount = (
random.randint(1, self.preCountMax) if srcLength < self.preCountMax else 0
)
# 随机字符
sb = str(addCharCount) + "|"
for i in range(addCharCount):
sb += self.randomChar[random.randint(0,self.randomCharLength-1)]
sb += self.randomChar[random.randint(0, self.randomCharLength - 1)]
sb += sourceStr
#尾部固定增加x个字符
# 尾部固定增加x个字符
for i in range(self.postCount):
sb += self.randomChar[random.randint(0,self.randomCharLength-1)]
sb += self.randomChar[random.randint(0, self.randomCharLength - 1)]
#base64 加密
# base64 加密
base64Str = self.base64Encode(sb)
destStr = ''
destStr = ""
for i in range(len(base64Str)):
#找到字符所在位置
# 找到字符所在位置
position = self.keyStr.find(base64Str[i])
#对字符进行转换
# 对字符进行转换
y = (self.encryptionA * position + self.encryptionB) % self.keyLength
#找到替换后的字符
# 找到替换后的字符
destStr += self.keyStr[y]
return destStr
def token(account,password):
url = 'http://ai.heclouds.com:9090/v1/user/oneNetLogin'
def token(account, password):
url = "http://ai.heclouds.com:9090/v1/user/oneNetLogin"
headers = {
'Content-Type': 'application/json',
"Content-Type": "application/json",
}
simpleencode = SimpleEncode()
data = {'account': account, 'password': simpleencode.encrpyt(password)}
data = {"account": account, "password": simpleencode.encrpyt(password)}
req = request.post(url, data=json.dumps(data), headers=headers)
#print(req.text)
# print(req.text)
try:
return eval(req.text)['data']['loginToken']
return eval(req.text)["data"]["loginToken"]
except:
raise ValueError( "[Cool.AI]:Wrong account or password ")
raise ValueError("[Cool.AI]:Wrong account or password ")
def post_ai(img,urlx,Token):
ur = 'http://183.230.40.32:9090/v1/aiApi/picture/MixPY'
url=ur.replace("MixPY",urlx)
headers ={
'Content-Type':'application/json',
'Login-Token':'Token'
}
headers['Login-Token']=Token
imge = img.compressed(quality=50)
file =imge.to_bytes()
str = base64.b64encode(file).decode()
data = {'picture':[str]}
req = request.post(url,data=json.dumps(data),headers=headers)
return json.loads(req.text)
#return eval(req.text)
def post_ai(img, urlx, Token):
ur = "http://183.230.40.32:9090/v1/aiApi/picture/MixPY"
url = ur.replace("MixPY", urlx)
headers = {"Content-Type": "application/json", "Login-Token": "Token"}
headers["Login-Token"] = Token
imge = img.compressed(quality=50)
file = imge.to_bytes()
str = base64.b64encode(file).decode()
data = {"picture": [str]}
req = request.post(url, data=json.dumps(data), headers=headers)
return json.loads(req.text)
# return eval(req.text)