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.refreshtime = refreshtime
        self.mypid= mypid
        #self.status=status
        self.path="/GPPD/heartbeat/mantid"
        self.statusList=['waiting','unconfigured','configuring','ready','running','paused','error']

    def setData(self):
        #print time.strftime('%Y-%m-%d %H:%M:%S.%03d')
        #print constParm.status
        tmp=self.neonRedis_read.get("/GPPD/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 type(jdata) 
        #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

