def read_file(self, datatype, index): try: f = open(self.file_1, 'r') self.file_lock(f, LOCK) f.seek(0, 0) line = f.readline() except IOError: log("JSON file doesnt exist/Cant Read", ERROR) log("Trying Direct Object Read", WARNING) return self.read_object(datatype, index) Temp = jsonpickle.decode(line) for case in switch(datatype): if case('DIN'): data = Temp.DIN[index].read() break if case('AOUT'): data = Temp.AOUT[index].read() break if case('AIN'): data = Temp.AIN[index].read() break if case('DOUT'): data = Temp.DOUT[index].read() break if case('DEV_CHAR'): data = Temp.DEV_CHAR[index].read() break if(sys.platform != 'win32'): self.file_lock(f, UNLOCK) f.close() log("Read " + str(data) + " from " + str(datatype) + str(index), DEBUG,(datetime.datetime.now()-self.start_time)) return data
def read_object(self, attribute, index=-1): if(index == -1): try: temp_type = self.Name_Hash[attribute] except KeyError: log("Error in attribute", ERROR) return sp_temp = re.split('/', temp_type) datatype = sp_temp[0] index = int(sp_temp[1]) else: datatype = attribute for case in switch(datatype): if case('DIN'): return self.DIN[index].read() break if case('AOUT'): return self.AOUT[index].read() break if case('AIN'): return self.AIN[index].read() break if case('DOUT'): return self.DOUT[index].read() break if case('DEV_CHAR'): return self.DEV_CHAR[index].read() break
def write_file(self, value, datatype, index): try: f = open(self.file_1, 'r+') except IOError: log("JSON File Doesn't Exist", ERROR) f = open(self.file_1, 'w') self.file_lock(f, LOCK) self.write_time = datetime.datetime.now() f.write(jsonpickle.encode(self)) self.file_lock(f, UNLOCK) f.close() self.Delete_All_Changes() log("Created JSON file", EVENT) return self.file_lock(f, LOCK) line = f.readline() Temp = jsonpickle.decode(line) for case in switch(datatype): if case('DIN'): Temp.DIN[index].write(value) if(self.app_name != "MQTT"): Temp.Record_Change('DIN/' + str(index)) break if case('AOUT'): Temp.AOUT[index].write(value) if(self.app_name != "MQTT"): Temp.Record_Change('AOUT/' + str(index)) break if case('AIN'): Temp.AIN[index].write(value) if(self.app_name != "MQTT"): Temp.Record_Change('AIN/' + str(index)) break if case('DOUT'): Temp.DOUT[index].write(value) if(self.app_name != "MQTT"): Temp.Record_Change('DOUT/' + str(index)) break if case('DEV_CHAR'): Temp.DEV_CHAR[index].write(value) if(self.app_name != "MQTT"): Temp.Record_Change('DEV_CHAR/' + str(index)) break if case(): log('Error in DataType', ERROR) break self.write_time = datetime.datetime.now() Temp.write_time = self.write_time f.seek(0, 0) f.write(jsonpickle.encode(Temp)) log("wrote " + str(value) + " to " + str(datatype) + str(index), DEBUG, (datetime.datetime.now()-self.start_time)) f.truncate() if(sys.platform != 'win32'): self.file_lock(f, UNLOCK) f.close() # self.Delete_All_Changes() return
def move(self, new_profile, datatype, index): for case in switch(datatype): if case('DIN'): self.DIN[index] = new_profile.DIN[index] self.Record_Change('DIN/' + str(index)) break if case('AOUT'): self.AOUT[index] = new_profile.AOUT[index] self.Record_Change('AOUT/' + str(index)) break if case('AIN'): self.AIN[index] = new_profile.AIN[index] self.Record_Change('AIN/' + str(index)) break if case('DOUT'): self.DOUT[index] = new_profile.DOUT[index] self.Record_Change('DOUT/' + str(index)) break if case('DEV_CHAR'): self.DEV_CHAR[index] = new_profile.DEV_CHAR[index] self.Record_Change('DEV_CHAR/' + str(index)) break log("updated " + str(datatype) + str(index), DEBUG)