我正在为我的 domotica 系统制作一个连接系统,但我遇到了一个我无法解决的错误。这是我的脚本:
import insteon
import time
insteon.connect('10.20.0.195', 9761)
while 1:
insteon.send('x10', 'N', '10', 'on')
time.sleep(5)
insteon.send('x10', 'N', '10', 'off')
模块 insteon:
#!/usr/bin/env python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(ip, port):
import socket
s.connect((ip, port))
def close():
import socket
s.close()
def send(prot, house, unit, action):
import socket
import binascii
import time
import sys
if prot == 'x10':
units = {'1': '6', '2': 'E', '3': '2', '4': 'A', '5': '1', '6': '9', '7': '5', '8': 'D', '9': '7', '10': 'F', '11': '3', '12': 'B', '13': '0', '14': '8', '15': '4', '16': 'C'}
houses = {'A': '6', 'B': 'E', 'C': '2', 'D': 'A', 'E': '1', 'F': '9', 'G': '5', 'H': 'D', 'I': '7', 'J': 'F', 'K': '3', 'L': 'B', 'M': '0', 'N': '8', 'O': '4', 'P': 'C'}
unitcode = units[unit]
housecode = houses[house]
smessage1 = str('0263') + str(housecode) + str(unitcode) + str('00')
if action == 'on':
smessage2 = str('0263') + str(housecode) + str('280')
elif action == 'off':
smessage2 = str('0263') + str(housecode) + str('380')
else:
sys.exit("Wrong action")
elif prot == 'insteon':
if action == 'on':
SMESSAGE1 = str('0262') + str(id) + str('0011ff')
elif action == 'off':
SMESSAGE1 = str('0262') + str(id) + str('001300')
else:
sys.exit("Wrong choice")
message1 = binascii.unhexlify(smessage1)
if prot == 'x10':
message2 = binascii.unhexlify(smessage2)
s.send(message1)
time.sleep(1)
if prot == 'x10':
s.send(message2)
time.sleep(2)
s.close() # You can't use the socket after this
def receive():
import socket
import binascii
import time
import sys
BUFFER_SIZE = 2048
data = s.recv(BUFFER_SIZE)
data2 = binascii.b2a_hex(data).decode('ascii')
return (data2)
但是,如果我运行脚本,我会得到:
Traceback (most recent call last):
File "Z:\programeren\repository's\red-cloud2\python backend\x10_server.py", line 8, in <module>
insteon.send('x10', 'N', '10', 'off')
File "Z:\programeren\repository's\red-cloud2\python backend\insteon.py", line 37, in send
s.send(message1)
OSError: [WinError 10038] an operation was attempted on something that is not a socket
我将 python 3 用于脚本,并从 Windows 7 pc 运行它。