def _on_event(self, e): eType = e["type"] if eType in ("Ping", "Starting"): # Just ignore ignore those pass elif eType == "StartupComplete": self.emit("startup-complete") elif eType == "StateChanged": state = e["data"]["to"] rid = e["data"]["folder"] self._folder_state_changed(rid, state, 0) elif eType in ("RemoteIndexUpdated"): pass elif eType == "DeviceConnected": nid = e["data"]["id"] self.emit("device-connected", nid) elif eType == "DeviceDisconnected": nid = e["data"]["id"] self.emit("device-disconnected", nid) self._request_last_seen() elif eType == "DeviceDiscovered": nid = e["data"]["device"] addresses = e["data"]["addrs"] self.emit("device-discovered", nid, addresses) elif eType == "FolderRejected": nid = e["data"]["device"] rid = e["data"]["folder"] self.emit("folder-rejected", nid, rid) elif eType == "DeviceRejected": nid = e["data"]["device"] address = e["data"]["address"] self.emit("device-rejected", nid, address) elif eType == "ItemStarted": rid = e["data"]["folder"] filename = e["data"]["item"] t = parsetime(e["time"]) self.emit("item-started", rid, filename, t) elif eType == "FolderCompletion": self._syncthing_cb_completion(e["data"]) elif eType == "FolderSummary": rid = e["data"]["folder"] self._syncthing_cb_folder_data(e["data"]["summary"], rid) elif eType == "LocalIndexUpdated": rid = e["data"]["folder"] if "name" in e["data"]: filename = e["data"]["name"] mtime = parsetime(e["data"]["modified"]) self.emit("item-updated", rid, filename, mtime) elif eType == "ConfigSaved": self.emit("config-saved") elif eType == "ItemFinished": # Not handled (yet?) pass elif eType == "DownloadProgress": # Not handled (yet?) pass else: log.warning("Unhandled event type: %s", e)
def _on_event(self, e): eType = e["type"] if eType in ("Ping", "Starting"): # Just ignore ignore those pass elif eType == "StartupComplete": self.emit("startup-complete") elif eType == "StateChanged": state = e["data"]["to"] rid = e["data"]["folder"] if self._folder_state_changed(rid, state, 0): self._needs_update.add(rid) elif eType in ("RemoteIndexUpdated"): rid = e["data"]["folder"] if (not rid in self._syncing_devices) and (not rid in self._scanning_folders): self._needs_update.add(rid) elif eType == "DeviceConnected": nid = e["data"]["id"] self.emit("device-connected", nid) self._request_completion(nid) elif eType == "DeviceDisconnected": nid = e["data"]["id"] self.emit("device-disconnected", nid) self._request_last_seen() elif eType == "DeviceDiscovered": nid = e["data"]["device"] addresses = e["data"]["addrs"] self.emit("device-discovered", nid, addresses) elif eType == "FolderRejected": nid = e["data"]["device"] rid = e["data"]["folder"] self.emit("folder-rejected", nid, rid) elif eType == "DeviceRejected": nid = e["data"]["device"] address = e["data"]["address"] self.emit("device-rejected", nid, address) elif eType == "ItemStarted": rid = e["data"]["folder"] filename = e["data"]["item"] t = parsetime(e["time"]) self.emit("item-started", rid, filename, t) elif eType == "LocalIndexUpdated": rid = e["data"]["folder"] filename = e["data"]["name"] mtime = parsetime(e["data"]["modified"]) if (not rid in self._syncing_devices) and (not rid in self._scanning_folders): self._needs_update.add(rid) self.emit("item-updated", rid, filename, mtime) elif eType == "ConfigSaved": self.emit("config-saved") elif eType == "DownloadProgress": # TODO: Find what exactly this does and how can I use it pass else: log.warning("Unhandled event type: %s", e)
def _on_event(self, e): eType = e["type"] if eType in ("Ping", "Starting", "StartupComplete"): # Just ignore ignore those pass elif eType == "StateChanged": state = e["data"]["to"] rid = e["data"]["repo"] if self._repo_state_changed(rid, state, 0): self._needs_update.add(rid) elif eType in ("RemoteIndexUpdated"): rid = e["data"]["repo"] if (not rid in self._syncing_nodes) and (not rid in self._scanning_repos): self._needs_update.add(rid) elif eType == "NodeConnected": nid = e["data"]["id"] self.emit("node-connected", nid) self._request_completion(nid) elif eType == "NodeDisconnected": nid = e["data"]["id"] self.emit("node-disconnected", nid) elif eType == "NodeDiscovered": nid = e["data"]["node"] addresses = e["data"]["addrs"] self.emit("node-discovered", nid, addresses) elif eType == "RepoRejected": nid = e["data"]["node"] rid = e["data"]["repo"] self.emit("repo-rejected", nid, rid) elif eType == "NodeRejected": nid = e["data"]["node"] address = e["data"]["address"] self.emit("node-rejected", nid, address) elif eType == "ItemStarted": rid = e["data"]["repo"] filename = e["data"]["item"] t = parsetime(e["time"]) self.emit("item-started", rid, filename, t) elif eType == "LocalIndexUpdated": rid = e["data"]["repo"] filename = e["data"]["name"] mtime = parsetime(e["data"]["modified"]) if (not rid in self._syncing_nodes) and (not rid in self._scanning_repos): self._needs_update.add(rid) self.emit("item-updated", rid, filename, mtime) else: print "Unhandled event type:", e
def _syncthing_cb_errors(self, errors): for e in errors["errors"]: t = parsetime(e["Time"]) if t > self._last_error_time: self.emit("error", e["Error"]) self._last_error_time = t self.timer("errors", self._refresh_interval * 5, self._rest_request, "errors", self._syncthing_cb_errors)
def _syncthing_cb_last_seen(self, data): for nid in data: if nid != HTTP_HEADERS: t = parsetime(data[nid]["lastSeen"]) if t < NEVER: t = None if not nid in self._last_seen or self._last_seen[nid] != t: self._last_seen[nid] = t self.emit('last-seen-changed', nid, t)
def _syncthing_cb_errors(self, errors): if errors["errors"] is not None: for e in errors["errors"]: if "time" in e: # TODO: Remove this next time support for older daemon is dropped t = parsetime(e["time"]) msg = e["error"] elif "when" in e: t = parsetime(e["when"]) msg = e["message"] else: # Can't decode this continue if t > self._last_error_time: self.emit("error", msg) self._last_error_time = t self.timer("errors", self._refresh_interval * 5, self._rest_request, "system/error", self._syncthing_cb_errors)
def _syncthing_cb_errors(self, errors): for e in errors: t = parsetime(e["Time"]) if t > self._last_error_time: self.emit("error", e["Error"]) self._last_error_time = t self.timer("errors", self._refresh_rate * 5, self._rest_request, "errors", self._syncthing_cb_errors)
def _init_event_pooling(self, events): if type(events) == list and len(events) > 0: self._last_id = events[-1]["id"] self._last_error_time = parsetime(events[-1]["time"]) self._rest_request("errors", self._syncthing_cb_errors) self._request_events() else: # Retry for invalid data self._rest_request("events?limit=1", self._init_event_pooling)
def _init_event_pooling(self, events): if type(events) == list and len(events) > 0: self._last_id = events[-1]["id"] try: self._last_error_time = parsetime(events[-1]["time"]) except ValueError: self._last_error_time = datetime.now() self._rest_request("system/error", self._syncthing_cb_errors) self._request_events() else: # Retry for invalid data self._rest_request("events?limit=1", self._init_event_pooling)
def _on_event(self, e): eType = e["type"] if eType in ("Ping", "Starting"): # Just ignore ignore those pass elif eType == "StartupComplete": self.emit("startup-complete") elif eType == "StateChanged": state = e["data"]["to"] rid = e["data"]["folder"] self._folder_state_changed(rid, state, 0) elif eType in ("RemoteIndexUpdated"): pass elif eType == "DeviceConnected": nid = e["data"]["id"] self.emit("device-connected", nid) elif eType == "DeviceDisconnected": nid = e["data"]["id"] self.emit("device-disconnected", nid) elif eType == "DeviceDiscovered": nid = e["data"]["device"] addresses = e["data"]["addrs"] self.emit("device-discovered", nid, addresses) elif eType == "DevicePaused": nid = e["data"]["device"] self.emit("device-paused", nid) elif eType == "DeviceResumed": nid = e["data"]["device"] self.emit("device-resumed", nid) self._request_last_seen() elif eType == "FolderRejected": nid = e["data"]["device"] rid = e["data"]["folder"] self.emit("folder-rejected", nid, rid) elif eType == "DeviceRejected": nid = e["data"]["device"] name = e["data"]["name"] address = e["data"]["address"] self.emit("device-rejected", nid, name, address) elif eType == "FolderScanProgress": rid = e["data"]["folder"] total = float(e["data"]["total"]) if total > 0: # ^^ just in case status = float(e["data"]["current"]) / total self.emit("folder-scan-progress", rid, status) elif eType == "ItemStarted": rid = e["data"]["folder"] filename = e["data"]["item"] t = parsetime(e["time"]) self.emit("item-started", rid, filename, t) elif eType == "FolderCompletion": self._syncthing_cb_completion(e["data"]) elif eType == "FolderSummary": rid = e["data"]["folder"] self._syncthing_cb_folder_data(e["data"]["summary"], rid) elif eType == "FolderErrors": rid = e["data"]["folder"] self.emit("folder-error", rid, e["data"]["errors"]) elif eType == "ConfigSaved": self.emit("config-saved") elif eType == "ItemFinished": rid = e["data"]["folder"] if e["data"]["error"] is None: filename = e["data"]["item"] t = parsetime(e["time"]) self.emit("item-updated", rid, filename, t) elif eType in ("ItemFinished", "DownloadProgress", "RelayStateChanged", "LocalIndexUpdated", "ListenAddressesChanged"): # Not handled pass else: log.warning("Unhandled event type: %s", e)
def _on_event(self, e): eType = e["type"] if eType in ("Ping", "Starting"): # Just ignore ignore those pass elif eType == "StartupComplete": self.emit("startup-complete") elif eType == "StateChanged": state = e["data"]["to"] rid = e["data"]["folder"] self._folder_state_changed(rid, state, 0) elif eType in ("RemoteIndexUpdated"): pass elif eType == "DeviceConnected": nid = e["data"]["id"] self.emit("device-connected", nid) elif eType == "DeviceDisconnected": nid = e["data"]["id"] self.emit("device-disconnected", nid) elif eType == "DeviceDiscovered": nid = e["data"]["device"] addresses = e["data"]["addrs"] self.emit("device-discovered", nid, addresses) elif eType == "DevicePaused": nid = e["data"]["device"] self.emit("device-paused", nid) elif eType == "DeviceResumed": nid = e["data"]["device"] self.emit("device-resumed", nid) self._request_last_seen() elif eType == "FolderRejected": nid = e["data"]["device"] rid = e["data"]["folder"] self.emit("folder-rejected", nid, rid) elif eType == "DeviceRejected": nid = e["data"]["device"] address = e["data"]["address"] self.emit("device-rejected", nid, address) elif eType == "FolderScanProgress": rid = e["data"]["folder"] total = float(e["data"]["total"]) if total > 0: # ^^ just in case status = float(e["data"]["current"]) / total self.emit("folder-scan-progress", rid, status) elif eType == "ItemStarted": rid = e["data"]["folder"] filename = e["data"]["item"] t = parsetime(e["time"]) self.emit("item-started", rid, filename, t) elif eType == "FolderCompletion": self._syncthing_cb_completion(e["data"]) elif eType == "FolderSummary": rid = e["data"]["folder"] self._syncthing_cb_folder_data(e["data"]["summary"], rid) elif eType == "FolderErrors": rid = e["data"]["folder"] self.emit("folder-error", rid, e["data"]["errors"]) elif eType == "ConfigSaved": self.emit("config-saved") elif eType == "ItemFinished": rid = e["data"]["folder"] if e["data"]["error"] is None: filename = e["data"]["item"] t = parsetime(e["time"]) self.emit("item-updated", rid, filename, t) elif eType in ("ItemFinished", "DownloadProgress", "RelayStateChanged", "LocalIndexUpdated"): # Not handled pass else: log.warning("Unhandled event type: %s", e)