()
| 451 | } |
| 452 | |
| 453 | func (p *Process) eventLoop() { |
| 454 | since := 0 |
| 455 | notScanned := make(map[string]struct{}) |
| 456 | start := time.Now() |
| 457 | for { |
| 458 | select { |
| 459 | case <-p.stopped: |
| 460 | return |
| 461 | default: |
| 462 | } |
| 463 | |
| 464 | evs, err := p.Events(since) |
| 465 | if err != nil { |
| 466 | if time.Since(start) < 5*time.Second { |
| 467 | // The API has probably not started yet, lets give it some time. |
| 468 | continue |
| 469 | } |
| 470 | |
| 471 | // If we're stopping, no need to print the error. |
| 472 | select { |
| 473 | case <-p.stopped: |
| 474 | return |
| 475 | default: |
| 476 | } |
| 477 | |
| 478 | log.Println("eventLoop: events:", err) |
| 479 | continue |
| 480 | } |
| 481 | |
| 482 | for _, ev := range evs { |
| 483 | if ev.ID != since+1 { |
| 484 | slog.Warn("Event ID jumped", "from", since, "to", ev.ID) |
| 485 | } |
| 486 | since = ev.ID |
| 487 | |
| 488 | switch ev.Type { |
| 489 | case "Starting": |
| 490 | // The Starting event tells us where the configuration is. Load |
| 491 | // it and populate our list of folders. |
| 492 | |
| 493 | data := ev.Data.(map[string]interface{}) |
| 494 | id, err := protocol.DeviceIDFromString(data["myID"].(string)) |
| 495 | if err != nil { |
| 496 | log.Println("eventLoop: DeviceIdFromString:", err) |
| 497 | continue |
| 498 | } |
| 499 | p.id = id |
| 500 | |
| 501 | home := data["home"].(string) |
| 502 | w, _, err := config.Load(filepath.Join(home, "config.xml"), protocol.LocalDeviceID, events.NoopLogger) |
| 503 | if err != nil { |
| 504 | log.Println("eventLoop: Starting:", err) |
| 505 | continue |
| 506 | } |
| 507 | for id := range w.Folders() { |
| 508 | p.eventMut.Lock() |
| 509 | p.folders = append(p.folders, id) |
| 510 | p.eventMut.Unlock() |
no test coverage detected