private async Task HostUpdated(ServiceDirectory directory, ServiceEntry entry) { RuntimeMonitor.Monitor.RegisterCall("HostUpdated"); LampClient newClient = new LampClient(ServiceEntryToUrl(entry), ServiceEntryToName(entry)); // Update info in DB using (var client = new DatabaseClient()) { var node = client.GetOne(newClient.Name); if (node != null) { node.LastSeen = entry.LastSeenAlive; node.Online = true; node.Url = newClient.Url; client.AddOrUpdate(node); } else { // For some reason, this node was updated but no longer in the database // Then treat it as an addition await HostDiscovered(directory, entry, true); } } }
protected virtual void Serve(LampRequest request) { var record = GetLampDb(request.Id); var client = new LampClient(record); record.ProcessStateChanges(request.Data); client.SetState().Wait(); // Synchronously set the state. It may throw an exception UpdateLampDb(record); }
protected void UpdateDb(LampClient lamp) { RuntimeMonitor.Monitor.RegisterCall("UpdateDb"); // Add new client to database if (lamp.StateSet) { using (var client = new DatabaseClient()) client.AddOrUpdate(lamp.Node); } }
private async Task HostDiscovered(ServiceDirectory directory, ServiceEntry entry, bool fromUpdate = false) { RuntimeMonitor.Monitor.RegisterCall("HostDiscovered"); Logger.Log("Host discovered: {0}{1}", entry.ToShortString(), fromUpdate ? " from HostUpdated event" : ""); LampClient newClient = new LampClient(ServiceEntryToUrl(entry), ServiceEntryToName(entry)); // Get status and add to DB await GetLampStatus(newClient); UpdateDb(newClient); }
protected async Task GetLampStatus(LampClient lamp) { RuntimeMonitor.Monitor.RegisterCall("GetLampStatus"); // Assume the lamp is online and get its state lamp.Online = true; try { await lamp.GetState(); } catch (Exception) { lamp.Online = false; } }
private async Task UpdateSingleLamp(DatabaseClient db, LampNode lamp) { RuntimeMonitor.Monitor.RegisterCall("UpdateSingleLamp"); var lampClient = new LampClient(lamp); try { await lampClient.GetState(); db.AddOrUpdate(lampClient.Node); } catch { // Error is ok; if the lamp is not seen, it will be removed } }