import redis
from app.app_config import *
 
 
class RedisHelper:
 
    def __init__(self):
        self.__conn = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
        self.chan_sub = 'session-channel'
        self.chan_pub = 'output-channel'
        self.chan_log = 'writelog-channel'
        self.chan_api = 'api-channel'
        self.chan_screen_pub = 'screen-channel'
 
    def public(self, msg):
        self.__conn.publish(self.chan_pub, msg)
        return True
 
    def subscribe_session(self):
        pub = self.__conn.pubsub()
        pub.subscribe(self.chan_sub)
        pub.parse_response()
        return pub
    
    def subscribe_volume(self):
        pub = self.__conn.pubsub()
        pub.subscribe(VALUECHPUB)
        pub.parse_response()
        return pub
    
    def lpush(self, data):
        self.__conn.lpush(self.chan_pub, data)
        return True
    
    def logpush(self, data):
        self.__conn.lpush(self.chan_log, data)
        return True
    
    def volumeEventPush(self, data):
        self.__conn.publish(EVENTCHSUB, data)
        return True
    
    def volumeItemPush(self, data):
        self.__conn.publish(WEBCHPUB, data)
        return True
    
    def apipush(self, data):
        self.__conn.lpush(self.chan_api, data)
        return True
    
    def screen_publish(self, msg):
        self.__conn.publish(self.chan_screen_pub, msg)
        return True