diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c57eca0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +NSIS/GreenITService_Installer.exe +Project/.vs/ +Project/GreenIT/bin +Project/GreenIT/obj \ No newline at end of file diff --git a/NSIS/GreenITService.nsi b/NSIS/GreenITService.nsi index 39d5d8f..6521449 100644 --- a/NSIS/GreenITService.nsi +++ b/NSIS/GreenITService.nsi @@ -1,4 +1,12 @@ -; Defines +##################################################################### +# Defines, includes and default settings +##################################################################### +!include "MUI2.nsh" +!include "nsDialogs.nsh" +!include "WordFunc.nsh" + +SetCompressor lzma + !define PRODUCT_NAME "GreenIT Service" !define PRODUCT_VERSION "1.0" !define PRODUCT_PUBLISHER "FactorFX" @@ -7,16 +15,28 @@ !define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" !define PRODUCT_UNINST_ROOT_KEY "HKLM" -SetCompressor lzma +Name "${PRODUCT_NAME}" +OutFile "GreenITService_Installer.exe" +InstallDir "$PROGRAMFILES\GreenIT Service" +InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" "" +ShowInstDetails hide +ShowUnInstDetails hide -; Includes -!include "MUI.nsh" -!include "nsDialogs.nsh" +# Vairables +Var Dialog +Var COLLECT_INFO_PERIOD_Label +Var COLLECT_INFO_PERIOD +Var UPLOAD_PERIOD_Label +Var UPLOAD_PERIOD +Var SAVE_INFO_PERIOD_Label +Var SAVE_INFO_PERIOD -; MUI Settings +##################################################################### +# MUI settings +##################################################################### !define MUI_ABORTWARNING -!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico" -!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico" +!define MUI_ICON "greenit.ico" +!define MUI_UNICON "greenit.ico" ; Welcome page !insertmacro MUI_PAGE_WELCOME @@ -29,194 +49,518 @@ Page custom ConfigPage ConfigPageLeave ; Instfiles page !insertmacro MUI_PAGE_INSTFILES ; Finish page -!define MUI_FINISHPAGE_RUN "$INSTDIR\GreenIT.exe" -!define MUI_FINISHPAGE_RUN_PARAMETERS "install start" !define MUI_FINISHPAGE_SHOWREADME "$INSTDIR\README.md" !insertmacro MUI_PAGE_FINISH - ; Uninstaller pages !insertmacro MUI_UNPAGE_INSTFILES - ; Language files !insertmacro MUI_LANGUAGE "English" - ; Reserve files -!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS +ReserveFile /plugin InstallOptions.dll -; MUI end ------ +##################################################################### +# Setup +##################################################################### +Function GetParameters + Push $R0 + Push $R1 + Push $R2 + Push $R3 -Name "${PRODUCT_NAME} ${PRODUCT_VERSION}" -OutFile "C:\Users\developpeur\Downloads\GreenITService_Installer.exe" -InstallDir "$PROGRAMFILES\GreenIT Service" -InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" "" -ShowInstDetails show -ShowUnInstDetails show + StrCpy $R2 1 + StrLen $R3 $CMDLINE -; Vairables -Var Dialog -Var COLLECT_INFO_PERIOD_Label -Var COLLECT_INFO_PERIOD -Var UPLOAD_PERIOD_Label -Var UPLOAD_PERIOD -Var SAVE_INFO_PERIOD_Label -Var SAVE_INFO_PERIOD + ;Check for quote or space + StrCpy $R0 $CMDLINE $R2 + StrCmp $R0 '"' 0 +3 + StrCpy $R1 '"' + Goto loop + StrCpy $R1 " " -Section "Main" SEC01 - SetOutPath "$INSTDIR" - SetOverwrite try - File "..\Project\GreenIT\bin\Release\net6.0\System.Diagnostics.EventLog.dll" - File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.exe" - File "..\Project\GreenIT\bin\Release\net6.0\config.json" - Call UploadConfig - AccessControl::GrantOnFile "$INSTDIR\config.ini" "(BU)" "GenericExecute + GenericRead + GenericWrite" - CreateDirectory "$SMPROGRAMS\GreenIT Service" - CreateShortCut "$SMPROGRAMS\GreenIT Service\GreenIT Service.lnk" "$INSTDIR\GreenIT.exe" - File "..\Project\GreenIT\bin\Release\net6.0\System.IO.Ports.dll" - File "..\Project\GreenIT\bin\Release\net6.0\Microsoft.Win32.SystemEvents.dll" - File "..\Project\GreenIT\bin\Release\net6.0\System.Management.dll" - SetOutPath "$INSTDIR\runtimes\win\lib\netstandard2.0" - File "..\Project\GreenIT\bin\Release\net6.0\runtimes\win\lib\netstandard2.0\System.IO.Ports.dll" - File "..\Project\GreenIT\bin\Release\net6.0\runtimes\win\lib\netstandard2.0\System.ServiceProcess.ServiceController.dll" - SetOutPath "$INSTDIR\runtimes\win\lib\netcoreapp2.0" - File "..\Project\GreenIT\bin\Release\net6.0\runtimes\win\lib\netcoreapp2.0\System.Diagnostics.EventLog.dll" - File "..\Project\GreenIT\bin\Release\net6.0\runtimes\win\lib\netcoreapp2.0\System.Management.dll" - SetOutPath "$INSTDIR\runtimes\win\lib\netcoreapp3.0" - File "..\Project\GreenIT\bin\Release\net6.0\runtimes\win\lib\netcoreapp3.0\Microsoft.Win32.SystemEvents.dll" - SetOutPath "$INSTDIR" - File "..\Project\GreenIT\bin\Release\net6.0\System.ServiceProcess.ServiceController.dll" - File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.deps.json" - File "..\Project\GreenIT\bin\Release\net6.0\HidLibrary.dll" - File "..\Project\GreenIT\bin\Release\net6.0\TopShelf.ServiceInstaller.dll" - File "..\Project\GreenIT\bin\Release\net6.0\System.CodeDom.dll" - File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.runtimeconfig.json" - File "..\Project\GreenIT\bin\Release\net6.0\OpenHardwareMonitorLib.dll" - File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.pdb" - File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.dll" - File "..\Project\GreenIT\bin\Release\net6.0\Topshelf.dll" - SetOverwrite ifnewer - File "..\README.md" - File "..\LICENSE.txt" -SectionEnd +loop: + IntOp $R2 $R2 + 1 + StrCpy $R0 $CMDLINE 1 $R2 + StrCmp $R0 $R1 get + StrCmp $R2 $R3 get + Goto loop -Section -AdditionalIcons - WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}" - CreateShortCut "$SMPROGRAMS\GreenIT Service\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url" - CreateShortCut "$SMPROGRAMS\GreenIT Service\Uninstall.lnk" "$INSTDIR\uninstall.exe" -SectionEnd +get: + IntOp $R2 $R2 + 1 + StrCpy $R0 $CMDLINE 1 $R2 + StrCmp $R0 " " get + StrCpy $R0 $CMDLINE "" $R2 -Section -Post - WriteUninstaller "$INSTDIR\uninstall.exe" - WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\GreenIT.exe" - WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)" - WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe" - WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\GreenIT.exe" - WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}" - WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}" - WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}" + Pop $R3 + Pop $R2 + Pop $R1 + Exch $R0 +FunctionEnd + +Function GetOptions + !define GetOptions `!insertmacro GetOptionsCall` + + !macro GetOptionsCall _PARAMETERS _OPTION _RESULT + Push `${_PARAMETERS}` + Push `${_OPTION}` + Call GetOptions + Pop ${_RESULT} + !macroend + + Exch $1 + Exch + Exch $0 + Exch + Push $2 + Push $3 + Push $4 + Push $5 + Push $6 + Push $7 + ClearErrors + + StrCpy $2 $1 '' 1 + StrCpy $1 $1 1 + StrLen $3 $2 + StrCpy $7 0 + +begin: + StrCpy $4 -1 + StrCpy $6 '' + +quote: + IntOp $4 $4 + 1 + StrCpy $5 $0 1 $4 + StrCmp $5$7 '0' notfound + StrCmp $5 '' trimright + StrCmp $5 '"' 0 +7 + StrCmp $6 '' 0 +3 + StrCpy $6 '"' + goto quote + StrCmp $6 '"' 0 +3 + StrCpy $6 '' + goto quote + StrCmp $5 `'` 0 +7 + StrCmp $6 `` 0 +3 + StrCpy $6 `'` + goto quote + StrCmp $6 `'` 0 +3 + StrCpy $6 `` + goto quote + StrCmp $5 '`' 0 +7 + StrCmp $6 '' 0 +3 + StrCpy $6 '`' + goto quote + StrCmp $6 '`' 0 +3 + StrCpy $6 '' + goto quote + StrCmp $6 '"' quote + StrCmp $6 `'` quote + StrCmp $6 '`' quote + StrCmp $5 $1 0 quote + StrCmp $7 0 trimleft trimright + +trimleft: + IntOp $4 $4 + 1 + StrCpy $5 $0 $3 $4 + StrCmp $5 '' notfound + StrCmp $5 $2 0 quote + IntOp $4 $4 + $3 + StrCpy $0 $0 '' $4 + StrCpy $4 $0 1 + StrCmp $4 ' ' 0 +3 + StrCpy $0 $0 '' 1 + goto -3 + StrCpy $7 1 + goto begin + +trimright: + StrCpy $0 $0 $4 + StrCpy $4 $0 1 -1 + StrCmp $4 ' ' 0 +3 + StrCpy $0 $0 -1 + goto -3 + StrCpy $3 $0 1 + StrCpy $4 $0 1 -1 + StrCmp $3 $4 0 end + StrCmp $3 '"' +3 + StrCmp $3 `'` +2 + StrCmp $3 '`' 0 end + StrCpy $0 $0 -1 1 + goto end + +notfound: + SetErrors + StrCpy $0 '' + +end: + Pop $7 + Pop $6 + Pop $5 + Pop $4 + Pop $3 + Pop $2 + Pop $1 + Exch $0 +FunctionEnd + +Function InstallService + IfSilent 0 +24 + Call GetParameters + Exch $R0 + ${GetOptions} $R0 "/collectPeriod" $R1 + IfErrors 0 +3 + MessageBox MB_OK "Missing collectPeriod setting!" + Abort + ${GetOptions} $R0 "/uploadPeriod" $R1 + IfErrors 0 +3 + MessageBox MB_OK "Missing uploadPeriod setting!" + Abort + ${GetOptions} $R0 "/savesPeriod" $R1 + IfErrors 0 +3 + MessageBox MB_OK "Missing savestPeriod setting!" + Abort + ExecWait '"$INSTDIR\GreenIT.exe" install' +FunctionEnd + +Function StartOrRestartService + ExecWait '"$INSTDIR\GreenIT.exe" stop' + ExecWait '"$INSTDIR\GreenIT.exe" start' +FunctionEnd + +Function .onInit + Call GetParameters + Exch $R0 + ${GetOptions} $R0 "/silent" $R1 + IfErrors +2 0 + SetSilent silent +FunctionEnd + +Section "Main" SEC01 + SetOutPath "$INSTDIR" + SetOverwrite try + File "..\Project\GreenIT\bin\Release\net6.0\System.Diagnostics.EventLog.dll" + File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.exe" + File "..\Project\GreenIT\bin\Release\net6.0\config.json" + Call UploadConfig + AccessControl::GrantOnFile "$INSTDIR\config.ini" "(BU)" "GenericExecute + GenericRead + GenericWrite" + CreateDirectory "$SMPROGRAMS\GreenIT Service" + CreateShortCut "$SMPROGRAMS\GreenIT Service\GreenIT Service.lnk" "$INSTDIR\GreenIT.exe" + File "..\Project\GreenIT\bin\Release\net6.0\System.IO.Ports.dll" + File "..\Project\GreenIT\bin\Release\net6.0\Microsoft.Win32.SystemEvents.dll" + File "..\Project\GreenIT\bin\Release\net6.0\System.Management.dll" + SetOutPath "$INSTDIR\runtimes\win\lib\netstandard2.0" + File "..\Project\GreenIT\bin\Release\net6.0\runtimes\win\lib\netstandard2.0\System.ServiceProcess.ServiceController.dll" + SetOutPath "$INSTDIR\runtimes\win\lib\netcoreapp2.0" + File "..\Project\GreenIT\bin\Release\net6.0\runtimes\win\lib\netcoreapp2.0\System.Diagnostics.EventLog.dll" + SetOutPath "$INSTDIR\runtimes\win\lib\netcoreapp3.0" + File "..\Project\GreenIT\bin\Release\net6.0\runtimes\win\lib\netcoreapp3.0\Microsoft.Win32.SystemEvents.dll" + SetOutPath "$INSTDIR" + File "..\Project\GreenIT\bin\Release\net6.0\System.ServiceProcess.ServiceController.dll" + File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.deps.json" + File "..\Project\GreenIT\bin\Release\net6.0\TopShelf.ServiceInstaller.dll" + File "..\Project\GreenIT\bin\Release\net6.0\System.CodeDom.dll" + File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.runtimeconfig.json" + File "..\Project\GreenIT\bin\Release\net6.0\LibreHardwareMonitorLib.dll" + File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.pdb" + File "..\Project\GreenIT\bin\Release\net6.0\GreenIT.dll" + File "..\Project\GreenIT\bin\Release\net6.0\Topshelf.dll" + SetOverwrite ifnewer + File "..\README.md" + File "..\LICENSE.txt" + Call InstallService + Call StartOrRestartService SectionEnd +##################################################################### +# Config page +##################################################################### Function ConfigPage - !insertmacro MUI_HEADER_TEXT "Configuration of service settings" "Configure your own service settings." - nsDialogs::Create 1018 - Pop $Dialog - - ${If} $Dialog == error - Abort - ${EndIf} - - ${NSD_CreateGroupBox} 0 0 100% 100% "Service configuration" - - ${NSD_CreateLabel} 5% 25% 55% 10u "Period between collecting information (in seconds) :" - Pop $COLLECT_INFO_PERIOD_Label - - ${NSD_CreateNumber} 65% 25% 20% 12u "1" - Pop $COLLECT_INFO_PERIOD - ${NSD_Edit_SetTextLimit} $COLLECT_INFO_PERIOD 4 - - ${NSD_CreateLabel} 5% 50% 45% 10u "Period between uploads (in minutes) :" - Pop $UPLOAD_PERIOD_Label - - ${NSD_CreateNumber} 65% 50% 20% 12u "0" - Pop $UPLOAD_PERIOD - ${NSD_Edit_SetTextLimit} $UPLOAD_PERIOD 4 - - ${NSD_CreateLabel} 5% 75% 40% 10u "Period between saves (in hours) :" - Pop $SAVE_INFO_PERIOD_Label - - ${NSD_CreateNumber} 65% 75% 20% 12u "1" - Pop $SAVE_INFO_PERIOD - ${NSD_Edit_SetTextLimit} $SAVE_INFO_PERIOD 4 - - nsDialogs::Show + !insertmacro MUI_HEADER_TEXT "Configuration of service settings" "Configure your own service settings." + nsDialogs::Create 1018 + Pop $Dialog + + ${If} $Dialog == error + Abort + ${EndIf} + + ${NSD_CreateGroupBox} 0 0 100% 100% "Service configuration" + + ${NSD_CreateLabel} 5% 25% 55% 10u "Period between collecting information (in seconds) :" + Pop $COLLECT_INFO_PERIOD_Label + + ${NSD_CreateNumber} 65% 25% 20% 12u "1" + Pop $COLLECT_INFO_PERIOD + ${NSD_Edit_SetTextLimit} $COLLECT_INFO_PERIOD 4 + + ${NSD_CreateLabel} 5% 50% 45% 10u "Period between uploads (in minutes) :" + Pop $UPLOAD_PERIOD_Label + + ${NSD_CreateNumber} 65% 50% 20% 12u "0" + Pop $UPLOAD_PERIOD + ${NSD_Edit_SetTextLimit} $UPLOAD_PERIOD 4 + + ${NSD_CreateLabel} 5% 75% 40% 10u "Period between saves (in hours) :" + Pop $SAVE_INFO_PERIOD_Label + + ${NSD_CreateNumber} 65% 75% 20% 12u "1" + Pop $SAVE_INFO_PERIOD + ${NSD_Edit_SetTextLimit} $SAVE_INFO_PERIOD 4 + + nsDialogs::Show FunctionEnd Function ConfigPageLeave - ${NSD_GetText} $COLLECT_INFO_PERIOD $0 - ${NSD_GetText} $UPLOAD_PERIOD $1 - ${NSD_GetText} $SAVE_INFO_PERIOD $2 + ${NSD_GetText} $COLLECT_INFO_PERIOD $0 + ${NSD_GetText} $UPLOAD_PERIOD $1 + ${NSD_GetText} $SAVE_INFO_PERIOD $2 FunctionEnd Function UploadConfig - nsJSON::Set /file `$INSTDIR\config.json` - nsJSON::Set `COLLECT_INFO_PERIOD` /value `"$0"` - nsJSON::Set `UPLOAD_PERIOD` /value `"$1"` - nsJSON::Set `SAVE_INFO_PERIOD` /value `"$2"` - nsJSON::Serialize /file `$INSTDIR\config.json` + IfSilent 0 +19 + Call GetParameters + Exch $R0 + ${GetOptions} $R0 "/collectPeriod=" $R1 + StrCpy $0 $R1 + ${GetOptions} $R0 "/uploadPeriod=" $R1 + StrCpy $1 $R1 + ${GetOptions} $R0 "/savesPeriod=" $R1 + StrCpy $2 $R1 + nsJSON::Set /file `$INSTDIR\config.json` + nsJSON::Set `COLLECT_INFO_PERIOD` /value `"$0"` + nsJSON::Set `UPLOAD_PERIOD` /value `"$1"` + nsJSON::Set `SAVE_INFO_PERIOD` /value `"$2"` + nsJSON::Serialize /file `$INSTDIR\config.json` FunctionEnd -Function un.onUninstSuccess - HideWindow - MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) a été désinstallé avec succès de votre ordinateur." +##################################################################### +# Addons +##################################################################### +Section -AdditionalIcons + WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}" + CreateShortCut "$SMPROGRAMS\GreenIT Service\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url" + CreateShortCut "$SMPROGRAMS\GreenIT Service\Uninstall.lnk" "$INSTDIR\uninstall.exe" +SectionEnd + +Section -Post + WriteUninstaller "$INSTDIR\uninstall.exe" + WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\GreenIT.exe" + WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)" + WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninstall.exe" + WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\GreenIT.exe" + WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}" + WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}" + WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}" +SectionEnd + +##################################################################### +# Uninstaller +##################################################################### +Function un.GetParameters + Push $R0 + Push $R1 + Push $R2 + Push $R3 + + StrCpy $R2 1 + StrLen $R3 $CMDLINE + + ;Check for quote or space + StrCpy $R0 $CMDLINE $R2 + StrCmp $R0 '"' 0 +3 + StrCpy $R1 '"' + Goto loop + StrCpy $R1 " " + +loop: + IntOp $R2 $R2 + 1 + StrCpy $R0 $CMDLINE 1 $R2 + StrCmp $R0 $R1 get + StrCmp $R2 $R3 get + Goto loop + +get: + IntOp $R2 $R2 + 1 + StrCpy $R0 $CMDLINE 1 $R2 + StrCmp $R0 " " get + StrCpy $R0 $CMDLINE "" $R2 + + Pop $R3 + Pop $R2 + Pop $R1 + Exch $R0 +FunctionEnd + +Function un.GetOptions + !define un.GetOptions `!insertmacro un.GetOptionsCall` + + !macro un.GetOptionsCall _PARAMETERS _OPTION _RESULT + Push `${_PARAMETERS}` + Push `${_OPTION}` + Call un.GetOptions + Pop ${_RESULT} + !macroend + + Exch $1 + Exch + Exch $0 + Exch + Push $2 + Push $3 + Push $4 + Push $5 + Push $6 + Push $7 + ClearErrors + + StrCpy $2 $1 '' 1 + StrCpy $1 $1 1 + StrLen $3 $2 + StrCpy $7 0 + +begin: + StrCpy $4 -1 + StrCpy $6 '' + +quote: + IntOp $4 $4 + 1 + StrCpy $5 $0 1 $4 + StrCmp $5$7 '0' notfound + StrCmp $5 '' trimright + StrCmp $5 '"' 0 +7 + StrCmp $6 '' 0 +3 + StrCpy $6 '"' + goto quote + StrCmp $6 '"' 0 +3 + StrCpy $6 '' + goto quote + StrCmp $5 `'` 0 +7 + StrCmp $6 `` 0 +3 + StrCpy $6 `'` + goto quote + StrCmp $6 `'` 0 +3 + StrCpy $6 `` + goto quote + StrCmp $5 '`' 0 +7 + StrCmp $6 '' 0 +3 + StrCpy $6 '`' + goto quote + StrCmp $6 '`' 0 +3 + StrCpy $6 '' + goto quote + StrCmp $6 '"' quote + StrCmp $6 `'` quote + StrCmp $6 '`' quote + StrCmp $5 $1 0 quote + StrCmp $7 0 trimleft trimright + +trimleft: + IntOp $4 $4 + 1 + StrCpy $5 $0 $3 $4 + StrCmp $5 '' notfound + StrCmp $5 $2 0 quote + IntOp $4 $4 + $3 + StrCpy $0 $0 '' $4 + StrCpy $4 $0 1 + StrCmp $4 ' ' 0 +3 + StrCpy $0 $0 '' 1 + goto -3 + StrCpy $7 1 + goto begin + +trimright: + StrCpy $0 $0 $4 + StrCpy $4 $0 1 -1 + StrCmp $4 ' ' 0 +3 + StrCpy $0 $0 -1 + goto -3 + StrCpy $3 $0 1 + StrCpy $4 $0 1 -1 + StrCmp $3 $4 0 end + StrCmp $3 '"' +3 + StrCmp $3 `'` +2 + StrCmp $3 '`' 0 end + StrCpy $0 $0 -1 1 + goto end + +notfound: + SetErrors + StrCpy $0 '' + +end: + Pop $7 + Pop $6 + Pop $5 + Pop $4 + Pop $3 + Pop $2 + Pop $1 + Exch $0 FunctionEnd Function un.onInit - MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Êtes-vous certains de vouloir désinstaller totalement $(^Name) et tous ses composants ?" IDYES +2 - Abort + Call un.GetParameters + Exch $R0 + ${un.GetOptions} $R0 "/silent" $R1 + IfErrors +2 0 + SetSilent silent + IfErrors 0 +3 + MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to uninstall $(^Name) ?" IDYES +2 + Abort +FunctionEnd + +Function un.StopAndUninstallService + ExecWait '"$INSTDIR\GreenIT.exe" stop' + ExecWait '"$INSTDIR\GreenIT.exe" uninstall' FunctionEnd Section Uninstall - ExecWait '"$INSTDIR\GreenIT.exe" uninstall' - Delete "$INSTDIR\${PRODUCT_NAME}.url" - Delete "$INSTDIR\uninstall.exe" - Delete "$INSTDIR\LICENSE.txt" - Delete "$INSTDIR\README.md" - Delete "$INSTDIR\Topshelf.dll" - Delete "$INSTDIR\GreenIT.dll" - Delete "$INSTDIR\GreenIT.pdb" - Delete "$INSTDIR\OpenHardwareMonitorLib.dll" - Delete "$INSTDIR\GreenIT.runtimeconfig.json" - Delete "$INSTDIR\System.CodeDom.dll" - Delete "$INSTDIR\TopShelf.ServiceInstaller.dll" - Delete "$INSTDIR\HidLibrary.dll" - Delete "$INSTDIR\GreenIT.deps.json" - Delete "$INSTDIR\System.ServiceProcess.ServiceController.dll" - Delete "$INSTDIR\runtimes\win\lib\netcoreapp3.0\Microsoft.Win32.SystemEvents.dll" - Delete "$INSTDIR\runtimes\win\lib\netcoreapp2.0\System.Management.dll" - Delete "$INSTDIR\runtimes\win\lib\netcoreapp2.0\System.Diagnostics.EventLog.dll" - Delete "$INSTDIR\runtimes\win\lib\netstandard2.0\System.ServiceProcess.ServiceController.dll" - Delete "$INSTDIR\runtimes\win\lib\netstandard2.0\System.IO.Ports.dll" - Delete "$INSTDIR\System.Management.dll" - Delete "$INSTDIR\Microsoft.Win32.SystemEvents.dll" - Delete "$INSTDIR\System.IO.Ports.dll" - Delete "$INSTDIR\GreenIT.exe" - Delete "$INSTDIR\config.json" - Delete "$INSTDIR\System.Diagnostics.EventLog.dll" - - Delete "$SMPROGRAMS\GreenIT Service\Uninstall.lnk" - Delete "$SMPROGRAMS\GreenIT Service\Website.lnk" - Delete "$DESKTOP\GreenIT Service.lnk" - Delete "$SMPROGRAMS\GreenIT Service\GreenIT Service.lnk" - - RMDir "$SMPROGRAMS\GreenIT Service" - RMDir "$INSTDIR\runtimes\win\lib\netstandard2.0" - RMDir "$INSTDIR\runtimes\win\lib\netcoreapp3.0" - RMDir "$INSTDIR\runtimes\win\lib\netcoreapp2.0" - RMDir "$INSTDIR\runtimes\win\lib" - RMDir "$INSTDIR\runtimes\win" - RMDir "$INSTDIR\runtimes" - RMDir "$INSTDIR" - - DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" - DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}" - SetAutoClose true -SectionEnd \ No newline at end of file + Call un.StopAndUninstallService + Delete "$INSTDIR\${PRODUCT_NAME}.url" + Delete "$INSTDIR\uninstall.exe" + Delete "$INSTDIR\LICENSE.txt" + Delete "$INSTDIR\README.md" + Delete "$INSTDIR\Topshelf.dll" + Delete "$INSTDIR\GreenIT.dll" + Delete "$INSTDIR\GreenIT.pdb" + Delete "$INSTDIR\LibreHardwareMonitorLib.dll" + Delete "$INSTDIR\GreenIT.runtimeconfig.json" + Delete "$INSTDIR\System.CodeDom.dll" + Delete "$INSTDIR\TopShelf.ServiceInstaller.dll" + Delete "$INSTDIR\GreenIT.deps.json" + Delete "$INSTDIR\System.ServiceProcess.ServiceController.dll" + Delete "$INSTDIR\runtimes\win\lib\netcoreapp3.0\Microsoft.Win32.SystemEvents.dll" + Delete "$INSTDIR\runtimes\win\lib\netcoreapp2.0\System.Diagnostics.EventLog.dll" + Delete "$INSTDIR\runtimes\win\lib\netstandard2.0\System.ServiceProcess.ServiceController.dll" + Delete "$INSTDIR\System.Management.dll" + Delete "$INSTDIR\Microsoft.Win32.SystemEvents.dll" + Delete "$INSTDIR\System.IO.Ports.dll" + Delete "$INSTDIR\GreenIT.exe" + Delete "$INSTDIR\config.json" + Delete "$INSTDIR\System.Diagnostics.EventLog.dll" + + Delete "$SMPROGRAMS\GreenIT Service\Uninstall.lnk" + Delete "$SMPROGRAMS\GreenIT Service\Website.lnk" + Delete "$DESKTOP\GreenIT Service.lnk" + Delete "$SMPROGRAMS\GreenIT Service\GreenIT Service.lnk" + + RMDir "$SMPROGRAMS\GreenIT Service" + RMDir "$INSTDIR\runtimes\win\lib\netstandard2.0" + RMDir "$INSTDIR\runtimes\win\lib\netcoreapp3.0" + RMDir "$INSTDIR\runtimes\win\lib\netcoreapp2.0" + RMDir "$INSTDIR\runtimes\win\lib" + RMDir "$INSTDIR\runtimes\win" + RMDir "$INSTDIR\runtimes" + RMDir "$INSTDIR" + + DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" + DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}" + SetAutoClose true +SectionEnd + +Function un.onUninstSuccess + HideWindow + Call un.GetParameters + Exch $R0 + IfSilent +2 0 + MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully uninstalled." +FunctionEnd \ No newline at end of file diff --git a/NSIS/greenit.ico b/NSIS/greenit.ico new file mode 100644 index 0000000..7bc7303 Binary files /dev/null and b/NSIS/greenit.ico differ diff --git a/Project/GreenIT/GreenIT.csproj b/Project/GreenIT/GreenIT.csproj index 363fd5d..6b84aca 100644 --- a/Project/GreenIT/GreenIT.csproj +++ b/Project/GreenIT/GreenIT.csproj @@ -7,7 +7,7 @@ enable app.manifest GreenIT.GreenITMain - ocs-icon.ico + greenit.ico @@ -22,12 +22,12 @@ Always - + - + diff --git a/Project/GreenIT/GreenITService.cs b/Project/GreenIT/GreenITService.cs index 4d7f1f6..7926b9c 100644 --- a/Project/GreenIT/GreenITService.cs +++ b/Project/GreenIT/GreenITService.cs @@ -1,4 +1,4 @@ -using GreenIT.OpenHardwareMonitor; +using GreenIT.LibreHardwareMonitor; using System.ComponentModel; using System.Globalization; using System.Text.Json; @@ -12,13 +12,13 @@ public class GreenITService private bool _allEngineState = false; private bool _serviceEngine = false; private bool _saveEngine = false; - private bool _openHardwareMonitorEngine = false; + private bool _LibreHardwareMonitorEngine = false; private bool _stopEngineSignal = false; private readonly Mutex _mutex; private readonly BackgroundWorker _service; private readonly BackgroundWorker _save; - private readonly BackgroundWorker _openHardwareMonitor; + private readonly BackgroundWorker _LibreHardwareMonitor; private readonly string _folderPath = @"C:\ProgramData\GreenIT"; private readonly string _dataFilePath = @"C:\ProgramData\GreenIT\data.json"; @@ -49,11 +49,11 @@ public GreenITService() }; _save.DoWork += SaveEngine; - _openHardwareMonitor = new BackgroundWorker + _LibreHardwareMonitor = new BackgroundWorker { WorkerSupportsCancellation = true }; - _openHardwareMonitor.DoWork += OpenHardwareMonitorEngine; + _LibreHardwareMonitor.DoWork += LibreHardwareMonitorEngine; _timestamp = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds(); } @@ -88,11 +88,11 @@ public void Start() Console.WriteLine("[INFO] " + DateTime.Now.ToString() + ": Starting engines..." + "\r"); _service.RunWorkerAsync(); _save.RunWorkerAsync(); - _openHardwareMonitor.RunWorkerAsync(); + _LibreHardwareMonitor.RunWorkerAsync(); while ( (_serviceEngine != true) && (_saveEngine != true) && - (_openHardwareMonitorEngine != true) + (_LibreHardwareMonitorEngine != true) ) Thread.Sleep(1000); _allEngineState = true; @@ -144,10 +144,10 @@ public void SaveEngine(object? sender, DoWorkEventArgs e) _saveEngine = false; } - public void OpenHardwareMonitorEngine(object? sender, DoWorkEventArgs e) + public void LibreHardwareMonitorEngine(object? sender, DoWorkEventArgs e) { - Console.WriteLine("[INFO] " + DateTime.Now.ToString() + ": OpenHardwareMonitor engine started" + "\r"); - _openHardwareMonitorEngine = true; + Console.WriteLine("[INFO] " + DateTime.Now.ToString() + ": LibreHardwareMonitor engine started" + "\r"); + _LibreHardwareMonitorEngine = true; while (_allEngineState != true) Thread.Sleep(100); Thread.Sleep(500); @@ -162,17 +162,17 @@ public void OpenHardwareMonitorEngine(object? sender, DoWorkEventArgs e) while (_stopEngineSignal == false) { _mutex.WaitOne(); - Console.WriteLine("[INFO] " + DateTime.Now.ToString() + ": Working on OpenHardwareMonitor engine" + "\r"); + Console.WriteLine("[INFO] " + DateTime.Now.ToString() + ": Working on LibreHardwareMonitor engine" + "\r"); List lines = new(); Match lineMatch; - JsonObject consumption = OpenHardwareMonitorModel.GetConsumption(); + JsonObject consumption = LibreHardwareMonitorModel.GetConsumption(); JsonObject data = new() { { "CONSUMPTION", "0" }, { "UPTIME", "0" } }; - string regex = @"""(?" + DateTime.Now.ToString("yyyy-MM-dd") + @")"": {""CONSUMPTION"":""(?[\s\S]+?)"",""UPTIME"":""(?[0-9]+)""},"; + string regex = @"""(?" + DateTime.Now.ToString("yyyy-MM-dd") + @")"": {""CONSUMPTION"":""(?[\s\S]+?)"",""UPTIME"":""(?[0-9]+)""},"; if (consumption != null) { @@ -229,7 +229,7 @@ public void OpenHardwareMonitorEngine(object? sender, DoWorkEventArgs e) if (DateTime.Now >= uploadTime) { - Console.WriteLine("[INFO] " + DateTime.Now.ToString() + ": OpenHardwareMonitor module is writing logs..." + "\r"); + Console.WriteLine("[INFO] " + DateTime.Now.ToString() + ": LibreHardwareMonitor module is writing logs..." + "\r"); bool todayLineExist = false; for (int i = 0; i < lines.Count; i++) { @@ -256,7 +256,7 @@ public void OpenHardwareMonitorEngine(object? sender, DoWorkEventArgs e) Thread.Sleep(int.Parse(_config["COLLECT_INFO_PERIOD"].ToString()) * 1000); } } - _openHardwareMonitorEngine = false; + _LibreHardwareMonitorEngine = false; } public void Stop() @@ -265,11 +265,11 @@ public void Stop() Console.WriteLine("[INFO] " + DateTime.Now.ToString() + ": Stopping engines" + "\r"); _service.CancelAsync(); - _openHardwareMonitor.CancelAsync(); + _LibreHardwareMonitor.CancelAsync(); while ( (_serviceEngine != true) && (_saveEngine != true) && - (_openHardwareMonitorEngine == true) + (_LibreHardwareMonitorEngine == true) ) Thread.Sleep(1000); Thread.Sleep(1000); diff --git a/Project/GreenIT/LibreHardwareMonitor/LibreHardwareMonitorModel.cs b/Project/GreenIT/LibreHardwareMonitor/LibreHardwareMonitorModel.cs new file mode 100644 index 0000000..cd7e792 --- /dev/null +++ b/Project/GreenIT/LibreHardwareMonitor/LibreHardwareMonitorModel.cs @@ -0,0 +1,36 @@ +using LibreHardwareMonitor.Hardware; +using System.Text.Json.Nodes; + +namespace GreenIT.LibreHardwareMonitor +{ + public static class LibreHardwareMonitorModel + { + private static readonly UpdateVisitor _visitor = new(); + private static readonly Computer _computer = new(); + public static JsonObject GetConsumption() + { + JsonObject consumption = new(); + _computer.Open(); + _computer.IsCpuEnabled = true; + _computer.Accept(_visitor); + foreach (IHardware hardware in _computer.Hardware) + { + foreach (ISensor sensor in hardware.Sensors) + { + if(sensor.SensorType == SensorType.Power && sensor.Name == "Package") + { + consumption.Add("EXIST", true); + consumption.Add("DATE", DateTime.Now.ToString("yyyy-MM-dd")); + if (sensor.Value > 50000) consumption.Add("CONSUMPTION", "VM detected"); + else + { + consumption.Add("CONSUMPTION", sensor.Value.ToString().Replace(",", ".")); + } + } + } + } + _computer.Close(); + return consumption; + } + } +} diff --git a/Project/GreenIT/OpenHardwareMonitor/UpdateVisitor.cs b/Project/GreenIT/LibreHardwareMonitor/UpdateVisitor.cs similarity index 92% rename from Project/GreenIT/OpenHardwareMonitor/UpdateVisitor.cs rename to Project/GreenIT/LibreHardwareMonitor/UpdateVisitor.cs index 83731b9..4bff7f6 100644 --- a/Project/GreenIT/OpenHardwareMonitor/UpdateVisitor.cs +++ b/Project/GreenIT/LibreHardwareMonitor/UpdateVisitor.cs @@ -1,4 +1,4 @@ -using OpenHardwareMonitor.Hardware; +using LibreHardwareMonitor.Hardware; namespace GreenIT { diff --git a/Project/GreenIT/OpenHardwareMonitor/OpenHardwareMonitorModel.cs b/Project/GreenIT/OpenHardwareMonitor/OpenHardwareMonitorModel.cs deleted file mode 100644 index 2fa3d99..0000000 --- a/Project/GreenIT/OpenHardwareMonitor/OpenHardwareMonitorModel.cs +++ /dev/null @@ -1,39 +0,0 @@ -using OpenHardwareMonitor.Hardware; -using System.Text.Json.Nodes; - -namespace GreenIT.OpenHardwareMonitor -{ - public static class OpenHardwareMonitorModel - { - private static readonly UpdateVisitor _visitor = new(); - private static readonly Computer _computer = new(); - public static JsonObject GetConsumption() - { - JsonObject consumption = new(); - _computer.Open(); - _computer.CPUEnabled = true; - _computer.Accept(_visitor); - for (int i = 0; i < _computer.Hardware.Length; i++) - { - if (_computer.Hardware[i].HardwareType == HardwareType.CPU) - { - for (int j = 0; j < _computer.Hardware[i].Sensors.Length; j++) - { - if ((_computer.Hardware[i].Sensors[j].SensorType == SensorType.Power) && (_computer.Hardware[i].Sensors[j].Name == "Package Power")) - { - consumption.Add("EXIST", true); - consumption.Add("DATE", DateTime.Now.ToString("yyyy-MM-dd")); - if (_computer.Hardware[i].Sensors[j].Value > 50000) consumption.Add("CONSUMPTION", "VM detected"); - else - { - consumption.Add("CONSUMPTION", _computer.Hardware[i].Sensors[j].Value.ToString().Replace(',', '.')); - } - } - } - } - } - _computer.Close(); - return consumption; - } - } -} diff --git a/Project/GreenIT/greenit.ico b/Project/GreenIT/greenit.ico new file mode 100644 index 0000000..7bc7303 Binary files /dev/null and b/Project/GreenIT/greenit.ico differ diff --git a/Project/GreenIT/ocs-icon.ico b/Project/GreenIT/ocs-icon.ico deleted file mode 100644 index 17b1acb..0000000 Binary files a/Project/GreenIT/ocs-icon.ico and /dev/null differ diff --git a/README.md b/README.md index 9a432eb..49a045c 100644 --- a/README.md +++ b/README.md @@ -7,62 +7,15 @@

Service GreenIT

Some Links :
- Ask question | - Website | - OCS Professional + Ask question | + Website | + OCS Professional

## Description - Currently supported on Windows, this service is made to gather power consumption information. -> _**IMPORTANT NOTE : This service is required if you use GreenIT Plugin downloadable here**_ - -## Prerequisites - -> _NOTE : Every agents which have the service installed can return GreenIT data._ - -- Windows 10 +> _**IMPORTANT NOTE : This service is required if you use GreenIT Plugin downloadable here**_ ## Installation - -To install the service on your agents : - -- Download the service here to get the executable and start it. - -- Go to the config page : - -

- Config Page -

- -- Configuration : - - - The period between collecting information is a time in seconds that will allow the service to start colecting information. - - > _Example : if you put it to 5, it will collect information every 5 seconds._ - - - The period between upload is a time in minutes that will allow the service to write information collected into the data file _(C:\\ProgramData\GreenIT\data.json)_ - - > _Example : if you put it to 5, it will write information into data file every 5 minutes. - > **If you put it to 0, it will write information in data file each data collect.**_ - - > _NOTE : "C:\\ProgramData" is an unvisible folder._ - - - The period between saves is a time in hours that will allow the service to create a ".bak" file next to data file to don't lose your last data. - - > _Example : if you put it to 5, it will create/update the backup file every 5 hours._ - -- Click on install and don't forget to check "Run GreenIT Service 1.0" at the end. - -> _IMPORTANT NOTE : It is important to check "Run GreenIT Service 1.0" to install the software as a Windows service !_ - -- If you haven't checked the box, you can install it manually after the install of the software : - - Open a terminal and go in the software installation folder path. - - Run the command `.\GreenIT.exe install` and wait until the plugin is installed. - -And yes, that's all :-) - -## For developers - -All the documentation for developers can be found [here](https://github.com/OCSInventory-NG/Wiki/tree/master/english/04.Management-console-and-its-advanced-features/GreenIT-service-developers.md) +There is a documentation to help you to clearly install the plugin and his dependencies available here. \ No newline at end of file diff --git a/images/GreenIT_configuration.png b/images/GreenIT_configuration.png deleted file mode 100644 index 1ec7696..0000000 Binary files a/images/GreenIT_configuration.png and /dev/null differ