353 lines
9.6 KiB
Go
353 lines
9.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"image/color"
|
|
"image/png"
|
|
"sort"
|
|
"strconv"
|
|
"time"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/app"
|
|
"fyne.io/fyne/v2/canvas"
|
|
"fyne.io/fyne/v2/container"
|
|
"fyne.io/fyne/v2/dialog"
|
|
"fyne.io/fyne/v2/layout"
|
|
"fyne.io/fyne/v2/storage"
|
|
"fyne.io/fyne/v2/theme"
|
|
"fyne.io/fyne/v2/widget"
|
|
"jonasled.dev/jonasled/otdr-reporter/fiber"
|
|
"jonasled.dev/jonasled/otdr-reporter/gotdr"
|
|
"jonasled.dev/jonasled/otdr-reporter/pdf"
|
|
"jonasled.dev/jonasled/otdr-reporter/update"
|
|
|
|
"gonum.org/v1/plot"
|
|
"gonum.org/v1/plot/plotter"
|
|
"gonum.org/v1/plot/plotutil"
|
|
"gonum.org/v1/plot/vg"
|
|
|
|
datepicker "github.com/sdassow/fyne-datepicker"
|
|
)
|
|
|
|
var events []pdf.EventRow
|
|
|
|
var UpdaterEnabled = "false"
|
|
|
|
func renderPlot(sor *gotdr.OtdrRawData, mainWindow *fyne.Window) []byte {
|
|
p := plot.New()
|
|
p.X.Label.Text = sor.FixedParams.Unit
|
|
p.X.AutoRescale = true
|
|
p.Y.Label.Text = "dB"
|
|
p.Y.AutoRescale = true
|
|
plotPoints := plotter.XYs{}
|
|
|
|
maxy := -100.0
|
|
miny := 0.0
|
|
for _, point := range sor.DataPoints {
|
|
if miny > float64(point[1]) {
|
|
miny = float64(point[1])
|
|
}
|
|
if maxy < float64(point[1]) {
|
|
maxy = float64(point[1])
|
|
}
|
|
|
|
plotPoints = append(plotPoints, plotter.XY{
|
|
X: float64(point[0]) / 100000,
|
|
Y: float64(point[1]),
|
|
})
|
|
}
|
|
plotutil.AddLines(p, "", plotPoints)
|
|
|
|
xTicks := []plot.Tick{}
|
|
for i := 0; i <= int(sor.FixedParams.Range[0]/100000); i += 50 {
|
|
xTicks = append(xTicks, plot.Tick{
|
|
Value: float64(i),
|
|
Label: strconv.Itoa(i),
|
|
})
|
|
}
|
|
p.X.Tick.Marker = plot.ConstantTicks(xTicks)
|
|
|
|
yTicks := []plot.Tick{}
|
|
for val := miny; val <= maxy; val += 1 {
|
|
if int(val)%5 == 0 {
|
|
yTicks = append(yTicks, plot.Tick{
|
|
Value: val,
|
|
Label: fmt.Sprintf("%.0f", val),
|
|
})
|
|
}
|
|
}
|
|
p.Y.Tick.Marker = plot.ConstantTicks(yTicks)
|
|
|
|
for _, ev := range sor.Events {
|
|
eventID := ev.EventNumber
|
|
|
|
// vertical line spanning current Y range
|
|
linePts := plotter.XYs{
|
|
{X: ev.EventLocM / 100000, Y: p.Y.Min},
|
|
{X: ev.EventLocM / 100000, Y: p.Y.Max},
|
|
}
|
|
vline, err := plotter.NewLine(linePts)
|
|
if err == nil {
|
|
vline.Color = color.RGBA{R: 200, G: 0, B: 0, A: 255} // red line
|
|
vline.Dashes = []vg.Length{vg.Points(3), vg.Points(3)} // dashed
|
|
p.Add(vline)
|
|
}
|
|
|
|
// label near top
|
|
lbl, err := plotter.NewLabels(plotter.XYLabels{
|
|
XYs: []plotter.XY{{X: ev.EventLocM / 100000, Y: p.Y.Max}},
|
|
Labels: []string{strconv.Itoa(int(eventID))},
|
|
})
|
|
if err == nil {
|
|
p.Add(lbl)
|
|
}
|
|
}
|
|
|
|
plot, err := p.WriterTo(16*vg.Inch, 4*vg.Inch, "png")
|
|
if err != nil {
|
|
dialog.NewInformation("Failed rendering graph", err.Error(), *mainWindow).Show()
|
|
}
|
|
var buf bytes.Buffer
|
|
plot.WriteTo(&buf)
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func main() {
|
|
application := app.NewWithID("de.jonasled.otdr-reporter")
|
|
mainWindow := application.NewWindow("OTDR Report")
|
|
tabs := container.NewAppTabs()
|
|
|
|
wavelengthLabel := widget.NewLabel("Wavelength (nm): ")
|
|
wavelengthInput := widget.NewEntry()
|
|
wavelength := container.NewGridWithColumns(2,
|
|
wavelengthLabel, wavelengthInput,
|
|
)
|
|
|
|
iorLabel := widget.NewLabel("IOR: ")
|
|
iorInput := widget.NewEntry()
|
|
ior := container.NewGridWithColumns(2,
|
|
iorLabel, iorInput,
|
|
)
|
|
|
|
pulsewidthLabel := widget.NewLabel("Pulse width (ns): ")
|
|
pulsewidthInput := widget.NewEntry()
|
|
pulsewidth := container.NewGridWithColumns(2,
|
|
pulsewidthLabel, pulsewidthInput,
|
|
)
|
|
|
|
fibertypeLabel := widget.NewLabel("Fiber type: ")
|
|
fibertypeInput := widget.NewEntry()
|
|
fibertype := container.NewGridWithColumns(2,
|
|
fibertypeLabel, fibertypeInput,
|
|
)
|
|
fiberNoLabel := widget.NewLabel("Fiber No.: ")
|
|
fiberNoInput := widget.NewEntry()
|
|
fiberNo := container.NewGridWithColumns(2,
|
|
fiberNoLabel, fiberNoInput,
|
|
)
|
|
fiberLenLabel := widget.NewLabel("Fiber length (m): ")
|
|
fiberLenInput := widget.NewEntry()
|
|
fiberLen := container.NewGridWithColumns(2,
|
|
fiberLenLabel, fiberLenInput,
|
|
)
|
|
|
|
locationaLabel := widget.NewLabel("Location start: ")
|
|
locationaInput := widget.NewEntry()
|
|
locationa := container.NewGridWithColumns(2,
|
|
locationaLabel, locationaInput,
|
|
)
|
|
|
|
locationbLabel := widget.NewLabel("Location end: ")
|
|
locationbInput := widget.NewEntry()
|
|
locationb := container.NewGridWithColumns(2,
|
|
locationbLabel, locationbInput,
|
|
)
|
|
|
|
projectLabel := widget.NewLabel("Project: ")
|
|
projectInput := widget.NewEntry()
|
|
project := container.NewGridWithColumns(2,
|
|
projectLabel, projectInput,
|
|
)
|
|
|
|
customerLabel := widget.NewLabel("Customer: ")
|
|
customerInput := widget.NewEntry()
|
|
customer := container.NewGridWithColumns(2,
|
|
customerLabel, customerInput,
|
|
)
|
|
|
|
remarksLabel := widget.NewLabel("Remarks: ")
|
|
remarksInput := widget.NewEntry()
|
|
remarks := container.NewGridWithColumns(2,
|
|
remarksLabel, remarksInput,
|
|
)
|
|
|
|
titleLabel := widget.NewLabel("Title: ")
|
|
titleInput := widget.NewEntry()
|
|
titleInput.SetText("OTDR Report")
|
|
title := container.NewGridWithColumns(2,
|
|
titleLabel, titleInput,
|
|
)
|
|
|
|
testerLabel := widget.NewLabel("Tester: ")
|
|
testerInput := widget.NewEntry()
|
|
testerInput.SetText(application.Preferences().String("tester"))
|
|
testerInput.OnChanged = func(s string) {
|
|
application.Preferences().SetString("tester", s)
|
|
}
|
|
tester := container.NewGridWithColumns(2,
|
|
testerLabel, testerInput,
|
|
)
|
|
|
|
contactInfoLabel := widget.NewLabel("Contact info: ")
|
|
contactInfoInput := widget.NewEntry()
|
|
contactInfoInput.SetText(application.Preferences().String("contact"))
|
|
contactInfoInput.OnChanged = func(s string) {
|
|
application.Preferences().SetString("contact", s)
|
|
}
|
|
contactInfo := container.NewGridWithColumns(2,
|
|
contactInfoLabel, contactInfoInput,
|
|
)
|
|
|
|
datetimeLabel := widget.NewLabel("Record time:")
|
|
datetimeInput := widget.NewEntry()
|
|
datetimeInput.SetPlaceHolder("0000/00/00 00:00")
|
|
datetimeInput.ActionItem = widget.NewButtonWithIcon("", theme.MoreHorizontalIcon(), func() {
|
|
when := time.Now()
|
|
|
|
if datetimeInput.Text != "" {
|
|
t, err := time.Parse("2006/01/02 15:04", datetimeInput.Text)
|
|
if err == nil {
|
|
when = t
|
|
}
|
|
}
|
|
|
|
picker := datepicker.NewDateTimePicker(when, time.Monday, func(when time.Time, ok bool) {
|
|
if ok {
|
|
datetimeInput.SetText(when.Format("2006/01/02 15:04"))
|
|
}
|
|
})
|
|
|
|
dialog.ShowCustomConfirm(
|
|
"Choose date and time",
|
|
"Ok",
|
|
"Cancel",
|
|
picker,
|
|
picker.OnActioned,
|
|
mainWindow,
|
|
)
|
|
})
|
|
datetime := container.NewGridWithColumns(2,
|
|
datetimeLabel, datetimeInput,
|
|
)
|
|
|
|
parametersLayout := container.NewVBox(
|
|
wavelength,
|
|
ior,
|
|
pulsewidth,
|
|
fibertype,
|
|
fiberNo,
|
|
fiberLen,
|
|
locationa,
|
|
locationb,
|
|
project,
|
|
customer,
|
|
remarks,
|
|
title,
|
|
tester,
|
|
contactInfo,
|
|
datetime,
|
|
)
|
|
|
|
plotCanvas := &canvas.Image{}
|
|
plotCanvas.ScaleMode = canvas.ImageScalePixels
|
|
plotCanvas.Resize(fyne.NewSize(1920, 782))
|
|
|
|
buttonGenerateReport := widget.NewButton("Generate report", func() {
|
|
|
|
onChosen := func(f fyne.URIWriteCloser, err error) {
|
|
recordtime, _ := time.Parse("2006/01/02 15:04", datetimeInput.Text)
|
|
pdf.RenderPDF(f.URI().Path(), titleInput.Text, wavelengthInput.Text, pulsewidthInput.Text, fibertypeInput.Text, locationaInput.Text, locationbInput.Text, projectInput.Text, customerInput.Text, remarksInput.Text, &plotCanvas.Image, iorInput.Text, testerInput.Text, contactInfoInput.Text, fiberNoInput.Text, events, fiberLenInput.Text, recordtime)
|
|
dialog.NewInformation("OTDR Report", "The report has been successfully generated", mainWindow).Show()
|
|
}
|
|
dialog := dialog.NewFileSave(onChosen, mainWindow)
|
|
dialog.SetFileName("report.pdf")
|
|
dialog.SetFilter(storage.NewExtensionFileFilter([]string{".pdf"}))
|
|
dialog.Show()
|
|
|
|
})
|
|
buttonGenerateReport.Disable()
|
|
|
|
buttonReadFile := widget.NewButton("Open .sor file", func() {
|
|
onChosen := func(f fyne.URIReadCloser, err error) {
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
if f == nil {
|
|
return
|
|
}
|
|
sor, err := gotdr.ReadSorFile(f.URI().Path())
|
|
if err != nil {
|
|
dialog.NewInformation("Failed reading files", err.Error(), mainWindow).Show()
|
|
return
|
|
}
|
|
|
|
wavelengthInput.SetText(sor.GenParams.OTDRWavelength)
|
|
iorInput.SetText(fmt.Sprintf("%f", sor.FixedParams.IOR))
|
|
pulsewidthInput.SetText(strconv.Itoa(int(sor.FixedParams.PulseWidth[0])))
|
|
fibertypeInput.SetText(fiber.FiberTypeName(sor.GenParams.FiberType))
|
|
locationaInput.SetText(sor.GenParams.LocationA)
|
|
locationbInput.SetText(sor.GenParams.LocationB)
|
|
fiberLenInput.SetText(strconv.FormatFloat(sor.FixedParams.Range[0]/100000, 'f', 2, 64))
|
|
datetimeInput.SetText(sor.FixedParams.DateTime.Format("2006/01/02 15:04"))
|
|
plot := renderPlot(&sor, &mainWindow)
|
|
image, _ := png.Decode(bytes.NewReader(plot))
|
|
plotCanvas.Image = image
|
|
plotCanvas.Resize(fyne.NewSize(float32(image.Bounds().Dx()), float32(image.Bounds().Dy())))
|
|
|
|
events = []pdf.EventRow{}
|
|
for _, ev := range sor.Events {
|
|
events = append(events, pdf.EventRow{
|
|
No: ev.EventNumber,
|
|
Type: ev.EventType,
|
|
PositionM: ev.EventLocM / float64(100000),
|
|
InsLossdB: ev.SpliceLoss,
|
|
ReflectdB: ev.RefLoss,
|
|
})
|
|
}
|
|
|
|
sort.Slice(events, func(i, j int) bool {
|
|
// convert string to int for safe sorting
|
|
ni := events[i].No
|
|
nj := events[j].No
|
|
return ni < nj
|
|
})
|
|
|
|
tabs.SelectIndex(1)
|
|
buttonGenerateReport.Enable()
|
|
|
|
}
|
|
dialog := dialog.NewFileOpen(onChosen, mainWindow)
|
|
dialog.SetFilter(storage.NewExtensionFileFilter([]string{".sor"}))
|
|
dialog.Show()
|
|
})
|
|
|
|
fileLayout := container.New(layout.NewVBoxLayout(), buttonReadFile, buttonGenerateReport)
|
|
|
|
tabs.Append(container.NewTabItem("File", fileLayout))
|
|
tabs.Append(container.NewTabItem("Parameters", parametersLayout))
|
|
tabs.Append(container.NewTabItem("Plot", plotCanvas))
|
|
|
|
mainContainer := container.New(layout.NewVBoxLayout(), tabs)
|
|
mainWindow.SetContent(mainContainer)
|
|
mainWindow.Resize(fyne.NewSize(700, 500))
|
|
|
|
if UpdaterEnabled == "true" {
|
|
update.CheckForUpdate(&application, &mainWindow)
|
|
}
|
|
|
|
mainWindow.ShowAndRun()
|
|
|
|
}
|