wrsbmkg

package module
v1.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

README

go-wrsbmkg Go Reference

Modul non-resmi WRS-BMKG yang digunakan untuk mendapatkan informasi gempa.

Catatan

Modul ini adalah modul non-resmi yang bukan dibuat oleh pihak-pihak BMKG. Modul ini hanya menggunakan API endpoint yang dibuat oleh pihak-pihak BMKG yang bekerja secara Polling.

Code Example

package main

import (
	"context"
	"fmt"
	"time"

	"codeberg.org/Yonle/go-wrsbmkg"
	"codeberg.org/Yonle/go-wrsbmkg/helper"
)

var narasi = make(chan string)

func main() {
	p := wrsbmkg.BuatPenerima()

	ctx := context.Background()
	p.MulaiPolling(ctx)

	fmt.Println("WRS-BMKG")
	fmt.Println("Informasi akan dimuat dalam 15 detik....")

	for {
		fmt.Println("---")
		select {
		case g := <-p.Gempa:
			gempa := helper.ParseGempa(g)

			fmt.Println("\nGEMPABUMI ---")
			fmt.Printf(
				"%s\n\n%s\n\n%s\n\n%s\n\n%s\n",
				gempa.Subject,
				gempa.Description,
				gempa.Area,
				gempa.Potential,
				gempa.Instruction,
			)

			go func() {
				teksNarasi, err := p.FetchNarasi(ctx, g.Info.EventID, time.Now().Add(48*time.Hour))
				if err != nil {
					return
				}

				narasi <- teksNarasi
			}()
		case r := <-p.Realtime:
			realtime := helper.ParseRealtime(r)
			fmt.Println("\nREALTIME ---")

			fmt.Printf(
				"%s\n"+
					"Tanggal   : %s\n"+
					"Magnitudo : %v\n"+
					"Kedalaman : %v\n"+
					"Koordinat : %s,%s\n"+
					"Fase      : %v\n"+
					"Status    : %s\n",
				realtime.Place,
				realtime.Time,
				realtime.Magnitude,
				realtime.Depth,
				realtime.Coordinates[1].(string),
				realtime.Coordinates[0].(string),
				realtime.Phase,
				realtime.Status,
			)
		case n := <-narasi:
			fmt.Println("\nNARASI ---")

			narasi := helper.CleanNarasi(n)
			fmt.Println(narasi)
		}
	}
}

Documentation

Lihat disini: Go Reference

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DEFAULT_API_URL string = "https://bmkg-content-inatews.storage.googleapis.com"
View Source
var ERR_TIMEOUT_WAITING_NARASI = errors.New("Timed out waiting for narasi")

Functions

This section is empty.

Types

type ObsArea added in v1.3.2

type ObsArea struct {
	Location  string `json:"location"`
	Latitude  string `json:"loclatitude"`
	Longitude string `json:"loclongitude"`
	Height    string `json:"height"` // in metres
	Date      string `json:"date"`
	Time      string `json:"time"`
}

Tsunami: Detected Zone

type Penerima

type Penerima struct {
	// Interval penerimaan informasi baru
	Interval time.Duration

	// Struct Raw_* dapat disimplikasi ke struct yang mudah dipakai
	// Dengan memakai modul codeberg.org/Yonle/go-wrsbmkg/helper
	GempaTerakhir    *Raw_DataGempa
	RealtimeTerakhir *Raw_QL

	Gempa    chan *Raw_DataGempa
	Realtime chan *Raw_QL

	API_URL string

	// Timeout dan segala lainnya berkaitan request, Atur dengan http.Client
	HTTP_Client *http.Client
}

Penerima data gempa yang akan diambil dari API BMKG.

Pastikan bahwa API_URL dan Interval sudah disertakan. Interval yang disarankan adalah `time.Second*15`

func BuatPenerima added in v1.2.0

func BuatPenerima() *Penerima

Ini akan memuat Penerima dengan parameter default.

func (*Penerima) DownloadGempa

func (p *Penerima) DownloadGempa(ctx context.Context) (*Raw_DataGempa, *http.Response, error)

Ini akan mendownload informasi gempa. Lihat data JSON asli di https://bmkg-content-inatews.storage.googleapis.com/datagempa.json

func (*Penerima) DownloadNarasi

func (p *Penerima) DownloadNarasi(ctx context.Context, eventid string) (narasi string, resp *http.Response, err error)

Ini akan mendownload teks narasi. Setiap narasi tidak langsung tersedia setelah peringatan gempa diumumkan, Melainkan memerlukan beberapa waktu.

Teks narasi yang diterima berbentuk HTML. Elemen HTML dapat dihilangkan dengan memakai codeberg.org/Yonle/go-wrsbmkg/helper.

func (*Penerima) DownloadRealtime

func (p *Penerima) DownloadRealtime(ctx context.Context) (*Raw_QL, *http.Response, error)

Ini akan mendownload data gempa realtime. Lihat data JSON asli di https://bmkg-content-inatews.storage.googleapis.com/lastQL.json

func (*Penerima) DownloadRiwayatGempa

func (p *Penerima) DownloadRiwayatGempa(ctx context.Context) (*Raw_QL, *http.Response, error)

Ini akan mendownload riwayat data gempa. Lihat data JSON asli di https://bmkg-content-inatews.storage.googleapis.com/gempaQL.json

func (*Penerima) FetchNarasi added in v1.3.0

func (p *Penerima) FetchNarasi(c context.Context, eventid string, deadline time.Time) (string, error)

FetchNarasi() untuk menunggu & menerima pesan narasi dari pihak BMKG setelah menerima alert. Fungsi ini bersifat synchronous. Disarankan untuk dijalankan dengan goroutine. Jika sudah melewati [deadline], Maka [err] akan mengreturn ERR_TIMEOUT_WAITING_NARASI

func (*Penerima) Get

func (p *Penerima) Get(ctx context.Context, path string) (*http.Response, error)

func (*Penerima) GetBody added in v1.1.1

func (p *Penerima) GetBody(ctx context.Context, path string) ([]byte, *http.Response, error)

func (*Penerima) MulaiPolling

func (p *Penerima) MulaiPolling(ctx context.Context) error

Fungsi ini akan mulai menerima data baru setiap waktu. Sebelum memanggil, Pastikan bahwa Interval sudah ditentukan di Penerima{}.

Jangan panggil fungsi ini jika Penerima sudah dijalankan, Kecuali sudah dihentikan dengan context.Context. Disarankan untuk menggunakan context.WithCancel untuk menghentikan penerimaan data.

func (*Penerima) PollingGempa

func (p *Penerima) PollingGempa(ctx context.Context)

func (*Penerima) PollingRealtime

func (p *Penerima) PollingRealtime(ctx context.Context)

type Raw_DataGempa added in v1.2.0

type Raw_DataGempa struct {
	Code       string        `json:"code"`
	Identifier string        `json:"identifier"`
	Info       Raw_InfoGempa `json:"info"`
	MsgType    string        `json:"msgType"`
	Scope      string        `json:"scope"`
	Sender     string        `json:"sender"`
	Sent       string        `json:"sent"`
	Status     string        `json:"status"`
}

Struct ini bisa disimplikasi dengan codeberg.org/Yonle/go-wrsbmkg/helper.

type Raw_InfoGempa added in v1.2.3

type Raw_InfoGempa struct {
	Area        string `json:"area"`
	Date        string `json:"date"`
	Depth       string `json:"depth"`
	Description string `json:"description"`
	Event       string `json:"event"`
	EventID     string `json:"eventid"`
	Felt        string `json:"felt"`
	Headline    string `json:"headline"`
	Instruction string `json:"instruction"`
	Latitude    string `json:"latitude"`
	Longitude   string `json:"longitude"`
	Magnitude   string `json:"magnitude"`
	Point       struct {
		Coordinates string `json:"coordinates"`
	} `json:"point"`
	Potential string `json:"potential"`
	Shakemap  string `json:"shakemap"`
	Subject   string `json:"subject"`
	Time      string `json:"time"`
	Timesent  string `json:"timesent"`

	// Properti-properti dibawah ini hanya tersedia saat Tsunami
	WZMap        string `json:"wzmap"`
	TTMap        string `json:"ttmap"`
	SSHMap       string `json:"sshmap"`
	Instruction1 string `json:"instruction1"`
	Instruction2 string `json:"instruction2"`
	Instruction3 string `json:"instruction3"`

	WZArea  []WZArea  `json:"wzarea"`
	ObsArea []ObsArea `json:"obsarea"`
}

type Raw_QL added in v1.2.0

type Raw_QL struct {
	Features []Raw_QL_Feature `json:"features"`
	Type     string           `json:"type"`
}

Struct ini bisa disimplikasi dengan codeberg.org/Yonle/go-wrsbmkg/helper.

type Raw_QL_Feature added in v1.2.0

type Raw_QL_Feature struct {
	Geometry struct {
		Coordinates []any  `json:"coordinates"`
		Type        string `json:"type"`
	} `json:"geometry"`
	Properties struct {
		Depth  string `json:"depth"`
		Fase   string `json:"fase"`
		ID     string `json:"id"`
		Mag    string `json:"mag"`
		Place  string `json:"place"`
		Status string `json:"status"`
		Time   string `json:"time"`
	} `json:"properties"`
	Type string `json:"type"`
}

type WZArea added in v1.2.1

type WZArea struct {
	Province string `json:"province"`
	District string `json:"district"`
	Level    string `json:"level"`
	Date     string `json:"date"`
	Time     string `json:"time"`
}

Tsunami: Warning Zone

Directories

Path Synopsis
examples
live command
riwayatgempa command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL