def delete(self, *args, **kwargs): """ delete请求,通过工厂id和班组id,删除班组信息, 删除班组信息的时候要删除班次中包含班组的记录设置为None :param args: :param kwargs: /factory_id/group_id :return: 成功返回204,未找到班组返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') if len(parameter_list) == 2: factory_id = parameter_list[0] group_id = parameter_list[1] group_obj = WorkGroupDB(factory_id=factory_id) result = group_obj.remove(id=group_id) schedule_obj = WorkScheduleDB(factory_id=factory_id) result1 = schedule_obj.set_none_for_groupid(group_id=group_id) group_obj.close() schedule_obj.close() if result and result1: SpiderApi.response(204) self.write_json(None, 204) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def get(self, *args, **kwargs): """ get请求: 通过工厂id和班次id获取班次的详细信息 :param args: :param kwargs: /factory_id/schedule_id :return: 成功返回班次的信息,未找到数据返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') if len(parameter_list) == 2: factory_id = parameter_list[0] schedule_id = parameter_list[1] schedule_obj = WorkScheduleDB(factory_id=factory_id) result = schedule_obj.find(id=schedule_id) schedule_obj.close() if result: SpiderApi.response(200) self.write_json(result) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def post(self, *args, **kwargs): """ post请求:通过工厂id,获取该工厂下所有的班次信息 :param args: page: 第几页 per_page: 每页显示的条数 :param kwargs: /factory_id :return: 成功返回班次详细信息列表和201,未找到班次信息返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) factory_id = kwargs['factory_id'] request_data = kwargs['request_data'] page = request_data.get('page') per_page = request_data.get('per_page') if factory_id and page and per_page: limit = int(per_page) offset = (int(page) - 1) * limit schedule_obj = WorkScheduleDB(factory_id=factory_id) result = schedule_obj.findall(factory_id, offset, limit) schedule_obj.close() if result: SpiderApi.response(201) self.write_json(result, 201) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def delete(self, *args, **kwargs): """ delete请求:通过factory_id和schedule_id,删除班次 :param args: :param kwargs: /factory_id/schedule_id :return: 成功返回204,未找到数据返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') if len(parameter_list) == 2: factory_id = parameter_list[0] schedule_id = parameter_list[1] schedule_obj = WorkScheduleDB(factory_id=factory_id) result = schedule_obj.remove(id=schedule_id) schedule_obj.close() if result: SpiderApi.response(204) self.write_json(None, 204) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def get_schedule_list(factory_id): """ 根据该工厂中的work_schedule获取班组的安排信息 :param factory_id: 工厂id :return: 成功返回班组的安排信息,包含班组,班次,时间段的迭代器 """ result = [] schedule_obj = WorkScheduleDB(factory_id=factory_id) schedule_iter = schedule_obj.get_group_and_schedule() workgroup_obj = WorkGroupDB(factory_id=factory_id) for schedule in schedule_iter: schedule['group_name'] = workgroup_obj.find( schedule['group_id'])["name"] result.append(schedule) return result
def put(self, *args, **kwargs): """ put请求,通过工厂id和班次id和需要更新的参数,更新班次 :param args: group_id: 班组信息 start_time: 开始时间 end_time: 结束时间 name: 班次名称 :param kwargs: /factory_id/schedule_id :return: 成功返回更新的设备id,未找到班次返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') request_data = kwargs['request_data'] group_id = request_data.get('group_id') start_time = request_data.get('start_time') end_time = request_data.get('end_time') name = request_data.get('name') if len(parameter_list) == 2 : factory_id = parameter_list[0] schedule_id = parameter_list[1] schedule_obj = WorkScheduleDB(factory_id=factory_id) result = schedule_obj.update(id=schedule_id, group_id=group_id, start_time=start_time, end_time=end_time, name=name) schedule_obj.close() if result: SpiderApi.response(200) self.write_json(result) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def post(self, *args, **kwargs): """ post:通过工厂id和班组的详细信息,添加班次信息 :param args: group_id: 班组信息 start_time: 开始时间 end_time: 结束时间 name: 班次名称 :param kwargs: /factory_id/ :return: 成功返回插入的班次id和201,插入失败返回status_24 参数错误返回status_22 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') request_data = kwargs['request_data'] group_id = request_data.get('group_id') start_time = request_data.get('start_time') end_time = request_data.get('end_time') name = request_data.get('name') if len(parameter_list) == 2 and group_id and start_time and end_time and name: factory_id = parameter_list[0] schedule_obj = WorkScheduleDB(factory_id=factory_id) result = schedule_obj.insert(group_id=group_id, start_time=start_time, end_time=end_time, name=name) schedule_obj.close() if result: SpiderApi.response(201) self.write_json(result, 201) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)
def post(self, *args, **kwargs): """ post请求,通过设备编号和工厂id获取设备的历史信息 :param args: group_id: 班组id ,全选为None start_time: 开始时间 end_time: 结束时间 search_field: 查找字段 equipment_id: 设备id :param kwargs: /factory_id/factory_number :return: 成功返回201以及包含警报信息的json,为请求到数据返回status_24 参数错误返回status_22,设备未绑定班次返回status_38 """ # api日志记录 SpiderApi.request(kwargs['c_user_id'], self.request.remote_ip, self.request.method, self.request.uri) parameter = kwargs['parameter'] parameter_list = parameter.split('/') request_data = kwargs['request_data'] group_id = request_data.get('group_id') start_time = request_data.get('start_time') end_time = request_data.get('end_time') search_field = request_data.get('search_field') equipment_id = request_data.get('equipment_id') factory_id = parameter_list[0] factory_number = parameter_list[1] # 对查询的参数进行判断 if search_field not in ALLOWED_PARA: SpiderApi.response(errors.status_37) raise HTTPError(**errors.status_37) if len(parameter_list ) == 2 and start_time and end_time and search_field: # 对时间间隔进行处理 start_time_list = map(int, start_time.split("-")) end_time_list = map(int, end_time.split("-")) start_datetime = datetime.datetime.combine( datetime.date(start_time_list[0], start_time_list[1], start_time_list[2]), datetime.time.min) end_datetime = datetime.datetime.combine( datetime.date(end_time_list[0], end_time_list[1], end_time_list[2]), datetime.time.min) # 读取中间表获取数据 # 首先要对时间进行区间和是否选择班次进行判断 大于30天 去每日报表查找,小于30天,去生产数据查找 time_interval = end_datetime - start_datetime if time_interval >= datetime.timedelta(30): if group_id: # 去班组报表中查询 group_report_obj = GroupReportDB(factory_id=factory_id, equipment_id=equipment_id) result = group_report_obj.get_group_rep_data( group_id=group_id, start_time=start_datetime, end_time=end_datetime, field=search_field) group_report_obj.close() else: # 去每日报表查询 daily_report_obj = DailyReportDB(factory_id=factory_id, equipment_id=equipment_id) result = daily_report_obj.get_daily_rep_data( start_time=start_datetime, end_time=end_datetime, field=search_field) daily_report_obj.close() else: # 获取班组绑定的班次 schedule_obj = WorkScheduleDB(factory_id=factory_id) schedule = schedule_obj.find(group_id=group_id) schedule_obj.close() # 对查找的设备id进行校验,判断是否传班组id,判断是否绑定了班次 if schedule is None and group_id: SpiderApi.response(errors.status_38) raise HTTPError(**errors.status_38) if group_id: # 对时间进行处理 start_list = map(int, schedule['start_time'].split(":")) end_list = map(int, schedule['end_time'].split(":")) start_datetime += datetime.timedelta(hours=start_list[0], minutes=start_list[1], microseconds=1) end_datetime += datetime.timedelta(hours=end_list[0], minutes=end_list[1], microseconds=1) # 去生产数据中查询,班次所绑定班次的记录 product_data_obj = ProductData(factory_number=factory_number, factory_id=factory_id) result = product_data_obj.get_product_data( field=search_field, start_time=start_datetime, end_time=end_datetime) product_data_obj.close() if result: SpiderApi.response(201) self.write_json(result, 201) else: SpiderApi.response(errors.status_24) raise HTTPError(**errors.status_24) else: SpiderApi.response(errors.status_22) raise HTTPError(**errors.status_22)