|
| 1 | +#! py3 |
| 2 | + |
| 3 | +import socket |
| 4 | +from collections import namedtuple |
| 5 | +from struct import * |
| 6 | + |
| 7 | +from pysdcp.protocol import * |
| 8 | + |
| 9 | +Header = namedtuple("Header", ['version', 'category', 'community']) |
| 10 | +ProjInfo = namedtuple("ProjInfo", ['id', 'product_name', 'serial_number', 'power_state', 'location']) |
| 11 | + |
| 12 | + |
| 13 | +def create_command_buffer(header: Header, action, command, data=None): |
| 14 | + # create bytearray in the right size |
| 15 | + if data is not None: |
| 16 | + my_buf = bytearray(12) |
| 17 | + else: |
| 18 | + my_buf = bytearray(10) |
| 19 | + # header |
| 20 | + my_buf[0] = 2 # only works with version 2, don't know why |
| 21 | + my_buf[1] = header.category |
| 22 | + # community |
| 23 | + my_buf[2] = ord(header.community[0]) |
| 24 | + my_buf[3] = ord(header.community[1]) |
| 25 | + my_buf[4] = ord(header.community[2]) |
| 26 | + my_buf[5] = ord(header.community[3]) |
| 27 | + # command |
| 28 | + my_buf[6] = action |
| 29 | + pack_into(">H", my_buf, 7, command) |
| 30 | + if data is not None: |
| 31 | + # add data len |
| 32 | + my_buf[9] = 2 # Data is always 2 bytes |
| 33 | + # add data |
| 34 | + pack_into(">H", my_buf, 10, data) |
| 35 | + else: |
| 36 | + my_buf[9] = 0 |
| 37 | + return my_buf |
| 38 | + |
| 39 | + |
| 40 | +def process_command_response(msgBuf): |
| 41 | + my_header = Header( |
| 42 | + version=int(msgBuf[0]), |
| 43 | + category=int(msgBuf[1]), |
| 44 | + community=decode_text_field(msgBuf[2:6])) |
| 45 | + is_success = bool(msgBuf[6]) |
| 46 | + command = unpack(">H", msgBuf[7:9])[0] |
| 47 | + data_len = int(msgBuf[9]) |
| 48 | + if data_len != 0: |
| 49 | + data = unpack(">H", msgBuf[10:10 + data_len])[0] |
| 50 | + else: |
| 51 | + data = None |
| 52 | + return my_header, is_success, command, data |
| 53 | + |
| 54 | + |
| 55 | +def process_SDAP(SDAP_buffer) -> (Header, ProjInfo): |
| 56 | + try: |
| 57 | + my_header = Header( |
| 58 | + version=int(SDAP_buffer[2]), |
| 59 | + category=int(SDAP_buffer[3]), |
| 60 | + community=decode_text_field(SDAP_buffer[4:8])) |
| 61 | + my_info = ProjInfo( |
| 62 | + id=SDAP_buffer[0:2].decode(), |
| 63 | + product_name=decode_text_field(SDAP_buffer[8:20]), |
| 64 | + serial_number=unpack('>I', SDAP_buffer[20:24])[0], |
| 65 | + power_state=unpack('>H', SDAP_buffer[24:26])[0], |
| 66 | + location=decode_text_field(SDAP_buffer[26:])) |
| 67 | + except Exception as e: |
| 68 | + print("Error parsing SDAP packet: {}".format(e)) |
| 69 | + raise |
| 70 | + return my_header, my_info |
| 71 | + |
| 72 | + |
| 73 | +def decode_text_field(buf): |
| 74 | + """ |
| 75 | + Convert char[] string in buffer to python str object |
| 76 | + :param buf: bytearray with array of chars |
| 77 | + :return: string |
| 78 | + """ |
| 79 | + return buf.decode().strip(b'\x00'.decode()) |
| 80 | + |
| 81 | + |
| 82 | +class Projector: |
| 83 | + def __init__(self, ip: str = None): |
| 84 | + """ |
| 85 | + Base class for projector communication. |
| 86 | + Enables communication with Projector, Sending commands and Querying Power State |
| 87 | + |
| 88 | + :param ip: str, IP address for projector. if given, will create a projector with default values to communicate |
| 89 | + with projector on the given ip. i.e. "10.0.0.5" |
| 90 | + """ |
| 91 | + self.info = ProjInfo( |
| 92 | + product_name=None, |
| 93 | + serial_number=None, |
| 94 | + power_state=None, |
| 95 | + location=None, |
| 96 | + id=None) |
| 97 | + if ip is None: |
| 98 | + # Create empty Projector object |
| 99 | + self.ip = None |
| 100 | + self.header = Header(version=None, category=None, community=None) |
| 101 | + self.is_init = False |
| 102 | + else: |
| 103 | + # Create projector from known ip |
| 104 | + # Set default values to enable immediately communication with known project (ip) |
| 105 | + self.ip = ip |
| 106 | + self.header = Header(category=10, version=2, community="SONY") |
| 107 | + self.is_init = True |
| 108 | + |
| 109 | + # Default ports |
| 110 | + self.UDP_IP = "" |
| 111 | + self.UDP_PORT = 53862 |
| 112 | + self.TCP_PORT = 53484 |
| 113 | + self.TCP_TIMEOUT = 2 |
| 114 | + self.UDP_TIMEOUT = 31 |
| 115 | + |
| 116 | + def __eq__(self, other): |
| 117 | + return self.info.serial_number == other.info.serail_number |
| 118 | + |
| 119 | + def _send_command(self, action, command, data=None, timeout=None): |
| 120 | + timeout = timeout if timeout is not None else self.TCP_TIMEOUT |
| 121 | + if not self.is_init: |
| 122 | + self.find_projector() |
| 123 | + if not self.is_init: |
| 124 | + raise Exception("No projector found and / or specified") |
| 125 | + |
| 126 | + my_buf = create_command_buffer(self.header, action, command, data) |
| 127 | + |
| 128 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 129 | + sock.settimeout(timeout) |
| 130 | + try: |
| 131 | + sock.connect((self.ip, self.TCP_PORT)) |
| 132 | + sent = sock.send(my_buf) |
| 133 | + except socket.timeout as e: |
| 134 | + raise Exception("Timeout while trying to send command {}".format(command)) from e |
| 135 | + |
| 136 | + if len(my_buf) != sent: |
| 137 | + raise ConnectionError( |
| 138 | + "Failed sending entire buffer to projector. Sent {} out of {} !".format(sent, len(my_buf))) |
| 139 | + response_buf = sock.recv(1024) |
| 140 | + sock.close() |
| 141 | + |
| 142 | + _, is_success, _, data = process_command_response(response_buf) |
| 143 | + |
| 144 | + if not is_success: |
| 145 | + raise Exception( |
| 146 | + "Received failed status from projector while sending command 0x{:x}. Error 0x{:x}".format(command, |
| 147 | + data)) |
| 148 | + return data |
| 149 | + |
| 150 | + def find_projector(self, udp_ip: str = None, udp_port: int = None, timeout=None): |
| 151 | + |
| 152 | + self.UDP_PORT = udp_port if udp_port is not None else self.UDP_PORT |
| 153 | + self.UDP_IP = udp_ip if udp_ip is not None else self.UDP_IP |
| 154 | + timeout = timeout if timeout is not None else self.UDP_TIMEOUT |
| 155 | + |
| 156 | + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 157 | + |
| 158 | + sock.bind((self.UDP_IP, self.UDP_PORT)) |
| 159 | + |
| 160 | + sock.settimeout(timeout) |
| 161 | + try: |
| 162 | + SDAP_buffer, addr = sock.recvfrom(1028) |
| 163 | + except socket.timeout as e: |
| 164 | + return False |
| 165 | + |
| 166 | + self.header, self.info = process_SDAP(SDAP_buffer) |
| 167 | + self.ip = addr[0] |
| 168 | + self.is_init = True |
| 169 | + |
| 170 | + def set_power(self, on=True): |
| 171 | + self._send_command(action=ACTIONS["SET"], command=COMMANDS["SET_POWER"], |
| 172 | + data=POWER_STATUS["START_UP"] if on else POWER_STATUS["STANDBY"]) |
| 173 | + return True |
| 174 | + |
| 175 | + def set_HDMI_input(self, hdmi_num: int): |
| 176 | + self._send_command(action=ACTIONS["SET"], command=COMMANDS["INPUT"], |
| 177 | + data=INPUTS["HDMI1"] if hdmi_num == 1 else INPUTS["HDMI2"]) |
| 178 | + return True |
| 179 | + |
| 180 | + def get_power(self): |
| 181 | + data = self._send_command(action=ACTIONS["GET"], command=COMMANDS["GET_STATUS_POWER"]) |
| 182 | + if data == POWER_STATUS["STANDBY"] or data == POWER_STATUS["COOLING"] or data == POWER_STATUS["COOLING2"]: |
| 183 | + return False |
| 184 | + else: |
| 185 | + return True |
| 186 | + |
| 187 | + def get_power_string(self): |
| 188 | + data = self._send_command(action=ACTIONS["GET"], command=COMMANDS["GET_STATUS_POWER"]) |
| 189 | + return list(POWER_STATUS.keys())[list(POWER_STATUS.values()).index(data)] |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == '__main__': |
| 193 | + # b = Projector() |
| 194 | + # b.find_projector(timeout=1) |
| 195 | + # # print(b.get_power()) |
| 196 | + # # b = Projector("10.0.0.139") |
| 197 | + # # # |
| 198 | + # print(b.get_power()) |
| 199 | + # print(b.set_power(False)) |
| 200 | + # # import time |
| 201 | + # # time.sleep(7) |
| 202 | + # print (b.set_HDMI_input(1)) |
| 203 | + # # time.sleep(7) |
| 204 | + # # print (b.set_HDMI_input(2)) |
| 205 | + pass |
0 commit comments