Commit e8395c0a authored by joliu's avatar joliu

修改最新通过的测试代码

parent 21b45a50
...@@ -129,3 +129,12 @@ Server loop running in thread:Thread-1 ...@@ -129,3 +129,12 @@ Server loop running in thread:Thread-1
Program started Program started
Server loop running in thread:Thread-1 Server loop running in thread:Thread-1
....waiting for connection ....waiting for connection
Program started
Server loop running in thread:Thread-1
....waiting for connection
Program started
Server loop running in thread:Thread-1
....waiting for connection
Program started
Server loop running in thread:Thread-1
....waiting for connection
...@@ -128,12 +128,6 @@ def sendBySocket(ip, port, cmd): ...@@ -128,12 +128,6 @@ def sendBySocket(ip, port, cmd):
print("Error receiving data: %s" % err_msg) print("Error receiving data: %s" % err_msg)
s.close() s.close()
return (-1, err_msg) return (-1, err_msg)
try:
response = s.recv(1024).decode()
except socket.error as err_msg:
print("Error receiving data: %s" % err_msg)
s.close()
return (-1, err_msg)
print(str(response)) print(str(response))
s.close() s.close()
......
No preview for this file type
No preview for this file type
...@@ -11,12 +11,18 @@ ...@@ -11,12 +11,18 @@
* date: 2018-3-22 * * date: 2018-3-22 *
**************************************************** ****************************************************
''' '''
from listenSer import sendBySocket
from listenSer import sendCommandToDevice
import sqlite3 import sqlite3
import time import time
import socket
# 发送控制指令到Device
def sendCommandToDevice(cmd):
# 通过容器的环境变量HOST获取绑定传感器的IP地址
ip, port = "192.168.12.75", 8085
return sendBySocket(ip, port, cmd)
def findTask(): def findTask():
try: try:
...@@ -37,6 +43,42 @@ def findTask(): ...@@ -37,6 +43,42 @@ def findTask():
return (status, output) return (status, output)
# 通过socket发送信息
def sendBySocket(ip, port, cmd):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err_msg:
print("Error creating socket:%s" % err_msg)
s.close()
return (-1, err_msg)
try:
s.connect((ip, port))
except socket.error as err_msg:
print("Address-related error connecting to server: %s" % err_msg)
s.close()
return (-1, err_msg)
print("****************send:" + cmd)
try:
s.sendall(cmd.encode())
except socket.error as err_msg:
print("Error sending data: %s" % err_msg)
s.close()
return (-1, err_msg)
try:
response = s.recv(1024).decode()
print(response)
except socket.error as err_msg:
print("Error receiving data: %s" % err_msg)
s.close()
return (-1, err_msg)
#print(str(response))
s.close()
# 程序运行正常返回目标传感器返回的数据
return (1, str(response))
# 根据符号来比较两个数值的大小 # 根据符号来比较两个数值的大小
def compare(signal, value1, value2): def compare(signal, value1, value2):
if signal == '>': if signal == '>':
...@@ -70,6 +112,8 @@ def mainWhileProcess(input_ctime): ...@@ -70,6 +112,8 @@ def mainWhileProcess(input_ctime):
(condition, command) = task.split(';') (condition, command) = task.split(';')
(ip, port, method) = command.split(':') (ip, port, method) = command.split(':')
# 构建控制命令
method = 'device&' + method
# 读取传感器数值 # 读取传感器数值
(status, output) = sendCommandToDevice(method) (status, output) = sendCommandToDevice(method)
# 千杀的dht11,需要处理下数据 # 千杀的dht11,需要处理下数据
...@@ -81,11 +125,13 @@ def mainWhileProcess(input_ctime): ...@@ -81,11 +125,13 @@ def mainWhileProcess(input_ctime):
if compare(condition[0], float(output), float(condition[1:])): if compare(condition[0], float(output), float(condition[1:])):
# 当结果为真,向目标传感器发出指令 # 当结果为真,向目标传感器发出指令
(status, output) = sendBySocket(ip, port, method) (status, output) = sendBySocket(ip, int(port), method)
print(output) #print(output)
else: else:
pass pass
print(1212)
if __name__ == "__main__": if __name__ == "__main__":
mainWhileProcess(5) mainWhileProcess(5)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment