def post_single(self):
     # pylint: disable=no-member
     parser = reqparse.RequestParser()
     parser.add_argument('Location',
                         type=str,
                         default=None,
                         help='plz type like the "121.297187,24.943325"')
     parser.add_argument('Id',
                         type=str,
                         default=None,
                         help='plz type like the "C1_313020000G_000026"')
     args = parser.parse_args()
     result_dict = {}
     result_dict['ScenicSpotInfo'] = (ScenicSpotInfo.get(args))
     for sta in ['STA_AirQuality_v2', 'STA_Rain']:
         # check if not have any station location, then insert all.
         if (CILocation.objects(station=sta).count() == 0):
             CILocation.insert_all(sta)
         loc_array = CILocation.get({
             **args, 'Station': sta,
             'Distance': 0.05
         })[0]['location']['coordinates']
         sta_info = Datastream.get_station_info(
             sta, ','.join(map(str, loc_array)))
         result_dict[sta] = sta_info
     return {'data': result_dict}
 def put(self):
     parser = reqparse.RequestParser()
     parser.add_argument('url', type=str, default=None)
     parser.add_argument('station', type=str, default='STA_Rain')
     args = parser.parse_args()
     station = args['Station']
     return CILocation.insert_all(station)
 def post(self):
     # pylint: disable=no-member
     # 1. 根據 get 得到的 coordinates (121.5359,25.2903) 後,再將該資訊打進以下網址的 Location
     # 2. 可得到相關資訊
     # http://127.0.0.1:5000/api/location?Location=121.5359,25.2903
     parser = reqparse.RequestParser()
     parser.add_argument('url', type=str, default=None)
     parser.add_argument('Station', type=str, default='STA_Rain')
     parser.add_argument('Location',
                         type=str,
                         default=None,
                         required=True,
                         help='plz type like the 121.4946,24.7783')
     args = parser.parse_args()
     station = args['Station']
     location = args['Location']
     # return Datastream.insert_all(station, location)
     if (CILocation.objects(station__in=station).count() < 0):
         CILocation.insert_all(station)
     return Datastream.get_station_info(station, location)
 def job(args: dict, q: Queue):
     info_dict = {}
     info_dict['ScenicSpotInfo'] = (ScenicSpotInfo.get(args))
     for sta in stations:
         ScenicSpotInfo_Loc = info_dict['ScenicSpotInfo'][0]['Location']
         loc_array = CILocation.get({
             'Location': ScenicSpotInfo_Loc,
             'Station': sta,
             'Distance': 0.1
         })[0]['location']['coordinates']
         sta_info = Datastream.get_station_info(
             sta, ','.join(map(str, loc_array)))
         info_dict[sta] = sta_info
     q.put(info_dict)
    def get(self):
        # 根據旅遊位置及該旅遊位置的半徑距離,以找出附近的氣象站位置。
        # 1. 先將“Location: 旅遊地點經緯度”與”Distance: 旅遊地點半徑範圍“傳送給以下端點
        # http://127.0.0.1:5000/api/location?Location=121.55292, 25.27472&Distance=0.05
        # 2. 隨後會收到以下結果, 需記下 coordinates 資訊, 以post到下一端點
        # [
        #     {
        #         "_id": {
        #             "$oid": "5eef0890db9b76aa90e69ad7"
        #         },
        #         "name": "雨量站-01A350-新北市石門區富貴角",
        #         "description": "雨量站-01A350-新北市石門區富貴角",
        #         "encodingType": "application/vnd.geo+json",
        #         "location": {
        #             "type": "Point",
        #             "coordinates": [
        #                 121.5359,
        #                 25.2903
        #             ]
        #         },
        #         "station": "STA_Rain",
        #         "_iot_id": 74,
        #         "_iot_selfLink": "https://sta.ci.taiwan.gov.tw/STA_Rain/v1.0/Locations(74)",
        #         "HistoricalLocations_iot_navigationLink": "https://sta.ci.taiwan.gov.tw/STA_Rain/v1.0/Locations(74)/HistoricalLocations",
        #         "Things_iot_navigationLink": "https://sta.ci.taiwan.gov.tw/STA_Rain/v1.0/Locations(74)/Things"
        #     }
        # ]

        parser = reqparse.RequestParser()
        parser.add_argument('Station', type=str, default="STA_Rain")
        parser.add_argument('Location',
                            type=str,
                            default=None,
                            help='plz type like the 121.297187,24.943325')
        parser.add_argument('Distance', type=str, default=None)
        args = parser.parse_args()
        result = CILocation.get(args)
        return result
    def post(self, **kwargs):
        # pylint: disable=no-member
        parser = reqparse.RequestParser()
        parser.add_argument('Location',
                            default=None,
                            type=str,
                            action='append',
                            help='plz type like the ["121.297187,24.943325"]')
        parser.add_argument('Id[]', default=None, type=str, action='append')
        parser.add_argument('Id',
                            default=None,
                            type=str,
                            action='append',
                            help='plz type like the ["C1_313020000G_000026"]')
        raw_args = kwargs.get('args_dict', parser.parse_args())
        print('raw_args')
        print(raw_args)

        if (raw_args['Id'] == None and raw_args['Id[]'] != None):
            raw_args['Id'] = raw_args['Id[]']

        Inside = 'Location' if raw_args['Location'] else 'Id'
        stations = ['STA_AirQuality_v2', 'STA_Rain']
        q = Queue()
        threads = []
        result_list = []

        for sta in stations:
            # check if not have any station location, then insert all.
            if (CILocation.objects(station=sta).count() == 0):
                CILocation.insert_all(station=sta)

        def job(args: dict, q: Queue):
            info_dict = {}
            info_dict['ScenicSpotInfo'] = (ScenicSpotInfo.get(args))
            for sta in stations:
                ScenicSpotInfo_Loc = info_dict['ScenicSpotInfo'][0]['Location']
                loc_array = CILocation.get({
                    'Location': ScenicSpotInfo_Loc,
                    'Station': sta,
                    'Distance': 0.1
                })[0]['location']['coordinates']
                sta_info = Datastream.get_station_info(
                    sta, ','.join(map(str, loc_array)))
                info_dict[sta] = sta_info
            q.put(info_dict)

        for raw_arg in raw_args[Inside]:
            t = threading.Thread(target=job, args=({Inside: raw_arg}, q))
            t.start()
            threads.append(t)

        for thread in threads:
            thread.join()

        for _ in raw_args[Inside]:
            result_list.append(q.get())
            # print('Name: {}, Id: {}'.format(a['ScenicSpotInfo'][0]['Name'], a['ScenicSpotInfo'][0]['Id']))

        # while raw_args[Inside]:
        #     args = {Inside: raw_args[Inside].pop()}
        #     info_dict = {}
        #     info_dict['ScenicSpotInfo'] = (ScenicSpotInfo.get(args))
        #     for sta in ['STA_AirQuality_v2', 'STA_Rain']:
        #         ScenicSpotInfo_Loc = info_dict['ScenicSpotInfo'][0]['Location']
        #         loc_array = CILocation.get({'Location': ScenicSpotInfo_Loc, 'Station': sta, 'Distance': 0.05})[0]['location']['coordinates']
        #         sta_info = Datastream.get_station_info(sta, ','.join(map(str, loc_array)))
        #         info_dict[sta] = sta_info
        #     result_list.append(info_dict)
        # print([ rl['ScenicSpotInfo'][0]['Location'] for rl in result_list])
        return {'data': result_list}
 def delete(self):
     result = CILocation.delete_all()
     return {'collection': result, 'status': "successed"}