def send_zmq(): nonlocal wheel, channel sha512 = hashlib.sha512() try: # TODO: RMQ channel??? # Note sending and receiving through a channel all communication # is binary for zmq (RMQ may be different for this functionality) # first = True op = None size = None while True: if first: first = False # Wait for peer to open compliment channel resp = jsonapi.loadb(channel.recv()) _log.debug(f"Got first response {resp}") if len(resp) > 1: op, size = resp else: op = resp[0] if op != 'fetch': raise ValueError( f'First channel response must be fetch but was {op}' ) if op == 'fetch': chunk = wheel.read(size) if chunk: _log.debug(f"Op was fetch sending {size}") sha512.update(chunk) channel.send(chunk) else: _log.debug(f"Op was fetch sending complete") channel.send(b'complete') gevent.sleep(10) break elif op == 'checksum': _log.debug(f"sending checksum {sha512.hexdigest()}") channel.send(sha512.digest()) _log.debug("Waiting for next response") # wait for next response resp = jsonapi.loadb(channel.recv()) if len(resp) > 1: op, size = resp else: op = resp[0] finally: _log.debug("Closing wheel and channel.") wheel.close() channel.close(linger=0) del channel
def set_heat_pgm(self,schedules,day=''): """ set heat program for a week or a specific day day = {'mon','tue','wed','thu','fri','sat','sun'} for a spefic day, say 'thu' .. code-block:: python t.set_heat_pgm('{"360, 80, 480, 80, 1080, 80, 1320 , 80",'thu') for a week .. code-block:: python t.set_heat_pgm('{ "1": [360, 70, 480, 70, 1080, 70, 1320, 70], "0": [360, 66, 480, 58, 1080, 66, 1320, 58], "3": [360, 66, 480, 58, 1080, 66, 1320, 58], "2": [360, 66, 480, 58, 1080, 66, 1320, 58], "5": [360, 66, 480, 58, 1080, 66, 1320, 58], "4": [360, 66, 480, 58, 1080, 66, 1320, 58], "6": [360, 66, 480, 58, 1080, 66, 1320, 58] }') """ schedule = str(schedules) if day =='': url = self.urladdress+"/program/heat" try: mode = (urllib.request.urlopen(url, jsonapi.dumps(schedules))) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed else: url = self.urladdress+"/program/heat/"+str(day) try: schedule_str = {} schedule_str = { str(self.day_num[day]): [int(e) if e.isdigit() else e for e in schedule.split(',')]} mode = (urllib.request.urlopen(url,jsonapi.dumps(schedule_str))) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed
def model(self): ''' Returns device model''' address= self.address+"/model" try: mode = (urllib.request.urlopen(address)) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed
def t_heat(self,data): ''' Sets heating setpoint''' msg = {"tmode":1,"t_heat":data} value = jsonapi.dumps(msg) try: mode = (urllib.request.urlopen(self.urladdress,value)) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed
def tstat(self): ''' Returns current deicve paramenters''' try: mode = (urllib.request.urlopen(self.urladdress)) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed
def hold(self,data): ''' Sets hold controls''' msg = {"hold":data} value = jsonapi.dumps(msg) try: mode = (urllib.request.urlopen(self.urladdress,value)) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed
def mode(self,data): ''' Sets operating mode''' msg = {"tmode":data} value = jsonapi.dumps(msg) try: mode = (urllib.request.urlopen(self.urladdress,value)) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed
def energy_led(self,data): ''' Controls energy led, possible values: 0,1,2,4''' url = self.urladdress+"/led" msg = { "energy_led" :int(data)} value = jsonapi.dumps(msg) try: mode = (urllib.request.urlopen(url,value)) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed
def t_setpoint(self,data,point,tmode=''): ''' Sets cooling setpoint''' if tmode == '': msg = { point : data } else : msg = {"tmode": tmode, point : data} value = jsonapi.dumps(msg) try: mode = (urllib.request.urlopen(self.urladdress,value)) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed
def send_file(self, to_peer, file_to_send): _log.debug(f"Sending file to peer {to_peer}") channel_name = "sending_file_channel" channel = self.vip.channel(to_peer, channel_name) _log.debug("Calling setup_send_file on receiver.") self.vip.rpc.call(to_peer, "setup_send_file", channel_name) gevent.sleep(0.5) _log.debug("After calling rpc method!") sha512 = hashlib.sha512() with open(file_to_send, "rb") as infile: first = True while True: with gevent.Timeout(120): _log.debug("Attempting to read from channel") # Protocol should be either a fetch or checksum my_data = channel.recv() op, size = jsonapi.loadb(my_data) # op, size = channel.recv_multipart() #_log.debug(f"Op size is {op} {size}") if first: first = False if op != 'fetch': channel.close(linger=0) del channel raise ValueError( "Invalid protocol detected should be [b'fetch', size] where size is the amount of data to retrieve." ) if op == 'fetch': chunk = infile.read(size) if chunk: sha512.update(chunk) # _log.debug(f"Sending chunk: {chunk}") channel.send(chunk) else: channel.send(b'complete') break elif op == 'checksum': _log.debug(f"Sending checksum: {sha512.hexdigest()}") channel.send(sha512.hexdigest().encode('utf-8')) _log.debug("Complete sending of file. Closing channel.") gevent.sleep(0.5) channel.close(linger=0) del channel
def get_cool_pgm(self,day=''): ''' get cool program for a week or a specific day day = {'mon','tue','wed','thu','fri','sat','sun'} for a specific day, say thursday: t.get_cool_pgm('thu') for a week: t.get_cool_pgm() ''' if day =='': url = self.urladdress+"/program/cool" else: url = self.urladdress+"/program/cool/"+str(day) try: mode = (urllib.request.urlopen(url)) parsed = jsonapi.loadb(mode.read()) return jsonapi.dumps(parsed) except Exception as parsed: return parsed