private void ClientOnReceivedData(TtpSshClient client, TesiraMessage message) { try { JToken json; switch (message.Type) { case TesiraMessageType.OkWithResponse: var response = message as TesiraResponse; if (response == null) { return; } if (response.Command == "DEVICE get networkStatus") { json = response.TryParseResponse(); if (json != null) { Debug.WriteSuccess("Network Config", "\r\n" + json.ToString(Formatting.Indented)); } } break; case TesiraMessageType.Notification: json = message.TryParseResponse(); if (json != null) { Debug.WriteSuccess(json.ToString(Formatting.Indented)); } break; default: break; } } catch (Exception e) { CloudLog.Exception(e, "Error handling received data"); } if (ReceivedData == null) { return; } try { ReceivedData(client, message); } catch (Exception e) { CloudLog.Exception(e, "Error calling event handler"); } }
private void ClientOnConnectionStatusChange(TtpSshClient client, TtpSshClient.ClientStatus status) { switch (status) { case TtpSshClient.ClientStatus.Connected: Send("DEVICE", TesiraCommand.Get, TesiraAttributeCode.NetworkStatus); Send("SESSION", TesiraCommand.Get, TesiraAttributeCode.Aliases); DeviceCommunicating = true; break; case TtpSshClient.ClientStatus.Disconnected: DeviceCommunicating = false; break; } }
protected virtual void OnConnectionStatusChange(TtpSshClient client, ClientStatus status) { var handler = ConnectionStatusChange; if (handler != null) { try { handler(client, status); } catch (Exception e) { CloudLog.Exception(e); } } }
private void OnReceivedData(TtpSshClient client, TesiraMessage message) { if (message.Type != TesiraMessageType.Notification && message.Id == InstanceTag) { var response = message as TesiraResponse; if (response != null && response.Type == TesiraMessageType.OkWithResponse) { Debug.WriteSuccess(GetType().Name + " \"" + InstanceTag + "\"", "Received {0}\r\n{1}", response.AttributeCode, response.TryParseResponse().ToString(Formatting.Indented)); ReceivedResponse(response); } } else if (message.Type == TesiraMessageType.Notification && Subscriptions.ContainsKey(message.Id)) { var response = message as TesiraNotification; if (response != null) { Debug.WriteSuccess(GetType().Name + " \"" + InstanceTag + "\"", "Received notification \"{0}\"\r\n{1}", Subscriptions[message.Id], response.TryParseResponse().ToString(Formatting.Indented)); ReceivedNotification(Subscriptions[message.Id], response.TryParseResponse()); } } }
public Tesira(string address, string username, string password) { _client = new TtpSshClient(address, username, password); _client.ReceivedData += ClientOnReceivedData; _client.ConnectionStatusChange += ClientOnConnectionStatusChange; }