def save_frame(self, frame, timestamp=0, origin=None, routed=False): """Saves a BayEOS Frame either as it is or wrapped in an Origin Frame.""" if not origin: self.__save_frame(frame, timestamp); else: if routed: origin_frame = BayEOSFrame.factory(0xd) else: origin_frame = BayEOSFrame.factory(0xb) origin_frame.create(origin=origin, nested_frame=frame) self.__save_frame(origin_frame.frame, timestamp)
def __post_file(self, file_name): """Reads one file and tries to send its content to the gateway. uses the requests library!! On success the file is deleted or renamed to *.bak ending. @return number of successfully posted frames in one file """ current_file = open(file_name, 'rb') # opens oldest file data={'sender': self.name} frames=[] timestamp = current_file.read(8) while timestamp: # until end of file timestamp = unpack('<d', timestamp)[0] frame_length = unpack('<h', current_file.read(2))[0] frame = current_file.read(frame_length) if frame: if self.absolute_time: # Timestamp Frame # millisecond resolution from 1970-01-01 wrapper_frame = BayEOSFrame.factory(0xc) else: # Delayed Frame wrapper_frame = BayEOSFrame.factory(0x7) wrapper_frame.create(frame, timestamp) frames.append(base64.b64encode(wrapper_frame.frame)) timestamp = current_file.read(8) current_file.close() backup_file_name = file_name.replace('.rd', '.bak') if self.backup_path: backup_file_name.replace(self.path, self.backup_path) if len(frames)==0: move(file_name, backup_file_name) logging.warning('No frames in file. Move to ' + backup_file_name) return 0 data['bayeosframes[]']=frames headers={'user-agent': 'BayEOS-Python-Gateway-Client/0.3.9'} try: r=requests.post(self.url,data=data,auth=(self.user, self.password),headers=headers, timeout=10) # r.raise_for_status() except requests.exceptions.RequestException as e: logging.warning('sender __post error:'+e) return 0 if r.status_code==200: # all fine! if self.remove: os.remove(file_name) else: move(file_name, backup_file_name) return len(frames) logging.warning('sender __post error code: '+r.status_code) return 0
def __send_file(self, file_name): """Reads one file and tries to send its content to the gateway. On success the file is deleted or renamed to *.bak ending. Always the oldest file is used. @return number of successfully posted frames in one file """ current_file = open(file_name, 'rb') # opens oldest file post_request = '&sender=' + urllib.quote_plus(self.name) frames = '' count_frames = 0 timestamp = current_file.read(8) while timestamp: # until end of file timestamp = unpack('<d', timestamp)[0] frame_length = unpack('<h', current_file.read(2))[0] frame = current_file.read(frame_length) if frame: count_frames += 1 if self.absolute_time: # Timestamp Frame # millisecond resolution from 1970-01-01 wrapper_frame = BayEOSFrame.factory(0xc) else: # Delayed Frame wrapper_frame = BayEOSFrame.factory(0x7) wrapper_frame.create(frame, timestamp) frames += '&bayeosframes[]=' + base64.urlsafe_b64encode(wrapper_frame.frame) timestamp = current_file.read(8) current_file.close() backup_file_name = file_name.replace('.rd', '.bak') if self.backup_path: backup_file_name.replace(self.path, self.backup_path) if frames: # content found for post request try: post_result = self.__post(post_request + frames) except: logging.warning('sender __post error') return 0 if post_result == 1: # successfuly posted if self.remove: os.remove(file_name) else: move(file_name, backup_file_name) return count_frames return 0 # post without success but error catched else: # broken file move(file_name, backup_file_name) logging.warning('No frames in file. Move to ' + backup_file_name) return 0
def save(self, values, value_type=0x41, offset=0, timestamp=0, origin=None): """Generic frame saving method. @param values: list with [channel index, value] tuples or just values (..,..) or [..,..] @param value_type: defines Offset and Data Type @param offset: defines Channel Offset @param timestamp: Unix epoch time stamp, if zero system time is used @param origin: if defined, it is used as a name """ data_frame = BayEOSFrame.factory(0x1) data_frame.create(values, value_type, offset) if not origin: self.__save_frame(data_frame.frame, timestamp) else: origin_frame = BayEOSFrame.factory(0xb) origin_frame.create(origin=origin, nested_frame=data_frame.frame) self.__save_frame(origin_frame.frame, timestamp)
def save(self, values, value_type=0x41, offset=0, timestamp=0, origin=None, routed=False): """Generic frame saving method. @param values: list with [channel index, value] tuples or just values (..,..) or [..,..] @param value_type: defines Offset and Data Type @param offset: defines Channel Offset @param timestamp: Unix epoch time stamp, if zero system time is used @param origin: if defined, it is used as a name @param routed: only relevant with origin - if true, routed origin is created """ data_frame = BayEOSFrame.factory(0x1) data_frame.create(values, value_type, offset) if not origin: self.__save_frame(data_frame.frame, timestamp) else: origin_frame = BayEOSFrame.factory(0xb) origin_frame.create(origin=origin, nested_frame=data_frame.frame) self.__save_frame(origin_frame.frame, timestamp)
def save_frame(self, frame, timestamp=0, origin=None): """Saves a BayEOS Frame either as it is or wrapped in an Origin Frame.""" if not origin: self.__save_frame(frame, timestamp); else: origin_frame = BayEOSFrame.factory(0xb) origin_frame.create(origin=origin, nested_frame=frame) self.__save_frame(origin_frame.frame, timestamp)
def save_msg(self, message, error=False, timestamp=0, origin=None): """Saves Messages or Error Messages to Gateway. @param message: String to send @param error: when true, an Error Message is sent @param timestamp: Unix epoch time stamp, if zero system time is used """ if error: msg_frame = BayEOSFrame.factory(0x5) # instantiate ErrorMessage Frame else: msg_frame = BayEOSFrame.factory(0x4) # instantiate Message Frame msg_frame.create(message) if not origin: self.__save_frame(msg_frame.frame, timestamp) else: origin_frame = BayEOSFrame.factory(0xb) origin_frame.create(origin=origin, nested_frame=msg_frame.frame) self.__save_frame(origin_frame.frame, timestamp)
def save_msg(self, message, error=False, timestamp=0, origin=None, routed=False): """Saves Messages or Error Messages to Gateway. @param message: String to send @param error: when true, an Error Message is sent @param timestamp: Unix epoch time stamp, if zero system time is used @param origin: if defined, it is used as a name @param routed: only relevant with origin - if true, routed origin is created """ if error: msg_frame = BayEOSFrame.factory(0x5) # instantiate ErrorMessage Frame else: msg_frame = BayEOSFrame.factory(0x4) # instantiate Message Frame msg_frame.create(message) if not origin: self.__save_frame(msg_frame.frame, timestamp) else: origin_frame = BayEOSFrame.factory(0xb) origin_frame.create(origin=origin, nested_frame=msg_frame.frame) self.__save_frame(origin_frame.frame, timestamp)
"""Creates example BayEOS Frames.""" from bayeosframe import BayEOSFrame # Data Frames, simple data_frame_simple = BayEOSFrame.factory(0x1) data_frame_simple.create(values=(2, 4), value_type=0x22) # Data Type Integer print data_frame_simple.parse() data_frame_simple.to_string() print data_frame_simple.get_name() print data_frame_simple.get_payload() print BayEOSFrame.parse_frame(data_frame_simple.frame) # Data Frame with Channel Offset data_frame_offset = BayEOSFrame.factory() # 0x1 is default data_frame_offset.create([2.3, 4.5], value_type=0x01, offset=2) # Data Type float data_frame_offset.to_string() # Data Frame with Channel Indices data_frame_indices = BayEOSFrame.factory() data_frame_indices.create(([3, 1], [2, 5]), value_type=0x41) # Data Type float data_frame_indices.to_string() data_frame_indices2 = BayEOSFrame.factory() data_frame_indices2.create(((3, 1), (2, 5)), value_type=0x42) # Data Type Integer data_frame_indices2.to_string() # Message Frames message_frame = BayEOSFrame().factory(0x4) message_frame.create(message="This is an important message.") message_frame.to_string()