global class ThermalMap {
    static Long lastDelTime = 0L;
    static void deleteThermal(Long delay){
        for (Thermal__c g:[select time__c from Thermal__c order by time__c asc]) {
            Long t = 0;
            if ( g.time__c != null){
                t = g.time__c.longValue();
            }
            if ( (System.currentTimeMillis() - t) > delay ){
                 delete g;
            } else {
                break;
            }
        }
     }
    static void deleteGlider(Long delay){
        for (Glider__c g:[select time__c from Glider__c order by time__c asc]) {
            Long t = 0;
            if ( g.time__c != null){
                t = g.time__c.longValue();
            }
            if ( (System.currentTimeMillis() - t) > delay ){
                 delete g;
            } else {
                break;
            }
        }
    }
    webservice static Thermal__c[] add(String area, String devid, Double lat, Double lon, Double alt, Double inc,
        Long hist, Double range, Double strong){
        System.debug('WebService ThermalMap#addが呼ばれた');
        if ( (System.currentTimeMillis() - lastDelTime) > 1000*60 ){      // １分以内は行わない
            deleteThermal(1000*60*60*48);                                 // ４８時間で削除
            deleteGlider(1000*60);                                        // １分で削除
            lastDelTime = System.currentTimeMillis();
        }
        if ( inc > 1 && inc < 10 ){                             // +1以上+9までにする、それ以外は異常データ
            Thermal__c t = new Thermal__c();
            t.area__c = area;
            t.devid__c = devid;
            t.lat__c = lat;
            t.lon__c = lon;
            t.alt__c = alt;
            t.inc__c = inc;
            t.time__c = System.currentTimeMillis();
            try{
                insert t;
            } catch (System.DmlException e) {
                System.debug('Thermal insertでエラーが発生');            // デバッグログへ例外情報を記述
                for (Integer k = 0; k < e.getNumDml(); k++) {
                    System.debug(e.getDmlMessage(k));
                }
                return null;
            }
        }
        Glider__c[] chk = [select area__c, devid__c from Glider__c where
            area__c = :area and devid__c = :devid];
        if ( chk.size() == 0 ){
            Glider__c g = new Glider__c();
            g.area__c = area;
            g.devid__c = devid;
            g.lat__c = lat;
            g.lon__c = lon;
            g.alt__c = alt;
            g.time__c = System.currentTimeMillis();
            try{
                insert g;
            } catch (System.DmlException e) {
                System.debug('Glider insertでエラーが発生');              // デバッグログへ例外情報を記述
                for (Integer k = 0; k < e.getNumDml(); k++) {
                    System.debug(e.getDmlMessage(k));
                }
                return null;
            }
        } else {
            chk[0].lat__c = lat;
            chk[0].lon__c = lon;
            chk[0].alt__c = alt;
            chk[0].time__c = System.currentTimeMillis();
            update chk;
        }
        hist = System.currentTimeMillis() - (hist * 60 * 1000);        // 現在時刻 − 何時間前まで有効 ＝ 有効になる時間（これ以降の時間が有効）
        Thermal__c[] ret = new List<Thermal__c>();
        for (Thermal__c t:[select area__c, devid__c, lat__c, lon__c, alt__c, inc__c, time__c from Thermal__c where area__c = :area order by time__c asc]) {
            if ( t.time__c < hist ){                        // 指定の時間前までの
                continue;
            }
            if ( t.inc__c < strong ){    // 指定のサーマルの強さ以上の
                continue;
            }
            if ( alt - range > t.alt__c || alt + range < t.alt__c ){    // 指定の高度範囲の
                continue;
            }
            ret.add(t);
        }
        for (Glider__c g:[select area__c, devid__c, lat__c, lon__c, alt__c, time__c from Glider__c where area__c = :area order by time__c asc]) {
            Thermal__c t = new Thermal__c();
            t.area__c = g.area__c;
            t.devid__c = g.devid__c;
            t.lat__c = g.lat__c;
            t.lon__c = g.lon__c;
            t.alt__c = g.alt__c;
            t.inc__c = -99;
            ret.add(t);
        }
        return ret;
    }
    static testMethod void test1(){
        add('testarea', 'devid', 95, 135, 800, 2, 30, 100, 5);
        deleteThermal(1000*60*60*48);
        deleteGlider(1000*60*60*48);
    }
}