# by Karl Wachs
# feb 5
# try to get roomba command
##
import SocketServer
import datetime, subprocess, os, time
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
#indigo.server.log( unicode(self.client_address))
#indigo.server.log( unicode(self.data))
if unicode(self.data).find("webhook.html")>-1:
indigo.server.log( "roomba send command")
indigo.variable.updateValue("RoombaUpdate",datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S"))
return
def killOldPgm(myPID,pgmToKill):
cmd= "ps -ef | grep "+pgmToKill+" | grep -v grep"
ret = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
lines=ret.split("\n")
for line in lines:
if len(line) < 10: continue
line=line.split()
pid=int(line[1])
if pid == int(myPID): continue
os.system("kill -9 "+str(pid))
indigo.server.log( "killed program: "+pgmToKill+" pid="+str(pid))
time.sleep(5)
if __name__ == "__main__":
PORT = 6193 ## port number used by roomba
ipAddress="192.168.1.100" ## ip number of your indigo MAC
try:
indigo.variable.create("RoombaUpdate","")
except:
pass
myPID = str(os.getpid())
indigo.server.log(" PID of roomba server is:" +myPID )
killOldPgm(myPID,"RoombaUpdate.py")
# Create the server, binding on port xxx
server = SocketServer.TCPServer((ipAddress, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
import simplejson as json
import urllib2
urlReturned = urllib2.urlopen('http://192.168.1.114/status.json')
#format api returned data
data_getStatus = urlReturned.read()
#interpret json
dict = json.loads(data_getStatus)
action = str(dict['action'])
indigo.variable.updateValue(12824087, action)
result = str(dict['result'])
indigo.variable.updateValue(1042192793, result)
name = str(dict['status']['name'])
indigo.variable.updateValue(1267855651, name)
battery_charge = str(dict['status']['battery_charge'])
indigo.variable.updateValue(1262851281, battery_charge)
capacity = str(dict['status']['capacity'])
indigo.variable.updateValue(720495017, capacity)
cleaner_state = str(dict['status']['cleaner_state'])
indigo.variable.updateValue(1925578960, cleaner_state)
cleaning = str(dict['status']['cleaning'])
indigo.variable.updateValue(1995969993, cleaning)
schedule_serial_number = str(dict['status']['schedule_serial_number'])
indigo.variable.updateValue(1720655499, schedule_serial_number)
near_homebase = str(dict['status']['near_homebase'])
indigo.variable.updateValue(321117213, near_homebase)
translateDict = {'st_base': 'On homebase: Not Charging', 'st_base_recon': 'On homebase: Reconditioning Charging', 'st_base_full': 'On homebase: Full Charging', 'st_base_trickle': 'On homebase: Trickle Charging', 'st_base_wait': 'On homebase: Waiting', 'st_plug': 'Plugged in: Not Charging', 'st_plug_recon': 'Plugged in: Reconditioning Charging', 'st_plug_full': 'Plugged in: Full Charging', 'st_plug_trickle': 'Plugged in: Trickle Charging', 'st_plug_wait': 'Plugged in: Waiting', 'st_stopped': 'Stopped', 'st_clean': 'Cleaning', 'st_cleanstop': 'Stopped with cleaning', 'st_clean_spot': 'Spot cleaning', 'st_clean_max': 'Max cleaning', 'st_delayed': 'Delayed cleaning will start soon ..', 'st_dock': 'Searching Homebase', 'st_pickup': 'Roomba picked up', 'st_remote': 'Remote control driving', 'st_wait': 'Waiting for command', 'st_off': 'Off', 'st_error': 'Error', 'st_locate': 'Find me!', 'st_unknown': 'Unknown state'}
translateFrom = indigo.variables[1925578960].value
translateTo = translateDict.get(translateFrom, 'error translation')
indigo.variable.updateValue(1017727089, value=translateTo)
# by Karl Wachs
# feb 5
# try to get roomba command
##
import SocketServer
import datetime, subprocess, os, time
import simplejson as json
import urllib2
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
#indigo.server.log( unicode(self.client_address))
#indigo.server.log( unicode(self.data))
if unicode(self.data).find("webhook.html")>-1:
indigo.server.log( "roomba send command")
indigo.variable.updateValue("RoombaUpdate",datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S"))
urlReturned = urllib2.urlopen('http://192.168.1.114/status.json')
#format api returned data
data_getStatus = urlReturned.read()
#interpret json
dict = json.loads(data_getStatus)
action = str(dict['action'])
indigo.variable.updateValue(12824087, action)
result = str(dict['result'])
indigo.variable.updateValue(1042192793, result)
name = str(dict['status']['name'])
indigo.variable.updateValue(1267855651, name)
battery_charge = str(dict['status']['battery_charge'])
indigo.variable.updateValue(1262851281, battery_charge)
capacity = str(dict['status']['capacity'])
indigo.variable.updateValue(720495017, capacity)
cleaner_state = str(dict['status']['cleaner_state'])
indigo.variable.updateValue(1925578960, cleaner_state)
cleaning = str(dict['status']['cleaning'])
indigo.variable.updateValue(1995969993, cleaning)
schedule_serial_number = str(dict['status']['schedule_serial_number'])
indigo.variable.updateValue(1720655499, schedule_serial_number)
near_homebase = str(dict['status']['near_homebase'])
indigo.variable.updateValue(321117213, near_homebase)
translateDict = {'st_base': 'On homebase: Not Charging', 'st_base_recon': 'On homebase: Reconditioning Charging', 'st_base_full': 'On homebase: Full Charging', 'st_base_trickle': 'On homebase: Trickle Charging', 'st_base_wait': 'On homebase: Waiting', 'st_plug': 'Plugged in: Not Charging', 'st_plug_recon': 'Plugged in: Reconditioning Charging', 'st_plug_full': 'Plugged in: Full Charging', 'st_plug_trickle': 'Plugged in: Trickle Charging', 'st_plug_wait': 'Plugged in: Waiting', 'st_stopped': 'Stopped', 'st_clean': 'Cleaning', 'st_cleanstop': 'Stopped with cleaning', 'st_clean_spot': 'Spot cleaning', 'st_clean_max': 'Max cleaning', 'st_delayed': 'Delayed cleaning will start soon ..', 'st_dock': 'Searching Homebase', 'st_pickup': 'Roomba picked up', 'st_remote': 'Remote control driving', 'st_wait': 'Waiting for command', 'st_off': 'Off', 'st_error': 'Error', 'st_locate': 'Find me!', 'st_unknown': 'Unknown state'}
translateFrom = indigo.variables[1925578960].value
translateTo = translateDict.get(translateFrom, 'error translation')
indigo.variable.updateValue(1017727089, value=translateTo)
return
def killOldPgm(myPID,pgmToKill):
cmd= "ps -ef | grep "+pgmToKill+" | grep -v grep"
ret = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
lines=ret.split("\n")
for line in lines:
if len(line) < 10: continue
line=line.split()
pid=int(line[1])
if pid == int(myPID): continue
os.system("kill -9 "+str(pid))
indigo.server.log( "killed program: "+pgmToKill+" pid="+str(pid))
time.sleep(5)
if __name__ == "__main__":
PORT = 6193 ## port number used by roomba
ipAddress="192.168.1.100" ## ip number of your indigo MAC
try:
indigo.variable.create("RoombaUpdate","")
except:
pass
myPID = str(os.getpid())
indigo.server.log(" PID of roomba server is:" +myPID )
killOldPgm(myPID,"RoombaUpdate.py")
# Create the server, binding on port xxx
server = SocketServer.TCPServer((ipAddress, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
import subprocess
pgmToKill="roomba.py"
cmd= "ps -ef | grep "+pgmToKill+" | grep -v grep"
ret = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
lines=ret.split("\n")
for line in lines:
if len(line) < 10: continue
line=line.split()
pid=int(line[1])
os.system("kill -9 "+str(pid))
indigo.server.log( "killed program: "+pgmToKill+" pid="+str(pid))
import time
nextCall=0.
lastState=""
while True:
theState= indigo.variables[1925578960].value # this is the state of roomba
if theState != lastState:
if theState == 'cleaner_state': ## you need to put the proper string here
nextCall=time.time()+30*60
lastState="cleaning"
else:
nextCall=time.time()+60
lastState="home"
if time.time() > nextCall:
continue # put your send msg to roomba here
if lastState !="home": # put the next msg time into nextCall: 1 minute or 30 minutes
nextCall=time.time() + 30*60
else:
nextCall=time.time()+60
time.sleep(5)
Users browsing this forum: No registered users and 2 guests