Code by Scott שאול בן ישוע
Verified Commit 8d5cd79c authored by Sha'ul ben Yeshua's avatar Sha'ul ben Yeshua 🎗
Browse files

New sample telnet driver

parent 1a6d9cf2
Pipeline #207 passed with stage
in 3 seconds
/*
A simple driver that will run a command via telnet
Useful with Rpi's
Copyright 2019 Sha'ul ben Yeshua / Scott Grayban
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
metadata {
definition (name: "Send Telnet Command", namespace: "sgrayban", author: "Scott Grayban")
{
capability "Initialize"
capability "Telnet"
capability "Switch"
command "Reboot Device"
}
attribute "Telnet", "String"
preferences {
input "ip", "text", title: "Target System IP", description: "", required: true, displayDuringSetup: true
input "port", "text", title: "Target System Telnet Port", description: "", required: true, displayDuringSetup: true
input "username", "text", title: "Username", description: "", required: true, displayDuringSetup: true
input "password", "text", title: "Password", description: "", required: true, displayDuringSetup: true
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
}
}
def initialize(){
log.debug "Connecting to telnet - IP = ${ip}, Port = ${port.toInteger()}, Username = ${username}, Password = ${password}"
telnetConnect([terminalType: 'VT100'], ip, port.toInteger(), username, password)
}
def installed(){
log.info "Installed"
log.info "Please enter your login credentials."
}
def updated(){
log.info "Preferences updated"
}
def on() {
initialize()
if (logEnable) log.info "Send Reboot"
sendEvent(name: "switch", value: "on")
def msg = "sudo reboot"
if (logEnable) log.info "Sending msg = ${msg}"
sendHubCommand(new hubitat.device.HubAction("""$msg\r\n""", hubitat.device.Protocol.TELNET))
runIn(2,sendEvent(name: "switch", value: "off"))
}
def reboot(){
on()
}
def parse(String msg) {
if (logEnable) log.debug "Telnet Response = ${msg}"
if (msg == "permitted by applicable law.") {
sendEvent(name: "Telnet", value: "Connected");
}
}
def telnetStatus(String status){
if (logEnable) log.info "telnetStatus- error: ${status}"
if (status == "receive error: Stream is closed"){
if (logEnable) log.error "Telnet connection dropped..."
sendEvent(name: "Telnet", value: "Disconnected")
initialize()
} else {
sendEvent(name: "Telnet", value: "Connected")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment