-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnetRemote.py
165 lines (147 loc) · 6.68 KB
/
netRemote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# lampControllerCommunicator
# Copyright (C) 2021 Hiroki Fujii <[email protected]>
import os
import win32api
import threading
import pickle
import time
import json
import wx
import requests
from logging import getLogger
import constants
import globalVars
import listManager
from soundPlayer.constants import *
from views import netController
class lampController(threading.Thread):
def __init__(self):
# 定数
self.TITLE = 0
self.PATH = 1
self.ARTIST = 2
self.ALBUM = 3
self.ALBUM_ARTIST = 4
self.LENGTH = 5
self.log=getLogger("%s.NetRemote" % (constants.LOG_PREFIX,))
# フォルダ一覧読み込み
if not os.path.isfile("netDirList.dat"):
self.netDirDict = {}
self.saveDirDict()
else:
f = open("netDirList.dat", "rb")
self.netDirDict = pickle.load(f)
f.close()
self.fileInfo = ["", "", "", "", "", 0]
self.exitFlag = False
self.requestFlag = True
self.playbackTime = 0
super().__init__()
def run(self):
sleep = constants.API_DEFAULT_INTERVAL
errorCount = 0 # 連続エラーで接続確認処理
while not self.exitFlag:
time.sleep(sleep)
if self.exitFlag: break
if not self.requestFlag: continue
if not globalVars.app.config.getboolean("network","ctrl_client",True): continue
if not globalVars.app.config.getstring("network", "user_name"): continue
if not globalVars.app.config.getstring("network", "software_key"): continue
# インターネット接続確認
if errorCount >= 10:
try:
response = requests.get("https://www.riken.jp/")
errorCount = 0
except: continue
try:
responseObject = requests.post(constants.API_COMUNICATION_URL, json=self.makeData(), timeout=5)
responseObject.encoding="utf-8"
resJson = responseObject.json()
sleep = int(resJson["apiSecInterval"])
for o in resJson["operation"]:
if o == "play": wx.CallAfter(globalVars.eventProcess.playButtonControl)
elif o == "previous": wx.CallAfter(globalVars.eventProcess.previousBtn)
elif o == "next": wx.CallAfter(globalVars.eventProcess.nextFile, True)
elif o == "stop": wx.CallAfter(globalVars.eventProcess.stop)
elif o == "volumeUp": wx.CallAfter(globalVars.eventProcess.changeVolume, 5)
elif o == "volumeDown": wx.CallAfter(globalVars.eventProcess.changeVolume, -5)
elif o == "repeatLoop": globalVars.eventProcess.repeatLoopCtrl()
elif o == "shuffle": globalVars.eventProcess.shuffleSw()
elif o == "clearAllLists": globalVars.eventProcess.clearAllLists()
elif "file/" in o or "playlist/" in o:
self.__fileProcess(o)
except Exception as e:
errorCount += 1
self.log.error(str(e))
def saveDirDict(self):
f = open("netDirList.dat", "wb")
pickle.dump(self.netDirDict, f)
f.close()
# コントローラ表示
def showController(self):
d = netController.show()
if d == wx.ID_CLOSE: self.requestFlag = False
elif d == wx.ID_OPEN: self.requestFlag = True
return
def exit(self):
self.exitFlag = True
def makeData(self):
if globalVars.play.getStatus() == PLAYER_STATUS_PLAYING: playStatus = "playing"
elif globalVars.play.getStatus() == PLAYER_STATUS_PAUSED: playStatus = "paused"
else: playStatus = "stopped"
userName = globalVars.app.config.getstring("network", "user_name")
softwareKey = globalVars.app.config.getstring("network", "software_key")
# リピートループ
if globalVars.eventProcess.repeatLoopFlag == 0: repeatLoop = "off"
elif globalVars.eventProcess.repeatLoopFlag == 1: repeatLoop = "repeat"
elif globalVars.eventProcess.repeatLoopFlag == 2: repeatLoop = "loop"
else: repeatLoop = ""
# シャッフル
if globalVars.eventProcess.shuffleCtrl == None: shuffle = "off"
else: shuffle = "on"
jData = {}
jData["apiVersion"] = 1
jData["authentication"] = {"userName": userName, "softwareKey": softwareKey}
jData["software"] = {
"driveSerialNo": win32api.GetVolumeInformation(os.environ["SystemRoot"][:3])[1],
"pcName": os.environ["COMPUTERNAME"]
}
jData["status"] = {
"playbackStatus": playStatus,
"repeatLoop": repeatLoop,
"shuffle": shuffle,
"fileTitle": self.fileInfo[self.TITLE],
"filePath": self.fileInfo[self.PATH],
"fileArtist": self.fileInfo[self.ARTIST],
"fileAlbum": self.fileInfo[self.ALBUM],
"fileAlbumArtist": self.fileInfo[self.ALBUM_ARTIST],
"playbackTime": self.__getPlaybackTime(),
"fileLength": self.fileInfo[self.LENGTH]
}
return jData
def setFileInfo(self):
if globalVars.eventProcess.playingList == constants.PLAYLIST: t = listManager.getTuple(constants.PLAYLIST)
else: t = globalVars.listInfo.playingTmp
if t[constants.ITEM_TITLE] == "": self.fileInfo = [t[constants.ITEM_NAME]]
else: self.fileInfo = [t[constants.ITEM_TITLE]]
self.fileInfo += [t[constants.ITEM_PATH],
t[constants.ITEM_ARTIST], t[constants.ITEM_ALBUM], t[constants.ITEM_ALBUMARTIST]]
if t[constants.ITEM_LENGTH] == None: self.fileInfo.append(0)
else: self.fileInfo.append(t[constants.ITEM_LENGTH])
def clearFileInfo(self):
self.fileInfo = ["", "", "", "", "", 0]
def __getPlaybackTime(self):
ret = self.playbackTime
self.playbackTime = 0
return ret
def setPlaybackTime(self, sec):
if (isinstance(sec, int) or isinstance(sec, float)) and sec >= 0:
self.playbackTime = int(sec)
else: self.playbackTime = 0
def __fileProcess(self, resString):
l = resString.split("/")
if (not l[0] in ["file", "playlist"]) or not (len(l)>=2 and l[1] in self.netDirDict): return
# ローカル用パスを構成
path = os.path.join(self.netDirDict[l[1]], "\\".join(l[2:]))
if l[0] == "file": wx.CallAfter(globalVars.eventProcess.forcePlay, path)
elif l[0] == "playlist": wx.CallAfter(listManager.addItems, [path], globalVars.app.hMainView.playlistView, id=--1, ignoreError=True)