import time
import multiprocessing
import json
class neonHeartBeat(multiprocessing.Process):
    def __init__(self, neonRedis, refreshtime, mypid):
        super(neonHeartBeat, self).__init__()
        #self.myDict=myDict
        self.neonRedis_write = neonRedis
        self.neonRedis_read = neonRedis
	self.mypid= mypid
        self.refreshtime = refreshtime
        #self.path="/SANS/heartbeat/mantid1"
        self.path="/SANS/heartbeat/mantid"
        self.statusList=['waiting','unconfigured','configuring','ready','running','paused','error']

    def setData(self):
	tmp=self.neonRedis_read.get("/SANS/stateMachine/status")
        status=int(tmp)
        data={}
        data['timestamp']=time.strftime('%Y-%m-%dT%H:%M:%S.%03d+08:00')
        data['status']=status
        data['pid']=self.mypid

        #jdata=json.dumps(data)
        #print "status: ",self.statusList[status], "----",status
        self.neonRedis_write.set(self.path, json.dumps(data))

    def process(self):
        self.setData()
        time.sleep(self.refreshtime)    

    def run(self):
        while True:
            try:
                self.process()
            except:
                break

