<commit_before>
# -*- coding: utf-8 -*-

# Copyright (c) 2015 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from calvin.runtime.south.calvinsys import base_calvinsys_object
from calvin.runtime.south.plugins.async import async

class Timer(base_calvinsys_object.BaseCalvinsysObject):
    """
    Timers - Handling of system timers
    """

    init_schema = {
        "description": "Initialize timer",
        "properties" : {
            "repeats" :  {
                "description": "Should the timer automatically reset once triggered?",
                "type": "boolean"
            }
        }
    }
    
    can_read_schema = {
        "description": "True if timer has triggered",
    }

    read_schema = {
        "description": "Ack triggered timer"
    }
    
    can_write_schema = {
        "description": "Always True"
    }
    
    write_schema = {
        "description": "Set or cancel timer; number indicates set/reset, and false cancels",
        "type": ["number", "boolean"]
    }

    def init(self, repeats=False, **kwargs):
        self._timer = None
        self._triggered = False
        self._timeout = None
        self._repeats = True

    def _timer_cb(self):
        self._triggered = True
        self.scheduler_wakeup()
        
    def can_read(self):
        return self._triggered
    
    def read(self):
        self._triggered = False
        if self._repeats :
            self._timer = async.DelayedCall(self._timeout, self._timer_cb)
    
    def write(self, set_reset_or_cancel):
        cancel_only = isinstance(set_reset_or_cancel, bool)

        # cancel timer if running
        if self._timer and self._timer.active():
            self._timer.cancel()
        
        if not cancel_only:
            self._timeout = float(set_reset_or_cancel)
            self._timer = async.DelayedCall(self._timeout, self._timer_cb)

    def can_write(self):
        # Can always stop & reset a timer
        return True
        
    def close(self):
        if self._timer:
            self._timer.cancel()
            del self._timer
            self._timer = None
        self._triggered = False
        
    # Serialize/deserialize calvinsys
    def serialize(self):
        return {"triggered": self._triggered, "timeout": self._timeout, 
                "repeats": self._repeats, "nexttrigger": self._timer.nextcall()}
                
    def deserialize(self, state):
        import time
        self._triggered = state["triggered"]
        self._timeout = state["timeout"]
        self._repeats = state["repeats"]
        if state["nexttrigger"]:
            timeout = state["nexttrigger"] - time.time()
            if timeout < 0: timeout = 0
            self._timer = async.DelayedCall(timeout,  self._timer_cb)
        return self<commit_msg>Timer: No need to serialize 'triggered' state<commit_after>
# -*- coding: utf-8 -*-

# Copyright (c) 2015 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from calvin.runtime.south.calvinsys import base_calvinsys_object
from calvin.runtime.south.plugins.async import async

class Timer(base_calvinsys_object.BaseCalvinsysObject):
    """
    Timers - Handling of system timers
    """

    init_schema = {
        "description": "Initialize timer",
        "properties" : {
            "repeats" :  {
                "description": "Should the timer automatically reset once triggered?",
                "type": "boolean"
            }
        }
    }
    
    can_read_schema = {
        "description": "True if timer has triggered",
    }

    read_schema = {
        "description": "Ack triggered timer"
    }
    
    can_write_schema = {
        "description": "Always True"
    }
    
    write_schema = {
        "description": "Set or cancel timer; number indicates set/reset, and false cancels",
        "type": ["number", "boolean"]
    }

    def init(self, repeats=False, **kwargs):
        self._timer = None
        self._triggered = False
        self._timeout = None
        self._repeats = True

    def _timer_cb(self):
        self._triggered = True
        self.scheduler_wakeup()
        
    def can_read(self):
        return self._triggered
    
    def read(self):
        self._triggered = False
        if self._repeats :
            self._timer = async.DelayedCall(self._timeout, self._timer_cb)
    
    def write(self, set_reset_or_cancel):
        cancel_only = isinstance(set_reset_or_cancel, bool)

        # cancel timer if running
        if self._timer and self._timer.active():
            self._timer.cancel()
        
        if not cancel_only:
            self._timeout = float(set_reset_or_cancel)
            self._timer = async.DelayedCall(self._timeout, self._timer_cb)

    def can_write(self):
        # Can always stop & reset a timer
        return True
        
    def close(self):
        if self._timer:
            self._timer.cancel()
            del self._timer
            self._timer = None
        self._triggered = False
        
    # Serialize/deserialize calvinsys
    def serialize(self):
        return {"timeout": self._timeout, "repeats": self._repeats, "nexttrigger": self._timer.nextcall()}
                
    def deserialize(self, state):
        import time
        self._timeout = state["timeout"]
        self._repeats = state["repeats"]
        if state["nexttrigger"]:
            timeout = state["nexttrigger"] - time.time()
            if timeout < 0: 
                timeout = 0
                self._triggered = True
            self._timer = async.DelayedCall(timeout,  self._timer_cb)
        return self
{'fraction_non_alphanumeric': 0.09484848484848485, 'fraction_numerical': 0.0033333333333333335, 'mean_word_length': 2.188888888888889, 'pattern_counts': {'":': 23, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1209483', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1816, 'n_words': 525}