Exemple #1
0
    def ramp(self, setpoint, maxStep=.5, disconnectWhenFinished=False):
        tag = self.pSetpointTag
        currentSetpoint = tagClient.readTags([tag])
        diff = setpoint - currentSetpoint
        if diff > maxStep:
            currentSetpoint += maxStep
        elif diff < -maxStep:
            currentSetpoint -= maxStep
        else:
            currentSetpoint += diff
        tagClient.writeTags([tag], [currentSetpoint])

        if abs(diff) > .001:
            #schedule a callback, allowing some time for actuation
            sched = datetime.now() + timedelta(seconds=1.5)
            Core.schedule(sched, self.ramp)
            print("{me} is scheduling another ramp call: {cs}".format(
                me=self.channelNumber, cs=currentSetpoint))
            if disconnectWhenFinished == True and setpoint == 0:
                print("ramp with disconnect completed, disconnecting {me}".
                      format(me=self.channelNumber))
                self.disconnect()
        else:
            print("{me} is done ramping to {set}".format(me=self.name,
                                                         set=setpoint))
Exemple #2
0
 def waitForSettle(self):
     current = tagClient.readTag([self.regItag])
     if abs(current) < .01:
         self.connected = self.disconnect()
     else:
         now = datetime.now()
         Core.schedule(now + timedelta(seconds=1), self.waitForSettle)
Exemple #3
0
 def disconnectSoft(self):
     #change setpoint to zero
     tagClient.writeTags([self.pSetpointTag], [0])
     #callback and keep calling back until the current is almost zero
     now = datetime.now()
     Core.schedule(now + timedelta(seconds=1), self.waitForSettle)
    def __init__(self, agent_name, market_name, verbose_logging, q_uc, building_topic, devices, **kwargs):
        super(UncontrolAgent, self).__init__(verbose_logging, **kwargs)
        self.market_name = market_name
        self.q_uc = q_uc		
        self.price_index=0
        self.price_min = 10.
        self.price_max = 100.
        self.infinity = 1000000
        self.num = 0
        self.market_num_indicate = 0		
        self.power_aggregation = []
        self.current_power = None
        self.demand_aggregation_master = {}
        self.demand_aggregation_working = {}
        self.agent_name = agent_name
        self.demand_curve = []
        for market in self.market_name:
            self.join_market(self.market, SELLER, self.reservation_callback, self.offer_callback,
                         None, self.price_callback, self.error_callback)
            self.demand_curve.append(PolyLine()

        self.building_topic = building_topic
        self.devices = devices


    @Core.receiver('onstart')
    def setup(self, sender, **kwargs):
        """
        Set up subscriptions for demand limiting case.
        :param sender:
        :param kwargs:
        :return:
        """
        if 1:
            for device, points in self.devices.items():
                device_topic = self.building_topic(unit=device)
                _log.debug('Subscribing to {}'.format(device_topic))
                self.demand_aggregation_master[device_topic] = points
                self.vip.pubsub.subscribe(peer='pubsub',
                                          prefix=device_topic,
                                          callback=self.aggregate_power)
            self.demand_aggregation_working = self.demand_aggregation_master.copy()
            _log.debug('Points are  {}'.format(self.demand_aggregation_working))

    def offer_callback(self, timestamp, market_name, buyer_seller):
        index = self.market_name.index(market_name)
        result, message = self.make_offer(market_name, buyer_seller, self.create_demand_curve(index))
        _log.debug("{}: demand max {} and min {} at {}".format(self.agent_name,
                                                               self.demand_curve[index].x(10),
                                                               self.demand_curve[index].x(100),
                                                               timestamp))
        _log.debug("{}: result of the make offer {} at {}".format(self.agent_name,
                                                                  result,
                                                                  timestamp))        
        
    def conversion_handler(self, conversion, point, data):
        expr = parse_expr(conversion)
        sym = symbols(point)
        point_list = [(point, data[point])]
        return float(expr.subs(point_list))

    def aggregate_power(self, peer, sender, bus, topic, headers, message):
        """
        Power measurements for devices are aggregated.
        :param peer:
        :param sender:
        :param bus:
        :param topic:
        :param headers:
        :param message:
        :return:
        """
        _log.debug("{}: received topic for power aggregation: {}".format(self.agent_name,
                                                                         topic))
        data = message[0]
        try:
            current_points = self.demand_aggregation_working.pop(topic)
        except KeyError:
            if self.power_aggregation:
                self.current_power = sum(self.power_aggregation)
            else:
                self.current_power = 0.
            self.demand_aggregation_working = self.demand_aggregation_master.copy()
         
        conversion = current_points.get("conversion")
        for point in current_points.get("points", []):
            if conversion is not None:
                value = float(self.conversion_handler(conversion, point, data))
            else:
                value = float(data[point])
            self.power_aggregation.append(value)
        if not self.demand_aggregation_working:
            if self.power_aggregation:
                self.current_power = sum(self.power_aggregation)

            else:
                self.current_power = 0.
            self.power_aggregation = []
            self.demand_aggregation_working = self.demand_aggregation_master.copy()
            _log.debug("Power check: {}".format(self.demand_aggregation_working))
            self.num=self.num+1
            if self.num>=59:
                   self.q_uc[self.market_num_indicate]=-self.current_power
                   self.market_num_indicate=self.market_num_indicate+1
                   if self.market_num_indicate>23:
                               self.market_num_indicate=0
                   self.num=0							   

        _log.debug("{}: updating power aggregation: {}".format(self.agent_name,
                                                           self.current_power))  
    def create_demand_curve(self, index):
        self.demand_curve[index] = PolyLine()
        p_min = 10.
        p_max = 100.
        qMin = self.q_uc[index]
        qMax = self.q_uc[index]
        if self.hvac_avail:
            self.demand_curve[index].add(Point(price=max(p_min, p_max), quantity=min(qMin, qMax)))
            self.demand_curve[index].add(Point(price=min(p_min, p_max), quantity=max(qMin, qMax)))
        else:
            self.demand_curve[index].add(Point(price=max(p_min, p_max), quantity=0.1))
            self.demand_curve[index].add(Point(price=min(p_min, p_max), quantity=0.1))
#        if self.hvac_avail:
#            _log.debug("{} - Tout {} - Tin {} - q {}".format(self.agent_name, self.tOut, self.tIn, self.qHvacSens))
        return self.demand_curve[index]


    def price_callback(self, timestamp, market_name, buyer_seller, price, quantity):
        _log.debug("{}: cleared price ({}, {}) for {} as {} at {}".format(self.agent_name,
                                                                          price,
                                                                          quantity,
                                                                          market_name,
                                                                          buyer_seller,
                                                                          timestamp))

    def error_callback(self, timestamp, market_name, buyer_seller, error_code, error_message, aux):
        _log.debug("{}: error for {} as {} at {} - Message: {}".format(self.agent_name,
                                                                       market_name,
                                                                       buyer_seller,
                                                                       timestamp,
                                                                       error_message))


def main():
    """Main method called to start the agent."""
    utils.vip_main(uncontrol_agent, version=__version__)


if __name__ == '__main__':
    # Entry point for script
    try:
        sys.exit(main())
    except KeyboardInterrupt:
        pass