def get_plot(self, max_points_x=None): result = [] for each_method in self.method_data_all: if each_method.time_start is None or each_method.time_end is None: continue if each_method.setpoint_end is None: setpoint_end = each_method.setpoint_start else: setpoint_end = each_method.setpoint_end result.append([ get_sec(each_method.time_start) * 1000, each_method.setpoint_start ]) result.append([get_sec(each_method.time_end) * 1000, setpoint_end]) result.append([get_sec(each_method.time_start) * 1000, None]) return result
def method_data(method_id): """ Returns options for a particular method This includes sets of (time, setpoint) data. """ # First method column with general information about method method = Method.query.filter(Method.id == method_id).first() # User-edited lines of each method method_data = MethodData.query.filter(MethodData.method_id == method.id) # Retrieve the order to display method data lines display_order = csv_to_list_of_int(method.method_order) last_method_data = None if display_order is not None: method_data = MethodData.query.filter( MethodData.method_id == method.id) last_method_data = method_data.filter( MethodData.id == display_order[-1]).first() method_list = [] if method.method_type == "Date": for each_method in method_data: if each_method.setpoint_end == None: setpoint_end = each_method.setpoint_start else: setpoint_end = each_method.setpoint_end start_time = datetime.datetime.strptime(each_method.time_start, '%Y-%m-%d %H:%M:%S') end_time = datetime.datetime.strptime(each_method.time_end, '%Y-%m-%d %H:%M:%S') is_dst = time.daylight and time.localtime().tm_isdst > 0 utc_offset_ms = (time.altzone if is_dst else time.timezone) method_list.append([ (int(start_time.strftime("%s")) - utc_offset_ms) * 1000, each_method.setpoint_start ]) method_list.append([ (int(end_time.strftime("%s")) - utc_offset_ms) * 1000, setpoint_end ]) method_list.append([ (int(start_time.strftime("%s")) - utc_offset_ms) * 1000, None ]) elif method.method_type == "Daily": method_data = method_data.filter(MethodData.time_start != None) method_data = method_data.filter(MethodData.time_end != None) method_data = method_data.filter(MethodData.relay_id == None) for each_method in method_data: if each_method.setpoint_end is None: setpoint_end = each_method.setpoint_start else: setpoint_end = each_method.setpoint_end method_list.append([ get_sec(each_method.time_start) * 1000, each_method.setpoint_start ]) method_list.append( [get_sec(each_method.time_end) * 1000, setpoint_end]) method_list.append([get_sec(each_method.time_start) * 1000, None]) elif method.method_type == "DailyBezier": points_x = 700 seconds_in_day = 60 * 60 * 24 P0 = (last_method_data.x0, last_method_data.y0) P1 = (last_method_data.x1, last_method_data.y1) P2 = (last_method_data.x2, last_method_data.y2) P3 = (last_method_data.x3, last_method_data.y3) for n in range(points_x): percent = n / float(points_x) second_of_day = percent * seconds_in_day if second_of_day == 0: continue y = bezier_curve_y_out(last_method_data.shift_angle, P0, P1, P2, P3, second_of_day) method_list.append([percent * seconds_in_day * 1000, y]) elif method.method_type == "DailySine": points_x = 700 seconds_in_day = 60 * 60 * 24 for n in range(points_x): percent = n / float(points_x) angle = n / float(points_x) * 360 y = sine_wave_y_out(last_method_data.amplitude, last_method_data.frequency, last_method_data.shift_angle, last_method_data.shift_y, angle) method_list.append([percent * seconds_in_day * 1000, y]) elif method.method_type == "Duration": first_entry = True start_duration = 0 end_duration = 0 method_data = method_data.filter(MethodData.relay_id == None) for each_method in method_data: if each_method.setpoint_end is None: setpoint_end = each_method.setpoint_start else: setpoint_end = each_method.setpoint_end if each_method.duration_sec == 0: pass # Method line is repeat command, don't add to method_list elif first_entry: method_list.append([0, each_method.setpoint_start]) method_list.append([each_method.duration_sec, setpoint_end]) start_duration += each_method.duration_sec first_entry = False else: end_duration = start_duration + each_method.duration_sec method_list.append( [start_duration, each_method.setpoint_start]) method_list.append([end_duration, setpoint_end]) start_duration += each_method.duration_sec return jsonify(method_list)
def method_data(method_type, method_id): """ Returns options for a particular method This includes sets of (time, setpoint) data. """ logger.debug('called method_data(method_type={type}, ' 'method_id={id})'.format(type=method_type, id=method_id)) if not logged_in(): return redirect(url_for('general_routes.home')) method = db_retrieve_table(current_app.config['MYCODO_DB_PATH'], Method) # First method column with general information about method method_key = method.filter(Method.method_id == method_id) method_key = method_key.filter(Method.method_order == 0).first() # User-edited lines of each method method = method.filter(Method.method_id == method_id) method = method.filter(Method.method_order > 0) method = method.filter(Method.relay_id == None) method = method.order_by(Method.method_order.asc()).all() method_list = [] if method_key.method_type == "Date": for each_method in method: if each_method.end_setpoint == None: end_setpoint = each_method.start_setpoint else: end_setpoint = each_method.end_setpoint start_time = datetime.datetime.strptime(each_method.start_time, '%Y-%m-%d %H:%M:%S') end_time = datetime.datetime.strptime(each_method.end_time, '%Y-%m-%d %H:%M:%S') is_dst = time.daylight and time.localtime().tm_isdst > 0 utc_offset_ms = (time.altzone if is_dst else time.timezone) method_list.append([ (int(start_time.strftime("%s")) - utc_offset_ms) * 1000, each_method.start_setpoint ]) method_list.append([ (int(end_time.strftime("%s")) - utc_offset_ms) * 1000, end_setpoint ]) method_list.append([ (int(start_time.strftime("%s")) - utc_offset_ms) * 1000, None ]) elif method_key.method_type == "Daily": for each_method in method: if each_method.end_setpoint is None: end_setpoint = each_method.start_setpoint else: end_setpoint = each_method.end_setpoint method_list.append([ get_sec(each_method.start_time) * 1000, each_method.start_setpoint ]) method_list.append( [get_sec(each_method.end_time) * 1000, end_setpoint]) method_list.append([get_sec(each_method.start_time) * 1000, None]) elif method_key.method_type == "DailyBezier": points_x = 700 seconds_in_day = 60 * 60 * 24 P0 = (method_key.x0, method_key.y0) P1 = (method_key.x1, method_key.y1) P2 = (method_key.x2, method_key.y2) P3 = (method_key.x3, method_key.y3) for n in range(points_x): percent = n / float(points_x) second_of_day = percent * seconds_in_day y = bezier_curve_y_out(method_key.shift_angle, P0, P1, P2, P3, second_of_day) method_list.append([percent * seconds_in_day * 1000, y]) elif method_key.method_type == "DailySine": points_x = 700 seconds_in_day = 60 * 60 * 24 for n in range(points_x): percent = n / float(points_x) angle = n / float(points_x) * 360 y = sine_wave_y_out(method_key.amplitude, method_key.frequency, method_key.shift_angle, method_key.shift_y, angle) method_list.append([percent * seconds_in_day * 1000, y]) elif method_key.method_type == "Duration": first_entry = True start_duration = 0 end_duration = 0 for each_method in method: if each_method.end_setpoint is None: end_setpoint = each_method.start_setpoint else: end_setpoint = each_method.end_setpoint if first_entry: method_list.append([0, each_method.start_setpoint]) method_list.append([each_method.duration_sec, end_setpoint]) start_duration += each_method.duration_sec first_entry = False else: end_duration = start_duration + each_method.duration_sec method_list.append( [start_duration, each_method.start_setpoint]) method_list.append([end_duration, end_setpoint]) start_duration += each_method.duration_sec return jsonify(method_list)