def start_cam(address): json_data = machine_data.create() while True: client_socket = socket.socket() client_socket.setblocking(False) try: client_socket.connect(address) except OSError as error: if error.args[0] not in [uerrno.EINPROGRESS, uerrno.ETIMEDOUT]: print(color.red()+'**** Error connecting ****', error+color.normal()) try: POST(client_socket, 'snap/dev_cam01', 'img',next(pic)) sleep(0.2) while True: res = str(client_socket.recv(2000)) command = str(res)[str(res).find('\\r\\n\\r\\n')+len('\\r\\n\\r\\n'):len(str(res))-1] json_command = ujson.loads(command) print(json_command) break except OSError as error: if error.args[0] not in [uerrno.EINPROGRESS, uerrno.ETIMEDOUT]: print(color.red()+'\t\t\t**** ERROR writing ****\n\t\t\t', error+color.normal()) attempts = 0 client_socket.close() # flash buffer and close socket del client_socket gc.collect() sleep(.1)
def start_com(address): json_data = machine_data.create() while True: #print(color.blue()+'{') client_socket = socket.socket() client_socket.setblocking(0) try: #print('\tConnect to',address) client_socket.connect(address) except OSError as error: if error.args[0] not in [uerrno.EINPROGRESS, uerrno.ETIMEDOUT]: print(color.red() + '**** Error connecting ****', error + color.normal()) attempts = 10 json_data = machine_data.get() while attempts: attempts = attempts - 1 #print('\t{\n\t\tAttempt to send data: ', attempts) try: #print('\t\t{\n\t\t\tSending data') client_socket.sendall(bytes(str(json_data), 'UTF-8')) #print('\t\t\tData Sent\n\t\t\t{') sleep(0.2) #print('\t\t\t\tWait for response') while True: buffer_data = client_socket.recv(2000) host_data = str(buffer_data) if host_data.find('null') == -1: parsed_input = str(host_data[2:len(host_data) - 1].replace("\'", "\"")) command_json = ujson.loads(parsed_input) machine_data.parse_data(command_json) attempts = 0 #print('\t\t\t\tGot response') break #print(color.red()+'\t\t\t}') except OSError as error: if error.args[0] not in [uerrno.EINPROGRESS, uerrno.ETIMEDOUT]: print( color.red() + '\t\t\t**** ERROR writing ****\n\t\t\t', error) attempts = 0 #print(color.red()+'\t\t}') sleep(0.1) break client_socket.close() #print(color.red()+'\t}\n') #print(color.red()+'}'+color.normal()) sleep(0.1)
def start_com(address): json_data = machine_data.create() while True: print(color.green() + 'Check cam_socket') cam_state = sget('cam') while cam_state != 'socket_closed': cam_state = sget('cam') sleep(.05) pass print(color.green() + 'cam_socket disconnected') try: sset('com', 'creating_socket') client_socket = socket.socket() print(color.normal() + '{') client_socket.setblocking(0) try: print(color.green() + '{\n\tConnect to', "asmon.com.ar:", 8080) sset('com', 'connecting') client_socket.connect(address) print('\t\t{\n\t\t\tSending data') POST(client_socket, 'esp_data/dev_cam01', 'json', machine_data.get()) print('\t\t\tData Sent\n\t\t\t{') print('\t\t\t\tWait for response') sleep(0.2) buffer_data = client_socket.recv(1024) while (len(buffer_data) > 0): print(buffer_data) print(color.blue() + '\t\t\t\tGot response') break connection = True except Exception as error: client_socket.close() connection = False print(color.red() + '\t\t\t**** ERROR Connecting ****\n\t\t\t', error) client_socket.close() # flash buffer and close socket del client_socket sset('com', 'socket_closed') except: print('failed to create socket') gc.collect() print(color.red() + '\t\t\t}') print(color.normal() + '\t\t}') print(color.normal() + '\t}\n') print('}') sleep(.5)
def start_com(address): global json_command json_data = machine_data.create() while True: cam_state = sget('com') if cam_state == 'socket_closed': sset('com','busy') try: client_socket = socket.socket() client_socket.setblocking(False) try: client_socket.connect(address) except OSError as error: if error.args[0] not in [uerrno.EINPROGRESS, uerrno.ETIMEDOUT]: print(color.red()+'**** Error connecting ****', error, color.normal()) try: POST(client_socket, 'esp_data/dev_cam01', 'json',machine_data.get()) while True: res = str(client_socket.recv(2000)) if res.find('200 OK') != -1: command = str(res)[str(res).find('\\r\\n\\r\\n')+len('\\r\\n\\r\\n'):len(str(res))-1] new_json_command = ujson.loads(command) if json_command != new_json_command: json_command = new_json_command print(json_command) break except OSError as error: if error.args[0] not in [uerrno.EINPROGRESS, uerrno.ETIMEDOUT]: print(color.red()+'\t\t\t**** ERROR writing ****\n\t\t\t', error, color.normal()) client_socket.close() # flash buffer and close socket del client_socket gc.collect() except OSError as error: print(color.red()+'\t\t\t**** ERROR creating socket ****\n\t\t\t', error, color.normal()) sset('com','socket_closed') sleep(.1)
'Content-Type': 'application/json', 'Content-Length': len(data_json), 'Connection': 'close', } response_headers_raw = ''.join('%s: %s\r\n' % (k, v) for k, v in response_headers.items()) response_proto = 'HTTP/1.1' response_status = '200' response_status_text = 'OK' r = '%s %s %s\r\n' % (response_proto, response_status, response_status_text) client.send(r.encode()) client.send(response_headers_raw.encode()) client.send('\r\n'.encode()) # to separate headers from body client.sendall(data_json.encode()) machine_data.create() html = create_html() com_data = """ <html> <input id="esp_data" style="padding: 8px 8px; display: block;width: 93.6%;" value="$machine_data" disabled> <script> machine_data = document.getElementById('esp_data').value; window.parent.document.getElementById('esp_data').value = machine_data; </script> </html> """