Exemplo n.º 1
0
	def maybe_up(self, resync = False):
		if self.running not in ("off","error"):
			return
		if self.last is not None:
			self.running = "next"
			self.next = time_delta(self.interval, now=self.last)
			self.waiter = callLater(False, self.next, self.do_pre)
Exemplo n.º 2
0
	def up(self, resync=False):
		if self.running not in ("off","error"):
			if not resync:
				raise AlreadyRunningError(self)
		if resync:
			self.do_sync()
		else:
			self.running = "next"
			self.next = time_delta(self.interval, now=self.last)
			self.waiter = callLater(False, self.next, self.do_pre)
Exemplo n.º 3
0
	def do_post(self):
		self.slotter = None
		if self.running != "during" or self.waiter is not None:
			log(ERROR,"timeslot error post",self.running,*self.name)
			return

		self.running = "next"
		simple_event("timeslot","end",*self.name, deprecated=True)
		simple_event("timeslot","state",*self.name, state="end")
		self.next = time_delta(self.interval, now=self.next)-dt.timedelta(0,self.duration)
		self.waiter = callLater(False, self.next, self.do_pre)
Exemplo n.º 4
0
	def do_pre(self):
		self.waiter = None
		if self.running != "next" or self.slotter is not None:
			log(ERROR,"timeslot error pre",self.running,*self.name)
			return

		if self.next is None:
			self.next = now()
		self.last = self.next

		self.running = "during"
		simple_event("timeslot","begin",*self.name, deprecated=True)
		simple_event("timeslot","state",*self.name, state="begin")
		self.next += dt.timedelta(0,self.duration)
		self.slotter = callLater(False,self.next, self.do_post)
Exemplo n.º 5
0
	def set_value(self,val=None):
		if val is None: val = self._value
		assert 0<=val<=1, u"Value is '%s', not in 0…1" % (val,)

		do = self.t_on if self.state else self.t_off
		self.t_off,self.t_on = self.new_value(val)
		dn = self.t_on if self.state else self.t_off

		self._value = val

		if do != dn:
			if self.timer is not None:
				self.timer.cancel()
			if dn is not None:
				self.next = (self.last if self.last is not None else now()) + dt.timedelta(0,dn)
				self.timer = callLater(False,self.next,self.do_timed_switch)
Exemplo n.º 6
0
	def do_switch(self):
		"""Click"""
		if self.state:
			self.state = 0
			tn = self.t_off
		else:
			self.state = 1
			tn = self.t_on

		simple_event("pwm","change",*self.name, value=self.state)
		try:
			self.last = self.next
			if tn is not None:
				self.next = self.last + dt.timedelta(0,tn)
				self.timer = callLater(False,self.next,self.do_timed_switch)
			else:
				self.next = None
		except Exception as e:
			fix_exception(e)
			process_failure(e)
			simple_event("pwm","error",*self.name, error=e)
Exemplo n.º 7
0
 def _start_timer(self):
     if self.timer is not None:
         self.timer = callLater(True, self.timeout, self.no_data)
Exemplo n.º 8
0
	def do_sync(self):
		self.down()
		self.running = "during"
		self.next = now()+dt.timedelta(0,self.duration/2)
		self.slotter = callLater(False,self.next, self.do_post)
Exemplo n.º 9
0
 def _start(self):
     if self._timer:
         self._timer.cancel()
     self.started = now()
     self._timer = callLater(False, self.end, self._timeout)
Exemplo n.º 10
0
	def _handler(self):
		"""\
			This is the receiver's main loop.

			Processing of incoming and outgoing data is serialized so that
			there will be no problems with concurrency.
			"""
		def doReOpen():
			m = MsgReOpen()
			if self.q is not None:
				self.q.put(m, block=False)

		state = "open" if self.state == "connected" else "closed" if self.channel is None else "connecting"
		log("conn",TRACE,"setstate init %s" % (state,))
		self.connect_timeout = self.initial_connect_timeout
		self.attempts = 0

		if not self.ondemand and state != "open":
			doReOpen()

		while True:
			msg = self.q.get()

			if isinstance(msg,MsgSender):
				self.senders[msg.prio].append(msg)
			elif isinstance(msg,MsgReceiver):
				if msg.blocking:
					self.receivers[msg.prio].insert(0,msg)
				else:
					self.receivers[msg.prio].append(msg)
			elif isinstance(msg,MsgIncoming):
				self._incoming(msg)
			elif isinstance(msg,MsgOpenMarker):
				log("conn",TRACE,"setstate %s %s" % (state,"connected"))
				state = "connected"

				self.connect_timeout = self.initial_connect_timeout
				self.attempts = 0

			elif isinstance(msg,MsgReOpen):
				if self.channel is None:
					log("conn",TRACE,"setstate %s %s" % (state,"want"))
					state = "want"

			elif isinstance(msg,MsgClosed):
				if self.channel is not None:
					if state != "waiting" and state != "connecting":
						log("conn",TRACE,"setstate2 %s %s" % (state,"closed"))
						state = "closed"
					self._teardown("ReOpen",external=False)
				if state == "closed" or state == "connecting":
					log("conn",TRACE,"setstate %s %s: wait %.3f" % (state,"waiting",self.connect_timeout))
					state = "waiting"
					callLater(True,self.connect_timeout,doReOpen)
					self._up_timeout()

			elif isinstance(msg,MsgError):
				self._error(msg.error)
			else:
				raise UnknownMessageType(msg)

			if self.ondemand and not self.n_outq:
				continue
			if state == "want"  or  state == "closed" and self.ondemand and self.n_outq:
				log("conn",TRACE,"setstate %s %s" % (state,"connecting"))
				state = "connecting"
				self._setup()
			if self.state != "connected":
				continue

			log("msg",TRACE,"states at run",self.state,state)
			done = False # marker for "don't send any more stuff"

			for mq in self.receivers:
				if done: break
				for m in mq:
					if m.blocking:
						log("msg",TRACE,"blocked by",str(m))
						done = True
			if done: continue

			for mq in self.senders:
				if done: break
				while len(mq):
					if done:
						break
					if self.channel is None:
						break

					msg = mq.pop(0)
					log("msg",TRACE,"send",str(msg))
					try:
						r = msg.send(self.channel)
					except Exception as ex:
						fix_exception(ex)
						r = ex
					else:
						self.last_sent = msg
						self.last_sent_at = now()
						self.n_sent_now += 1
					log("msg",TRACE,"send result",r)
					if r is RECV_AGAIN:
						if msg.blocking:
							self.receivers[msg.prio].insert(0,msg)
						else:
							self.receivers[msg.prio].append(msg)
					elif r is SEND_AGAIN:
						if msg.blocking:
							self.senders[msg.prio].insert(0,msg)
						else:
							self.senders[msg.prio].append(msg)
					elif isinstance(r,SEND_LATER):
						raise NotImplementedError("Queueing doesn't work yet")
					elif isinstance(r,MSG_ERROR):
						try:
							raise r
						except Exception as r:
							fix_exception(r)
							process_failure(r)
					elif isinstance(r,Exception):
						reraise(r)
					else:
						msg.done()

					if msg.blocking:
						done = True
						break

				# while setting up, only process PRIO_CONNECT messages
				if state != "connected":
					log(TRACE,"NotConn",self.senders)
					break
Exemplo n.º 11
0
	def _start(self):
		if self._timer:
			self._timer.cancel()
		self.started = now()
		self._timer = callLater(False,self.end,self._timeout)
Exemplo n.º 12
0
	def _set_timeout(self):
		if self.timeout is not None:
			self._timer = callLater(True,self.timeout,self._timeout)
Exemplo n.º 13
0
	def arm(self,sending):
		if self.timer is None:
			self.timer = callLater(True,self.reply_timeout,self.send_me)
Exemplo n.º 14
0
    def __init__(self, queue):
        super(WAGOinitMsg, self).__init__()

        self.queue = queue
        self.start_timer = callLater(True, 0.2 if TESTING else 2,
                                     self.start_timed_out)
Exemplo n.º 15
0
Arquivo: wago.py Projeto: smurfix/MoaT
	def ping_start(self):
		if self.ping_timer is not None:
			self.ping_timer.cancel()
		self.ping_timer = callLater(True, self.max_ping_timeout, self.ping_timed_out)
Exemplo n.º 16
0
 def ping_start(self):
     if self.ping_timer is not None:
         self.ping_timer.cancel()
     self.ping_timer = callLater(True, self.max_ping_timeout,
                                 self.ping_timed_out)
Exemplo n.º 17
0
 def do_restart(self):
     if not self.stopped:
         callLater(True, 5, self.do_start)
Exemplo n.º 18
0
	def _start_timer(self):
		if self.timer is not None:
			self.timer = callLater(True,self.timeout, self.no_data)
Exemplo n.º 19
0
	def do_restart(self):
		if not self.stopped:
			callLater(True,5,self.do_start)
Exemplo n.º 20
0
def looper():
    while True:
        print("L")
        sleep(1)


def ready():
    c = Context()
    for f in sys.argv[1:]:
        parse(f, ctx=c)

    #c.logger=parse_logger
    if os.isatty(0):
        i = InteractiveInterpreter
    else:
        i = Interpreter
    print("""Ready. Type «help» if you don't know what to do.""")
    try:
        parse(sys.stdin, interpreter=i, ctx=c)
    except Exception as e:
        fix_exception(e)
        reporter(e)
    shut_down()


from gevent import spawn, sleep
#spawn(looper)
callLater(False, 0.1, ready)
mainloop()
Exemplo n.º 21
0
Arquivo: main.py Projeto: smurfix/MoaT
	print("Error:",err)
	
def looper():
	while True:
		print("L")
		sleep(1)

def ready():
	c=Context()
	for f in sys.argv[1:]:
		parse(f, ctx=c)
	
	#c.logger=parse_logger
	if os.isatty(0):
		i = InteractiveInterpreter
	else:
		i = Interpreter
	print("""Ready. Type «help» if you don't know what to do.""")
	try:
		parse(sys.stdin, interpreter=i, ctx=c)
	except Exception as e:
		fix_exception(e)
		reporter(e)
	shut_down()

from gevent import spawn,sleep
#spawn(looper)
callLater(False,0.1,ready)
mainloop()

Exemplo n.º 22
0
Arquivo: wago.py Projeto: smurfix/MoaT
	def __init__(self,queue):
		super(WAGOinitMsg,self).__init__()

		self.queue = queue
		self.start_timer = callLater(True, 0.2 if TESTING else 2, self.start_timed_out)