1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mojoの停止をPythonとSkypeで監視

Last updated at Posted at 2015-02-05

3DプリンタMojoの停止をPythonが監視してSkype経由で知らせてくれるスクリプトです.

Mojoのログを監視してとりあえず全てのログをSkypeのチャットで送る.
停止した時は電話をかけることにする.

Mojoが再開可能な状態で頻繁に停止するので停止したら知らせるようにしました.
自動で再開しないのはプリントの異常を目視で確認する必要があるためです

使用ライブラリ

  • Skype4py (Skype for Desktopと接続)
  • watchdog (Mojoのログを監視)
  • numpy (ログを読むのに使う)

使い方

MojoのログへのパスとSkypeの連絡先を設定するだけ
実行すれば監視がスタートする

参考

スクリプト

# -*- coding: utf-8 -*-

import time
import os
import re
import numpy as np
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
import Skype4Py

# Mojoのログへのパス
BASEDIR = "C:/ProgramData/Mojo/Mojo 3D Printer Software/Log/"
log_filename = "SystemLog.log"

# 連絡先
user_name = 'Frend name'

# -------Skype4Pyのexamples->callfriend.pyからコピペ(call用)--------->
# This variable will get its actual value in OnCall handler
CallStatus = 0

# Here we define a set of call statuses that indicate a call
# has been either aborted or finished
CallIsFinished = set([Skype4Py.clsFailed, Skype4Py.clsFinished,
                       Skype4Py.clsMissed, Skype4Py.clsRefused,
                       Skype4Py.clsBusy, Skype4Py.clsCancelled]);

def AttachmentStatusText(status):
   return skype.Convert.AttachmentStatusToText(status)

def CallStatusText(status):
    return skype.Convert.CallStatusToText(status)

# This handler is fired when status of Call object has changed
def OnCall(call, status):
    global CallStatus
    CallStatus = status
    print 'Call status: ' + CallStatusText(status)

# This handler is fired when Skype attatchment status changes
def OnAttach(status):
    print 'API attachment status: ' + AttachmentStatusText(status)
    if status == Skype4Py.apiAttachAvailable:
        skype.Attach()
# <--コピペここまで----------------------------------------------------

# Skypeにアタッチ・チャット設定
skype = Skype4Py.Skype()
if not skype.Client.IsRunning:
    print 'Starting Skype..'
    skype.Client.Start()
skype.Attach()
skype.OnAttachmentStatus = OnAttach
skype.OnCallStatus = OnCall
chat = skype.CreateChatWith(user_name)
chat.SendMessage('Mojo''s watchdog has been started!')

# Mojoログ監視
class ChangeHandler(FileSystemEventHandler):

    def on_modified(self, event):
        if event.is_directory:
            return
        else:
            time.sleep(5)
            data = np.loadtxt(
               os.path.join(BASEDIR, log_filename),
               delimiter="\n", dtype=str)
            #-1で最後の行(最新のログ情報)だけ出力
            print(data[-1])
            chat.SendMessage(data[-1])
            #Pauseが含まれるときは電話をかける
            n = len(re.findall('pause', data[-1])) + \
                len(re.findall('Paused', data[-1]))
            if n > 0:
			     skype.PlaceCall(user_name)
                while not CallStatus in CallIsFinished:
                    pass

# "ファイル監視にwatchdogがかなり便利な件"のコードをコピペ
if __name__ in '__main__':
    while 1:
        event_handler = ChangeHandler()
        observer = Observer()
        observer.schedule(event_handler, BASEDIR, recursive=True)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?