Commit 4697b8ec authored by joliu's avatar joliu

修复由于启动太快导致数据库没有建立而运行出错的bug

parent 321b8085
from controllMatrix import *
from listenSer import sendBySocket
import os
import time
import socket
# 发送控制指令到传感器设备,参数cmd,控制命令
def sendCommandToDevice(cmd):
response = 'error'
# 通过容器的环境变量HOST获取绑定传感器的IP地址
ip, port = os.getenv('HOST'), 8085
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.gaierror as err_msg:
print('Address-related error connecting to server: %s' % err_msg)
s.close()
return (-1, err_msg)
print(updateDeviceTask('25', '192.168.12.1', 25))
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()
except socket.error as err_msg:
print('Error receiving data: %s' % err_msg)
s.close()
return (-1, err_msg)
s.close()
# 程序运行正常,返回传感器传递的值
return (1, str(response))
while True:
if getDeviceTask()[0] == -1:
ctime = 5
(status, output) = getDeviceTask()
if status == -1:
print(output)
continue
(id, data, dstIP, ctime) = getDeviceTask()[1]
print(data)
if data == 25:
print('success')
for data in getDeviceTask()[1]:
(id, data, dstIP, ctime) = data
(ip, port) = dstIP.split(':')
print(data)
(status, deviceValue) = sendCommandToDevice('on')
if status == -1:
print(deviceValue)
continue
time.sleep(2)
try:
deviceValue = float(deviceValue.split('&')[0])
except Exception as err:
print(err,deviceValue)
if deviceValue > data:
print('1')
msg = 'device&%s&1' % os.getenv('HOSTNAME')
else:
print('0')
msg = 'device&%s&0' % os.getenv('HOSTNAME')
(status, recvdata) = sendBySocket(ip, int(port), method)
time.sleep(ctime)
from controllMatrix import *
from resolveMatrix import runTask
import os
# 发送控制指令到传感器设备,参数cmd,控制命令
def sendCommandToDevice(cmd):
response = 'error'
# 通过容器的环境变量HOST获取绑定传感器的IP地址
ip, port = os.getenv('HOST'), 8085
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.gaierror as err_msg:
print('Address-related error connecting to server: %s' % err_msg)
s.close()
return (-1, err_msg)
while True:
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()
except socket.error as err_msg:
print('Error receiving data: %s' % err_msg)
s.close()
return (-1, err_msg)
s.close()
# 程序运行正常,返回传感器传递的值
return (1, str(response))
if __name__ == '__main__':
time.sleep(10)
while True:
(status, output) = runTask()
if status == -1:
print(output)
time.sleep(20)
continue
else:
if output == 1:
msg = 'on'
print('on')
else:
msg = 'off'
print('off')
print(sendCommandToDevice(msg))
ctime = getCircleTime()
print(runTask())
time.sleep(ctime)
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